Class: DaruLite::Index
- Includes:
- Enumerable
- Defined in:
- lib/daru_lite/index/index.rb
Overview
rubocop:disable Metrics/ClassLength
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#relation_hash ⇒ Object
readonly
Returns the value of attribute relation_hash.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Class Method Summary collapse
- .__new__ ⇒ Object
- ._load(data) ⇒ Object
- .coerce(maybe_index) ⇒ Object
- .inherited(subclass) ⇒ Object
-
.new(*args) ⇒ Object
We over-ride the .new method so that any sort of Index can be generated from DaruLite::Index based on the types of arguments supplied.
Instance Method Summary collapse
-
#&(other) ⇒ Object
Produce a new index from the set intersection of two indexes.
- #==(other) ⇒ Object
- #[](key, *rest) ⇒ Object
- #_dump ⇒ Object
- #add(*indexes) ⇒ Object
-
#at(*positions) ⇒ object
Takes positional values and returns subset of the self capturing the indexes at mentioned positions.
-
#conform ⇒ Object
Provide an Index for sub vector produced.
-
#delete_at(position) ⇒ object
Takes a positional value and returns a new Index without the element at given position.
- #dup ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #include?(index) ⇒ Boolean
-
#initialize(index, opts = {}) ⇒ Index
constructor
A new instance of Index.
- #inspect(threshold = 20) ⇒ Object
-
#is_values(*indexes) ⇒ DaruLite::Vector
Return vector of booleans with value at ith position is either true or false depending upon whether index value at position i is equal to any of the values passed in the argument or not.
- #key(value) ⇒ Object
-
#pos(*indexes) ⇒ Object
Returns positions given indexes or positions.
- #reorder(new_order) ⇒ Object
- #slice(*args) ⇒ Object
-
#sort(opts = {}) ⇒ Index
Sorts a ‘Index`, according to its values.
- #subset(*indexes) ⇒ Object
- #subset_slice(*args) ⇒ Object
- #to_a ⇒ Object
- #to_df ⇒ Object
-
#valid?(*indexes) ⇒ true, false
Returns true if all arguments are either a valid category or position.
-
#|(other) ⇒ Object
Produce new index from the set union of two indexes.
Constructor Details
#initialize(index, opts = {}) ⇒ Index
Returns a new instance of Index.
67 68 69 70 71 72 73 |
# File 'lib/daru_lite/index/index.rb', line 67 def initialize(index, opts = {}) index = guess_index index @relation_hash = index.each_with_index.to_h.freeze @keys = @relation_hash.keys @size = @relation_hash.size @name = opts[:name] end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
48 49 50 |
# File 'lib/daru_lite/index/index.rb', line 48 def name @name end |
#relation_hash ⇒ Object (readonly)
Returns the value of attribute relation_hash.
47 48 49 |
# File 'lib/daru_lite/index/index.rb', line 47 def relation_hash @relation_hash end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
47 48 49 |
# File 'lib/daru_lite/index/index.rb', line 47 def size @size end |
Class Method Details
.__new__ ⇒ Object
13 |
# File 'lib/daru_lite/index/index.rb', line 13 alias __new__ new |
._load(data) ⇒ Object
267 268 269 270 271 |
# File 'lib/daru_lite/index/index.rb', line 267 def self._load(data) h = Marshal.load data DaruLite::Index.new(h[:relation_hash].keys) end |
.coerce(maybe_index) ⇒ Object
36 37 38 |
# File 'lib/daru_lite/index/index.rb', line 36 def self.coerce(maybe_index) maybe_index.is_a?(Index) ? maybe_index : DaruLite::Index.new(maybe_index) end |
.inherited(subclass) ⇒ Object
15 16 17 18 19 |
# File 'lib/daru_lite/index/index.rb', line 15 def inherited(subclass) class << subclass alias_method :new, :__new__ end end |
.new(*args) ⇒ Object
We over-ride the .new method so that any sort of Index can be generated from DaruLite::Index based on the types of arguments supplied.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/daru_lite/index/index.rb', line 24 def self.new(*args, &) # FIXME: I'm not sure this clever trick really deserves our attention. # Most of common ruby libraries just avoid it in favor of usual # factor method, like `Index.create`. When `Index.new(...).class != Index` # it just leads to confusion and surprises. - zverok, 2016-05-18 source = args.first MultiIndex.try_from_tuples(source) || DateTimeIndex.try_create(source) || allocate.tap { |i| i.send(:initialize, *args, &) } end |
Instance Method Details
#&(other) ⇒ Object
Produce a new index from the set intersection of two indexes
199 200 201 |
# File 'lib/daru_lite/index/index.rb', line 199 def &(other) Index.new(to_a & other.to_a) end |
#==(other) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/daru_lite/index/index.rb', line 75 def ==(other) return false if self.class != other.class || other.size != @size @keys == other.to_a && @relation_hash.values == other.relation_hash.values end |
#[](key, *rest) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/daru_lite/index/index.rb', line 82 def [](key, *rest) if key.is_a?(Range) by_range key elsif !rest.empty? by_multi_key key, *rest else by_single_key key end end |
#_dump ⇒ Object
263 264 265 |
# File 'lib/daru_lite/index/index.rb', line 263 def _dump(*) Marshal.dump(relation_hash: @relation_hash) end |
#add(*indexes) ⇒ Object
246 247 248 |
# File 'lib/daru_lite/index/index.rb', line 246 def add(*indexes) DaruLite::Index.new(to_a + indexes) end |
#at(*positions) ⇒ object
Takes positional values and returns subset of the self
capturing the indexes at mentioned positions
145 146 147 148 149 150 151 152 153 |
# File 'lib/daru_lite/index/index.rb', line 145 def at(*positions) positions = preprocess_positions(*positions) validate_positions(*positions) if positions.is_a? Integer key(positions) else self.class.new(positions.map { |v| key(v) }) end end |
#conform ⇒ Object
Provide an Index for sub vector produced
277 278 279 |
# File 'lib/daru_lite/index/index.rb', line 277 def conform(*) self end |
#delete_at(position) ⇒ object
Takes a positional value and returns a new Index without the element at given position
257 258 259 260 261 |
# File 'lib/daru_lite/index/index.rb', line 257 def delete_at(position) indexes = to_a indexes.delete_at(position) self.class.new(indexes) end |
#dup ⇒ Object
242 243 244 |
# File 'lib/daru_lite/index/index.rb', line 242 def dup DaruLite::Index.new @keys, name: @name end |
#each(&block) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/daru_lite/index/index.rb', line 40 def each(&block) return to_enum(:each) unless block @relation_hash.each_key(&block) self end |
#empty? ⇒ Boolean
238 239 240 |
# File 'lib/daru_lite/index/index.rb', line 238 def empty? @size.zero? end |
#include?(index) ⇒ Boolean
213 214 215 |
# File 'lib/daru_lite/index/index.rb', line 213 def include?(index) @relation_hash.key? index end |
#inspect(threshold = 20) ⇒ Object
155 156 157 158 159 160 161 162 |
# File 'lib/daru_lite/index/index.rb', line 155 def inspect(threshold = 20) name_part = @name ? "#{@name} " : '' if size <= threshold "#<#{self.class}(#{size}): #{name_part}{#{to_a.join(', ')}}>" else "#<#{self.class}(#{size}): #{name_part}{#{to_a.first(threshold).join(', ')} ... #{to_a.last}}>" end end |
#is_values(*indexes) ⇒ DaruLite::Vector
Do not use it to check for Float::NAN as Float::NAN == Float::NAN is false
Return vector of booleans with value at ith position is either true or false depending upon whether index value at position i is equal to any of the values passed in the argument or not
233 234 235 236 |
# File 'lib/daru_lite/index/index.rb', line 233 def is_values(*indexes) # rubocop:disable Naming/PredicatePrefix bool_array = @keys.map { |r| indexes.include?(r) } DaruLite::Vector.new(bool_array) end |
#key(value) ⇒ Object
207 208 209 210 211 |
# File 'lib/daru_lite/index/index.rb', line 207 def key(value) return nil unless value.is_a?(Numeric) @keys[value] end |
#pos(*indexes) ⇒ Object
If the arugent is both a valid index and a valid position, it will treated as valid index
Returns positions given indexes or positions
112 113 114 115 116 117 118 119 120 |
# File 'lib/daru_lite/index/index.rb', line 112 def pos(*indexes) indexes = preprocess_range(indexes.first) if indexes.first.is_a? Range if indexes.size == 1 numeric_pos indexes.first else indexes.map { |index| numeric_pos index } end end |
#reorder(new_order) ⇒ Object
281 282 283 284 |
# File 'lib/daru_lite/index/index.rb', line 281 def reorder(new_order) from = to_a self.class.new(new_order.map { |i| from[i] }) end |
#slice(*args) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/daru_lite/index/index.rb', line 164 def slice(*args) start = args[0] en = args[1] start_idx = @relation_hash[start] en_idx = @relation_hash[en] if start_idx.nil? nil elsif en_idx.nil? Array(start_idx..(size - 1)) else Array(start_idx..en_idx) end end |
#sort(opts = {}) ⇒ Index
Sorts a ‘Index`, according to its values. Defaults to ascending order sorting.
300 301 302 303 304 305 306 307 |
# File 'lib/daru_lite/index/index.rb', line 300 def sort(opts = {}) opts = { ascending: true }.merge(opts) new_index = @keys.sort new_index = new_index.reverse unless opts[:ascending] self.class.new(new_index) end |
#subset(*indexes) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/daru_lite/index/index.rb', line 122 def subset(*indexes) if indexes.first.is_a? Range start = indexes.first.begin en = indexes.first.end subset_slice start, en elsif include? indexes.first # Assume 'indexes' contain indexes not positions DaruLite::Index.new indexes else # Assume 'indexes' contain positions not indexes DaruLite::Index.new(indexes.map { |k| key k }) end end |
#subset_slice(*args) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/daru_lite/index/index.rb', line 180 def subset_slice(*args) start = args[0] en = args[1] if start.is_a?(Integer) && en.is_a?(Integer) Index.new @keys[start..en] else start_idx = @relation_hash[start] en_idx = @relation_hash[en] Index.new @keys[start_idx..en_idx] end end |
#to_a ⇒ Object
203 204 205 |
# File 'lib/daru_lite/index/index.rb', line 203 def to_a @keys.dup end |
#to_df ⇒ Object
309 310 311 |
# File 'lib/daru_lite/index/index.rb', line 309 def to_df DaruLite::DataFrame.new(name => to_a) end |
#valid?(*indexes) ⇒ true, false
Returns true if all arguments are either a valid category or position
100 101 102 |
# File 'lib/daru_lite/index/index.rb', line 100 def valid?(*indexes) indexes.all? { |i| to_a.include?(i) || (i.is_a?(Numeric) && i < size) } end |