Class: Daru::Index

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, values = nil) ⇒ Index

Returns a new instance of Index.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/daru/index.rb', line 20

def initialize index, values=nil
  @relation_hash = {}

  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

  if values.nil?
    index.each_with_index do |n, idx|
      n = n.to_sym unless n.is_a?(Integer)

      @relation_hash[n] = idx 
    end
  else
    raise IndexError, "Size of values : #{values.size} and index : #{index.size} do not match" if
      index.size != values.size

    values.each_with_index do |value,i|
      @relation_hash[index[i]] = value
    end
  end

  @relation_hash.freeze
  @size = @relation_hash.size

  if index[0].is_a?(Integer)
    @index_class = Integer
  else
    @index_class = Symbol
  end
end

Instance Attribute Details

#index_classObject (readonly)

Returns the value of attribute index_class.



18
19
20
# File 'lib/daru/index.rb', line 18

def index_class
  @index_class
end

#relation_hashObject (readonly)

Returns the value of attribute relation_hash.



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

def relation_hash
  @relation_hash
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

Class Method Details

._load(data) ⇒ Object



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

def self._load data
  h = Marshal.load data

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

Instance Method Details

#+(other) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/daru/index.rb', line 82

def +(other)
  if other.respond_to? :relation_hash #another index object
    (@relation_hash.keys + other.relation_hash.keys).uniq.to_index
  elsif other.is_a?(Symbol) or other.is_a?(Integer)
    (@relation_hash.keys << other).uniq.to_index
  else
    (@relation_hash.keys + other).uniq.to_index
  end
end

#==(other) ⇒ Object



52
53
54
55
56
# File 'lib/daru/index.rb', line 52

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

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

#[](key) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/daru/index.rb', line 58

def [](key)
  case key
  when Range
    if key.first.is_a?(Integer) and key.last.is_a?(Integer)
      first = key.first
      last = key.last
    else
      first = @relation_hash[key.first]
      last  = @relation_hash[key.last]
    end

    indexes = []
    (first..last).each do |idx|
      indexes << @relation_hash.key(idx)
    end

    Daru::Index.new indexes, (first..last).to_a
  when Array # works only with numeric indices
    Daru::Index.new key.map { |k| @relation_hash.key(k) }, key
  else
    @relation_hash[key]
  end
end

#_dump(depth) ⇒ Object



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

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

#dupObject



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

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

#each(&block) ⇒ Object



5
6
7
8
# File 'lib/daru/index.rb', line 5

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

#empty?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/daru/index.rb', line 104

def empty?
  @relation_hash.empty?
end

#include?(index) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/daru/index.rb', line 100

def include? index
  @relation_hash.has_key? index
end

#key(value) ⇒ Object



96
97
98
# File 'lib/daru/index.rb', line 96

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

#map(&block) ⇒ Object



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

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

#to_aObject



92
93
94
# File 'lib/daru/index.rb', line 92

def to_a
  @relation_hash.keys
end