-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperties2.go
86 lines (67 loc) · 1.52 KB
/
properties2.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
package properties
import (
"container/list"
"sync"
)
// sequence hashtable
type sequenceTable struct {
mutex sync.Mutex
mapper map[interface{}]interface{}
element map[interface{}]*list.Element
list *list.List
}
func NewHashtable2() Hashtable {
return &sequenceTable{
mapper: map[interface{}]interface{}{},
element: map[interface{}]*list.Element{},
list: list.New(),
}
}
func (s *sequenceTable) New() Hashtable {
return NewHashtable2()
}
func (s *sequenceTable) Put(key, value interface{}) interface{} {
s.mutex.Lock()
defer s.mutex.Unlock()
old := s.mapper[key]
s.mapper[key] = value
s.element[key] = s.list.PushBack(key)
return old
}
func (s *sequenceTable) Get(key interface{}) interface{} {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.mapper[key]
}
func (s *sequenceTable) Remove(key interface{}) {
s.mutex.Lock()
defer s.mutex.Unlock()
delete(s.mapper, key)
s.list.Remove(s.element[key])
delete(s.element, key)
}
func (s *sequenceTable) Size() int {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.list.Len()
}
func (s *sequenceTable) Keys() []interface{} {
s.mutex.Lock()
defer s.mutex.Unlock()
var keys = make([]interface{}, 0, s.list.Len())
for node := s.list.Front(); node != nil; node = node.Next() {
keys = append(keys, node.Value)
}
return keys
}
// sequence properties
type Properties2 struct {
Properties
}
func NewProperties2() *Properties2 {
properties := NewProperties()
properties.Hashtable = NewHashtable2()
return &Properties2{
Properties: *properties,
}
}