Class: IndexedArray
- Inherits:
-
Object
- Object
- IndexedArray
- Includes:
- Enumerable
- Defined in:
- lib/xamplr/indexed-array.rb
Instance Method Summary collapse
- #[](index) ⇒ Object (also: #slice)
- #[]=(index, value) ⇒ Object
- #clear ⇒ Object
- #delete(index) ⇒ Object (also: #delete_at)
- #each ⇒ Object
- #each_index ⇒ Object
- #each_key_value ⇒ Object
- #first ⇒ Object
-
#initialize(parent = nil) ⇒ IndexedArray
constructor
A new instance of IndexedArray.
- #keys ⇒ Object
- #last ⇒ Object
- #size ⇒ Object (also: #length)
- #sort ⇒ Object
- #sort! ⇒ Object
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 |
#clear ⇒ Object
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 64 65 66 67 68 69 70 71 |
# 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 } @map.keys.each do |k| v = @map[k] @map[k] = (v - 1) if pos < v end 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 } @map.keys.each do |k| v = @map[k] @map[k] = (v - 1) if pos < v end return @store.delete_at(pos) end end return nil end |
#each ⇒ Object
89 90 91 92 93 |
# File 'lib/xamplr/indexed-array.rb', line 89 def each @store.each do |obj | yield(obj) end end |
#each_index ⇒ Object
95 96 97 |
# File 'lib/xamplr/indexed-array.rb', line 95 def each_index @store.each_index { | i | yield(i) } end |
#each_key_value ⇒ Object
99 100 101 |
# File 'lib/xamplr/indexed-array.rb', line 99 def each_key_value @map.each { | k, v | yield(k, @store[v]) } end |
#first ⇒ Object
81 82 83 |
# File 'lib/xamplr/indexed-array.rb', line 81 def first @store.first end |
#keys ⇒ Object
43 44 45 |
# File 'lib/xamplr/indexed-array.rb', line 43 def keys return @map.keys end |
#last ⇒ Object
85 86 87 |
# File 'lib/xamplr/indexed-array.rb', line 85 def last @store.last end |
#size ⇒ Object Also known as: length
75 76 77 |
# File 'lib/xamplr/indexed-array.rb', line 75 def size @store.size end |
#sort ⇒ Object
103 104 105 |
# File 'lib/xamplr/indexed-array.rb', line 103 def sort @store.sort { | a, b | yield(a, b) } end |
#sort! ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/xamplr/indexed-array.rb', line 107 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 |