Skip to content

Commit f42231d

Browse files
committed
chore+feat: refactor triplestore and cardinality types
1 parent f1e40a5 commit f42231d

9 files changed

Lines changed: 396 additions & 196 deletions

File tree

algo/reach.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (s *ReachabilityCache) OrReach(node uint64, direction graph.Direction, dupl
278278
// from the result before the XOR operation.
279279
func (s *ReachabilityCache) XorReach(node uint64, direction graph.Direction, duplex cardinality.Duplex[uint64]) {
280280
// Reach bitmap will contain the member due to resolution of component reach
281-
reachBitmap := s.ReachOfComponentContainingMember(node, direction).Clone()
281+
reachBitmap := s.ReachOfComponentContainingMember(node, direction).Clone().(cardinality.Duplex[uint64])
282282
reachBitmap.Remove(node)
283283

284284
duplex.Xor(reachBitmap)

cardinality/cardinality.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ type DuplexConstructor[T uint32 | uint64] func() Duplex[T]
77
// Provider describes the most basic functionality of a cardinality provider algorithm: adding elements to the provider
88
// and producing the cardinality of those elements.
99
type Provider[T uint32 | uint64] interface {
10-
Add(value ...T)
11-
Or(other Provider[T])
12-
Clear()
1310
Cardinality() uint64
1411
}
1512

@@ -26,13 +23,21 @@ func CloneProvider[T uint32 | uint64](provider Provider[T]) Provider[T] {
2623
}
2724
}
2825

26+
type ImmutableSimplex[T uint32 | uint64] interface {
27+
Provider[T]
28+
29+
Clone() Simplex[T]
30+
}
31+
2932
// Simplex is a one-way cardinality provider that does not allow a user to retrieve encoded values back out of the
3033
// provider. This interface is suitable for algorithms such as HyperLogLog which utilizes a hash function to merge
3134
// identifiers into the cardinality provider.
3235
type Simplex[T uint32 | uint64] interface {
33-
Provider[T]
36+
ImmutableSimplex[T]
3437

35-
Clone() Simplex[T]
38+
Add(value ...T)
39+
Or(other Provider[T])
40+
Clear()
3641
}
3742

3843
// Iterator allows enumeration of a duplex cardinality provider without requiring the allocation of the provider's set.
@@ -41,18 +46,27 @@ type Iterator[T uint32 | uint64] interface {
4146
Next() T
4247
}
4348

49+
type ImmutableDuplex[T uint32 | uint64] interface {
50+
Provider[T]
51+
52+
Slice() []T
53+
Contains(value T) bool
54+
Each(delegate func(value T) bool)
55+
56+
Clone() Duplex[T]
57+
}
58+
4459
// Duplex is a two-way cardinality provider that allows a user to retrieve encoded values back out of the provider. This
4560
// interface is suitable for algorithms that behave similar to bitvectors.
4661
type Duplex[T uint32 | uint64] interface {
47-
Provider[T]
62+
ImmutableDuplex[T]
4863

64+
Add(value ...T)
65+
CheckedAdd(value T) bool
66+
Remove(value T)
67+
Or(other Provider[T])
4968
Xor(other Provider[T])
5069
And(other Provider[T])
5170
AndNot(other Provider[T])
52-
Remove(value T)
53-
Slice() []T
54-
Contains(value T) bool
55-
Each(delegate func(value T) bool)
56-
CheckedAdd(value T) bool
57-
Clone() Duplex[T]
71+
Clear()
5872
}

cardinality/roaring32.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ func (s bitmap32) CheckedAdd(value uint32) bool {
6868
}
6969

7070
func (s bitmap32) Add(values ...uint32) {
71-
s.bitmap.AddMany(values)
71+
switch len(values) {
72+
case 0:
73+
case 1:
74+
s.bitmap.Add(values[0])
75+
default:
76+
s.bitmap.AddMany(values)
77+
}
7278
}
7379

7480
func (s bitmap32) Remove(value uint32) {

cardinality/roaring64.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ func (s bitmap64) CheckedAdd(value uint64) bool {
6868
}
6969

7070
func (s bitmap64) Add(values ...uint64) {
71-
s.bitmap.AddMany(values)
71+
switch len(values) {
72+
case 0:
73+
case 1:
74+
s.bitmap.Add(values[0])
75+
default:
76+
s.bitmap.AddMany(values)
77+
}
7278
}
7379

7480
func (s bitmap64) Remove(value uint64) {

0 commit comments

Comments
 (0)