-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_st.go
113 lines (93 loc) · 2.1 KB
/
array_st.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
package st
import (
"errors"
. "go-algorithms/data_structures/types"
)
const DefaultCapacity = 10
type Option func(*ArrayST)
// ArrayST 平行数组实现符号表
type ArrayST struct {
keys []Key // 符号表键的存储数组
values []Value // 符号表值的存储数组
size int // 符号表的键值对个数
capacity int // 符号表的容量
}
// WithCapacity 设置符号表的初始容量
func WithCapacity(capacity int) Option {
return func(st *ArrayST) {
st.capacity = capacity
}
}
// NewArrayST 返回一个空符号表
func NewArrayST(opts ...Option) *ArrayST {
st := ArrayST{
size: 0,
capacity: DefaultCapacity,
}
for _, o := range opts {
o(&st)
}
st.keys = make([]Key, st.capacity)
st.values = make([]Value, st.capacity)
return &st
}
func (st *ArrayST) Put(key Key, value Value) {
for i := 0; i < st.size; i++ {
if st.keys[i] == key {
st.values[i] = value
return
}
}
if st.size == st.capacity {
st.resize(st.capacity * 2)
}
st.keys[st.size] = key
st.values[st.size] = value
st.size++
}
func (st *ArrayST) Get(key Key) (Value, error) {
for i := 0; i < st.size; i++ {
if st.keys[i] == key {
return st.values[i], nil
}
}
return 0, errors.New("keyError")
}
func (st *ArrayST) Delete(key Key) {
for i := 0; i < st.size; i++ {
if st.keys[i] == key {
st.keys = append(st.keys[:i], st.keys[i+1:]...)
st.values = append(st.values[:i], st.values[i+1:]...)
st.size--
if st.size == st.capacity/4 && st.capacity/2 != 0 {
st.resize(st.capacity >> 1)
}
}
}
}
func (st *ArrayST) Contains(key Key) bool {
for i := 0; i < st.size; i++ {
if st.keys[i] == key {
return true
}
}
return false
}
func (st *ArrayST) IsEmpty() bool {
return st.size == 0
}
func (st *ArrayST) Size() int {
return st.size
}
func (st *ArrayST) Keys() []Key {
return st.keys[:st.size]
}
// resize 符号表扩缩容
func (st *ArrayST) resize(capacity int) {
newKeys, newValues := make([]Key, capacity), make([]Value, capacity)
copy(newKeys, st.keys)
copy(newValues, st.values)
st.keys = newKeys
st.values = newValues
st.capacity = capacity
}