-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c18bf92
commit dc82206
Showing
4 changed files
with
199 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package mm | ||
|
||
type Vector[T any] struct { | ||
data []T | ||
cap int | ||
len int | ||
} | ||
|
||
func createVector[T any](len int, cap int) *Vector[T] { | ||
vector := Alloc[Vector[T]]() | ||
vector.cap = cap | ||
vector.len = len | ||
vector.data = AllocMany[T](vector.cap) | ||
|
||
return vector | ||
} | ||
|
||
// NewVector creates a new empty vector, if args not provided | ||
// it will create an empty vector, if only one arg is provided | ||
// it will init a vector with len and cap equal to the provided arg, | ||
// if two args are provided it will init a vector with len = args[0] cap = args[1] | ||
func NewVector[T any](args ...int) *Vector[T] { | ||
switch len(args) { | ||
case 0: | ||
return createVector[T](0, 1) | ||
case 1: | ||
return createVector[T](args[0], args[0]) | ||
default: | ||
return createVector[T](args[0], args[1]) | ||
} | ||
} | ||
|
||
// InitVector initializes a new vector with the T elements provided and sets | ||
// it's len and cap to len(values) | ||
func InitVector[T any](values ...T) *Vector[T] { | ||
vector := createVector[T](len(values), len(values)) | ||
for _, v := range values { | ||
vector.data[0] = v | ||
} | ||
return vector | ||
} | ||
|
||
// Push pushes T to the vector, grows if needed. | ||
func (v *Vector[T]) Push(value T) { | ||
if v.len == v.cap { | ||
v.data = Reallocate(v.data, v.cap*2) | ||
v.cap *= 2 | ||
} | ||
|
||
v.data[v.len] = value | ||
v.len++ | ||
} | ||
|
||
// Pop pops T from the vector | ||
func (v *Vector[T]) Pop() T { | ||
v.len-- | ||
return v.data[v.len] | ||
} | ||
|
||
// Len gets vector len | ||
func (v *Vector[T]) Len() int { | ||
return v.len | ||
} | ||
|
||
// Cap gets vector capacity (underling memory length). | ||
func (v *Vector[T]) Cap() int { | ||
return v.cap | ||
} | ||
|
||
// Slice gets a slice representing the vector | ||
// CAUTION: don't append to this slice, this is only used | ||
// if you want to loop on the vec elements | ||
func (v *Vector[T]) Slice() []T { | ||
return v.data[:v.len] | ||
} | ||
|
||
// Deallocate deallocats the vector | ||
func (v *Vector[T]) Deallocate() { | ||
FreeMany(v.data) | ||
Free(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package mm | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func BenchmarkSlice(b *testing.B) { | ||
for i := 0; i <= b.N; i++ { | ||
numbers := []int{} | ||
|
||
for j := 0; j < LOOP_TIMES; j++ { | ||
numbers = append(numbers, j) | ||
} | ||
|
||
for j := 0; j < LOOP_TIMES; j++ { | ||
// Pop | ||
numbers = numbers[:len(numbers)-1] | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkVector(b *testing.B) { | ||
for i := 0; i <= b.N; i++ { | ||
numbersVec := NewVector[int]() | ||
defer numbersVec.Deallocate() | ||
|
||
for j := 0; j < LOOP_TIMES; j++ { | ||
numbersVec.Push(j) | ||
} | ||
|
||
for j := 0; j < LOOP_TIMES; j++ { | ||
numbersVec.Pop() | ||
} | ||
} | ||
} | ||
|
||
func TestVector(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
v := NewVector[int]() | ||
defer v.Deallocate() | ||
|
||
v.Push(1) | ||
v.Push(2) | ||
v.Push(3) | ||
|
||
assert.Equal(3, v.Len()) | ||
assert.Equal(4, v.Cap()) | ||
assert.Equal([]int{1, 2, 3}, v.Slice()) | ||
assert.Equal(3, v.Pop()) | ||
assert.Equal(2, v.Pop()) | ||
assert.Equal(1, v.Pop()) | ||
} | ||
|
||
func TestVectorInit(t *testing.T) { | ||
t.Run("Init with no args", func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
v := NewVector[int]() | ||
defer v.Deallocate() | ||
|
||
assert.Equal(0, v.Len()) | ||
assert.Equal(1, v.Cap()) | ||
}) | ||
|
||
t.Run("Init with one arg", func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
v := NewVector[int](5) | ||
defer v.Deallocate() | ||
|
||
assert.Equal(5, v.Len()) | ||
assert.Equal(5, v.Cap()) | ||
}) | ||
|
||
t.Run("Init with two args", func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
v := NewVector[int](5, 6) | ||
defer v.Deallocate() | ||
|
||
assert.Equal(5, v.Len()) | ||
assert.Equal(6, v.Cap()) | ||
}) | ||
|
||
t.Run("Init vector with slice", func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
v := InitVector(1, 2, 3) | ||
defer v.Deallocate() | ||
|
||
assert.Equal(3, v.Len()) | ||
assert.Equal(3, v.Cap()) | ||
}) | ||
} |