-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
item.go
164 lines (125 loc) · 3.2 KB
/
item.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Package skitlistmap ... concurrent akiplist map implementatin
// Copyright 2201 Kazuhisa TAKEI<xtakei@rytr.jp>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package skiplistmap
import (
"sync"
"unsafe"
"github.com/kazu/elist_head"
)
type entryHMap struct {
key interface{}
value interface{}
MapHead
}
func NewEntryMap(key, value interface{}) *entryHMap {
return &entryHMap{
key: key,
value: value,
}
}
var (
emptyEntryHMap *entryHMap = nil
emptyBucket *bucket = nil
EmptyEntryHMap *entryHMap = nil
)
func entryHMapFromPlistHead(head unsafe.Pointer) *entryHMap {
return (*entryHMap)(ElementOf(head, entryHMapOffset))
}
func entryHMapFromListHead(head *elist_head.ListHead) *entryHMap {
return entryHMapFromPlistHead(unsafe.Pointer(head))
}
func (e *entryHMap) entryHMapromListHead(lhead *elist_head.ListHead) *entryHMap {
return entryHMapFromListHead(lhead)
}
func (s *entryHMap) HmapEntryFromListHead(lhead *elist_head.ListHead) HMapEntry {
return s.entryHMapromListHead(lhead)
}
func (s *entryHMap) Key() interface{} {
return s.key
}
func (s *entryHMap) Value() interface{} {
return s.value
}
func (s *entryHMap) SetValue(v interface{}) bool {
s.value = v
return true
}
func (s *entryHMap) Next() HMapEntry {
return s.entryHMapromListHead(s.PtrListHead().DirectNext())
}
func (s *entryHMap) Prev() HMapEntry {
return s.entryHMapromListHead(s.PtrListHead().DirectPrev())
}
func (s *entryHMap) PtrMapHead() *MapHead {
return &s.MapHead
}
const entryHMapOffset = unsafe.Offsetof(EmptyEntryHMap.ListHead)
func (s *entryHMap) Offset() uintptr {
return entryHMapOffset
}
func (s *entryHMap) Delete() {
s.key = nil
s.MapHead.state |= mapIsDeleted
}
func (s *entryHMap) KeyHash() (uint64, uint64) {
return KeyToHash(s.key)
}
type HMapEntry interface {
Offset() uintptr
PtrMapHead() *MapHead
PtrListHead() *elist_head.ListHead
HmapEntryFromListHead(*elist_head.ListHead) HMapEntry
Next() HMapEntry
Prev() HMapEntry
}
type MapItem interface {
Key() interface{} // require order for HMap
Value() interface{} // require order for HMap
SetValue(interface{}) bool
KeyHash() (uint64, uint64)
Delete()
HMapEntry
}
type CondOfFinder func(ehead *entryHMap) bool
func CondOfFind(reverse uint64, l sync.Locker) CondOfFinder {
return func(ehead *entryHMap) bool {
if EnableStats {
l.Lock()
DebugStats[CntSearchEntry]++
l.Unlock()
}
return reverse <= ehead.reverse
}
}
type entryBuffer struct {
entries []entryHMap
elist_head.ListHead
}
func (ebuf *entryBuffer) Len() int {
return len(ebuf.entries)
}
func (ebuf *entryBuffer) init(cap int) {
ebuf.entries = make([]entryHMap, 1, cap)
}
func (ebuf *entryBuffer) getEntryFromPool(idx int) *entryHMap {
// if len(ebuf.entries) == 1 {
// ebuf.entries = ebuf.entries[:2]
// e := &ebuf.entries[1]
// e.reverse = reverse
// return e
// }
// lastE := ebuf.entries[len(ebuf.entries)-1]
// if lastE.reverse <
if cap(ebuf.entries) <= idx {
// FIXME: goto nextbuffer
}
if len(ebuf.entries) == idx {
ebuf.entries = ebuf.entries[:idx+1]
}
ebuf.entries[idx].MapHead = ebuf.entries[idx-1].MapHead
e := &ebuf.entries[idx]
e.reverse = 0
return e
}