Skip to content

Commit

Permalink
Merge pull request #328 from DevelopingCoder/set_group_cnt
Browse files Browse the repository at this point in the history
Added lset group_count operator
  • Loading branch information
jhellerstein authored Nov 22, 2017
2 parents 3f417b3 + 4a980c0 commit 9b665b9
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/bud/lattice-lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,26 @@ def inspect
Bud::BoolLattice.new(@v.member? i)
end

monotone :group_count do |key_cols|
# Assume key_cols for now gives indices
rv = Hash.new(Bud::MaxLattice.new(0))
@v.each do |t|
unless t.class == Array
raise Bud::TypeError, "group_count only works if lset elements are type Array"
end

key = []
key_cols.each do |ind|
if ind >= t.length
raise Bud::Error, "lset element in group_count does not have column index #{ind}"
end
key << t[ind]
end
rv[key] += 1
end
Bud::MapLattice.new(rv)
end

morph :pro do |&blk|
# We don't use Set#map, since it returns an Array (ugh).
rv = Set.new
Expand Down
89 changes: 89 additions & 0 deletions test/tc_lattice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,34 @@ class SetProduct
end
end

class SetSimpleGroupCnt
# Groups by first column
include Bud

state do
lset :s1
lmap :res
end

bloom do
res <= s1.group_count([0])
end
end

class SetMultipleGroupCnt
# Groups by first column and third column
include Bud

state do
lset :s1
lmap :res
end

bloom do
res <= s1.group_count([0, 2])
end
end

class SetEqjoin
include Bud

Expand Down Expand Up @@ -1141,6 +1169,67 @@ def test_set_method_compose
assert_equal(true, i.done.current_value.reveal)
end

def test_set_simple_groupcnt
i = SetSimpleGroupCnt.new
i.tick
expected = Hash.new(Bud::MaxLattice.new(0))
assert_equal(expected, i.res.current_value.reveal)

i.s1 <+ [['a1', 1]]
expected[['a1']] = Bud::MaxLattice.new(1)
i.tick
assert_equal(expected, i.res.current_value.reveal)

i.s1 <+ [['a2', 2]]
expected[['a2']] = Bud::MaxLattice.new(1)
i.tick
assert_equal(expected, i.res.current_value.reveal)

i.s1 <+ [['a1', 3], ['a1', 4]]
expected[['a1']] = Bud::MaxLattice.new(3)
i.tick
assert_equal(expected, i.res.current_value.reveal)
end

def test_set_multiple_groupcnt
i = SetMultipleGroupCnt.new
i.tick
expected = Hash.new(Bud::MaxLattice.new(0))
assert_equal(expected, i.res.current_value.reveal)

i.s1 <+ [['a1', 'b1', 'c1', 1]]
expected[['a1', 'c1']] = Bud::MaxLattice.new(1)
i.tick
assert_equal(expected, i.res.current_value.reveal)

i.s1 <+ [['a2', 'b2', 'c2', 2]]
expected[['a2', 'c2']] = Bud::MaxLattice.new(1)
i.tick
assert_equal(expected, i.res.current_value.reveal)

i.s1 <+ [['a1', 'b3', 'c1', 3], ['a1', 'b4', 'c1', 4]]
expected[['a1', 'c1']] = Bud::MaxLattice.new(3)
i.tick
assert_equal(expected, i.res.current_value.reveal)
end

def test_set_error_groupcnt
i = SetSimpleGroupCnt.new
i.tick

i.s1 <+ ['a1']
assert_raises(Bud::TypeError) do
i.tick
end

i = SetSimpleGroupCnt.new
i.tick
i.s1 <+ [['a1', 1], []]
assert_raises(Bud::Error) do
i.tick
end
end

def test_set_product
i = SetProduct.new
i.tick
Expand Down

0 comments on commit 9b665b9

Please sign in to comment.