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.



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

def initialize index
  index = 0                         if index.nil?
  index = Array.new(index) { |i| i} if index.is_a? Integer
  index = index.to_a                if index.is_a? Daru::Index

  @relation_hash = {}
  index.each_with_index do |n, idx|
    @relation_hash[n] = idx 
  end

  @relation_hash.freeze
  @keys = @relation_hash.keys
  @size = @relation_hash.size
end

Instance Attribute Details

#relation_hashObject (readonly)

Returns the value of attribute relation_hash.



50
51
52
# File 'lib/daru/index.rb', line 50

def relation_hash
  @relation_hash
end

#sizeObject (readonly)

Returns the value of attribute size.



50
51
52
# File 'lib/daru/index.rb', line 50

def size
  @size
end

Class Method Details

.__new__Object



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

alias :__new__ :new

._load(data) ⇒ Object



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

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
35
36
37
38
39
# File 'lib/daru/index.rb', line 23

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

  idx =
  if source and source[0].is_a?(Array)
    Daru::MultiIndex.from_tuples source
  elsif source and source.is_a?(Array) and !source.empty? and 
    source.all? { |e| e.is_a?(DateTime) }
    Daru::DateTimeIndex.new(source, freq: :infer)
  else
    i = self.allocate
    i.send :initialize, *args, &block
    i
  end

  idx
end

Instance Method Details

#&(other) ⇒ Object

Produce a new index from the set intersection of two indexes



113
114
115
# File 'lib/daru/index.rb', line 113

def & other
  
end

#==(other) ⇒ Object



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

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

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

#[](*key) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/daru/index.rb', line 74

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

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

    slice first, last
  when key.size > 1
    Daru::Index.new key.map { |k| self[k] }
  else
    v = @relation_hash[loc]
    return loc if v.nil?
    v
  end
end

#_dump(depth) ⇒ Object



137
138
139
# File 'lib/daru/index.rb', line 137

def _dump depth
  Marshal.dump({relation_hash: @relation_hash})
end

#dupObject



133
134
135
# File 'lib/daru/index.rb', line 133

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

#each(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/daru/index.rb', line 129

def empty?
  @relation_hash.empty?
end

#include?(index) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/daru/index.rb', line 125

def include? index
  @relation_hash.has_key? index
end

#key(value) ⇒ Object



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

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

#map(&block) ⇒ Object



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

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

#slice(*args) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/daru/index.rb', line 92

def slice *args
  start   = args[0]
  en      = args[1]
  indexes = []

  if start.is_a?(Integer) and 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



117
118
119
# File 'lib/daru/index.rb', line 117

def to_a
  @relation_hash.keys
end

#|(other) ⇒ Object

Produce new index from the set union of two indexes.



108
109
110
# File 'lib/daru/index.rb', line 108

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