-
Notifications
You must be signed in to change notification settings - Fork 0
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
ffc4d4d
commit 279e0c5
Showing
6 changed files
with
198 additions
and
78 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
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,92 @@ | ||
package list | ||
|
||
import ( | ||
"github.com/outofforest/quantum/alloc" | ||
"github.com/outofforest/quantum/types" | ||
) | ||
|
||
// StoreAddress adds address to the list. | ||
func StoreAddress( | ||
listTail *types.NodeAddress, | ||
nodeAddress types.NodeAddress, | ||
state *alloc.State, | ||
allocator *alloc.Allocator, | ||
) (bool, error) { | ||
if *listTail == 0 { | ||
var err error | ||
*listTail, err = allocator.Allocate() | ||
if err != nil { | ||
return false, err | ||
} | ||
node := ProjectNode(state.Node(*listTail)) | ||
|
||
node.Slots[0] = nodeAddress | ||
node.NumOfAddresses = 1 | ||
// This is needed because list nodes are not zeroed. | ||
node.Next = 0 | ||
|
||
return true, nil | ||
} | ||
|
||
node := ProjectNode(state.Node(*listTail)) | ||
if node.NumOfAddresses < NumOfAddresses { | ||
node.Slots[node.NumOfAddresses] = nodeAddress | ||
node.NumOfAddresses++ | ||
|
||
return false, nil | ||
} | ||
|
||
var err error | ||
*listTail, err = allocator.Allocate() | ||
if err != nil { | ||
return false, err | ||
} | ||
node.Next = *listTail | ||
node = ProjectNode(state.Node(*listTail)) | ||
|
||
node.Slots[0] = nodeAddress | ||
node.NumOfAddresses = 1 | ||
// This is needed because list nodes are not zeroed. | ||
node.Next = 0 | ||
|
||
return true, nil | ||
} | ||
|
||
// Merge merges two lists. | ||
func Merge( | ||
list1Front, list1Tail *types.NodeAddress, | ||
list2Front, list2Tail types.NodeAddress, | ||
state *alloc.State, | ||
) { | ||
if list2Front == 0 { | ||
return | ||
} | ||
if *list1Front == 0 { | ||
*list1Front = list2Front | ||
} else { | ||
node := ProjectNode(state.Node(*list1Tail)) | ||
node.Next = list2Front | ||
} | ||
|
||
*list1Tail = list2Tail | ||
} | ||
|
||
// Iterator iterates over addresses in the list. | ||
func Iterator(listFront types.NodeAddress, state *alloc.State) func(func(types.NodeAddress) bool) { | ||
return func(yield func(types.NodeAddress) bool) { | ||
for { | ||
if listFront == 0 { | ||
return | ||
} | ||
|
||
node := ProjectNode(state.Node(listFront)) | ||
for i := range node.NumOfAddresses { | ||
if !yield(node.Slots[i]) { | ||
return | ||
} | ||
} | ||
|
||
listFront = node.Next | ||
} | ||
} | ||
} |
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,23 @@ | ||
package list | ||
|
||
import ( | ||
"unsafe" | ||
|
||
"github.com/outofforest/quantum/types" | ||
) | ||
|
||
// NumOfAddresses defines number of available slots in the list node. | ||
const NumOfAddresses = 510 | ||
|
||
// Node represents list node. | ||
type Node struct { | ||
Slots [NumOfAddresses]types.NodeAddress | ||
|
||
Next types.NodeAddress | ||
NumOfAddresses uint16 | ||
} | ||
|
||
// ProjectNode projects node to list node. | ||
func ProjectNode(n unsafe.Pointer) *Node { | ||
return (*Node)(n) | ||
} |
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,14 @@ | ||
package list | ||
|
||
import ( | ||
"testing" | ||
"unsafe" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/outofforest/quantum/types" | ||
) | ||
|
||
func TestPointerNode(t *testing.T) { | ||
require.LessOrEqual(t, unsafe.Sizeof(Node{}), uintptr(types.NodeLength)) | ||
} |