Skip to content

Commit 39e3ffb

Browse files
committed
feat: more examples
1 parent addfc4c commit 39e3ffb

File tree

5 files changed

+168
-2
lines changed

5 files changed

+168
-2
lines changed

allocator/example_test.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import (
66

77
"github.com/joetifa2003/mm-go"
88
"github.com/joetifa2003/mm-go/allocator"
9+
"github.com/joetifa2003/mm-go/linkedlist"
10+
"github.com/joetifa2003/mm-go/mmstring"
11+
"github.com/joetifa2003/mm-go/vector"
912
)
1013

11-
func ExampleNewC() {
14+
func Example() {
1215
alloc := allocator.NewC()
1316
defer alloc.Destroy()
1417

@@ -21,6 +24,49 @@ func ExampleNewC() {
2124
// Output: 15
2225
}
2326

27+
type MyStruct struct {
28+
a int
29+
b float32
30+
}
31+
32+
func Example_datastructures() {
33+
alloc := allocator.NewC()
34+
defer alloc.Destroy() // all the memory allocated bellow will be freed, no need to free it manually.
35+
36+
p := allocator.Alloc[MyStruct](alloc)
37+
defer allocator.Free(alloc, p)
38+
39+
p.a = 100
40+
p.b = 200
41+
42+
fmt.Println(*p)
43+
44+
v := vector.New[int](alloc)
45+
defer v.Free()
46+
v.Push(15)
47+
v.Push(70)
48+
49+
for _, i := range v.Iter() {
50+
fmt.Println(i)
51+
}
52+
53+
l := linkedlist.New[*mmstring.MMString](alloc)
54+
defer l.Free()
55+
l.PushBack(mmstring.From(alloc, "hello"))
56+
l.PushBack(mmstring.From(alloc, "world"))
57+
58+
for _, i := range l.Iter() {
59+
fmt.Println(i.GetGoString())
60+
}
61+
62+
// Output:
63+
// {100 200}
64+
// 15
65+
// 70
66+
// hello
67+
// world
68+
}
69+
2470
func ExampleAlloc() {
2571
alloc := allocator.NewC()
2672
defer alloc.Destroy()
@@ -99,7 +145,7 @@ func ExampleNewAllocator() {
99145
)
100146

101147
// Check how C allocator is implemented
102-
// or batchallocator soruce for a reference
148+
// or batchallocator source for a reference
103149

104150
_ = alloc
105151
}

batchallocator/example_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package batchallocator_test
22

33
import (
4+
"fmt"
5+
46
"github.com/joetifa2003/mm-go"
57
"github.com/joetifa2003/mm-go/allocator"
68
"github.com/joetifa2003/mm-go/batchallocator"
9+
"github.com/joetifa2003/mm-go/linkedlist"
10+
"github.com/joetifa2003/mm-go/mmstring"
11+
"github.com/joetifa2003/mm-go/vector"
712
)
813

914
func Example() {
@@ -15,6 +20,33 @@ func Example() {
1520
defer allocator.Free(alloc, ptr) // this can removed and the memory will be freed.
1621
}
1722

23+
func Example_arena() {
24+
alloc := batchallocator.New(allocator.NewC())
25+
defer alloc.Destroy() // all the memory allocated bellow will be freed, no need to free it manually.
26+
27+
v := vector.New[int](alloc)
28+
v.Push(15)
29+
v.Push(70)
30+
31+
for _, i := range v.Iter() {
32+
fmt.Println(i)
33+
}
34+
35+
l := linkedlist.New[*mmstring.MMString](alloc)
36+
l.PushBack(mmstring.From(alloc, "hello"))
37+
l.PushBack(mmstring.From(alloc, "world"))
38+
39+
for _, i := range l.Iter() {
40+
fmt.Println(i.GetGoString())
41+
}
42+
43+
// Output:
44+
// 15
45+
// 70
46+
// hello
47+
// world
48+
}
49+
1850
func ExampleWithBucketSize() {
1951
alloc := batchallocator.New(
2052
allocator.NewC(),

mm.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ func SizeOf[T any]() int {
77
var zeroV T
88
return int(unsafe.Sizeof(zeroV))
99
}
10+
11+
// Zero returns a zero value of T
12+
func Zero[T any]() T {
13+
var zeroV T
14+
return zeroV
15+
}

mmstring/example_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package mmstring_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/joetifa2003/mm-go/allocator"
7+
"github.com/joetifa2003/mm-go/batchallocator"
8+
"github.com/joetifa2003/mm-go/hashmap"
9+
"github.com/joetifa2003/mm-go/mmstring"
10+
)
11+
12+
func Example() {
13+
alloc := allocator.NewC()
14+
defer alloc.Destroy()
15+
16+
s := mmstring.New(alloc)
17+
defer s.Free()
18+
19+
s.AppendGoString("Hello ")
20+
s.AppendGoString("World")
21+
22+
s2 := mmstring.From(alloc, "Foo Bar")
23+
defer s2.Free()
24+
25+
fmt.Println(s.GetGoString())
26+
fmt.Println(s2.GetGoString())
27+
28+
// Output:
29+
// Hello World
30+
// Foo Bar
31+
}
32+
33+
func Example_complex() {
34+
alloc := batchallocator.New(allocator.NewC())
35+
defer alloc.Destroy() // all the memory allocated bellow will be freed, no need to free it manually.
36+
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"))
40+
41+
for k, v := range m.Iter() {
42+
fmt.Println(k.GetGoString(), v.GetGoString())
43+
}
44+
45+
// Output:
46+
// hello world
47+
// foo bar
48+
}

vector/example_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package vector_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/joetifa2003/mm-go/allocator"
7+
"github.com/joetifa2003/mm-go/vector"
8+
)
9+
10+
func Example() {
11+
alloc := allocator.NewC()
12+
v := vector.New[int](alloc)
13+
v.Push(1)
14+
v.Push(2)
15+
v.Push(3)
16+
17+
fmt.Println("Length:", v.Len())
18+
for i := 0; i < v.Len(); i++ {
19+
fmt.Println(v.At(i))
20+
}
21+
22+
for _, k := range v.Iter() {
23+
fmt.Println(k)
24+
}
25+
26+
// Output:
27+
// Length: 3
28+
// 1
29+
// 2
30+
// 3
31+
// 1
32+
// 2
33+
// 3
34+
}

0 commit comments

Comments
 (0)