Skip to content

Commit

Permalink
Use a HashSet in MultiMap
Browse files Browse the repository at this point in the history
Also, rename NewMap to NewHashMap

Signed-off-by: Elazar Gershuni <[email protected]>
  • Loading branch information
elazarg committed Jun 25, 2024
1 parent 54d1a32 commit 77f2879
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ A collection of Golang packages with models for cartesian products and network r
* `Triple` - A simple generic triple
* `HashMap` - A generic map for mapping any Hashable key to any Comparable.
* `HashSet` - A generic `Set` for storing any Hashable.
* `MultiMap` - A map for mapping any Hashable key to a set of Comparable values.
* `MultiMap` - A map for mapping any Hashable key to a set of Hashable values.
* `ProductLeft` - A `Product` of two sets, implemented using a map where each key-values pair represents the cartesian product of the two sets.
* `LeftTripleSet`, RightTripleSet, OuterTripleSet - `TripleSet` implementations.
* `DisjointSum` - A sum type for two tagged sets.
Expand Down
6 changes: 3 additions & 3 deletions pkg/ds/hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type HashMap[K Hashable[K], V Comparable[V]] struct {
m map[int][]Pair[K, V]
}

// NewMap creates a new empty hash map.
func NewMap[K Hashable[K], V Comparable[V]]() *HashMap[K, V] {
// NewHashMap creates a new empty hash map.
func NewHashMap[K Hashable[K], V Comparable[V]]() *HashMap[K, V] {
return &HashMap[K, V]{m: map[int][]Pair[K, V]{}}
}

Expand Down Expand Up @@ -53,7 +53,7 @@ func (m *HashMap[K, V]) Insert(k K, v V) {

// Copy returns a deep copy of the map.
func (m *HashMap[K, V]) Copy() *HashMap[K, V] {
res := NewMap[K, V]()
res := NewHashMap[K, V]()
for _, p := range m.Pairs() {
res.Insert(p.Left, p.Right)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/ds/hashmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func less(p1, p2 ds.Pair[Int, Int]) bool {
func assertMapEmpty(t *testing.T, m *Map) {
t.Helper()
assertEmpty(t, m)
require.True(t, m.Equal(ds.NewMap[Int, Int]()))
require.True(t, m.Equal(ds.NewHashMap[Int, Int]()))
_, ok := m.At(Int{1})
require.False(t, ok)

Expand Down Expand Up @@ -65,7 +65,7 @@ func assertMapSingle(t *testing.T, m *Map, key, value int) {
require.True(t, values[0].int == value)
}

m1 := ds.NewMap[Int, Int]()
m1 := ds.NewHashMap[Int, Int]()
assertNotEqual(t, m, m1)

m1.Insert(Int{key}, Int{value})
Expand Down Expand Up @@ -135,7 +135,7 @@ func assertMapDouble(t *testing.T, m *Map, key1, value1, key2, value2 int) {
require.True(t, values[1].int == v2)
}

m1 := ds.NewMap[Int, Int]()
m1 := ds.NewHashMap[Int, Int]()
assertNotEqual(t, m, m1)

m1.Insert(Int{key2}, Int{value2})
Expand All @@ -157,7 +157,7 @@ func assertMapDouble(t *testing.T, m *Map, key1, value1, key2, value2 int) {
func TestMap(t *testing.T) {
var m, dupl *Map

m = ds.NewMap[Int, Int]()
m = ds.NewHashMap[Int, Int]()
assertMapEmpty(t, m)
m.Delete(Int{1})
assertMapEmpty(t, m)
Expand Down
10 changes: 5 additions & 5 deletions pkg/ds/product_left.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ProductLeft[K Set[K], V Set[V]] struct {
// NewProductLeft with parameters [K, V] creates an empty Product[K, V] object,
// implemented using K sets are keys and V sets as values.
func NewProductLeft[K Set[K], V Set[V]]() *ProductLeft[K, V] {
return &ProductLeft[K, V]{m: NewMap[K, V]()}
return &ProductLeft[K, V]{m: NewHashMap[K, V]()}
}

// CartesianPairLeft returns a new Product object holding the cartesian product of the input sets k x v.
Expand Down Expand Up @@ -128,7 +128,7 @@ func (m *ProductLeft[K, V]) Union(other Product[K, V]) Product[K, V] {
if other.IsEmpty() {
return m.Copy()
}
remainingFromSelf := NewMap[K, K]()
remainingFromSelf := NewHashMap[K, K]()
for _, k := range m.m.Keys() {
remainingFromSelf.Insert(k, k)
}
Expand Down Expand Up @@ -221,17 +221,17 @@ func (m *ProductLeft[K, V]) canonicalize() {
m.m.Delete(k)
}
}
newM := NewMap[K, V]()
newM := NewHashMap[K, V]()
for _, p := range InverseMap(m.m).MultiPairs() {
items := p.Value.Items()
items := p.Right.Items()

Check failure on line 226 in pkg/ds/product_left.go

View workflow job for this annotation

GitHub Actions / golangci-lint

p.Right undefined (type MultiPair[V, K] has no field or method Right)

Check failure on line 226 in pkg/ds/product_left.go

View workflow job for this annotation

GitHub Actions / golangci-lint

p.Right undefined (type MultiPair[V, K] has no field or method Right)

Check failure on line 226 in pkg/ds/product_left.go

View workflow job for this annotation

GitHub Actions / golangci-lint

p.Right undefined (type MultiPair[V, K] has no field or method Right)

Check failure on line 226 in pkg/ds/product_left.go

View workflow job for this annotation

GitHub Actions / build-and-test

p.Right undefined (type MultiPair[V, K] has no field or method Right)
if len(items) == 0 {
continue
}
newKey := items[0]
for _, v := range items[1:] {
newKey = newKey.Union(v)
}
newM.Insert(newKey, p.Key)
newM.Insert(newKey, p.Left)

Check failure on line 234 in pkg/ds/product_left.go

View workflow job for this annotation

GitHub Actions / golangci-lint

p.Left undefined (type MultiPair[V, K] has no field or method Left) (typecheck)

Check failure on line 234 in pkg/ds/product_left.go

View workflow job for this annotation

GitHub Actions / golangci-lint

p.Left undefined (type MultiPair[V, K] has no field or method Left)) (typecheck)

Check failure on line 234 in pkg/ds/product_left.go

View workflow job for this annotation

GitHub Actions / golangci-lint

p.Left undefined (type MultiPair[V, K] has no field or method Left)) (typecheck)

Check failure on line 234 in pkg/ds/product_left.go

View workflow job for this annotation

GitHub Actions / build-and-test

p.Left undefined (type MultiPair[V, K] has no field or method Left)
}
m.m = newM
}
Expand Down

0 comments on commit 77f2879

Please sign in to comment.