-
Notifications
You must be signed in to change notification settings - Fork 2
/
liblist_unsafe.sh
285 lines (259 loc) · 7.92 KB
/
liblist_unsafe.sh
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/sh
#
# The MIT License (MIT)
#
# Copyright (c) 2017-2018 Thomas "Ventto" Venriès <thomas.venries@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
##
# @brief Prints a list from arguments
# @usage list <elt> <elt> ...
# @print The list result
#
list () {
for e; do [ -n "$e" ] && echo "$e"; done
}
##
# @brief Prints the number of elements in the list
# @usage list_size <lst>
# @print The size of the list as positive integer
#
list_size () {
test "$#" -ne 1 && { echo "0"; return; }
eval "test -n \"\$$1\"" && { eval "echo \"\$$1\" | wc -l"; return; }
echo '0'
}
##
# @brief Returns whether the list is empty(1) or not(0)
# @usage list_empty <lst>
# @return 0 or 1
#
list_empty () {
eval "test -z \"\$$1\""
}
##
# @brief Adds a new element at the beginning of the list
# @usage list_push_front <lst> <elt>
# @out Set the variable passed by reference
#
list_push_front () {
test "$#" -ne 2 && return 1
eval "test -n \"\$$1\"" || { eval "$1='$2'"; return; }
eval "$1=\"\$(printf '%s\n%s' '$2' \"\$$1\")\"";
}
##
# @brief Adds a new element at the end of the list
# @usage list_push_back <lst> <elt>
# @out Set the variable passed by reference
#
list_push_back () {
test "$#" -ne 2 && return 1
eval "test -n \"\$$1\"" || { eval "$1='$2'"; return; }
eval "$1=\"\$(printf '%s\n%s' \"\$$1\" '$2')\"";
}
##
# @brief Inserts new elements in the list before a specified position
# @usage list_insert <lst> <elt> <index>
# @out Set the variable passed by reference
#
list_insert () {
test "$#" -ne 3 && return 1; i="$3"; [ "$i" != '$' ] && i=$((i+1))
eval "$1=\"\$(echo \"\$$1\" | sed \"${i}i${2}\")\""
}
##
# @brief Modifies an element from the list at a specified position
# @usage list_set <lst> <elt> <index>
# @out Set the variable passed by reference
#
list_set () {
test "$#" -ne 3 && return 1
i="$3"; i=$((i+1)); eval "$1=\"\$(echo \"\$$1\" | sed \"${i}s/.*/$2/\")\""
}
##
# @brief Extracts a range of elements from the list between two specified
# positions
# @usage list_extract <lst> <from_index> <to_index>
# @out Set the variable passed by reference
#
list_extract () {
test "$#" -ne 3 && return 1; i="$2"; j="$3"; i=$((i+1)); j=$((j+1))
eval "$1=\"\$(echo \"\$$1\" | sed -n \"${i},${j}p\")\""
}
##
# @brief Replaces all elements from the list with a specified element
# @usage list_replace <lst> <old_elt> <new_elt>
# @out Set the variable passed by reference
#
list_replace () {
test "$#" -ne 3 && return 1
eval "$1=\"\$(echo \"\$$1\" | sed -e \"s/^$2$/$3/g\")\""
}
##
# @brief Prints the element at a specified position
# @usage list_get <lst> <index>
# @print The element found
#
list_get () {
test "$#" -ne 2 && return 1; i="$2"; i=$((i+1));
eval "echo \"\$$1\" | sed -n \"${i}p\""
}
##
# @brief Prints the head of the list
# @usage list_front <lst>
# @print The element found
#
list_front () {
test "$#" -ne 1 && return 1; eval "echo \"\$$1\" | sed -n '1p'"
}
##
# @brief Prints the queue of the list
# @usage list_back <lst>
# @print The element found
#
list_back () {
test "$#" -ne 1 && return 1; eval "echo \"\$$1\" | sed -n '\$p'"
}
##
# @brief Removes the first-hit element from a list
# @usage list_erase <lst> <elt>
# @out Set the variable passed by reference
#
list_erase () {
test "$#" -ne 2 && return 1
eval "$1=\"\$(echo \"\$$1\" | sed -e \"0,/^$2$/ s///\" -e '/^$/d')\""
}
##
# @brief Removes a range of elements from a list between two specified
# positions
# @usage list_erase_range <lst> <from_index> <to_index>
# @out Set the variable passed by reference
#
list_erase_range () {
test "$#" -ne 3 && return 1
i="$2"; j="$3"; i=$((i+1)); j=$((j+1));
eval "$1=\"\$(echo \"\$$1\" | sed \"${i},${j}d\")\""
}
##
# @brief Removes all elements from a specified position
# @usage list_erase_from <lst> <index>
# @out Set the variable passed by reference
#
list_erase_from () {
test "$#" -ne 2 && return 1
i="$2"; i=$((i+1)); eval "$1=\"\$(echo \"\$$1\" | sed \"${i},\\\$d\")\""
}
##
# @brief Removes the element at a specified position
# @usage list_eraseat <lst> <index>
# @out Set the variable passed by reference
#
list_eraseat () {
test "$#" -ne 2 && return 1
i="$2"; i=$((i+1)); eval "$1=\"\$(echo \"\$$1\" | sed \"${i}d\")\""
}
##
# @brief Removes all the elements from the list, which are equal to given
# element
# @usage list_remove <lst> <elt>
# @out Set the variable passed by reference
#
list_remove () {
test "$#" -ne 2 && return 1
eval "$1=\"\$(echo \"\$$1\" | sed -e \"/^$2$/d\")\""
}
##
# @brief Removes the first element of the list
# @usage list_pop_front <lst>
# @out Set the variable passed by reference
#
list_pop_front () {
test "$#" -ne 1 && return 1; eval "$1=\"\$(echo \"\$$1\" | sed '1d')\""
}
##
# @brief Removes the last element of the list
# @usage list_pop_back <lst>
# @out Set the variable passed by reference
#
list_pop_back () {
test "$#" -ne 1 && return 1; eval "$1=\"\$(echo \"\$$1\" | sed '\$d')\""
}
##
# @brief Prints the index of a specified element
# @usage list_indexof <lst> <elt>
# @print positive integer or -1 if not found
#
list_indexof () {
test "$#" -ne 2 && return 1; i=0
eval "for e in \$$1; do
[ \"\$e\" = '$2' ] && { echo \"\$i\"; return 0; }; i=\$((i+1));
done"
return 1
}
##
# @brief Returns whether the list contains a specified element(0) or not(1)
# @usage list_contains <lst> <elt>
# @return 0 or 1
#
list_contains () {
test "$#" -ne 2 && return 1
eval "for e in \$$1; do [ \"\$e\" = '$2' ] && return 0; done; return 1"
}
##
# @brief Prints the number of a specified element in the list
# @usage list_count <lst> <elt>
# @print The number of elements as positive integer
#
list_count () {
test "$#" -ne 2 && { echo '0'; return; }
eval "i=0; for e in \$$1; do [ \"\$e\" = '$2' ] && { i=\$((i+1)); };done;"
echo "$i"
}
##
# @brief Maps every element of the list
# @usage list_maps <lst> <func>
# @out Set the variable passed by reference
#
list_map () {
test "$#" -ne 2 && return 1
eval "$1=\"\$(for e in \$$1; do eval \"$2 \$e\"; done)\""
}
##
# @brief Reverses the list
# @usage list_reverse <lst>
# @out Set the variable passed by reference
#
list_reverse() {
test "$#" -ne 1 && return 1
eval "$1=\"\$(echo \"\$$1\" | sed '1!x;H;1h;\$!d;g')\""
}
##
# @brief Sorts the list
# @usage list_sort <lst>
# @out Set the variable passed by reference
#
list_sort () {
test "$#" -ne 1 && return 1; eval "$1=\"\$(echo \"\$$1\" | sort -n)\""
}
##
# @brief Sorts and reverses the sense of the list
# @usage list_sort_reverse <lst>
# @out Set the variable passed by reference
#
list_sort_reverse () {
test "$#" -ne 1 && return 1; eval "$1=\"\$(echo \"\$$1\" | sort -nr)\""
}