Class: Daru::Index

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/daru/index.rb

Direct Known Subclasses

DateTimeIndex, MultiIndex

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ Index

Returns a new instance of Index.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/daru/index.rb', line 47

def initialize index
  index =
    case index
    when nil
      []
    when Integer
      index.times.to_a
    when Enumerable
      index.to_a
    else
      raise ArgumentError,
        "Cannot create index from #{index.class} #{index.inspect}"
    end

  @relation_hash = index.each_with_index.to_h.freeze
  @keys = @relation_hash.keys
  @size = @relation_hash.size
end

Instance Attribute Details

#relation_hashObject (readonly)

Returns the value of attribute relation_hash.



45
46
47
# File 'lib/daru/index.rb', line 45

def relation_hash
  @relation_hash
end

#sizeObject (readonly)

Returns the value of attribute size.



45
46
47
# File 'lib/daru/index.rb', line 45

def size
  @size
end

Class Method Details

.__new__Object



12
# File 'lib/daru/index.rb', line 12

alias :__new__ :new

._load(data) ⇒ Object



147
148
149
150
151
# File 'lib/daru/index.rb', line 147

def self._load data
  h = Marshal.load data

  Daru::Index.new(h[:relation_hash].keys)
end

.inherited(subclass) ⇒ Object



14
15
16
17
18
# File 'lib/daru/index.rb', line 14

def inherited subclass
  class << subclass
    alias :new :__new__
  end
end

.new(*args, &block) ⇒ Object

We over-ride the .new method so that any sort of Index can be generated from Daru::Index based on the types of arguments supplied.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/daru/index.rb', line 23

def self.new *args, &block
  source = args[0]

  if source.respond_to?(:first) && source.first.is_a?(Array)
    Daru::MultiIndex.from_tuples source
  elsif source && source.is_a?(Array) && !source.empty? &&
        source.all? { |e| e.is_a?(DateTime) }
    Daru::DateTimeIndex.new(source, freq: :infer)
  else
    allocate.tap { |i| i.send :initialize, *args, &block }
  end
end

Instance Method Details

#&(other) ⇒ Object

Produce a new index from the set intersection of two indexes



120
121
# File 'lib/daru/index.rb', line 120

def & other
end

#==(other) ⇒ Object



66
67
68
69
70
71
# File 'lib/daru/index.rb', line 66

def ==(other)
  return false if self.class != other.class || other.size != @size

  @relation_hash.keys == other.to_a &&
    @relation_hash.values == other.relation_hash.values
end

#[](*key) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/daru/index.rb', line 73

def [](*key)
  loc = key[0]

  case
  when loc.is_a?(Range)
    first = loc.first
    last = loc.last

    slice first, last
  when key.size > 1
    if include? key[0]
      Daru::Index.new key.map { |k| k }
    else
      # Assume the user is specifing values for index not keys
      # Return index object having keys corresponding to values provided
      Daru::Index.new key.map { |k| key k }
    end
  else
    v = @relation_hash[loc]
    unless v
      return loc if loc.is_a?(Numeric) && loc < size
      raise IndexError, "Specified index #{loc.inspect} does not exist"
    end
    v
  end
end

#_dumpObject



143
144
145
# File 'lib/daru/index.rb', line 143

def _dump(*)
  Marshal.dump(relation_hash: @relation_hash)
end

#conformObject

Provide an Index for sub vector produced

Parameters:

  • input_indexes (Array)

    the input by user to index the vector

Returns:

  • (Object)

    the Index object for sub vector produced



157
158
159
# File 'lib/daru/index.rb', line 157

def conform(*)
  self
end

#dupObject



139
140
141
# File 'lib/daru/index.rb', line 139

def dup
  Daru::Index.new @relation_hash.keys
end

#each(&block) ⇒ Object



36
37
38
39
# File 'lib/daru/index.rb', line 36

def each(&block)
  @relation_hash.each_key(&block)
  self
end

#empty?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/daru/index.rb', line 135

def empty?
  @relation_hash.empty?
end

#include?(index) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/daru/index.rb', line 131

def include? index
  @relation_hash.key? index
end

#key(value) ⇒ Object



127
128
129
# File 'lib/daru/index.rb', line 127

def key(value)
  @relation_hash.keys[value]
end

#map(&block) ⇒ Object



41
42
43
# File 'lib/daru/index.rb', line 41

def map(&block)
  to_a.map(&block)
end

#slice(*args) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/daru/index.rb', line 100

def 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_aObject



123
124
125
# File 'lib/daru/index.rb', line 123

def to_a
  @relation_hash.keys
end

#|(other) ⇒ Object

Produce new index from the set union of two indexes.



115
116
117
# File 'lib/daru/index.rb', line 115

def |(other)
  Index.new(to_a | other.to_a)
end