forked from cornelk/hashmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listelement.go
47 lines (40 loc) · 1.37 KB
/
listelement.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package hashmap
import (
"sync/atomic"
"unsafe"
)
// ListElement is an element of a list.
type ListElement struct {
keyHash uintptr
previousElement unsafe.Pointer // is nil for the first item in list
nextElement unsafe.Pointer // is nil for the last item in list
key interface{}
value unsafe.Pointer
deleted uintptr // marks the item as deleting or deleted
}
// Value returns the value of the list item.
func (e *ListElement) Value() (value interface{}) {
return *(*interface{})(atomic.LoadPointer(&e.value))
}
// Next returns the item on the right.
func (e *ListElement) Next() *ListElement {
return (*ListElement)(atomic.LoadPointer(&e.nextElement))
}
// Previous returns the item on the left.
func (e *ListElement) Previous() *ListElement {
return (*ListElement)(atomic.LoadPointer(&e.previousElement))
}
// setValue sets the value of the item.
// The value needs to be wrapped in unsafe.Pointer already.
func (e *ListElement) setValue(value unsafe.Pointer) {
atomic.StorePointer(&e.value, value)
}
// casValue compares and swaps the values of the item.
// The to value needs to be wrapped in unsafe.Pointer already.
func (e *ListElement) casValue(from interface{}, to unsafe.Pointer) bool {
old := atomic.LoadPointer(&e.value)
if *(*interface{})(old) != from {
return false
}
return atomic.CompareAndSwapPointer(&e.value, old, to)
}