Issues (22)

array.go (22 issues)

Severity
1
/**
2
  * Author: JeffreyBool
3
  * Date: 2019/4/12
4
  * Time: 11:28
5
  * Software: GoLand
6
*/
7
8
package array
9
10
import (
11
	"errors"
12
	"fmt"
13
)
14
15
/**
16
 * 1) 数组的插入、删除、按照下标随机访问操作;
17
 * 2)数组中的数据是interface类型的;
18
 */
19
20
type Array struct {
0 ignored issues
show
exported type Array should have comment or be unexported
Loading history...
21
	data []interface{}
22
	size int
23
}
24
25
//数组初始化内存 (可以指定长度,默认为10个长度)
0 ignored issues
show
comment on exported function NewArray should be of the form "NewArray ..."
Loading history...
26
func NewArray(capacity ...int) (array *Array) {
27
	if len(capacity) >= 1 && capacity[0] != 0 {
28
		array = &Array{
29
			data: make([]interface{}, capacity[0]),
30
			size: 0,
31
		}
32
	} else {
33
		array = &Array{
34
			data: make([]interface{}, 10),
35
			size: 0,
36
		}
37
	}
38
39
	return
40
}
41
42
//判断索引是否越界
43
func (array *Array) checkIndex(index int) bool {
44
	if index < 0 || index >= array.size {
45
		return true
46
	}
47
48
	return false
49
}
50
51
//数组扩容
52
func (array *Array) resize(capacity int) {
53
	newArray := make([]interface{}, capacity)
54
	for i := 0; i < array.size; i ++ {
55
		newArray[i] = array.data[i]
56
	}
57
	array.data = newArray
58
	newArray = nil
59
}
60
61
//获取数组容量
0 ignored issues
show
comment on exported method Array.GetCapacity should be of the form "GetCapacity ..."
Loading history...
62
func (array *Array) GetCapacity() int {
63
	return cap(array.data)
64
}
65
66
//获取数组长度
0 ignored issues
show
comment on exported method Array.GetSize should be of the form "GetSize ..."
Loading history...
67
func (array *Array) GetSize() int {
68
	return array.size
69
}
70
71
//判断数组是否为空
0 ignored issues
show
comment on exported method Array.IsEmpty should be of the form "IsEmpty ..."
Loading history...
72
func (array *Array) IsEmpty() bool {
73
	return array.size == 0
74
}
75
76
//向数组头插入元素
0 ignored issues
show
comment on exported method Array.AddFirst should be of the form "AddFirst ..."
Loading history...
77
func (array *Array) AddFirst(value interface{}) error {
78
	return array.Add(0, value)
79
}
80
81
//向数组尾插入元素
0 ignored issues
show
comment on exported method Array.AddLast should be of the form "AddLast ..."
Loading history...
82
func (array *Array) AddLast(value interface{}) error {
83
	return array.Add(array.size, value)
84
}
85
86
//在 index 位置,插入元素e, 时间复杂度 O(m+n)
0 ignored issues
show
comment on exported method Array.Add should be of the form "Add ..."
Loading history...
87
func (array *Array) Add(index int, value interface{}) (err error) {
88
	if index < 0 || index > array.size {
89
		err = errors.New("Add failed. Require index >= 0 and index <= size.")
0 ignored issues
show
error strings should not be capitalized or end with punctuation or a newline
Loading history...
90
		return
91
	}
92
93
	// 如果当前元素个数等于数组容量,则将数组扩容为原来的2倍
94
	cap := array.GetCapacity()
95
	if array.size == cap {
96
		array.resize(cap * 2)
97
	}
98
99
	for i := array.size - 1; i >= index; i-- {
100
		array.data[i+1] = array.data[i]
101
	}
102
103
	array.data[index] = value
104
	array.size++
105
	return
106
}
107
108
//获取对应 index 位置的元素
0 ignored issues
show
comment on exported method Array.Get should be of the form "Get ..."
Loading history...
109
func (array *Array) Get(index int) (value interface{}, err error) {
110
	if array.checkIndex(index) {
111
		err = errors.New("Get failed. Index is illegal.")
0 ignored issues
show
error strings should not be capitalized or end with punctuation or a newline
Loading history...
112
		return
113
	}
114
115
	value = array.data[index]
116
	return
117
}
118
119
//修改 index 位置的元素
0 ignored issues
show
comment on exported method Array.Set should be of the form "Set ..."
Loading history...
120
func (array *Array) Set(index int, value interface{}) (err error) {
121
	if array.checkIndex(index) {
122
		err = errors.New("Set failed. Index is illegal.")
0 ignored issues
show
error strings should not be capitalized or end with punctuation or a newline
Loading history...
123
		return
124
	}
125
126
	array.data[index] = value
127
	return
128
}
129
130
//查找数组中是否有元素
0 ignored issues
show
comment on exported method Array.Contains should be of the form "Contains ..."
Loading history...
131
func (array *Array) Contains(value interface{}) bool {
132
	for i := 0; i < array.size; i++ {
133
		if array.data[i] == value {
134
			return true
135
		}
136
	}
137
138
	return false
139
}
140
141
//通过索引查找数组,索引范围[0,n-1](未找到,返回 -1)
0 ignored issues
show
comment on exported method Array.Find should be of the form "Find ..."
Loading history...
142
func (array *Array) Find(value interface{}) int {
143
	for i := 0; i < array.size; i++ {
144
		if array.data[i] == value {
145
			return i
146
		}
147
	}
148
149
	return -1
150
}
151
152
// 删除 index 位置的元素,并返回
0 ignored issues
show
comment on exported method Array.Remove should be of the form "Remove ..."
Loading history...
153
func (array *Array) Remove(index int) (value interface{}, err error) {
154
	if array.checkIndex(index) {
155
		err = errors.New("Remove failed. Index is illegal.")
0 ignored issues
show
error strings should not be capitalized or end with punctuation or a newline
Loading history...
156
		return
157
	}
158
159
	value = array.data[index]
160
	for i := index + 1; i < array.size; i++ {
161
		//数据全部往前挪动一位,覆盖需要删除的元素
162
		array.data[i-1] = array.data[i]
163
	}
164
165
	array.size--
166
	array.data[array.size] = nil //loitering objects != memory leak
167
168
	cap := array.GetCapacity()
169
	if array.size == cap/4 && cap/2 != 0 {
170
		array.resize(cap / 2)
171
	}
172
	return
173
}
174
175
//删除数组首个元素
0 ignored issues
show
comment on exported method Array.RemoveFirst should be of the form "RemoveFirst ..."
Loading history...
176
func (array *Array) RemoveFirst() (interface{}, error) {
177
	return array.Remove(0)
178
}
179
180
//删除末尾元素
0 ignored issues
show
comment on exported method Array.RemoveLast should be of the form "RemoveLast ..."
Loading history...
181
func (array *Array) RemoveLast() (interface{}, error) {
182
	return array.Remove(int(array.size - 1))
183
}
184
185
//从数组中删除指定元素
0 ignored issues
show
comment on exported method Array.RemoveElement should be of the form "RemoveElement ..."
Loading history...
186
func (array *Array) RemoveElement(value interface{}) (e interface{}, err error) {
187
	index := array.Find(value)
188
	if index != -1 {
189
		e, err = array.Remove(index)
190
	}
191
	return
192
}
193
194
//清空数组
0 ignored issues
show
comment on exported method Array.Clear should be of the form "Clear ..."
Loading history...
195
func (array *Array) Clear() {
196
	array.data = make([]interface{}, array.size)
197
	array.size = 0
198
}
199
200
//打印数列
0 ignored issues
show
comment on exported method Array.PrintIn should be of the form "PrintIn ..."
Loading history...
201
func (array *Array) PrintIn() {
202
	var format string
203
	format = fmt.Sprintf("Array: size = %d , capacity = %d\n",array.size, cap(array.data))
204
	format += "["
205
	for i := 0; i < array.GetSize(); i++ {
206
		format += fmt.Sprintf("%+v", array.data[i])
207
		if i != array.size -1 {
208
			format += ", "
209
		}
210
	}
211
	format += "]"
212
	fmt.Println(format)
213
}
214