class Array
Public Class Methods
test()
click to toggle source
# File lib/hdatastructures/hlist.rb, line 78 def self.test() list = [] #(20).downto(1) { |i| for i in 0..9 list.insertLast(HRecord.new(i)) end list.deleteFirst() list.deleteLast() list.deleteAt(2) list.insert(0, HRecord.new(0)) list.insert(3, "ciao") list.insertLast(HRecord.new(9)) list[15] = 20 list[16] = :end list.swap(15, 16) list.show() p list.indexOf(HRecord.new(9)) p [1,6,5,4,3,23,2].herbertsort end
Public Instance Methods
<(array)
click to toggle source
# File lib/hdatastructures/hlist.rb, line 29 def < (array) for i in 0...self.count return true unless self[i] return false unless array[i] return true if self[i] < array[i] return false if self[i] > array[i] end return false end
addIfNotPresent(value)
click to toggle source
# File lib/hdatastructures/hlist.rb, line 13 def addIfNotPresent(value) self << value unless self.include?(value) return value end
column(fieldName, recordKey = nil)
click to toggle source
# File lib/hdb/hodb.rb, line 45 def column(fieldName, recordKey = nil) result = [] self.each do |row| data = row[fieldName.to_s] data = data.value(recordKey) if data.class == HRecord and recordKey result << data end return result end
deleteAt(index)
click to toggle source
# File lib/hdatastructures/hlist.rb, line 20 def deleteAt(index) self.delete_at(index) if index end
filter(fields)
click to toggle source
the follow function didn't use return a table that contains only fields table = [{a:1, b:2, c:3}, {a:4, b:5, c:6}] table.filter([:a, :c]) => [{a:1, c:3}, {a:4, c:6}]
# File lib/hdb/hodb.rb, line 11 def filter(fields) result = [] self.each do |row| result << row.select { |key, value| fields.include?(key) } end return result end
herbertsort(asc = true, &block)
click to toggle source
# File lib/hdatastructures/hlist.rb, line 43 def herbertsort(asc = true, &block) #block = lambda { |data| data } unless block block = Proc.new { |data| data } unless block return [] if empty? pivot = delete_at(rand(size)) left, right = hpartition { |value| block.call(value) < block.call(pivot) } if asc left, right = hpartition { |value| !(block.call(value) < block.call(pivot)) } unless asc return *left.herbertsort(asc, &block), pivot, *right.herbertsort(asc, &block) end
herbertsort!(asc = true, &block)
click to toggle source
- 4,2,1,5,3].herbertsort => [1, 2, 3, 4, 5
- [4,0],,[1,0],,[3,0]].herbertsort => [[1, 0], [2, 0], [3, 0], [4, 0], [5, 0]
- [4,1],,[1,0],,[3,0]].herbertsort => [[1, 0], [2, 0], [3, 0], [4, 0], [4, 1]
- [4,0],,[1,0],,[3,0]].herbertsort => [[1, 0], [2, 0], [3, 0], [4, 0], [5, 0]
-
Sorting by qty
# File lib/hdatastructures/hlist.rb, line 64 def herbertsort!(asc = true, &block) self.herbertsort(asc, &block).each_with_index { |data, i| self[i] = data } end
hjoin(separator, quoteChar = '"')
click to toggle source
# File lib/hdb/hdb.rb, line 42 def hjoin(separator, quoteChar = '"') arr = [] self.each { |value| arr << "#{quoteChar}#{value}#{quoteChar}" } return arr.join(separator) end
hpartition() { |value| ... }
click to toggle source
# File lib/hdatastructures/hlist.rb, line 39 def hpartition return self.select { |value| value if yield(value) }, self.select { |value| value unless yield(value) } end
show(key = :key)
click to toggle source
# File lib/hdatastructures/hlist.rb, line 68 def show(key = :key) self.each do |data| if data.class == HRecord data.show(key) else puts data.to_s end end end
sortByList(fieldName, list)
click to toggle source
# File lib/hdb/hodb.rb, line 22 def sortByList(fieldName, list) return self unless list hash = {} self.each { |row| hash[row[fieldName]] = row } list.each_with_index { |id, i| self[i] = hash[id] } return self end
sortByPositionList(list)
click to toggle source
if list = [3,2,1,0] => it reverses the @table records
# File lib/hdb/hodb.rb, line 34 def sortByPositionList(list) return self unless list tableCopy = [] self.each_with_index { |row, i| tableCopy[i] = row } list.each_with_index { |id, i| self[i] = tableCopy[list[i]] } return self end
swap(index1, index2)
click to toggle source
# File lib/hdatastructures/hlist.rb, line 24 def swap(index1, index2) # Parallel Assignment: eg a,b = 1,2 self[index1], self[index2] = self[index2], self[index1] end
to_js_format()
click to toggle source
# File lib/hdb/hdb.rb, line 49 def to_js_format # used in HWidget::buildSignature result = [] self.each do |value| tmp = (value.class <= String) ? "#{value.to_js_format}" : value result << tmp end return "[#{result.join(", ")}]" end