This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
180 lines (152 loc) · 4.2 KB
/
list.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2012 Dobrosław Żybort
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package listdict
import (
"errors"
"fmt"
"reflect"
"strings"
)
// Simple list
type List []interface{}
// Return new List with specified length
func NewList(length int) List {
return make(List, length)
}
var (
// ErrRemoveFromEmptyList is returned when user want to remove element
// from empty list
ErrRemoveFromEmptyList = errors.
New("Trying to remove element from empty list")
)
//=============================================================================
// Append adds an element to the end of the list.
func (list *List) Append(values ...interface{}) {
*list = append(*list, values...)
}
// AppendIfMissing adds an element to the end of the list if it's not already
// in the list.
func (list *List) AppendIfMissing(value interface{}) {
for _, ele := range *list {
if ele == value {
// Element exists, exit
goto exit
}
}
*list = append(*list, value)
exit:
}
// Count returns the number of times value appears in the list.
func (list List) Count(value interface{}) int {
counter := 0
for _, listValue := range list {
if listValue == value {
counter++
}
}
return counter
}
// Delete removes element with given index from the list.
func (list *List) Delete(index int) error {
if len(*list) <= 0 {
return ErrRemoveFromEmptyList
}
listLen := len(*list)
copy((*list)[index:], (*list)[index+1:])
(*list)[listLen-1] = nil
*list = (*list)[:listLen-1]
return nil
}
// Extend one list with the contents of the other list.
func (list *List) Extend(otherList List) {
for _, value := range otherList {
*list = append(*list, value)
}
}
// Index returns the index of the first item in the list whose value is val.
// It is -1 if there is no such item.
func (list List) Index(val interface{}) (int, error) {
for index, listValue := range list {
if listValue == val {
return index, nil
}
}
errorString := fmt.Sprintf("%v is not in list", val)
return -1, errors.New(errorString)
}
// Insert an element at a given position. If the position is past the end
// of the list, append to the end.
func (list *List) Insert(index int, values ...interface{}) {
if len(*list) > index {
for i := 0; i < len(values); i++ {
*list = append(*list, 0)
}
copy((*list)[index+len(values):], (*list)[index:])
copy((*list)[index:], values)
} else {
*list = append(*list, values...)
}
}
// IsEqual returns true if lists are equal.
func (list List) IsEqual(otherList List) bool {
return reflect.DeepEqual(list, otherList)
}
// Remove and returns the last element in the list.
func (list *List) Pop() (interface{}, error) {
if len(*list) <= 0 {
return nil, ErrRemoveFromEmptyList
}
listLen := len(*list)
val := (*list)[listLen-1]
(*list).Delete(listLen - 1)
return val, nil
}
// Remove and returns the element at the given position in the list.
func (list *List) PopItem(index int) (interface{}, error) {
if len(*list) <= 0 {
return nil, ErrRemoveFromEmptyList
}
val := (*list)[index]
(*list).Delete(index)
return val, nil
}
// Remove the first element from the list whose value matches the given value.
// Error if no match is found.
func (list *List) Remove(val interface{}) error {
errorString := fmt.Sprintf("%v is not in list", val)
if len(*list) > 0 {
for index, listValue := range *list {
if listValue == val {
(*list).Delete(index)
return nil
}
}
}
return errors.New(errorString)
}
// Reverse the elements of the list in place.
func (list *List) Reverse() {
if len(*list) > 0 {
maxIndex := len(*list) - 1
for index := 0; index < (maxIndex/2)+1; index++ {
(*list)[index], (*list)[maxIndex-index] =
(*list)[maxIndex-index], (*list)[index]
}
}
}
// Sort the list in place ordering elements from smallest to largest.
//func (list *List) Sort() {
//}
// String returns list values as string
// l := listdict.List{"one", 2, "three"}
// l.String() => "one, 2, three"
func (list List) String() string {
var out []string
for _, val := range list {
out = append(out, fmt.Sprintf("%v", val))
}
return strings.Join(out, ", ")
}