Class: IndexedArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/xamplr/indexed-array.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ IndexedArray

Returns a new instance of IndexedArray.



4
5
6
7
8
# File 'lib/xamplr/indexed-array.rb', line 4

def initialize(parent=nil)
  @map = {}
  @store = []
  @parent = nil
end

Instance Method Details

#[](index) ⇒ Object Also known as: slice



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/xamplr/indexed-array.rb', line 10

def [](index)
  if String === index or Symbol === index then
    pos = @map[index]
    if pos then
      return @store[pos]
    else
      return nil
    end
  else
    return @store[index]
  end
end

#[]=(index, value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/xamplr/indexed-array.rb', line 25

def []=(index, value)
  if String === index or Symbol === index then
    pos = @map[index]
    unless pos then
      pos = @store.size
      @map[index] = pos
    end
    return @store[pos] = value
  else
    raise XamplException.new(:non_string_or_symbol_key)
  end
end

#clearObject



38
39
40
41
# File 'lib/xamplr/indexed-array.rb', line 38

def clear
  @map = {}
  @store = []
end

#delete(index) ⇒ Object Also known as: delete_at



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/xamplr/indexed-array.rb', line 47

def delete(index)
  if String === index or Symbol === index then
    pos = @map.delete(index)
    if pos then
      @map.each{ | k, v| @map[k] = (v - 1) if pos < v }
      return @store.delete_at(pos)
    end
  else
    key = @map.index(index)
    pos = @map.delete(key) if key
    if pos then
      @map.each{ | k, v | @map[k] = (v - 1) if pos < v }
      return @store.delete_at(pos)
    end
  end
  return nil
end

#eachObject



81
82
83
84
85
# File 'lib/xamplr/indexed-array.rb', line 81

def each
  @store.each do  |obj |
    yield(obj)
  end
end

#each_indexObject



87
88
89
# File 'lib/xamplr/indexed-array.rb', line 87

def each_index
  @store.each_index { | i | yield(i) }
end

#each_key_valueObject



91
92
93
# File 'lib/xamplr/indexed-array.rb', line 91

def each_key_value
  @map.each { | k, v | yield(k, @store[v]) }
end

#firstObject



73
74
75
# File 'lib/xamplr/indexed-array.rb', line 73

def first
  @store.first
end

#keysObject



43
44
45
# File 'lib/xamplr/indexed-array.rb', line 43

def keys
  return @map.keys
end

#lastObject



77
78
79
# File 'lib/xamplr/indexed-array.rb', line 77

def last
  @store.last
end

#sizeObject Also known as: length



67
68
69
# File 'lib/xamplr/indexed-array.rb', line 67

def size
  @store.size
end

#sortObject



95
96
97
# File 'lib/xamplr/indexed-array.rb', line 95

def sort
  @store.sort { | a, b | yield(a, b) }
end

#sort!Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/xamplr/indexed-array.rb', line 99

def sort!
  arr = []
  @map.each do |index, pos|
    arr << [index, @store[pos]]
  end
  arr.sort! do |a, b|
    yield(a[1], b[1])
  end
  @map = {}
  @store = []
  arr.each do |pair|
    @map[pair[0]] = @store.size
    @store << pair[1]
  end
end