Class SparseArray
In: lib/hashery/sparsearray.rb
Parent: Hash

SparseArray

SparseArray is an implemenation of the Array class using only Hashes. Regular Arrays are never used except once to delegate the pack method, (and *args parameters of course). SparseArray is almost fully compatible with Array. There are still a few missing methods that came in with Ruby 1.9 that need to be added, and negative indexes are not quite fully supported yet.

Benchmarks comparing Ruby 1.6 circa 2004 compared to Ruby 1.8.7 circa 2010, show that Ruby‘s Array implementation has improved quite a bit. Where as Array was about 2-4 times faster than SparseArray in 2004, it is now over 10x faster, and able to handle large sparse arrays quite easily. Though surely SparseArray could still be improved, SparseArray is little more than an interesting novelty at this point, as opposed to a useful class, but we will keep her nonetheless for simple interests sake.

NOTE: SparseArray is also the first piece of code I used TDD to create.

Copyright (c) 2004 Thomas Sawyer

Methods

&   *   +   -   <<   <=>   ===   []   []   []=   assoc   at   collect   collect!   compact   compact!   concat   count   delete   delete_at   delete_if   each   each_index   eql?   fill   first   flatten   flatten!   include?   insert   join   last   map!   new   new_h   nitems   pack   pop   push   qsort   rassoc   reindex   reindex!   reject!   reverse   reverse!   reverse_each   rindex   shift   shuffle   shuffle!   slice   slice!   sort   sort!   to_a   to_ary   to_h   to_s   uniq   uniq!   unshift   values_at   |  

External Aliases

[] -> get
[]= -> set
delete -> qdelete
 
delete_if -> qdelete_if

Public Class methods

Public Instance methods

TODO: Ranges with negative indexes not yet supported.

empty? okay as is

map!()

Alias for collect!

def replace(ha)

  if ha.length < self.length
    (ha.length..self.length-1).each { |i| self.delete(i) }
    (0..ha.length-1).each { |i| self.set(i,ha[i]) }
  end

end

[Validate]