-
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
39e3ffb
commit c9157ad
Showing
12 changed files
with
797 additions
and
109 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,34 @@ | ||
package hashmap | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/joetifa2003/mm-go/allocator" | ||
"github.com/joetifa2003/mm-go/batchallocator" | ||
) | ||
|
||
func Example() { | ||
alloc := batchallocator.New(allocator.NewC()) | ||
defer alloc.Destroy() | ||
|
||
hm := New[int, int](alloc) | ||
defer hm.Free() // can be removed | ||
|
||
hm.Set(1, 10) | ||
hm.Set(2, 20) | ||
hm.Set(3, 30) | ||
|
||
sumKeys := 0 | ||
sumValues := 0 | ||
for k, v := range hm.Iter() { | ||
sumKeys += k | ||
sumValues += v | ||
} | ||
|
||
fmt.Println(sumKeys) | ||
fmt.Println(sumValues) | ||
|
||
// Output: | ||
// 6 | ||
// 60 | ||
} |
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,33 @@ | ||
package linkedlist | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/joetifa2003/mm-go/allocator" | ||
) | ||
|
||
func Example() { | ||
alloc := allocator.NewC() | ||
defer alloc.Destroy() | ||
|
||
ll := New[int](alloc) | ||
defer ll.Free() | ||
|
||
ll.PushBack(1) | ||
ll.PushBack(2) | ||
ll.PushBack(3) | ||
ll.PushBack(4) | ||
|
||
fmt.Println("PopBack:", ll.PopBack()) | ||
fmt.Println("PopFront:", ll.PopFront()) | ||
|
||
for _, i := range ll.Iter() { | ||
fmt.Println(i) | ||
} | ||
|
||
// Output: | ||
// PopBack: 4 | ||
// PopFront: 1 | ||
// 2 | ||
// 3 | ||
} |
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,56 @@ | ||
package minheap_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/joetifa2003/mm-go/allocator" | ||
"github.com/joetifa2003/mm-go/minheap" | ||
) | ||
|
||
func int_less(a, b int) bool { return a < b } | ||
|
||
func Example() { | ||
alloc := allocator.NewC() | ||
defer alloc.Destroy() | ||
|
||
h := minheap.New[int](alloc, int_less) | ||
|
||
// Push some values onto the heap | ||
h.Push(2) | ||
h.Push(1) | ||
h.Push(4) | ||
h.Push(3) | ||
h.Push(5) | ||
|
||
// Pop the minimum value from the heap | ||
fmt.Println(h.Pop()) | ||
fmt.Println(h.Pop()) | ||
|
||
// Output: | ||
// 1 | ||
// 2 | ||
} | ||
|
||
func int_greater(a, b int) bool { return a > b } | ||
|
||
func Example_MaxHeap() { | ||
alloc := allocator.NewC() | ||
defer alloc.Destroy() | ||
|
||
h := minheap.New[int](alloc, int_greater) | ||
|
||
// Push some values onto the heap | ||
h.Push(2) | ||
h.Push(1) | ||
h.Push(4) | ||
h.Push(3) | ||
h.Push(5) | ||
|
||
// Pop the max value from the heap | ||
fmt.Println(h.Pop()) | ||
fmt.Println(h.Pop()) | ||
|
||
// Output: | ||
// 5 | ||
// 4 | ||
} |
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,51 @@ | ||
package typedarena_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/joetifa2003/mm-go/allocator" | ||
"github.com/joetifa2003/mm-go/typedarena" | ||
) | ||
|
||
type Entity struct { | ||
VelocityX float32 | ||
VelocityY float32 | ||
PositionX float32 | ||
PositionY float32 | ||
} | ||
|
||
func Example() { | ||
alloc := allocator.NewC() | ||
defer alloc.Destroy() | ||
|
||
arena := typedarena.New[Entity]( | ||
alloc, | ||
10, | ||
) | ||
defer arena.Free() // frees all memory | ||
|
||
for i := 0; i < 10; i++ { | ||
e := arena.Alloc() // *Entity | ||
e.VelocityX = float32(i) | ||
e.VelocityY = float32(i) | ||
e.PositionX = float32(i) | ||
e.PositionY = float32(i) | ||
fmt.Println(e.VelocityX, e.VelocityY, e.PositionX, e.PositionY) | ||
} | ||
|
||
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) | ||
|
||
_ = entities | ||
|
||
// Output: | ||
// 0 0 0 0 | ||
// 1 1 1 1 | ||
// 2 2 2 2 | ||
// 3 3 3 3 | ||
// 4 4 4 4 | ||
// 5 5 5 5 | ||
// 6 6 6 6 | ||
// 7 7 7 7 | ||
// 8 8 8 8 | ||
// 9 9 9 9 | ||
} |
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