Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions arraydeque/arraydeque.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,27 @@ type ArrayDeque[T any] struct {

// config holds the values for configuring a ArrayDeque.
type config struct {
Capacity int
capacity int
}

// Option configures a ArrayDeque config
type Option func(*config)

// WithCapacity configures the initial capacity of the ArrayDeque.
func WithCapacity(capacity int) Option {
return func(c *config) {
c.capacity = capacity
}
}

// New creates an empty ArrayDeque whose initial size is 0.
func New[T any](opts ...Option) *ArrayDeque[T] {
config := defaultConfig()
for _, option := range opts {
option(config)
}
return &ArrayDeque[T]{
slice: make([]T, config.Capacity),
slice: make([]T, config.capacity),
}
}

Expand Down Expand Up @@ -262,6 +269,6 @@ func (d *ArrayDeque[T]) resize() {

func defaultConfig() *config {
return &config{
Capacity: DefaultCapacity,
capacity: DefaultCapacity,
}
}
4 changes: 2 additions & 2 deletions arraydeque/arraydeque_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func BenchmarkArrayDeque_AddRemove(b *testing.B) {
}

func BenchmarkArrayDeque_AddFront_Preallocated(b *testing.B) {
d := New[int](func(c *config) { c.Capacity = b.N })
d := New[int](func(c *config) { c.capacity = b.N })
b.ResetTimer()
for i := 0; i < b.N; i++ {
d.AddFront(i)
}
}

func BenchmarkArrayDeque_AddBack_Preallocated(b *testing.B) {
d := New[int](func(c *config) { c.Capacity = b.N })
d := New[int](func(c *config) { c.capacity = b.N })
b.ResetTimer()
for i := 0; i < b.N; i++ {
d.AddBack(i)
Expand Down
11 changes: 10 additions & 1 deletion arraydeque/arraydeque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,23 @@ func TestNew(t *testing.T) {
},
{
name: "zero_capacity",
opts: []Option{func(c *config) { c.Capacity = 0 }},
opts: []Option{WithCapacity(0)},
check: func(t *testing.T, d *ArrayDeque[int]) {
d.Add(1) // Should not panic
if size := d.Size(); size != 1 {
t.Errorf("expected size 1, got: %d", size)
}
},
},
{
name: "custom_capacity",
opts: []Option{WithCapacity(10)},
check: func(t *testing.T, d *ArrayDeque[int]) {
if cap(d.slice) != 10 {
t.Errorf("expected capacity 10, got: %d", cap(d.slice))
}
},
},
}

for _, tc := range cases {
Expand Down
26 changes: 13 additions & 13 deletions bitset/bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
)

const (
DefaultNumBits = 64
wordSize = 64
wordFmt = "%016X"
DefaultCapacity = 64
wordSize = 64
wordFmt = "%016X"
)

// BitSet represents a vector of bits that grows as needed.
Expand All @@ -27,31 +27,31 @@ var _ collections.MutableNavigableSet[int] = (*BitSet)(nil)

// config holds the values for configuring a BitSet.
type config struct {
numBits int
capacity int
}

// Option configures a BitSet config
type Option func(*config)

// NumBits provides the option to set the number of bits used in a BitSet.
func NumBits(n int) Option {
// WithCapacity provides the option to set the number of bits used in a BitSet.
func WithCapacity(n int) Option {
return func(c *config) {
c.numBits = n
c.capacity = n
}
}

// New creates a BitSet whose initial size is large enough to explicitly
// represent bits with indices in the range 0 through NumBits-1. If no
// configuration is used the DefaultNumBits is used as the number of bits.
// configuration is used the DefaultCapacity is used as the number of bits.
// All bits are initially false.
func New(opts ...Option) *BitSet {
config := defaultConfig()
for _, option := range opts {
option(config)
}
ensureNonNegative(config.numBits)
ensureNonNegative(config.capacity)
return &BitSet{
bits: make([]uint64, (config.numBits/wordSize)+min(1, config.numBits%wordSize)),
bits: make([]uint64, (config.capacity/wordSize)+min(1, config.capacity%wordSize)),
maxWordInUse: 0,
size: 0,
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func (b *BitSet) FlipRange(start int, end int) {

// FromBytes returns new BitSet containing all the bits in the given byte array.
func FromBytes(bytes []byte) *BitSet {
b := New(NumBits(len(bytes) * 8))
b := New(WithCapacity(len(bytes) * 8))
k := 0
for i := 0; i < len(bytes); i += 8 {
word := uint64(0)
Expand Down Expand Up @@ -252,7 +252,7 @@ func convert(bit int) (int, int) {

func defaultConfig() *config {
return &config{
numBits: DefaultNumBits,
capacity: DefaultCapacity,
}
}

Expand Down Expand Up @@ -381,7 +381,7 @@ func (b *BitSet) RetainAll(col collections.Collection[int]) {
}

// For generic non-set collections, build a temporary BitSet to avoid O(N*M).
temp := New(NumBits(b.Capacity()))
temp := New(WithCapacity(b.Capacity()))
for v := range col.All() {
if b.Contains(v) {
temp.SetBit(v)
Expand Down
12 changes: 6 additions & 6 deletions bitset/bitset_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
)

func BenchmarkSetBit(b *testing.B) {
bs := New(NumBits(10000))
bs := New(WithCapacity(10000))
b.ResetTimer()
for i := 0; i < b.N; i++ {
bs.SetBit(i % 10000)
}
}

func BenchmarkGetBit(b *testing.B) {
bs := New(NumBits(10000))
bs := New(WithCapacity(10000))
for i := 0; i < 10000; i++ {
if i%2 == 0 {
bs.SetBit(i)
Expand All @@ -26,7 +26,7 @@ func BenchmarkGetBit(b *testing.B) {
}

func BenchmarkClearBit(b *testing.B) {
bs := New(NumBits(10000))
bs := New(WithCapacity(10000))
for i := 0; i < 10000; i++ {
bs.SetBit(i)
}
Expand All @@ -37,15 +37,15 @@ func BenchmarkClearBit(b *testing.B) {
}

func BenchmarkFlipRange(b *testing.B) {
bs := New(NumBits(100_000))
bs := New(WithCapacity(100_000))
b.ResetTimer()
for i := 0; i < b.N; i++ {
bs.FlipRange(0, 100_000)
}
}

func BenchmarkSetBits(b *testing.B) {
bs := New(NumBits(100_000))
bs := New(WithCapacity(100_000))
for i := 0; i < 100_000; i++ {
if i%2 == 0 {
bs.SetBit(i)
Expand All @@ -68,7 +68,7 @@ func BenchmarkPrimesLessThan(b *testing.B) {

func BenchmarkSetBits_Sparse(b *testing.B) {
// Allocate 1 million bits
bs := New(NumBits(1_000_000))
bs := New(WithCapacity(1_000_000))
// Only set a few bits at the very beginning
bs.SetBit(0)
bs.SetBit(5)
Expand Down
4 changes: 2 additions & 2 deletions bitset/bitset_mutable_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestBitSet_MutableSet(t *testing.T) {
t.Parallel()
b := New(NumBits(100))
b := New(WithCapacity(100))
b.Add(10)
b.Add(20)

Expand Down Expand Up @@ -51,7 +51,7 @@ func TestBitSet_MutableSet(t *testing.T) {
t.Errorf("AddAll failed")
}

other := New(NumBits(100))
other := New(WithCapacity(100))
other.Add(2)
other.Add(3)
other.Add(4)
Expand Down
Loading
Loading