comparison tests/farray-alist.t @ 756:33df05108ba1

farray.sh: New implementation alists: searching is done using a binary search now while preserving insertion order. The implementation uses two key lists: The first one is sorted and is used for searching using binary search. The second is a double-linked list and is used for remembering the insertion order.
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 19 Oct 2024 22:40:11 +0200
parents 4101e755b3e7
children 7ead30e3b2f9
comparison
equal deleted inserted replaced
755:f6d296c5868e 756:33df05108ba1
16 ============================== 16 ==============================
17 17
18 Create an empty alist 18 Create an empty alist
19 19
20 $ falist_create LIST 20 $ falist_create LIST
21 Has some initial global variables set
22 $ check_no_alist_artifacts
23 _farr_KV_[a-f0-9]+_B=0 (re)
24 _farr_KV_[a-f0-9]+_C=1 (re)
25 _farr_KV_[a-f0-9]+_P='0;0' (re)
26 _farr_KV_[a-f0-9]+__=0 (re)
27 [1]
28
21 $ falist_length _i LIST 29 $ falist_length _i LIST
22 $ echo "$_i" 30 $ echo "$_i"
23 0 31 0
24 $ test "${_i}" -eq 0 32 $ test "${_i}" -eq 0
25 $ falist_print_length LIST 33 $ falist_print_length LIST
35 ERROR: object `LIST' not created properly: token empty 43 ERROR: object `LIST' not created properly: token empty
36 [1] 44 [1]
37 45
38 $ check_no_alist_artifacts 46 $ check_no_alist_artifacts
39 47
48 $ falist_create LIST
49 $ falist_clear LIST
50 $ falist_istrue LIST
51 [1]
52 $ falist_debug LIST
53 DEBUG: alist `LIST' has length 0
54 $ falist_release LIST
55 $ ( falist_release LIST )
56 ERROR: object `LIST' not created properly: token empty
57 [1]
58 $ check_no_alist_artifacts
59
60
61 Creation and Destruction with one Item
62 ======================================
63
64 Create an alist with one item
65
66 $ falist_create LIST K1 V1
67 $ falist_length _i LIST
68 $ echo "$_i"
69 1
70 $ test "${_i}" -eq 1
71 $ falist_print_length LIST
72 1 (no-eol)
73
74 $ falist_istrue LIST
75 $ falist_debug LIST
76 DEBUG: alist `LIST' has length 1
77 DEBUG: the items:
78 DEBUG: `K1' -> `V1'
79 $ falist_release LIST
80 $ ( falist_release LIST )
81 ERROR: object `LIST' not created properly: token empty
82 [1]
83
84 $ check_no_alist_artifacts
85
86 One Item that replaces the first item
87
88 $ falist_create LIST K1 V1 K1 "V1 1"
89 $ falist_length _i LIST
90 $ echo "$_i"
91 1
92 $ test "${_i}" -eq 1
93 $ falist_print_length LIST
94 1 (no-eol)
95
96 $ falist_istrue LIST
97 $ falist_debug LIST
98 DEBUG: alist `LIST' has length 1
99 DEBUG: the items:
100 DEBUG: `K1' -> `V1 1'
101 $ falist_release LIST
102 $ ( falist_release LIST )
103 ERROR: object `LIST' not created properly: token empty
104 [1]
105
106 $ check_no_alist_artifacts
107
108
109 Creation and Destruction with more Items
110 ========================================
111
112 Create an alist with two items
113
114 $ falist_create LIST K1 V1 K2 V2
115 $ falist_debug LIST
116 DEBUG: alist `LIST' has length 2
117 DEBUG: the items:
118 DEBUG: `K1' -> `V1'
119 DEBUG: `K2' -> `V2'
120
121 $ falist_release LIST
122
123 $ check_no_alist_artifacts
124
125 Create with inverse insertion order
126
127 $ falist_create LIST K2 V2 K1 V1
128 $ falist_debug LIST
129 DEBUG: alist `LIST' has length 2
130 DEBUG: the items:
131 DEBUG: `K2' -> `V2'
132 DEBUG: `K1' -> `V1'
133
134 $ falist_release LIST
135
136 Insert at the beginning
137
138
139 $ falist_create LIST K2 V2 K1 V1 K0 V0
140 $ falist_debug LIST
141 DEBUG: alist `LIST' has length 3
142 DEBUG: the items:
143 DEBUG: `K2' -> `V2'
144 DEBUG: `K1' -> `V1'
145 DEBUG: `K0' -> `V0'
146
147 Replace: beginning
148
149 $ falist_set $LIST K2 $'V2 \' \\ "$abc'
150 $ falist_debug LIST
151 DEBUG: alist `LIST' has length 3
152 DEBUG: the items:
153 DEBUG: `K2' -> `V2 ' \ "$abc'
154 DEBUG: `K1' -> `V1'
155 DEBUG: `K0' -> `V0'
156
157 Replace: mid
158
159 $ falist_set $LIST K1 'V1 1'
160 $ falist_debug LIST
161 DEBUG: alist `LIST' has length 3
162 DEBUG: the items:
163 DEBUG: `K2' -> `V2 ' \ "$abc'
164 DEBUG: `K1' -> `V1 1'
165 DEBUG: `K0' -> `V0'
166
167 Replace: end
168
169 $ falist_set $LIST K0 'V0 1'
170 $ falist_debug LIST
171 DEBUG: alist `LIST' has length 3
172 DEBUG: the items:
173 DEBUG: `K2' -> `V2 ' \ "$abc'
174 DEBUG: `K1' -> `V1 1'
175 DEBUG: `K0' -> `V0 1'
176
177 Insert in the midele again
178
179 $ falist_set LIST K1-1 'V1-1'
180 $ falist_debug LIST
181 DEBUG: alist `LIST' has length 4
182 DEBUG: the items:
183 DEBUG: `K2' -> `V2 ' \ "$abc'
184 DEBUG: `K1' -> `V1 1'
185 DEBUG: `K0' -> `V0 1'
186 DEBUG: `K1-1' -> `V1-1'
187
188 Clear resets to initial values
189
190 $ falist_clear LIST
191 $ check_no_alist_artifacts
192 _farr_KV_[a-f0-9]+_B=0 (re)
193 _farr_KV_[a-f0-9]+_C=1 (re)
194 _farr_KV_[a-f0-9]+_P='0;0' (re)
195 _farr_KV_[a-f0-9]+__=0 (re)
196 [1]
197
198 $ falist_print_length LIST
199 0 (no-eol)
200 $ falist_release LIST
201 $ check_no_alist_artifacts
202
40 203
41 Clear 204 Clear
42 ===== 205 =====
43 206
44 $ falist_create LIST 207 $ falist_create LIST
45 $ falist_istrue LIST 208 $ falist_istrue LIST
46 [1] 209 [1]
210 $ falist_set LIST K2 V2
211 $ falist_istrue LIST
47 $ falist_set LIST K1 V1 212 $ falist_set LIST K1 V1
48 $ falist_istrue LIST
49 $ falist_set LIST K2 V2
50 $ falist_debug LIST 213 $ falist_debug LIST
51 DEBUG: alist `LIST' has length 2 214 DEBUG: alist `LIST' has length 2
52 DEBUG: the items: 215 DEBUG: the items:
53 DEBUG: `K1' -> `V1' 216 DEBUG: `K2' -> `V2'
54 DEBUG: `K2' -> `V2' 217 DEBUG: `K1' -> `V1'
55 $ falist_length _i LIST 218 $ falist_length _i LIST
56 $ echo "$_i" 219 $ echo "$_i"
57 2 220 2
58 $ falist_print_length LIST 221 $ falist_print_length LIST
59 2 (no-eol) 222 2 (no-eol)
65 $ falist_istrue LIST 228 $ falist_istrue LIST
66 [1] 229 [1]
67 $ falist_print_length LIST 230 $ falist_print_length LIST
68 0 (no-eol) 231 0 (no-eol)
69 232
233 Clear resets to initial values
234
235 $ falist_clear LIST
236 $ check_no_alist_artifacts
237 _farr_KV_[a-f0-9]+_B=0 (re)
238 _farr_KV_[a-f0-9]+_C=1 (re)
239 _farr_KV_[a-f0-9]+_P='0;0' (re)
240 _farr_KV_[a-f0-9]+__=0 (re)
241 [1]
242 $ falist_istrue LIST
243 [1]
70 $ falist_release LIST 244 $ falist_release LIST
71 $ falist_istrue LIST 245 $ falist_istrue LIST
72 ERROR: object `LIST' not created properly: token empty 246 ERROR: object `LIST' not created properly: token empty
73 [1] 247 [1]
74 $ check_no_alist_artifacts 248 $ check_no_alist_artifacts
249
250
251 Optimized Adding
252 ================
253
254 $ falist_create LIST
255 $ falist_add LIST k1 v1
256 $ falist_add LIST k2 v2
257 Would violate order requirements
258 $ ( falist_add LIST k0 v0 )
259 ERROR: falist_add() would violate key order
260 [70]
261 $ ( falist_add LIST k2 v2-2 )
262 ERROR: falist_add() would violate key order
263 [70]
264 $ falist_add $LIST k3 $'" 111222333" \\\'444555666 ' # '
265 $ falist_debug LIST
266 DEBUG: alist `LIST' has length 3
267 DEBUG: the items:
268 DEBUG: `k1' -> `v1'
269 DEBUG: `k2' -> `v2'
270 DEBUG: `k3' -> `" 111222333" \'444555666 '
271 $ falist_add $LIST k4 'v4 1' k5 v5
272 $ falist_debug LIST
273 DEBUG: alist `LIST' has length 5
274 DEBUG: the items:
275 DEBUG: `k1' -> `v1'
276 DEBUG: `k2' -> `v2'
277 DEBUG: `k3' -> `" 111222333" \'444555666 '
278 DEBUG: `k4' -> `v4 1'
279 DEBUG: `k5' -> `v5'
280 $ falist_release LIST
281 $ check_no_alist_artifacts
282
283
284 Iteration
285 =========
286
287 $ falist_create LIST
288 $ falist_set LIST K1 V1
289 $ falist_set LIST K2 "V2 2"
290 $ falist_set LIST K3 $'" 111222333" \\\'444555 ' # '
291
292 Consistency check of storage cookies
293
294 $ _c="$(falist_cookie_first LIST)"
295 $ echo $_c
296 1/0;2@[a-f0-9]+ (re)
297 $ _cnull="$(falist_cookie_prev "${_c}")"
298 $ echo "${_cnull}"
299 0/0;0@[a-f0-9]+ (re)
300 $ _c2="$(falist_cookie_next "$_c")"
301 $ echo "${_c2}"
302 2/1;3@[a-f0-9]+ (re)
303 $ _c3="$(falist_cookie_prev "${_c2}")"
304 $ test "${_c}" = "${_c3}"
305 $ _c=$(falist_cookie_last LIST)
306 $ echo $_c
307 3/2;0@[a-f0-9]+ (re)
308 $ _cnull="$(falist_cookie_next "${_c}")"
309 $ echo "${_cnull}"
310 0/0;0@[a-f0-9]+ (re)
311
312 MANUAL (by cookie, forward in insertion order)
313
314 $ _pos="$(falist_cookie_first LIST)"
315 > while falist_tryget_item_at _k _v "$_pos"; do
316 > printf $'%s -> `%s\'\\n' "$_k" "$_v"
317 > _pos="$(falist_cookie_next "$_pos")"
318 > done
319 K1 -> `V1'
320 K2 -> `V2 2'
321 K3 -> `" 111222333" \'444555 '
322
323 MANUAL (by cookie, reversed insertion order)
324
325 $ _pos="$(falist_cookie_last LIST)"
326 > while falist_tryget_item_at _k _v "$_pos"; do
327 > printf $'`%s\' <- %s\\n' "$_v" "$_k"
328 > _pos="$(falist_cookie_prev "$_pos")"
329 > done
330 `" 111222333" \'444555 ' <- K3
331 `V2 2' <- K2
332 `V1' <- K1
333
334 MANUAL values (by cookie, forward in insertion order)
335
336 $ _pos="$(falist_cookie_first LIST)"
337 > while falist_tryget_value_at _v "$_pos"; do
338 > printf $'`%s\'\\n' "$_v"
339 > _pos="$(falist_cookie_next "$_pos")"
340 > done
341 `V1'
342 `V2 2'
343 `" 111222333" \'444555 '
344
345
346 MANUAL keys (by cookie, reversed insertion order)
347
348 $ _pos="$(falist_cookie_last LIST)"
349 > while falist_tryget_key_at _k "$_pos"; do
350 > printf '%s\n' "$_k"
351 > _pos="$(falist_cookie_prev "$_pos")"
352 > done
353 K3
354 K2
355 K1
356
357 ITERATE (for each, by name)
358
359 $ falist_for_each LIST $'printf "EACH: %s key \\`%s\\\', value \\`%s\\\' at cookie %s\\n"' # `
360 EACH: LIST key `K1', value `V1' at cookie 1/[0-9]+;[0-9]+@[a-f0-9]+ (re)
361 EACH: LIST key `K2', value `V2 2' at cookie 2/[0-9]+;[0-9]+@[a-f0-9]+ (re)
362 EACH: LIST key `K3', value `" 111222333" \\'444555 ' at cookie 3/[0-9]+;[0-9]+@[a-f0-9]+ (re)
363
364 ITERATE (for each, by value)
365
366 $ falist_for_each "$LIST" $'printf "EACH: %s key \\`%s\\\', value \\`%s\\\' at cookie %s\\n"' # `
367 EACH: _farr_KV\[\?,\?\]:[a-f0-9]+ key `K1', value `V1' at cookie 1/[0-9]+;[0-9]+@[a-f0-9]+ (re)
368 EACH: _farr_KV\[\?,\?\]:[a-f0-9]+ key `K2', value `V2 2' at cookie 2/[0-9]+;[0-9]+@[a-f0-9]+ (re)
369 EACH: _farr_KV\[\?,\?\]:[a-f0-9]+ key `K3', value `" 111222333" \\'444555 ' at cookie 3/[0-9]+;[0-9]+@[a-f0-9]+ (re)
370
371 $ falist_clear LIST
372
373 $ falist_release LIST
374 $ falist_release LIST
375 ERROR: object `LIST' not created properly: token empty
376 [1]
377 $ check_no_alist_artifacts
378
379
380 Deleting
381 ========
382
383 $ falist_create LIST
384 $ (falist_trydel LIST foo)
385 [1]
386
387 $ falist_release LIST
388 $ check_no_alist_artifacts
389
390 $ falist_create LIST K1 V1
391 $ (falist_trydel LIST foo)
392 [1]
393 $ falist_debug LIST
394 DEBUG: alist `LIST' has length 1
395 DEBUG: the items:
396 DEBUG: `K1' -> `V1'
397 $ falist_istrue LIST
398 $ falist_trydel LIST K1
399 $ falist_debug LIST
400 DEBUG: alist `LIST' has length 0
401 $ check_no_alist_artifacts
402 _farr_KV_[a-f0-9]+_B=0 (re)
403 _farr_KV_[a-f0-9]+_C=1 (re)
404 _farr_KV_[a-f0-9]+_P='0;0' (re)
405 _farr_KV_[a-f0-9]+__=0 (re)
406 [1]
407 $ (falist_istrue LIST)
408 [1]
409 $ falist_release LIST
410 $ check_no_alist_artifacts
411
412 $ falist_create LIST K1 V1 K2 V2
413 $ (falist_trydel LIST foo)
414 [1]
415 $ falist_trydel LIST K1
416 $ falist_debug LIST
417 DEBUG: alist `LIST' has length 1
418 DEBUG: the items:
419 DEBUG: `K2' -> `V2'
420 $ falist_trydel LIST K2
421 $ falist_debug LIST
422 DEBUG: alist `LIST' has length 0
423 $ falist_release LIST
424 $ check_no_alist_artifacts
425
426 $ falist_create LIST K1 V1 K2 V2
427 $ (falist_trydel LIST foo)
428 [1]
429 $ falist_trydel LIST K2
430 $ falist_debug LIST
431 DEBUG: alist `LIST' has length 1
432 DEBUG: the items:
433 DEBUG: `K1' -> `V1'
434 $ falist_trydel LIST K1
435 $ falist_debug LIST
436 DEBUG: alist `LIST' has length 0
437 $ falist_release LIST
438 $ check_no_alist_artifacts
439
440 $ falist_create LIST K1 V1 K2 V2 K3 V3
441 $ (falist_trydel LIST foo)
442 [1]
443 $ falist_trydel LIST K1
444 $ falist_debug LIST
445 DEBUG: alist `LIST' has length 2
446 DEBUG: the items:
447 DEBUG: `K2' -> `V2'
448 DEBUG: `K3' -> `V3'
449 $ falist_release LIST
450 $ check_no_alist_artifacts
451
452 $ falist_create LIST K1 V1 K2 V2 K3 V3
453 $ (falist_trydel LIST foo)
454 [1]
455 $ falist_trydel LIST K2
456 $ falist_debug LIST
457 DEBUG: alist `LIST' has length 2
458 DEBUG: the items:
459 DEBUG: `K1' -> `V1'
460 DEBUG: `K3' -> `V3'
461 $ falist_release LIST
462 $ check_no_alist_artifacts
463
464 $ falist_create LIST K1 V1 K2 V2 K3 V3
465 $ (falist_trydel LIST foo)
466 [1]
467 $ falist_trydel LIST K3
468 $ falist_debug LIST
469 DEBUG: alist `LIST' has length 2
470 DEBUG: the items:
471 DEBUG: `K1' -> `V1'
472 DEBUG: `K2' -> `V2'
473 $ falist_release LIST
474 $ check_no_alist_artifacts
475
476 $ falist_create LIST K1 V1 K2 V2 K3 V3
477 $ (falist_trydel LIST foo)
478 [1]
479 $ falist_trydel LIST K2
480 $ falist_trydel LIST K1
481 $ falist_trydel LIST K3
482 $ falist_debug LIST
483 DEBUG: alist `LIST' has length 0
484 Check that the binary search list is cleaned up
485 $ check_no_alist_artifacts
486 _farr_KV_[a-f0-9]+_B=0 (re)
487 _farr_KV_[a-f0-9]+_C=1 (re)
488 _farr_KV_[a-f0-9]+_P='0;0' (re)
489 _farr_KV_[a-f0-9]+__=0 (re)
490 [1]
491 $ falist_release LIST
492 $ check_no_alist_artifacts
493
494 $ falist_create LIST K1 V1 K2 V2 K3 V3
495 $ (falist_trydel LIST foo)
496 [1]
497 $ falist_trydel LIST K2
498 Skipping tombstones
499 $ (falist_trydel LIST K2)
500 [1]
501 $ falist_trydel LIST K3
502 $ (falist_trydel LIST K3)
503 [1]
504 $ falist_debug LIST
505 DEBUG: alist `LIST' has length 1
506 DEBUG: the items:
507 DEBUG: `K1' -> `V1'
508 Check that the binary search list is cleaned up properly: just the first
509 storage entries are left
510 $ check_no_alist_artifacts
511 _farr_KV_[a-f0-9]+_B=1 (re)
512 _farr_KV_[a-f0-9]+_C=1 (re)
513 _farr_KV_[a-f0-9]+_P='1;1' (re)
514 _farr_KV_[a-f0-9]+__=1 (re)
515 _farr_Kb_[a-f0-9]+_1='1;V@K1' (re)
516 _farr_Ks_[a-f0-9]+_1='0;0@K1' (re)
517 _farr_Vs_[a-f0-9]+_1=V1 (re)
518 [1]
519 Can re-add K2 now again
520 $ falist_add LIST K2 'V2 2'
521 $ falist_release LIST
522 $ check_no_alist_artifacts
523
524 Try this with reversed insertion order also
525
526 $ falist_create LIST K3 V3 K2 V2 K1 V1
527 $ falist_find _var LIST K3
528 Cookie has structure <storage-ptr>/<storage-prev-ptr>;<storage-next-ptr>@<object-token>
529 $ echo "$_var"
530 1/[0-9]+;[0-9]+@[a-f0-9]+ (re)
531 $ falist_contains LIST K3
532 $ falist_find _var LIST K2
533 $ echo "$_var"
534 2/[0-9]+;[0-9]+@[a-f0-9]+ (re)
535 $ falist_contains LIST K2
536 $ falist_find _var LIST K1
537 $ echo "$_var"
538 3/[0-9]+;[0-9]+@[a-f0-9]+ (re)
539 $ falist_contains LIST K1
540 $ (falist_trydel LIST foo)
541 [1]
542 $ falist_trydel LIST K2
543 Skipping tombstones
544 $ (falist_trydel LIST K2)
545 [1]
546 $ falist_trydel LIST K3
547 $ (falist_trydel LIST K3)
548 [1]
549 $ falist_debug LIST
550 DEBUG: alist `LIST' has length 1
551 DEBUG: the items:
552 DEBUG: `K1' -> `V1'
553 Check that the binary search list is cleaned up properly: just the first
554 storage entries are left
555 $ check_no_alist_artifacts
556 _farr_KV_[a-f0-9]+_B=1 (re)
557 _farr_KV_[a-f0-9]+_C=1 (re)
558 _farr_KV_[a-f0-9]+_P='3;3' (re)
559 _farr_KV_[a-f0-9]+__=1 (re)
560 _farr_Kb_[a-f0-9]+_1='3;V@K1' (re)
561 _farr_Ks_[a-f0-9]+_3='0;0@K1' (re)
562 _farr_Vs_[a-f0-9]+_3=V1 (re)
563 [1]
564 Can re-add K2 now again
565 $ falist_add LIST K2 'V2 2'
566 $ falist_find _var LIST K2
567 $ falist_contains LIST K2
568 But K3 is tombstoned/deleted
569 $ (falist_find _var LIST K3)
570 [1]
571 $ (falist_contains LIST K3)
572 [1]
573 $ farray_create ARRAY
574 $ falist_keys ARRAY LIST
575 $ farray_debug ARRAY
576 DEBUG: array `ARRAY' has length 2
577 DEBUG: the items:
578 DEBUG: 1: `K1'
579 DEBUG: 2: `K2'
580 $ farray_release ARRAY
581 $ farray_create ARRAY
582 $ falist_values ARRAY LIST
583 $ farray_debug ARRAY
584 DEBUG: array `ARRAY' has length 2
585 DEBUG: the items:
586 DEBUG: 1: `V1'
587 DEBUG: 2: `V2 2'
588 $ farray_release ARRAY
589 $ farray_create ARRAY
590 $ falist_items ARRAY LIST
591 $ farray_debug ARRAY
592 DEBUG: array `ARRAY' has length 4
593 DEBUG: the items:
594 DEBUG: 1: `K1'
595 DEBUG: 2: `V1'
596 DEBUG: 3: `K2'
597 DEBUG: 4: `V2 2'
598 $ farray_release ARRAY
599 $ falist_release LIST
600 $ check_no_alist_artifacts
601 $ check_no_array_artifacts
75 602
76 603
77 Get / Set / Contains / Find Index 604 Get / Set / Contains / Find Index
78 ================================= 605 =================================
79 606
106 3 633 3
107 $ falist_print_length LIST 634 $ falist_print_length LIST
108 3 (no-eol) 635 3 (no-eol)
109 636
110 $ falist_contains LIST K1 637 $ falist_contains LIST K1
111 $ falist_find idx LIST K1 638 $ falist_find cookie LIST K1
112 $ test "$idx" -eq 1 639 $ echo "$cookie"
640 1/[0-9]+;[0-9]+@[a-f0-9]+ (re)
113 $ falist_contains LIST K 641 $ falist_contains LIST K
114 [1] 642 [1]
115 $ falist_find idx LIST K 643 $ falist_find cookie LIST K
116 [1] 644 [1]
117 $ falist_get _var LIST K2 645 $ falist_get _var LIST K2
118 $ echo "$_var" 646 $ echo "$_var"
119 V2 2 647 V2 2
120 $ falist_tryget _var LIST K1 648 $ falist_tryget _var LIST K1
121 $ echo "$_var" 649 $ echo "$_var"
122 V1 650 V1
123 $ falist_tryget _i LIST K 651 $ falist_tryget _i LIST K
124 [1] 652 [1]
125 653
126 $ falist_get _var LIST K2 _idx 654 $ falist_get _var LIST K2 _cookie
127 $ echo "$_var" 655 $ echo "$_var"
128 V2 2 656 V2 2
129 $ echo "$_idx" 657 $ echo "$_cookie"
130 2 658 2/[0-9]+;[0-9]+@[a-f0-9]+ (re)
131 $ falist_tryget _var LIST K1 _idx 659 $ falist_tryget _var LIST K1 _cookie
132 $ echo "$_var" 660 $ echo "$_var"
133 V1 661 V1
134 $ echo "$_idx" 662 $ echo "$_cookie"
135 1 663 1/[0-9]+;[0-9]+@[a-f0-9]+ (re)
136 $ falist_tryget _i LIST K _idx 664 $ falist_tryget _i LIST K _cookie
137 [1] 665 [1]
138 $ _var="$(falist_print_length NON_EXISTING_LIST)" 666 $ _var="$(falist_print_length NON_EXISTING_LIST)"
139 ERROR: object `NON_EXISTING_LIST' not created properly: token empty 667 ERROR: object `NON_EXISTING_LIST' not created properly: token empty
140 $ echo "${_var}" 668 $ echo "${_var}"
141 -1 669 -1
146 DEBUG: `K1' -> `V1' 674 DEBUG: `K1' -> `V1'
147 DEBUG: `K2' -> `V2 2' 675 DEBUG: `K2' -> `V2 2'
148 DEBUG: `K3' -> `" 111222333" \'444555 ' 676 DEBUG: `K3' -> `" 111222333" \'444555 '
149 DEBUG: `K4' -> `V4' 677 DEBUG: `K4' -> `V4'
150 $ falist_release LIST 678 $ falist_release LIST
151 $ check_no_alist_artifacts
152
153 Special case empty list (because of start/stop extra indexes)
154
155 $ falist_create LIST
156 $ falist_find _var LIST foo
157 [1]
158 $ falist_release LIST
159 $ check_no_alist_artifacts
160
161
162 Add
163 ===
164
165 $ falist_create LIST
166 $ falist_add LIST K1 'V 1'
167 $ falist_add LIST K2 'V 2'
168 $ falist_add LIST K3 $'" 111222333" \\\'444555666 ' # '
169 $ falist_debug LIST
170 DEBUG: alist `LIST' has length 3
171 DEBUG: the items:
172 DEBUG: `K1' -> `V 1'
173 DEBUG: `K2' -> `V 2'
174 DEBUG: `K3' -> `" 111222333" \'444555666 '
175 Yes: make a duplicate key
176 $ falist_add LIST K3 $'" 111222333" \\\'444555666 ' #
177 $ falist_debug LIST
178 DEBUG: alist `LIST' has length 4
179 DEBUG: the items:
180 DEBUG: `K1' -> `V 1'
181 DEBUG: `K2' -> `V 2'
182 DEBUG: `K3' -> `" 111222333" \'444555666 '
183 DEBUG: `K3' -> `" 111222333" \'444555666 '
184 $ falist_release LIST
185 $ check_no_alist_artifacts
186
187
188 Iteration
189 =========
190
191 ITERATE (manual indexing)
192
193 $ falist_create LIST
194 $ falist_set LIST K1 V1
195 $ falist_set LIST K2 "V2 2"
196 $ falist_set LIST K3 $'" 111222333" \\\'444555 ' # '
197
198 Iteration by indexing key and values separately
199
200 $ _i=1
201 > while falist_tryget_key_at_index _k LIST ${_i}; do
202 > # cannot fail under "normal" circumstances
203 > falist_tryget_value_at_index _v LIST ${_i}
204 > printf " KEY: \`%s', VAL: \`%s'\\n" "${_k}" "${_v}"
205 > _i=$((_i + 1))
206 > done
207 KEY: `K1', VAL: `V1'
208 KEY: `K2', VAL: `V2 2'
209 KEY: `K3', VAL: `" 111222333" \'444555 '
210
211 ITERATE (manual indexing over items)
212
213 $ _i=1
214 > while falist_tryget_item_at_index _k _v LIST ${_i}; do
215 > printf " KEY: \`%s', VAL: \`%s'\\n" "${_k}" "${_v}"
216 > _i=$((_i + 1))
217 > done
218 KEY: `K1', VAL: `V1'
219 KEY: `K2', VAL: `V2 2'
220 KEY: `K3', VAL: `" 111222333" \'444555 '
221
222 ITERATE (for each, by name)
223
224 $ falist_for_each LIST $'printf "EACH: %s key \\`%s\\\', value \\`%s\\\' at idx %d\\n"' # `
225 EACH: LIST key `K1', value `V1' at idx 1
226 EACH: LIST key `K2', value `V2 2' at idx 2
227 EACH: LIST key `K3', value `" 111222333" \'444555 ' at idx 3
228
229 ITERATE (for each, by value)
230
231 $ falist_for_each "$LIST" $'printf "EACH: %s key \\`%s\\\', value \\`%s\\\' at idx %d\\n"' # `
232 EACH: _farr_KV\[\?,\?\]:[a-f0-9]+ key `K1', value `V1' at idx 1 (re)
233 EACH: _farr_KV\[\?,\?\]:[a-f0-9]+ key `K2', value `V2 2' at idx 2 (re)
234 EACH: _farr_KV\[\?,\?\]:[a-f0-9]+ key `K3', value `" 111222333" \\'444555 ' at idx 3 (re)
235
236 $ falist_clear LIST
237 $ falist_release LIST
238 $ falist_release LIST
239 ERROR: object `LIST' not created properly: token empty
240 [1]
241
242 $ check_no_alist_artifacts
243
244
245 Valid and Invalid Indices
246
247 $ falist_create LIST
248 $ falist_set LIST 'KEY 1' 'VAL 1'
249 $ falist_set LIST 'KEY 2' 'VAL 2'
250 $ falist_set LIST 'KEY 3' 'VAL 3'
251
252 $ (falist_tryget_item_at_index _k _v LIST "")
253 ERROR: missing index
254 [70]
255
256 $ (falist_tryget_item_at_index _k _v LIST)
257 ERROR: missing index
258 [70]
259
260 $ falist_tryget_item_at_index _k _v LIST 4
261 [1]
262
263 $ falist_tryget_item_at_index _k _v LIST 0
264 $ printf '%s:%s' "$_k" "$_v"
265 KEY 3:VAL 3 (no-eol)
266
267 $ falist_tryget_item_at_index _k _v LIST -2
268 $ printf '%s:%s' "$_k" "$_v"
269 KEY 1:VAL 1 (no-eol)
270
271 $ falist_tryget_item_at_index _k _v LIST -3
272 [1]
273
274 $ falist_release LIST
275 $ check_no_alist_artifacts
276
277
278 Deletion of keys
279 ================
280
281 $ falist_create LIST
282 $ falist_set LIST 'key 1' 'value 1'
283 $ falist_set LIST 'key 2' 'value 2'
284 $ falist_set LIST 'key 3' 'value 3'
285 $ falist_set LIST 'key 4' 'value 4'
286 $ falist_set LIST 'key 5' 'value 5'
287 $ falist_trydel LIST 'key 1'
288 $ falist_trydel LIST 'key 5'
289 $ falist_trydel LIST 'key 3'
290 $ falist_debug LIST
291 DEBUG: alist `LIST' has length 2
292 DEBUG: the items:
293 DEBUG: `key 2' -> `value 2'
294 DEBUG: `key 4' -> `value 4'
295 $ falist_trydel LIST "non-existing key"
296 [1]
297 $ falist_print_length LIST
298 2 (no-eol)
299 $ falist_get _var LIST 'key 2'
300 $ printf '%s' "$_var"
301 value 2 (no-eol)
302 $ falist_get _var LIST 'key 4'
303 $ printf '%s' "$_var"
304 value 4 (no-eol)
305
306 $ falist_release LIST
307
308 $ check_no_alist_artifacts
309
310
311 Compare
312 =======
313
314 $ falist_create LIST1 K1 V1 K2 V2
315 $ falist_debug LIST1
316 DEBUG: alist `LIST1' has length 2
317 DEBUG: the items:
318 DEBUG: `K1' -> `V1'
319 DEBUG: `K2' -> `V2'
320
321 $ falist_create LIST2 K2 V2 K1 V1
322 $ falist_debug LIST2
323 DEBUG: alist `LIST2' has length 2
324 DEBUG: the items:
325 DEBUG: `K2' -> `V2'
326 DEBUG: `K1' -> `V1'
327
328 $ falist_create LIST3 K1 V1 K2 V2
329 $ falist_debug LIST3
330 DEBUG: alist `LIST3' has length 2
331 DEBUG: the items:
332 DEBUG: `K1' -> `V1'
333 DEBUG: `K2' -> `V2'
334
335 $ falist_create LIST4 K1 V1 K2 V2-4
336 $ falist_debug LIST4
337 DEBUG: alist `LIST4' has length 2
338 DEBUG: the items:
339 DEBUG: `K1' -> `V1'
340 DEBUG: `K2' -> `V2-4'
341
342 $ falist_are_equal LIST1 LIST2
343 $ falist_are_equal LIST1 LIST4
344 [1]
345 $ falist_are_equal_with_order LIST1 LIST2
346 [1]
347 $ falist_are_equal_with_order LIST1 LIST3
348
349 $ falist_clear LIST2
350 $ falist_are_equal LIST1 LIST2
351 [1]
352
353 $ falist_clear LIST3
354 $ falist_are_equal_with_order LIST2 LIST3
355
356 $ falist_release LIST1
357 $ falist_release LIST2
358 $ falist_release LIST3
359 $ falist_release LIST4
360
361 $ check_no_alist_artifacts
362
363
364 Updating
365 ========
366
367 $ falist_create ARR "Key 1" "Value 1" "Key 2" 'Value 2 '\'''
368 $ falist_create UPDATE1 "Key 1" "Value 1" "Key 2" 'Value 2 '\'''
369 $ falist_create UPDATE2 "Key 2" 'Value 2 (Updated) '\''' "Key 3" "Value 3"
370 $ falist_create EMPTY
371
372 $ falist_are_equal_with_order ARR UPDATE1
373 $ falist_are_equal_with_order ARR UPDATE2
374 [1]
375
376 $ falist_update ARR UPDATE1
377 $ falist_are_equal_with_order ARR UPDATE1
378
379 $ falist_update ARR UPDATE2
380 $ falist_debug ARR
381 DEBUG: alist `ARR' has length 3
382 DEBUG: the items:
383 DEBUG: `Key 1' -> `Value 1'
384 DEBUG: `Key 2' -> `Value 2 (Updated) ''
385 DEBUG: `Key 3' -> `Value 3'
386
387 Updating an into an empty alist is just a copy
388
389 $ falist_update EMPTY UPDATE1
390 $ falist_debug EMPTY
391 DEBUG: alist `EMPTY' has length 2
392 DEBUG: the items:
393 DEBUG: `Key 1' -> `Value 1'
394 DEBUG: `Key 2' -> `Value 2 ''
395 $ falist_debug UPDATE1
396 DEBUG: alist `UPDATE1' has length 2
397 DEBUG: the items:
398 DEBUG: `Key 1' -> `Value 1'
399 DEBUG: `Key 2' -> `Value 2 ''
400
401 $ falist_release ARR
402 $ falist_release UPDATE1
403 $ falist_release UPDATE2
404 $ falist_release EMPTY
405
406 $ check_no_alist_artifacts
407
408
409 Merging
410 =======
411
412 $ falist_create RES
413 $ falist_create INPUT1 "K1" "V1" "K2" "V2" "K3" "V3"
414 $ falist_create INPUT2
415 $ falist_merge "$RES" "$INPUT1" "$INPUT2"
416 $ falist_release INPUT1
417 $ falist_release INPUT2
418 $ falist_debug RES
419 DEBUG: alist `RES' has length 3
420 DEBUG: the items:
421 DEBUG: `K1' -> `V1'
422 DEBUG: `K2' -> `V2'
423 DEBUG: `K3' -> `V3'
424 $ falist_release RES
425 $ check_no_alist_artifacts
426
427 $ falist_create RES
428 $ falist_create INPUT1
429 $ falist_create INPUT2 "k1" "v1" "k2" "v2" "k3" "v3"
430 $ falist_merge "$RES" "$INPUT1" "$INPUT2"
431 $ falist_release INPUT1
432 $ falist_release INPUT2
433 $ falist_debug RES
434 DEBUG: alist `RES' has length 3
435 DEBUG: the items:
436 DEBUG: `k1' -> `v1'
437 DEBUG: `k2' -> `v2'
438 DEBUG: `k3' -> `v3'
439 $ falist_release RES
440 $ check_no_alist_artifacts
441
442 $ falist_create RES
443 $ falist_create INPUT1 "K1" "V1" "K2" "V2" "K3" "V3"
444 $ falist_create INPUT2 "k1" "v1" "k2" "v2" "k3" "v3" "k4" "v4"
445 $ falist_merge "$RES" "$INPUT1" "$INPUT2"
446 $ falist_release INPUT1
447 $ falist_release INPUT2
448 $ falist_debug RES
449 DEBUG: alist `RES' has length 7
450 DEBUG: the items:
451 DEBUG: `K1' -> `V1'
452 DEBUG: `K2' -> `V2'
453 DEBUG: `K3' -> `V3'
454 DEBUG: `k1' -> `v1'
455 DEBUG: `k2' -> `v2'
456 DEBUG: `k3' -> `v3'
457 DEBUG: `k4' -> `v4'
458 $ falist_release RES
459 $ check_no_alist_artifacts
460
461 $ falist_create RES
462 $ falist_create INPUT1 "k1" "v1" "k2" "v2" "k3" "v3" "k4" "v4"
463 $ falist_create INPUT2 "K1" "V1" "K2" "V2" "K3" "V3"
464 $ falist_merge "$RES" "$INPUT1" "$INPUT2"
465 $ falist_release INPUT1
466 $ falist_release INPUT2
467 $ falist_debug RES
468 DEBUG: alist `RES' has length 7
469 DEBUG: the items:
470 DEBUG: `K1' -> `V1'
471 DEBUG: `K2' -> `V2'
472 DEBUG: `K3' -> `V3'
473 DEBUG: `k1' -> `v1'
474 DEBUG: `k2' -> `v2'
475 DEBUG: `k3' -> `v3'
476 DEBUG: `k4' -> `v4'
477 $ falist_release RES
478 $ check_no_alist_artifacts
479
480 $ falist_create RES
481 $ falist_create INPUT1 "K1" "V1" "K3" "V3" "k1" "v1" "k4" "v4"
482 $ falist_create INPUT2 "K2" "V2" "k2" "v2" "k3" "v3"
483 $ falist_merge "$RES" "$INPUT1" "$INPUT2"
484 $ falist_release INPUT1
485 $ falist_release INPUT2
486 $ falist_debug RES
487 DEBUG: alist `RES' has length 7
488 DEBUG: the items:
489 DEBUG: `K1' -> `V1'
490 DEBUG: `K2' -> `V2'
491 DEBUG: `K3' -> `V3'
492 DEBUG: `k1' -> `v1'
493 DEBUG: `k2' -> `v2'
494 DEBUG: `k3' -> `v3'
495 DEBUG: `k4' -> `v4'
496 $ falist_release RES
497 $ check_no_alist_artifacts
498
499 $ falist_create RES
500 $ falist_create INPUT1 "K1" "V1" "K3" "V3" "k1" "v1" "k4" "v4"
501 $ falist_create INPUT2 "K2" "V2" "k2" "v2" "k3" "v3"
502 $ falist_merge "$RES" INPUT2 INPUT1
503 $ falist_release INPUT1
504 $ falist_release INPUT2
505 $ falist_debug RES
506 DEBUG: alist `RES' has length 7
507 DEBUG: the items:
508 DEBUG: `K1' -> `V1'
509 DEBUG: `K2' -> `V2'
510 DEBUG: `K3' -> `V3'
511 DEBUG: `k1' -> `v1'
512 DEBUG: `k2' -> `v2'
513 DEBUG: `k3' -> `v3'
514 DEBUG: `k4' -> `v4'
515 $ falist_release RES
516 $ check_no_alist_artifacts 679 $ check_no_alist_artifacts
517 680
518 681
519 Items / Keys / Values 682 Items / Keys / Values
520 ===================== 683 =====================
547 $ falist_release LIST 710 $ falist_release LIST
548 $ farray_release KEYS 711 $ farray_release KEYS
549 $ farray_release VALUES 712 $ farray_release VALUES
550 $ farray_release ITEMS 713 $ farray_release ITEMS
551 714
715 $ check_no_alist_artifacts
716
717
718 Compare
719 =======
720
721 $ falist_create LIST1 K1 V1 K2 V2
722 $ falist_debug LIST1
723 DEBUG: alist `LIST1' has length 2
724 DEBUG: the items:
725 DEBUG: `K1' -> `V1'
726 DEBUG: `K2' -> `V2'
727
728 $ falist_create LIST2 K2 V2 K1 V1
729 $ falist_debug LIST2
730 DEBUG: alist `LIST2' has length 2
731 DEBUG: the items:
732 DEBUG: `K2' -> `V2'
733 DEBUG: `K1' -> `V1'
734
735 $ falist_create LIST3 K1 V1 K2 V2
736 $ falist_debug LIST3
737 DEBUG: alist `LIST3' has length 2
738 DEBUG: the items:
739 DEBUG: `K1' -> `V1'
740 DEBUG: `K2' -> `V2'
741
742 $ falist_create LIST4 K1 V1 K2 V2-4
743 $ falist_debug LIST4
744 DEBUG: alist `LIST4' has length 2
745 DEBUG: the items:
746 DEBUG: `K1' -> `V1'
747 DEBUG: `K2' -> `V2-4'
748
749 $ falist_are_equal LIST1 LIST2
750 $ falist_are_equal LIST1 LIST4
751 [1]
752 $ falist_are_equal_with_order LIST1 LIST2
753 [1]
754 $ falist_are_equal_with_order LIST1 LIST3
755
756 $ falist_clear LIST2
757 $ falist_are_equal LIST1 LIST2
758 [1]
759
760 $ falist_clear LIST3
761 $ falist_are_equal_with_order LIST2 LIST3
762
763 $ falist_release LIST1
764 $ falist_release LIST2
765 $ falist_release LIST3
766 $ falist_release LIST4
767
768 $ check_no_alist_artifacts
769
770
771 Updating
772 ========
773
774 Just replace existing items
775
776 $ falist_create LIST k2 v2 k4 v4 k6 v6 k8 v8
777 $ falist_debug LIST
778 DEBUG: alist `LIST' has length 4
779 DEBUG: the items:
780 DEBUG: `k2' -> `v2'
781 DEBUG: `k4' -> `v4'
782 DEBUG: `k6' -> `v6'
783 DEBUG: `k8' -> `v8'
784
785 $ falist_create UPDATE1 k2 v2-2 k4 v4-2 k8 v8-2
786 $ falist_update LIST UPDATE1
787 $ falist_debug LIST
788 DEBUG: alist `LIST' has length 4
789 DEBUG: the items:
790 DEBUG: `k2' -> `v2-2'
791 DEBUG: `k4' -> `v4-2'
792 DEBUG: `k6' -> `v6'
793 DEBUG: `k8' -> `v8-2'
794
795 $ falist_release LIST
796 $ falist_release UPDATE1
797 $ check_no_alist_artifacts
798
799
800 Handle previously deleted items also
801
802 $ falist_create LIST k2 v2 k4 v4 k6 v6 k8 v8
803 $ falist_trydel LIST k2
804 $ falist_create UPDATE1 k2 v2-2 k4 v4-2 k8 v8-2
805 $ falist_update LIST UPDATE1
806 $ falist_debug LIST
807 DEBUG: alist `LIST' has length 4
808 DEBUG: the items:
809 DEBUG: `k4' -> `v4-2'
810 DEBUG: `k6' -> `v6'
811 DEBUG: `k8' -> `v8-2'
812 DEBUG: `k2' -> `v2-2'
813
814 $ falist_release LIST
815 $ falist_release UPDATE1
816 $ check_no_alist_artifacts
817
818 $ falist_create LIST k2 v2 k4 v4 k6 v6 k8 v8
819 $ falist_trydel LIST k4
820 $ falist_create UPDATE1 k2 v2-2 k4 v4-2 k8 v8-2
821 $ falist_update LIST UPDATE1
822 $ falist_debug LIST
823 DEBUG: alist `LIST' has length 4
824 DEBUG: the items:
825 DEBUG: `k2' -> `v2-2'
826 DEBUG: `k6' -> `v6'
827 DEBUG: `k8' -> `v8-2'
828 DEBUG: `k4' -> `v4-2'
829
830 $ falist_release LIST
831 $ falist_release UPDATE1
832 $ check_no_alist_artifacts
833
834
835 Just appending
836
837 $ falist_create LIST k2 v2 k4 v4 k6 v6 k8 v8
838 $ falist_create UPDATE1 k9 v9 k91 v91
839 $ falist_update LIST UPDATE1
840 $ falist_debug LIST
841 DEBUG: alist `LIST' has length 6
842 DEBUG: the items:
843 DEBUG: `k2' -> `v2'
844 DEBUG: `k4' -> `v4'
845 DEBUG: `k6' -> `v6'
846 DEBUG: `k8' -> `v8'
847 DEBUG: `k9' -> `v9'
848 DEBUG: `k91' -> `v91'
849
850 $ falist_release LIST
851 $ falist_release UPDATE1
852 $ check_no_alist_artifacts
853
854
855 Append after deletion
856
857 $ falist_create LIST k2 v2 k4 v4 k6 v6 k8 v8
858 $ falist_trydel LIST k8
859 $ falist_create UPDATE1 k2 v2-2 k4 v4-2 k8 v8-2
860 $ falist_update LIST UPDATE1
861 $ falist_debug LIST
862 DEBUG: alist `LIST' has length 4
863 DEBUG: the items:
864 DEBUG: `k2' -> `v2-2'
865 DEBUG: `k4' -> `v4-2'
866 DEBUG: `k6' -> `v6'
867 DEBUG: `k8' -> `v8-2'
868
869 $ falist_release LIST
870 $ falist_release UPDATE1
871 $ check_no_alist_artifacts
872
873 Insertions
874
875 $ falist_create LIST k2 v2 k4 v4 k6 v6 k8 v8
876 $ falist_create UPDATE1 k1 v1 k3 v3 k7 v7
877 $ falist_update LIST UPDATE1
878 $ falist_debug LIST
879 DEBUG: alist `LIST' has length 7
880 DEBUG: the items:
881 DEBUG: `k2' -> `v2'
882 DEBUG: `k4' -> `v4'
883 DEBUG: `k6' -> `v6'
884 DEBUG: `k8' -> `v8'
885 DEBUG: `k1' -> `v1'
886 DEBUG: `k3' -> `v3'
887 DEBUG: `k7' -> `v7'
888
889 $ falist_release LIST
890 $ falist_release UPDATE1
552 $ check_no_alist_artifacts 891 $ check_no_alist_artifacts
553 892
554 893
555 Cross Type Checks 894 Cross Type Checks
556 ================= 895 =================