class Symian::SortedArray

the SortedArray class was taken from the ruby cookbook

Public Class Methods

new(*args, &sort_by) click to toggle source
Calls superclass method
# File lib/symian/sorted_array.rb, line 4
def initialize(*args, &sort_by)
  @sort_by = sort_by || Proc.new { |x,y| x <=> y }
  super(*args)
  sort! &sort_by
end

Public Instance Methods

<<(el) click to toggle source
# File lib/symian/sorted_array.rb, line 31
def <<(el)
  insert(0, el)
end
Also aliased as: push, unshift
insert(i, v) click to toggle source
Calls superclass method
# File lib/symian/sorted_array.rb, line 10
def insert(i, v)
  if size == 0 or v < self[0]
    super(0, v) 
  elsif v > self[-1]
    super(-1, v) 
  else
    left = 0
    right = size - 1
    middle = (left + right)/2
    while left < right
      if v >= self[middle]
        left = middle + 1
      else
        right = middle
      end
      middle = (left + right)/2
    end
    super(middle, v)
  end
end
push(el)
Alias for: <<
reverse!() click to toggle source
# File lib/symian/sorted_array.rb, line 53
def reverse!
  # do nothing: reversing the array would disorder it.
end
unshift(el)
Alias for: <<