Class: KXI::Collections::Enumerable Abstract
- Inherits:
-
Object
- Object
- KXI::Collections::Enumerable
- Defined in:
- lib/kxi/collections/enumerable.rb
Overview
Order of items is not guaranteed
Allows it’s superclass to be enumerated
Direct Known Subclasses
Instance Method Summary collapse
-
#aggregate(start = nil) {|current, item| ... } ⇒ Object
Aggregates all items of collection.
-
#all {|item| ... } ⇒ Bool
Checks if all items in collection satisfy given function.
-
#any ⇒ Bool
Checks if collection contains any item.
-
#can_alter? ⇒ Bool
Indicates whether collection can be altered.
-
#count ⇒ Number
Counts items in collection.
-
#enumerator {|enumerator| ... } ⇒ nil
Allows protected usage of Enumerator within bounds of block.
-
#first(df = nil) ⇒ Object
Gets first item, or default.
-
#first! ⇒ Object
Gets first item.
-
#foreach {|item| ... } ⇒ nil
Calls block with each item of collection.
-
#initialize ⇒ Enumerable
constructor
Instantiates the Enumerable class.
-
#last(df = nil) ⇒ Object
Gets last item, or default.
-
#last! ⇒ Object
Gets last item.
-
#max(&block) ⇒ Object
Obtains item with the first maximal value.
-
#min(&block) ⇒ Object
Obtains item with the first minimal value.
-
#of_type(*klass) ⇒ KXI::Collections::ArrayCollection
Selects only those items that are of at least one of specified types.
-
#of_type!(*klass) ⇒ KXI::Collections::ArrayCollection
Selects only those items that are exactly of at least one of specified types.
-
#order_by(&block) ⇒ KXI::Collections::ArrayCollection
Orders collection to ascending order.
-
#order_by_descending(&block) ⇒ KXI::Collections::ArrayCollection
Orders collection to descending order.
-
#select {|item| ... } ⇒ KXI::Collections::ArrayCollection
Selects specific values.
-
#select_many {|item| ... } ⇒ KXI::Collections::ArrayCollection
Concatenates selected collections of items.
-
#skip(count) ⇒ KXI::Collections::ArrayCollection
Skips given number of items and returns the rest.
-
#take(count) ⇒ KXI::Collections::ArrayCollection
Returns first N elements.
-
#to_array ⇒ Array
Coverts collection to array.
-
#where {|item| ... } ⇒ KXI::Collections::ArrayCollection
Selects only those items that satisfy given function.
Constructor Details
#initialize ⇒ Enumerable
Instantiates the KXI::Collections::Enumerable class
10 11 12 13 |
# File 'lib/kxi/collections/enumerable.rb', line 10 def initialize @locks = 0 @alter = false end |
Instance Method Details
#aggregate(start = nil) {|current, item| ... } ⇒ Object
Aggregates all items of collection
315 316 317 318 319 320 |
# File 'lib/kxi/collections/enumerable.rb', line 315 def aggregate(start = nil) foreach do |item| start = yield(start, item) end return start end |
#all {|item| ... } ⇒ Bool
Checks if all items in collection satisfy given function
158 159 160 161 162 163 |
# File 'lib/kxi/collections/enumerable.rb', line 158 def all foreach do |item| return false unless yield(item) end return true end |
#any ⇒ Bool #any {|item| ... } ⇒ Bool
Checks if collection contains any item
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/kxi/collections/enumerable.rb', line 139 def any if block_given? foreach do |item| return true if yield(item) end return false else enumerator do |e| return false unless e.rewind return true end end end |
#can_alter? ⇒ Bool
Indicates whether collection can be altered
502 503 504 |
# File 'lib/kxi/collections/enumerable.rb', line 502 def can_alter? @locks == 0 and not @alter end |
#count ⇒ Number #count {|item| ... } ⇒ Number
Counts items in collection
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/kxi/collections/enumerable.rb', line 110 def count count = 0 if block_given? foreach do |item| count = count + 1 if yield(item) end else enumerator do |e| return 0 unless e.rewind count += 1 while e.next count = count + 1 end end end return count end |
#enumerator {|enumerator| ... } ⇒ nil
Allows protected usage of KXI::Collections::Enumerator within bounds of block
21 22 23 24 25 26 |
# File 'lib/kxi/collections/enumerable.rb', line 21 def enumerator lock do yield(create_enumerator) end return nil end |
#first(df = nil) ⇒ Object? #first(df = nil) {|item| ... } ⇒ Object?
Gets first item, or default
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/kxi/collections/enumerable.rb', line 177 def first(df = nil) if block_given? foreach do |item| return item if yield(item) end else enumerator do |e| return e.current if e.rewind end end return df end |
#first! ⇒ Object? #first! {|item| ... } ⇒ Object?
Gets first item
203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/kxi/collections/enumerable.rb', line 203 def first! if block_given? foreach do |item| return item if yield(item) end else enumerator do |e| return e.current if e.rewind end end raise(KXI::Exceptions::CollectionException.new("Collection contains no #{(block_given? ? 'such ' : '')}items!")) end |
#foreach {|item| ... } ⇒ nil
Calls block with each item of collection
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/kxi/collections/enumerable.rb', line 41 def foreach enumerator do |e| return nil unless e.rewind loop do yield(e.current) break unless e.next end end return nil end |
#last(df = nil) ⇒ Object? #last(df = nil) {|item| ... } ⇒ Object?
Gets last item, or default
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/kxi/collections/enumerable.rb', line 228 def last(df = nil) found = false ret = nil if block_given? foreach do |item| if yield(item) ret = item found = true end end else foreach do |item| ret = item found = true end end return df unless found return ret end |
#last! ⇒ Object? #last! {|item| ... } ⇒ Object?
Gets last item
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/kxi/collections/enumerable.rb', line 260 def last! found = false ret = nil if block_given? foreach do |item| if yield(item) ret = item found = true end end else foreach do |item| ret = item found = true end end raise(KXI::Exceptions::CollectionException.new("Collection contains no #{(block_given? ? 'such ' : '')}items!")) unless found return ret end |
#max ⇒ Object #max {|what, with| ... } ⇒ Object
Obtains item with the first maximal value
430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/kxi/collections/enumerable.rb', line 430 def max(&block) block = lambda { |a, b| a <=> b } if block == nil ret = nil first = true foreach do |i| if first ret = i first = false elsif block.call(i, ret) > 0 ret = i end end return ret end |
#min ⇒ Object #min {|what, with| ... } ⇒ Object
Obtains item with the first minimal value
456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/kxi/collections/enumerable.rb', line 456 def min(&block) block = lambda { |a, b| a <=> b } if block == nil ret = nil first = true foreach do |i| if first ret = i first = false elsif block.call(i, ret) < 0 ret = i end end return ret end |
#of_type(*klass) ⇒ KXI::Collections::ArrayCollection
Selects only those items that are of at least one of specified types
389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/kxi/collections/enumerable.rb', line 389 def of_type(*klass) ret = [] foreach do |i| klass.each do |k| if i.is_a?(k) ret.push(i) break end end end return KXI::Collections::ArrayCollection.new(ret) end |
#of_type!(*klass) ⇒ KXI::Collections::ArrayCollection
Selects only those items that are exactly of at least one of specified types
406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/kxi/collections/enumerable.rb', line 406 def of_type!(*klass) ret = [] foreach do |i| klass.each do |k| if i.class == k ret.push(i) break end end end return KXI::Collections::ArrayCollection.new(ret) end |
#order_by ⇒ KXI::Collections::ArrayCollection #order_by {|what, with| ... } ⇒ KXI::Collections::ArrayCollection
Ordering is done using merge sort algorithm
Orders collection to ascending order
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/kxi/collections/enumerable.rb', line 335 def order_by(&block) block = lambda { |a, b| a <=> b } if block == nil groups = [] g = [] foreach do |item| if g.length == 0 g.push(item) else if block.call(g.last, item) <= 0 g.push(item) else groups.push(g) g = [item] end end end groups.push(g) if g.length > 0 return KXI::Collections::ArrayCollection.new if groups.length == 0 while groups.length > 1 merges = [] while groups.length >= 2 merges.push(merge(groups.shift, groups.shift, &block)) end merges.push(groups[0]) if groups.length > 0 groups = merges end return KXI::Collections::ArrayCollection.new(groups[0]) end |
#order_by ⇒ KXI::Collections::ArrayCollection #order_by {|what, with| ... } ⇒ KXI::Collections::ArrayCollection
Ordering is done using merge sort algorithm
Orders collection to descending order
377 378 379 380 381 382 383 |
# File 'lib/kxi/collections/enumerable.rb', line 377 def order_by_descending(&block) if block == nil return order_by { |a, b| b <=> a } else return order_by { |a, b| -block.call(a, b) } end end |
#select {|item| ... } ⇒ KXI::Collections::ArrayCollection
Selects specific values
57 58 59 60 61 62 63 |
# File 'lib/kxi/collections/enumerable.rb', line 57 def select ret = [] foreach do |i| ret.push(yield(i)) end return KXI::Collections::ArrayCollection.new(ret) end |
#select_many {|item| ... } ⇒ KXI::Collections::ArrayCollection
Concatenates selected collections of items
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/kxi/collections/enumerable.rb', line 71 def select_many ret = [] foreach do |i| col = yield(i) if col.is_a?(Array) col.each { |j| ret.push(j) } elsif col.is_a?(Enumerable) col.foreach { |j| ret.push(j) } else raise(KXI::Exceptions::CollectionException.new('Selection function returned invalid type of object!')) end end return KXI::Collections::ArrayCollection.new(ret) end |
#skip(count) ⇒ KXI::Collections::ArrayCollection
Skips given number of items and returns the rest
283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/kxi/collections/enumerable.rb', line 283 def skip(count) ret = [] foreach do |item| if count > 0 count -= 1 else ret.push(item) end end return KXI::Collections::ArrayCollection.new(ret) end |
#take(count) ⇒ KXI::Collections::ArrayCollection
Returns first N elements
298 299 300 301 302 303 304 305 306 |
# File 'lib/kxi/collections/enumerable.rb', line 298 def take(count) ret = [] foreach do |item| break unless count > 0 count -= 1 ret.push(item) end return KXI::Collections::ArrayCollection.new(ret) end |
#to_array ⇒ Array
Coverts collection to array
473 474 475 476 477 |
# File 'lib/kxi/collections/enumerable.rb', line 473 def to_array ret = [] foreach { |item| ret.push(item) } return ret end |
#where {|item| ... } ⇒ KXI::Collections::ArrayCollection
Selects only those items that satisfy given function
91 92 93 94 95 96 97 |
# File 'lib/kxi/collections/enumerable.rb', line 91 def where ret = [] foreach do |i| ret.push(i) if yield(i) end return KXI::Collections::ArrayCollection.new(ret) end |