Skip to content

Commit c9157ad

Browse files
committed
feat: update documentation
1 parent 39e3ffb commit c9157ad

File tree

12 files changed

+797
-109
lines changed

12 files changed

+797
-109
lines changed

README.md

+604-92
Large diffs are not rendered by default.

batchallocator/batch.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ type BatchAllocator struct {
4747

4848
type BatchAllocatorOption func(alloc *BatchAllocator)
4949

50-
// Option to specify bucket size when creating BatchAllocator
50+
// WithBucketSize Option to specify bucket size when creating BatchAllocator
51+
// You can allocate more memory than the bucketsize in one allocation, it will allocate a new bucket and put the data in it.
5152
func WithBucketSize(size int) BatchAllocatorOption {
5253
return func(alloc *BatchAllocator) {
5354
alloc.bucketSize = size

batchallocator/example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func Example_arena() {
5050
func ExampleWithBucketSize() {
5151
alloc := batchallocator.New(
5252
allocator.NewC(),
53-
batchallocator.WithBucketSize(mm.SizeOf[int]()*15), // configure the allocator to allocate size of 15 ints in one go.
53+
batchallocator.WithBucketSize(mm.SizeOf[int]()*15), // configure the allocator to allocate size of 15 ints per bucket.
5454
)
5555
defer alloc.Destroy()
5656

hashmap/example_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package hashmap
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/joetifa2003/mm-go/allocator"
7+
"github.com/joetifa2003/mm-go/batchallocator"
8+
)
9+
10+
func Example() {
11+
alloc := batchallocator.New(allocator.NewC())
12+
defer alloc.Destroy()
13+
14+
hm := New[int, int](alloc)
15+
defer hm.Free() // can be removed
16+
17+
hm.Set(1, 10)
18+
hm.Set(2, 20)
19+
hm.Set(3, 30)
20+
21+
sumKeys := 0
22+
sumValues := 0
23+
for k, v := range hm.Iter() {
24+
sumKeys += k
25+
sumValues += v
26+
}
27+
28+
fmt.Println(sumKeys)
29+
fmt.Println(sumValues)
30+
31+
// Output:
32+
// 6
33+
// 60
34+
}

hashmap/hashmap.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ func (hm *Hashmap[K, V]) extend() {
4747
}
4848

4949
for _, p := range pairs.Iter() {
50-
hm.Insert(p.key, p.value)
50+
hm.Set(p.key, p.value)
5151
}
5252
}
5353
}
5454

55-
// Insert inserts a new value V if key K doesn't exist,
55+
// Set inserts a new value V if key K doesn't exist,
5656
// Otherwise update the key K with value V
57-
func (hm *Hashmap[K, V]) Insert(key K, value V) {
57+
func (hm *Hashmap[K, V]) Set(key K, value V) {
5858
if ptr, exists := hm.GetPtr(key); exists {
5959
*ptr = value
6060
return

hashmap/hashmap_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ func BenchmarkHashmapCAlloc(b *testing.B) {
2929
h := hashmap.New[int, int](alloc)
3030

3131
for i := 0; i < TIMES; i++ {
32-
h.Insert(i, i)
32+
h.Set(i, i)
3333
}
3434

35-
runtime.GC()
3635
h.Free()
3736
alloc.Destroy()
3837
}
@@ -44,10 +43,9 @@ func BenchmarkHashmapBatchAlloc(b *testing.B) {
4443
h := hashmap.New[int, int](alloc)
4544

4645
for i := 0; i < TIMES; i++ {
47-
h.Insert(i, i)
46+
h.Set(i, i)
4847
}
4948

50-
runtime.GC()
5149
h.Free()
5250
alloc.Destroy()
5351
}

linkedlist/example_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package linkedlist
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/joetifa2003/mm-go/allocator"
7+
)
8+
9+
func Example() {
10+
alloc := allocator.NewC()
11+
defer alloc.Destroy()
12+
13+
ll := New[int](alloc)
14+
defer ll.Free()
15+
16+
ll.PushBack(1)
17+
ll.PushBack(2)
18+
ll.PushBack(3)
19+
ll.PushBack(4)
20+
21+
fmt.Println("PopBack:", ll.PopBack())
22+
fmt.Println("PopFront:", ll.PopFront())
23+
24+
for _, i := range ll.Iter() {
25+
fmt.Println(i)
26+
}
27+
28+
// Output:
29+
// PopBack: 4
30+
// PopFront: 1
31+
// 2
32+
// 3
33+
}

minheap/example_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package minheap_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/joetifa2003/mm-go/allocator"
7+
"github.com/joetifa2003/mm-go/minheap"
8+
)
9+
10+
func int_less(a, b int) bool { return a < b }
11+
12+
func Example() {
13+
alloc := allocator.NewC()
14+
defer alloc.Destroy()
15+
16+
h := minheap.New[int](alloc, int_less)
17+
18+
// Push some values onto the heap
19+
h.Push(2)
20+
h.Push(1)
21+
h.Push(4)
22+
h.Push(3)
23+
h.Push(5)
24+
25+
// Pop the minimum value from the heap
26+
fmt.Println(h.Pop())
27+
fmt.Println(h.Pop())
28+
29+
// Output:
30+
// 1
31+
// 2
32+
}
33+
34+
func int_greater(a, b int) bool { return a > b }
35+
36+
func Example_MaxHeap() {
37+
alloc := allocator.NewC()
38+
defer alloc.Destroy()
39+
40+
h := minheap.New[int](alloc, int_greater)
41+
42+
// Push some values onto the heap
43+
h.Push(2)
44+
h.Push(1)
45+
h.Push(4)
46+
h.Push(3)
47+
h.Push(5)
48+
49+
// Pop the max value from the heap
50+
fmt.Println(h.Pop())
51+
fmt.Println(h.Pop())
52+
53+
// Output:
54+
// 5
55+
// 4
56+
}

minheap/minheap.go

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func (h *MinHeap[T]) Free() {
5959
allocator.Free(h.alloc, h)
6060
}
6161

62+
// Remove the first element that makes f return true
6263
func (h *MinHeap[T]) Remove(f func(T) bool) {
6364
for i := 0; i < h.data.Len(); i++ {
6465
if f(h.data.UnsafeAt(i)) {
@@ -120,6 +121,7 @@ func (h *MinHeap[T]) removeAt(index int) {
120121
}
121122
}
122123

124+
// Iter returns an iterator over the elements of the heap.
123125
func (h *MinHeap[T]) Iter() iter.Seq2[int, T] {
124126
return h.data.Iter()
125127
}

mmstring/example_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55

66
"github.com/joetifa2003/mm-go/allocator"
77
"github.com/joetifa2003/mm-go/batchallocator"
8-
"github.com/joetifa2003/mm-go/hashmap"
98
"github.com/joetifa2003/mm-go/mmstring"
9+
"github.com/joetifa2003/mm-go/vector"
1010
)
1111

1212
func Example() {
@@ -30,19 +30,19 @@ func Example() {
3030
// Foo Bar
3131
}
3232

33-
func Example_complex() {
33+
func Example_datastructures() {
3434
alloc := batchallocator.New(allocator.NewC())
3535
defer alloc.Destroy() // all the memory allocated bellow will be freed, no need to free it manually.
3636

37-
m := hashmap.New[*mmstring.MMString, *mmstring.MMString](alloc)
38-
m.Insert(mmstring.From(alloc, "hello"), mmstring.From(alloc, "world"))
39-
m.Insert(mmstring.From(alloc, "foo"), mmstring.From(alloc, "bar"))
37+
m := vector.New[*mmstring.MMString](alloc)
38+
m.Push(mmstring.From(alloc, "hello"))
39+
m.Push(mmstring.From(alloc, "world"))
4040

4141
for k, v := range m.Iter() {
42-
fmt.Println(k.GetGoString(), v.GetGoString())
42+
fmt.Println(k, v.GetGoString())
4343
}
4444

4545
// Output:
46-
// hello world
47-
// foo bar
46+
// 0 hello
47+
// 1 world
4848
}

typedarena/example_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package typedarena_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/joetifa2003/mm-go/allocator"
7+
"github.com/joetifa2003/mm-go/typedarena"
8+
)
9+
10+
type Entity struct {
11+
VelocityX float32
12+
VelocityY float32
13+
PositionX float32
14+
PositionY float32
15+
}
16+
17+
func Example() {
18+
alloc := allocator.NewC()
19+
defer alloc.Destroy()
20+
21+
arena := typedarena.New[Entity](
22+
alloc,
23+
10,
24+
)
25+
defer arena.Free() // frees all memory
26+
27+
for i := 0; i < 10; i++ {
28+
e := arena.Alloc() // *Entity
29+
e.VelocityX = float32(i)
30+
e.VelocityY = float32(i)
31+
e.PositionX = float32(i)
32+
e.PositionY = float32(i)
33+
fmt.Println(e.VelocityX, e.VelocityY, e.PositionX, e.PositionY)
34+
}
35+
36+
entities := arena.AllocMany(10) // allocate slice of 10 entities (cannot exceed 10 here because chunk size is 10 above, this limitation doesn't exist in batchallocator)
37+
38+
_ = entities
39+
40+
// Output:
41+
// 0 0 0 0
42+
// 1 1 1 1
43+
// 2 2 2 2
44+
// 3 3 3 3
45+
// 4 4 4 4
46+
// 5 5 5 5
47+
// 6 6 6 6
48+
// 7 7 7 7
49+
// 8 8 8 8
50+
// 9 9 9 9
51+
}

typedarena/typed_arena.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// typedarena is a growable typed arena that allocates memory in fixed chunks , it's faster that batchallocator but more limited, you can use batchallocator if you want to allocate multiple different types, and you want to use an arena like behavior spanning multiple datastructures (like vector, linkedlist, hashmap etc..), typedarena is much faster when you are only allocating one type.
12
package typedarena
23

34
import (

0 commit comments

Comments
 (0)