Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/hdb/hdb.rb,
lib/hdb/hodb.rb,
lib/hdb/hmysql.rb,
lib/hdb/hmysql2.rb,
lib/hdatastructures/hlist.rb

Direct Known Subclasses

HFieldTable

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.testObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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

Instance Method Details

#<(array) ⇒ Object



29
30
31
32
33
34
35
36
37
# 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) ⇒ Object



13
14
15
16
17
18
# File 'lib/hdatastructures/hlist.rb', line 13

def addIfNotPresent(value)

  self << value unless self.include?(value)
  return value

end

#column(fieldName, recordKey = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# 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

#filter(fields) ⇒ Object

the follow function didn’t use return a table that contains only fields table = [b:2, c:3, b:5, c:6] table.filter([:a, :c]) => [c:3, c:6]



11
12
13
14
15
16
17
18
19
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object

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]

Sorting by qty

price:3, price:2, price:1].herbertsort { |data| data[:qty

} =>

:price=>3, :price=>2, :price=>1

Sorting by qty and by price

price:3, price:2, price:1].herbertsort { |data| [data, data

}



64
65
66
# 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 = '"') ⇒ Object



42
43
44
45
46
47
# File 'lib/hdb/hdb.rb', line 42

def hjoin(separator, quoteChar = '"')

  arr = []
  self.each { |value| arr << "#{quoteChar}#{value}#{quoteChar}" }
  return arr.join(separator)
end

#hpartitionObject



39
40
41
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object

if list = [3,2,1,0] => it reverses the @table records



34
35
36
37
38
39
40
41
42
43
# 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) ⇒ Object



24
25
26
27
# 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_formatObject

used in HWidget::buildSignature



49
50
51
52
53
54
55
56
# 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