Skip to content

Commit

Permalink
Added lset group_count operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Tan authored and Anthony Tan committed Nov 5, 2017
1 parent 3f417b3 commit 507ee8d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/bud/lattice-lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,19 @@ 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|
key = []
key_cols.each do |ind|
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
72 changes: 72 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,50 @@ 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_product
i = SetProduct.new
i.tick
Expand Down

0 comments on commit 507ee8d

Please sign in to comment.