All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_list.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2017 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
19 #include <aerospike/as_bytes.h>
20 #include <aerospike/as_double.h>
21 #include <aerospike/as_integer.h>
22 #include <aerospike/as_iterator.h>
23 #include <aerospike/as_string.h>
24 #include <aerospike/as_util.h>
25 #include <aerospike/as_val.h>
26 
27 #include <stdbool.h>
28 #include <stdint.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /******************************************************************************
35  * TYPES
36  *****************************************************************************/
37 
38 union as_list_iterator_u;
39 
40 struct as_list_hooks_s;
41 
42 /**
43  * Callback function for `as_list_foreach()`. Called for each element in the
44  * list.
45  *
46  * @param value The value of the current element.
47  * @param udata The user-data provided to the `as_list_foreach()`.
48  *
49  * @return true to continue iterating through the list.
50  * false to stop iterating.
51  */
52 typedef bool (* as_list_foreach_callback) (as_val * value, void * udata);
53 
54 /**
55  * as_list is an interface for List based data types.
56  *
57  * Implementations:
58  * - as_arraylist
59  *
60  * @extends as_val
61  * @ingroup aerospike_t
62  */
63 typedef struct as_list_s {
64 
65  /**
66  * @private
67  * as_list is a subtype of as_val.
68  * You can cast as_list to as_val.
69  */
70  as_val _;
71 
72  /**
73  * Information for this instance of as_list.
74  */
75  uint32_t flags;
76 
77  /**
78  * Hooks for subtypes of as_list to implement.
79  */
80  const struct as_list_hooks_s * hooks;
81 
82 } as_list;
83 
84 /**
85  * List Function Hooks
86  */
87 typedef struct as_list_hooks_s {
88 
89  /***************************************************************************
90  * instance hooks
91  **************************************************************************/
92 
93  /**
94  * Releases the subtype of as_list.
95  *
96  * @param map The map instance to destroy.
97  *
98  * @return true on success. Otherwise false.
99  */
100  bool (* destroy)(as_list * list);
101 
102  /***************************************************************************
103  * info hooks
104  **************************************************************************/
105 
106  /**
107  * The hash value of an as_list.
108  *
109  * @param list The list to get the hashcode value for.
110  *
111  * @return The hashcode value.
112  */
113  uint32_t (* hashcode)(const as_list * list);
114 
115  /**
116  * The size of the as_list.
117  *
118  * @param map The map to get the size of.
119  *
120  * @return The number of entries in the map.
121  */
122  uint32_t (* size)(const as_list * list);
123 
124  /***************************************************************************
125  * get hooks
126  **************************************************************************/
127 
128  /**
129  * Get the value at a given index of the list.
130  *
131  * @param list The list to get the value from.
132  * @param index The index of the value.
133  *
134  * @return The value at the given index on success. Otherwie NULL.
135  */
136  as_val * (* get)(const as_list * list, uint32_t index);
137 
138  /**
139  * Get the int64_t value at a given index of the list.
140  *
141  * @param list The list to get the value from.
142  * @param index The index of the value.
143  *
144  * @return The value at the given index on success. Otherwie NULL.
145  */
146  int64_t (* get_int64)(const as_list * list, uint32_t index);
147 
148  /**
149  * Get the double value at a given index of the list.
150  *
151  * @param list The list to get the value from.
152  * @param index The index of the value.
153  *
154  * @return The value at the given index on success. Otherwie NULL.
155  */
156  double (* get_double)(const as_list * list, uint32_t index);
157 
158  /**
159  * Get the NULL-terminated string value at a given index of the list.
160  *
161  * @param list The list to get the value from.
162  * @param index The index of the value.
163  *
164  * @return The value at the given index on success. Otherwie NULL.
165  */
166  char * (* get_str)(const as_list * list, uint32_t index);
167 
168  /***************************************************************************
169  * set hooks
170  **************************************************************************/
171 
172  /**
173  * Set a value at the given index of the list.
174  *
175  * @param list The list to get the value from.
176  * @param index The index of the value.
177  * @param value The value for the given index.
178  *
179  * @return The value at the given index on success. Otherwie NULL.
180  */
181  int (* set)(as_list * list, uint32_t index, as_val * value);
182 
183  /**
184  * Set an int64_t value at the given index of the list.
185  *
186  * @param list The list to get the value from.
187  * @param index The index of the value.
188  * @param value The value for the given index.
189  *
190  * @return The value at the given index on success. Otherwie NULL.
191  */
192  int (* set_int64)(as_list * list, uint32_t index, int64_t value);
193 
194  /**
195  * Set a double value at the given index of the list.
196  *
197  * @param list The list to get the value from.
198  * @param index The index of the value.
199  * @param value The value for the given index.
200  *
201  * @return The value at the given index on success. Otherwie NULL.
202  */
203  int (* set_double)(as_list * list, uint32_t index, double value);
204 
205  /**
206  * Set a NULL-terminated string value at the given index of the list.
207  *
208  * @param list The list to get the value from.
209  * @param index The index of the value.
210  * @param value The value for the given index.
211  *
212  * @return The value at the given index on success. Otherwie NULL.
213  */
214  int (* set_str)(as_list * list, uint32_t index, const char * value);
215 
216  /***************************************************************************
217  * insert hooks
218  **************************************************************************/
219 
220  /**
221  * Insert a value at the given index of the list.
222  *
223  * @param list The list to insert the value into.
224  * @param index The index of the value.
225  * @param value The value for the given index.
226  *
227  * @return AS_ARRAYLIST_OK on success. Otherwise an error occurred.
228  */
229  int (* insert)(as_list * list, uint32_t index, as_val * value);
230 
231  /**
232  * Insert an int64_t value at the given index of the list.
233  *
234  * @param list The list to insert the value into.
235  * @param index The index of the value.
236  * @param value The value for the given index.
237  *
238  * @return AS_ARRAYLIST_OK on success. Otherwise an error occurred.
239  */
240  int (* insert_int64)(as_list * list, uint32_t index, int64_t value);
241 
242  /**
243  * Insert a double value at the given index of the list.
244  *
245  * @param list The list to insert the value into.
246  * @param index The index of the value.
247  * @param value The value for the given index.
248  *
249  * @return AS_ARRAYLIST_OK on success. Otherwise an error occurred.
250  */
251  int (* insert_double)(as_list * list, uint32_t index, double value);
252 
253  /**
254  * Insert a NULL-terminated string value at the given index of the list.
255  *
256  * @param list The list to insert the value into.
257  * @param index The index of the value.
258  * @param value The value for the given index.
259  *
260  * @return AS_ARRAYLIST_OK on success. Otherwise an error occurred.
261  */
262  int (* insert_str)(as_list * list, uint32_t index, const char * value);
263 
264  /***************************************************************************
265  * append hooks
266  **************************************************************************/
267 
268  /**
269  * Append a value to the list.
270  *
271  * @param list The list to append to.
272  * @param value The value to append to the list.
273  *
274  * @return 0 on success. Otherwise an error occurred.
275  */
276  int (* append)(as_list * list, as_val * value);
277 
278  /**
279  * Append an int64_t value to the list.
280  *
281  * @param list The list to append to.
282  * @param value The value to append to the list.
283  *
284  * @return 0 on success. Otherwise an error occurred.
285  */
286  int (* append_int64)(as_list * list, int64_t value);
287 
288  /**
289  * Append a double value to the list.
290  *
291  * @param list The list to append to.
292  * @param value The value to append to the list.
293  *
294  * @return 0 on success. Otherwise an error occurred.
295  */
296  int (* append_double)(as_list * list, double value);
297 
298  /**
299  * Append a NULL-terminates string value to the list.
300  *
301  * @param list The list to append to.
302  * @param value The value to append to the list.
303  *
304  * @return 0 on success. Otherwise an error occurred.
305  */
306  int (* append_str)(as_list * list, const char * value);
307 
308  /***************************************************************************
309  * prepend hooks
310  **************************************************************************/
311 
312  /**
313  * Prepend the value to the list.
314  *
315  * @param list The list to prepend to.
316  * @param value The value to prepend to the list.
317  *
318  * @return 0 on success. Otherwise an error occurred.
319  */
320  int (* prepend)(as_list * list, as_val * value);
321 
322  /**
323  * Prepend an int64_t value to the list.
324  *
325  * @param list The list to prepend to.
326  * @param value The value to prepend to the list.
327  *
328  * @return 0 on success. Otherwise an error occurred.
329  */
330  int (* prepend_int64)(as_list * list, int64_t value);
331 
332  /**
333  * Prepend a double value to the list.
334  *
335  * @param list The list to prepend to.
336  * @param value The value to prepend to the list.
337  *
338  * @return 0 on success. Otherwise an error occurred.
339  */
340  int (* prepend_double)(as_list * list, double value);
341 
342  /**
343  * Prepend a NULL-terminates string value to the list.
344  *
345  * @param list The list to prepend to.
346  * @param value The value to prepend to the list.
347  *
348  * @return 0 on success. Otherwise an error occurred.
349  */
350  int (* prepend_str)(as_list * list, const char * value);
351 
352  /***************************************************************************
353  * remove hook
354  **************************************************************************/
355 
356  /**
357  * Remove element at specified index.
358  *
359  * Any elements beyond specified index will be shifted so their indexes
360  * decrease by 1. The element at specified index will be destroyed.
361  *
362  * @param list The list.
363  * @param index The index of the element to remove.
364  *
365  * @return 0 on success. Otherwise an error occurred.
366  */
367  int (* remove)(as_list * list, uint32_t index);
368 
369  /***************************************************************************
370  * accessor and modifier hooks
371  **************************************************************************/
372 
373  /**
374  * Append all elements of list2, in order, to list. No new list object is
375  * created.
376  *
377  * @param list The list to append to.
378  * @param list2 The list from which to append.
379  *
380  * @return 0 on success. Otherwise an error occurred.
381  */
382  int (* concat)(as_list * list, const as_list * list2);
383 
384  /**
385  * Delete (and destroy) all elements at and beyond specified index. Capacity is
386  * not reduced.
387  *
388  * @param list The list to trim.
389  * @param index The index from which to trim.
390  *
391  * @return 0 on success. Otherwise an error occurred.
392  */
393  int (* trim)(as_list * list, uint32_t index);
394 
395  /**
396  * Return the first element in the list.
397  *
398  * @param list The list to get the value from.
399  *
400  * @return The first value in the list. Otherwise NULL.
401  */
402  as_val * (* head)(const as_list * list);
403 
404  /**
405  * Return all but the first element of the list, returning a new list.
406  *
407  * @param list The list to get the list from.
408  *
409  * @return The tail of the list. Otherwise NULL.
410  */
411  as_list * (* tail)(const as_list * list);
412 
413  /**
414  * Drop the first n element of the list, returning a new list.
415  *
416  * @param list The list.
417  * @param n The number of element to drop.
418  *
419  * @return A new list containing the remaining elements. Otherwise NULL.
420  */
421  as_list * (* drop)(const as_list * list, uint32_t n);
422 
423  /**
424  * Take the first n element of the list, returning a new list.
425  *
426  * @param list The list.
427  * @param n The number of element to take.
428  *
429  * @return A new list containing the remaining elements. Otherwise NULL.
430  */
431  as_list * (* take)(const as_list * list, uint32_t n);
432 
433  /***************************************************************************
434  * iteration hooks
435  **************************************************************************/
436 
437  /**
438  * Iterate over each element in the list can call the callback function.
439  *
440  * @param map The map to iterate.
441  * @param callback The function to call for each element in the list.
442  * @param udata User-data to be passed to the callback.
443  *
444  * @return true on success. Otherwise false.
445  */
446  bool (* foreach)(const as_list * list, as_list_foreach_callback callback, void * udata);
447 
448  /**
449  * Create and initialize a new heap allocated iterator to traverse over the list.
450  *
451  * @param list The list to iterate.
452  *
453  * @return true on success. Otherwise false.
454  */
455  union as_list_iterator_u * (* iterator_new)(const as_list * list);
456 
457  /**
458  * Initializes a stack allocated iterator to traverse over the list.
459  *
460  * @param list The list to iterate.
461  *
462  * @return true on success. Otherwise false.
463  */
464  union as_list_iterator_u * (* iterator_init)(const as_list * list, union as_list_iterator_u * it);
465 
466 } as_list_hooks;
467 
468 /*******************************************************************************
469  * INSTANCE FUNCTIONS
470  ******************************************************************************/
471 
472 /**
473  * @private
474  * Utilized by subtypes of as_list to initialize the parent.
475  *
476  * @param list The list to initialize.
477  * @param free If true, then as_list_destroy() will free the list.
478  * @param hooks Implementaton for the list interface.
479  *
480  * @return On success, the initialized list. Otherwise NULL.
481  * @relatesalso as_list
482  */
483 as_list * as_list_cons(as_list * list, bool free, const as_list_hooks * hooks);
484 
485 /**
486  * Initialize a stack allocated list.
487  *
488  * @param list Stack allocated list to initialize.
489  * @param hooks Implementaton for the list interface.
490  *
491  * @return On succes, the initialized list. Otherwise NULL.
492  * @relatesalso as_list
493  */
494 as_list * as_list_init(as_list * list, const as_list_hooks * hooks);
495 
496 /**
497  * Create and initialize a new heap allocated list.
498  *
499  * @param hooks Implementaton for the list interface.
500  *
501  * @return On succes, a new list. Otherwise NULL.
502  * @relatesalso as_list
503  */
504 as_list * as_list_new(const as_list_hooks * hooks);
505 
506 /**
507  * Destroy the list and associated resources.
508  *
509  * @param list The list to destroy.
510  * @relatesalso as_list
511  */
512 static inline void as_list_destroy(as_list * list)
513 {
514  as_val_destroy((as_val *) list);
515 }
516 
517 /******************************************************************************
518  * INFO FUNCTIONS
519  *****************************************************************************/
520 
521 /**
522  * Get the hashcode value for the list.
523  *
524  * @param list The list.
525  *
526  * @return The hashcode of the list.
527  * @relatesalso as_list
528  */
529 static inline uint32_t as_list_hashcode(as_list * list)
530 {
531  return as_util_hook(hashcode, 0, list);
532 }
533 
534 /**
535  * Number of elements in the list.
536  *
537  * @param list The list.
538  *
539  * @return The size of the list.
540  * @relatesalso as_list
541  */
542 static inline uint32_t as_list_size(const as_list * list)
543 {
544  return as_util_hook(size, 0, list);
545 }
546 
547 /******************************************************************************
548  * ACCESSOR AND MODIFIER FUNCTIONS
549  *****************************************************************************/
550 
551 /**
552  * Append all elements of list2, in order, to list. No new list object is
553  * created.
554  *
555  * @param list The list to append to.
556  * @param list2 The list from which to append.
557  *
558  * @return 0 on success. Otherwise an error occurred.
559  * @relatesalso as_list
560  */
561 static inline int as_list_concat(as_list * list, const as_list * list2)
562 {
563  return as_util_hook(concat, 1, list, list2);
564 }
565 
566 /**
567  * Delete (and destroy) all elements at and beyond specified index. Capacity is
568  * not reduced.
569  *
570  * @param list The list to trim.
571  * @param index The index from which to trim.
572  *
573  * @return 0 on success. Otherwise an error occurred.
574  * @relatesalso as_list
575  */
576 static inline int as_list_trim(as_list * list, uint32_t index)
577 {
578  return as_util_hook(trim, 1, list, index);
579 }
580 
581 /**
582  * The first element in the list.
583  *
584  * @param list The list to get the head value from.
585  *
586  * @return The first value of the list on success. Otherwise NULL.
587  * @relatesalso as_list
588  */
589 static inline as_val * as_list_head(const as_list * list)
590 {
591  return as_util_hook(head, NULL, list);
592 }
593 
594 /**
595  * All elements after the first element in the list.
596  *
597  * @param list The list to get the tail from.
598  *
599  * @return On success, the tail of the list. Otherwise NULL.
600  * @relatesalso as_list
601  */
602 static inline as_list * as_list_tail(const as_list * list)
603 {
604  return as_util_hook(tail, NULL, list);
605 }
606 
607 /**
608  * Create a new list containing all elements, except the first n elements, of the list.
609  *
610  * @param list The list to drop elements from.
611  * @param n The number of elements to drop.
612  *
613  * @return On success, a new list containing the remaining elements. Otherwise NULL.
614  * @relatesalso as_list
615  */
616 static inline as_list * as_list_drop(const as_list * list, uint32_t n)
617 {
618  return as_util_hook(drop, NULL, list, n);
619 }
620 
621 /**
622  * Creates a new list containing the first n elements of the list.
623  *
624  * @param list The list to drop elements from.
625  * @param n The number of elements to take.
626  *
627  * @return On success, a new list containing the selected elements. Otherwise NULL.
628  * @relatesalso as_list
629  */
630 static inline as_list * as_list_take(const as_list * list, uint32_t n)
631 {
632  return as_util_hook(take, NULL, list, n);
633 }
634 
635 /******************************************************************************
636  * GET FUNCTIONS
637  *****************************************************************************/
638 
639 /**
640  * Get the value at specified index as an as_val.
641  *
642  * @param list The list to get the value from.
643  * @param i The index of the value to get from the list.
644  *
645  * @return On success, the value at the given index. Otherwise NULL.
646  * @relatesalso as_list
647  */
648 static inline as_val * as_list_get(const as_list * list, uint32_t i)
649 {
650  return as_util_hook(get, NULL, list, i);
651 }
652 
653 /**
654  * Get the value at specified index as an int64_t.
655  *
656  * @param list The list to get the value from.
657  * @param i The index of the value to get from the list.
658  *
659  * @return On success, the value at the given index. Otherwise NULL.
660  * @relatesalso as_list
661  */
662 static inline int64_t as_list_get_int64(const as_list * list, uint32_t i)
663 {
664  return as_util_hook(get_int64, 0, list, i);
665 }
666 
667 /**
668  * Get the value at specified index as a double.
669  *
670  * @param list The list to get the value from.
671  * @param i The index of the value to get from the list.
672  *
673  * @return On success, the value at the given index. Otherwise NULL.
674  * @relatesalso as_list
675  */
676 static inline double as_list_get_double(const as_list * list, uint32_t i)
677 {
678  return as_util_hook(get_double, 0.0, list, i);
679 }
680 
681 /**
682  * Get the value at specified index as an NULL terminated string.
683  *
684  * @param list The list to get the value from.
685  * @param i The index of the value to get from the list.
686  *
687  * @return On success, the value at the given index. Otherwise NULL.
688  * @relatesalso as_list
689  */
690 static inline char * as_list_get_str(const as_list * list, uint32_t i)
691 {
692  return as_util_hook(get_str, NULL, list, i);
693 }
694 
695 /**
696  * Get the value at specified index as an as_integer.
697  *
698  * @param list The list to get the value from.
699  * @param i The index of the value to get from the list.
700  *
701  * @return On success, the value at the given index. Otherwise NULL.
702  * @relatesalso as_list
703  */
704 static inline as_integer * as_list_get_integer(const as_list * list, uint32_t i)
705 {
706  return as_integer_fromval(as_list_get(list, i));
707 }
708 
709 /**
710  * Get the value at specified index as an as_double.
711  *
712  * @param list The list to get the value from.
713  * @param i The index of the value to get from the list.
714  *
715  * @return On success, the value at the given index. Otherwise NULL.
716  * @relatesalso as_list
717  */
718 static inline as_double * as_list_get_as_double(const as_list * list, uint32_t i)
719 {
720  return as_double_fromval(as_list_get(list, i));
721 }
722 
723 /**
724  * Get the value at specified index as an as_val.
725  *
726  * @param list The list to get the value from.
727  * @param i The index of the value to get from the list.
728  *
729  * @return On success, the value at the given index. Otherwise NULL.
730  * @relatesalso as_list
731  */
732 static inline as_string * as_list_get_string(const as_list * list, uint32_t i)
733 {
734  return as_string_fromval(as_list_get(list, i));
735 }
736 
737 /**
738  * Get the value at specified index as an as_val.
739  *
740  * @param list The list to get the value from.
741  * @param i The index of the value to get from the list.
742  *
743  * @return On success, the value at the given index. Otherwise NULL.
744  * @relatesalso as_list
745  */
746 static inline as_bytes * as_list_get_bytes(const as_list * list, uint32_t i)
747 {
748  return as_bytes_fromval(as_list_get(list, i));
749 }
750 
751 /**
752  * Get the value at specified index as an as_val.
753  *
754  * @param list The list to get the value from.
755  * @param i The index of the value to get from the list.
756  *
757  * @return On success, the value at the given index. Otherwise NULL.
758  * @relatesalso as_list
759  */
760 static inline as_list * as_list_get_list(const as_list * list, uint32_t i)
761 {
762  as_val * v = as_list_get(list, i);
763  return (as_list *) (v && v->type == AS_LIST ? v : NULL);
764 }
765 
766 /**
767  * Get the value at specified index as an as_val.
768  *
769  * @param list The list to get the value from.
770  * @param i The index of the value to get from the list.
771  *
772  * @return On success, the value at the given index. Otherwise NULL.
773  * @relatesalso as_list
774  */
775 static inline struct as_map_s * as_list_get_map(const as_list * list, uint32_t i)
776 {
777  as_val * v = as_list_get(list, i);
778  return (struct as_map_s *) (v && v->type == AS_MAP ? v : NULL);
779 }
780 
781 /******************************************************************************
782  * SET FUNCTIONS
783  *****************************************************************************/
784 
785 /**
786  * Set the value at specified index as an as_val.
787  *
788  * @param list The list.
789  * @param i The index of the value to set in the list.
790  * @param value The value to set at the given index.
791  *
792  * @return 0 on success. Otherwise an error occurred.
793  * @relatesalso as_list
794  */
795 static inline int as_list_set(as_list * list, uint32_t i, as_val * value)
796 {
797  return as_util_hook(set, 1, list, i, value);
798 }
799 
800 /**
801  * Set an int64_t at specified index as an as_val.
802  *
803  * @param list The list.
804  * @param i The index of the value to set in the list.
805  * @param value The value to set at the given index.
806  *
807  * @return 0 on success. Otherwise an error occurred.
808  * @relatesalso as_list
809  */
810 static inline int as_list_set_int64(as_list * list, uint32_t i, int64_t value)
811 {
812  return as_util_hook(set_int64, 1, list, i, value);
813 }
814 
815 /**
816  * Set a double at specified index as an as_val.
817  *
818  * @param list The list.
819  * @param i The index of the value to set in the list.
820  * @param value The value to set at the given index.
821  *
822  * @return 0 on success. Otherwise an error occurred.
823  * @relatesalso as_list
824  */
825 static inline int as_list_set_double(as_list * list, uint32_t i, double value)
826 {
827  return as_util_hook(set_double, 1, list, i, value);
828 }
829 
830 /**
831  * Set a NULL-terminated string at specified index as an as_val.
832  *
833  * @param list The list.
834  * @param i The index of the value to set in the list.
835  * @param value The value to set at the given index.
836  *
837  * @return 0 on success. Otherwise an error occurred.
838  * @relatesalso as_list
839  */
840 static inline int as_list_set_str(as_list * list, uint32_t i, const char * value)
841 {
842  return as_util_hook(set_str, 1, list, i, value);
843 }
844 
845 /**
846  * Set an as_integer at specified index as an as_val.
847  *
848  * @param list The list.
849  * @param i The index of the value to set in the list.
850  * @param value The value to set at the given index.
851  *
852  * @return 0 on success. Otherwise an error ocucrred.
853  * @relatesalso as_list
854  */
855 static inline int as_list_set_integer(as_list * list, uint32_t i, as_integer * value)
856 {
857  return as_list_set(list, i, (as_val *) value);
858 }
859 
860 /**
861  * Set an as_double at specified index as an as_val.
862  *
863  * @param list The list.
864  * @param i The index of the value to set in the list.
865  * @param value The value to set at the given index.
866  *
867  * @return 0 on success. Otherwise an error ocucrred.
868  * @relatesalso as_list
869  */
870 static inline int as_list_set_as_double(as_list * list, uint32_t i, as_double * value)
871 {
872  return as_list_set(list, i, (as_val *) value);
873 }
874 
875 /**
876  * Set an as_string at specified index as an as_val.
877  *
878  * @param list The list.
879  * @param i The index of the value to set in the list.
880  * @param value The value to set at the given index.
881  *
882  * @return 0 on success. Otherwise an error occurred.
883  * @relatesalso as_list
884  */
885 static inline int as_list_set_string(as_list * list, uint32_t i, as_string * value)
886 {
887  return as_list_set(list, i, (as_val *) value);
888 }
889 
890 /**
891  * Set an as_bytes at specified index as an as_val.
892  *
893  * @param list The list.
894  * @param i The index of the value to set in the list.
895  * @param value The value to set at the given index.
896  *
897  * @return 0 on success. Otherwise an error occurred.
898  * @relatesalso as_list
899  */
900 static inline int as_list_set_bytes(as_list * list, uint32_t i, as_bytes * value)
901 {
902  return as_list_set(list, i, (as_val *) value);
903 }
904 
905 /**
906  * Set an as_list at specified index as an as_val.
907  *
908  * @param list The list.
909  * @param i The index of the value to set in the list.
910  * @param value The value to set at the given index.
911  *
912  * @return 0 on success. Otherwise an error occurred.
913  * @relatesalso as_list
914  */
915 static inline int as_list_set_list(as_list * list, uint32_t i, as_list * value)
916 {
917  return as_list_set(list, i, (as_val *) value);
918 }
919 
920 /**
921  * Set an as_map at specified index as an as_val.
922  *
923  * @param list The list.
924  * @param i The index of the value to set in the list.
925  * @param value The value to set at the given index.
926  *
927  * @return 0 on success. Otherwise an error occurred.
928  * @relatesalso as_list
929  */
930 static inline int as_list_set_map(as_list * list, uint32_t i, struct as_map_s * value)
931 {
932  return as_list_set(list, i, (as_val *) value);
933 }
934 
935 /******************************************************************************
936  * INSERT FUNCTIONS
937  *****************************************************************************/
938 
939 /**
940  * Insert a value at the specified index of the list.
941  *
942  * Any elements at and beyond specified index will be shifted so their indexes
943  * increase by 1. It's ok to insert beyond the current end of the list.
944  *
945  * @param list The list.
946  * @param i The index at which to insert.
947  * @param value The value to insert at the given index.
948  *
949  * @return 0 on success. Otherwise an error occurred.
950  * @relatesalso as_list
951  */
952 static inline int as_list_insert(as_list * list, uint32_t i, as_val * value)
953 {
954  return as_util_hook(insert, 1, list, i, value);
955 }
956 
957 /**
958  * Insert an int64_t at specified index as an as_val.
959  *
960  * @param list The list.
961  * @param i The index at which to insert.
962  * @param value The value to insert at the given index.
963  *
964  * @return 0 on success. Otherwise an error occurred.
965  * @relatesalso as_list
966  */
967 static inline int as_list_insert_int64(as_list * list, uint32_t i, int64_t value)
968 {
969  return as_util_hook(insert_int64, 1, list, i, value);
970 }
971 
972 /**
973  * Insert a double at specified index as an as_val.
974  *
975  * @param list The list.
976  * @param i The index at which to insert.
977  * @param value The value to insert at the given index.
978  *
979  * @return 0 on success. Otherwise an error occurred.
980  * @relatesalso as_list
981  */
982 static inline int as_list_insert_double(as_list * list, uint32_t i, double value)
983 {
984  return as_util_hook(insert_double, 1, list, i, value);
985 }
986 
987 /**
988  * Insert a NULL-terminated string at specified index as an as_val.
989  *
990  * @param list The list.
991  * @param i The index at which to insert.
992  * @param value The value to insert at the given index.
993  *
994  * @return 0 on success. Otherwise an error occurred.
995  * @relatesalso as_list
996  */
997 static inline int as_list_insert_str(as_list * list, uint32_t i, const char * value)
998 {
999  return as_util_hook(insert_str, 1, list, i, value);
1000 }
1001 
1002 /**
1003  * Insert an as_integer at specified index as an as_val.
1004  *
1005  * @param list The list.
1006  * @param i The index at which to insert.
1007  * @param value The value to insert at the given index.
1008  *
1009  * @return 0 on success. Otherwise an error ocucrred.
1010  * @relatesalso as_list
1011  */
1012 static inline int as_list_insert_integer(as_list * list, uint32_t i, as_integer * value)
1013 {
1014  return as_list_insert(list, i, (as_val *) value);
1015 }
1016 
1017 /**
1018  * Insert an as_double at specified index as an as_val.
1019  *
1020  * @param list The list.
1021  * @param i The index at which to insert.
1022  * @param value The value to insert at the given index.
1023  *
1024  * @return 0 on success. Otherwise an error ocucrred.
1025  * @relatesalso as_list
1026  */
1027 static inline int as_list_insert_as_double(as_list * list, uint32_t i, as_double * value)
1028 {
1029  return as_list_insert(list, i, (as_val *) value);
1030 }
1031 
1032 /**
1033  * Insert an as_string at specified index as an as_val.
1034  *
1035  * @param list The list.
1036  * @param i The index at which to insert.
1037  * @param value The value to insert at the given index.
1038  *
1039  * @return 0 on success. Otherwise an error occurred.
1040  * @relatesalso as_list
1041  */
1042 static inline int as_list_insert_string(as_list * list, uint32_t i, as_string * value)
1043 {
1044  return as_list_insert(list, i, (as_val *) value);
1045 }
1046 
1047 /**
1048  * Insert an as_bytes at specified index as an as_val.
1049  *
1050  * @param list The list.
1051  * @param i The index at which to insert.
1052  * @param value The value to insert at the given index.
1053  *
1054  * @return 0 on success. Otherwise an error occurred.
1055  * @relatesalso as_list
1056  */
1057 static inline int as_list_insert_bytes(as_list * list, uint32_t i, as_bytes * value)
1058 {
1059  return as_list_insert(list, i, (as_val *) value);
1060 }
1061 
1062 /**
1063  * Insert an as_list at specified index as an as_val.
1064  *
1065  * @param list The list.
1066  * @param i The index at which to insert.
1067  * @param value The value to insert at the given index.
1068  *
1069  * @return 0 on success. Otherwise an error occurred.
1070  * @relatesalso as_list
1071  */
1072 static inline int as_list_insert_list(as_list * list, uint32_t i, as_list * value)
1073 {
1074  return as_list_insert(list, i, (as_val *) value);
1075 }
1076 
1077 /**
1078  * Insert an as_map at specified index as an as_val.
1079  *
1080  * @param list The list.
1081  * @param i The index at which to insert.
1082  * @param value The value to insert at the given index.
1083  *
1084  * @return 0 on success. Otherwise an error occurred.
1085  * @relatesalso as_list
1086  */
1087 static inline int as_list_insert_map(as_list * list, uint32_t i, struct as_map_s * value)
1088 {
1089  return as_list_insert(list, i, (as_val *) value);
1090 }
1091 
1092 /******************************************************************************
1093  * APPEND FUNCTIONS
1094  *****************************************************************************/
1095 
1096 /**
1097  * Append a value to the list.
1098  *
1099  * @param list The list.
1100  * @param value The value to append to the list.
1101  *
1102  * @return 0 on success. Otherwise an error occurred.
1103  * @relatesalso as_list
1104  */
1105 static inline int as_list_append(as_list * list, as_val * value)
1106 {
1107  return as_util_hook(append, 1, list, value);
1108 }
1109 
1110 /**
1111  * Append an int64_t to the list.
1112  *
1113  * @param list The list.
1114  * @param value The value to append to the list.
1115  *
1116  * @return 0 on success. Otherwise an error occurred.
1117  * @relatesalso as_list
1118  */
1119 static inline int as_list_append_int64(as_list * list, int64_t value)
1120 {
1121  return as_util_hook(append_int64, 1, list, value);
1122 }
1123 
1124 /**
1125  * Append a double to the list.
1126  *
1127  * @param list The list.
1128  * @param value The value to append to the list.
1129  *
1130  * @return 0 on success. Otherwise an error occurred.
1131  * @relatesalso as_list
1132  */
1133 static inline int as_list_append_double(as_list * list, double value)
1134 {
1135  return as_util_hook(append_double, 1, list, value);
1136 }
1137 
1138 /**
1139  * Append a NULL-terminated string to the list.
1140  *
1141  * @param list The list.
1142  * @param value The value to append to the list.
1143  *
1144  * @return 0 on success. Otherwise an error occurred.
1145  * @relatesalso as_list
1146  */
1147 static inline int as_list_append_str(as_list * list, const char * value)
1148 {
1149  return as_util_hook(append_str, 1, list, value);
1150 }
1151 
1152 /**
1153  * Append an as_integer to the list.
1154  *
1155  * @param list The list.
1156  * @param value The value to append to the list.
1157  *
1158  * @return 0 on success. Otherwise an error occurred.
1159  * @relatesalso as_list
1160  */
1161 static inline int as_list_append_integer(as_list * list, as_integer * value)
1162 {
1163  return as_list_append(list, (as_val *) value);
1164 }
1165 
1166 /**
1167  * Append an as_double to the list.
1168  *
1169  * @param list The list.
1170  * @param value The value to append to the list.
1171  *
1172  * @return 0 on success. Otherwise an error occurred.
1173  * @relatesalso as_list
1174  */
1175 static inline int as_list_append_as_double(as_list * list, as_double * value)
1176 {
1177  return as_list_append(list, (as_val *) value);
1178 }
1179 
1180 /**
1181  * Append an as_string to the list.
1182  *
1183  * @param list The list.
1184  * @param value The value to append to the list.
1185  *
1186  * @return 0 on success. Otherwise an error occurred.
1187  * @relatesalso as_list
1188  */
1189 static inline int as_list_append_string(as_list * list, as_string * value)
1190 {
1191  return as_list_append(list, (as_val *) value);
1192 }
1193 
1194 /**
1195  * Append an as_bytes to the list.
1196  *
1197  * @param list The list.
1198  * @param value The value to append to the list.
1199  *
1200  * @return 0 on success. Otherwise an error occurred.
1201  * @relatesalso as_list
1202  */
1203 static inline int as_list_append_bytes(as_list * list, as_bytes * value)
1204 {
1205  return as_list_append(list, (as_val *) value);
1206 }
1207 
1208 /**
1209  * Append an as_list to the list.
1210  *
1211  * @param list The list.
1212  * @param value The value to append to the list.
1213  *
1214  * @return 0 on success. Otherwise an error occurred.
1215  * @relatesalso as_list
1216  */
1217 static inline int as_list_append_list(as_list * list, as_list * value)
1218 {
1219  return as_list_append(list, (as_val *) value);
1220 }
1221 
1222 /**
1223  * Append an as_map to the list.
1224  *
1225  * @param list The list.
1226  * @param value The value to append to the list.
1227  *
1228  * @return 0 on success. Otherwise an error occurred.
1229  * @relatesalso as_list
1230  */
1231 static inline int as_list_append_map(as_list * list, struct as_map_s * value)
1232 {
1233  return as_list_append(list, (as_val *) value);
1234 }
1235 
1236 /******************************************************************************
1237  * PREPEND FUNCTIONS
1238  *****************************************************************************/
1239 
1240 /**
1241  * Prepend a value to the list.
1242  *
1243  * @param list The list.
1244  * @param value The value to prepend to the list.
1245  *
1246  * @return 0 on success. Otherwise an error occurred.
1247  * @relatesalso as_list
1248  */
1249 static inline int as_list_prepend(as_list * list, as_val * value)
1250 {
1251  return as_util_hook(prepend, 1, list, value);
1252 }
1253 
1254 /**
1255  * Prepend an int64_t value to the list.
1256  *
1257  * @param list The list.
1258  * @param value The value to prepend to the list.
1259  *
1260  * @return 0 on success. Otherwise an error occurred.
1261  * @relatesalso as_list
1262  */
1263 static inline int as_list_prepend_int64(as_list * list, int64_t value)
1264 {
1265  return as_util_hook(prepend_int64, 1, list, value);
1266 }
1267 
1268 /**
1269  * Prepend a double value to the list.
1270  *
1271  * @param list The list.
1272  * @param value The value to prepend to the list.
1273  *
1274  * @return 0 on success. Otherwise an error occurred.
1275  * @relatesalso as_list
1276  */
1277 static inline int as_list_prepend_double(as_list * list, double value)
1278 {
1279  return as_util_hook(prepend_double, 1, list, value);
1280 }
1281 
1282 /**
1283  * Prepend a NULL-terminated string to the list.
1284  *
1285  * @param list The list.
1286  * @param value The value to prepend to the list.
1287  *
1288  * @return 0 on success. Otherwise an error occurred.
1289  * @relatesalso as_list
1290  */
1291 static inline int as_list_prepend_str(as_list * list, const char * value)
1292 {
1293  return as_util_hook(prepend_str, 1, list, value);
1294 }
1295 
1296 /**
1297  * Prepend an as_integer to the list.
1298  *
1299  * @param list The list.
1300  * @param value The value to prepend to the list.
1301  *
1302  * @return 0 on success. Otherwise an error occurred.
1303  * @relatesalso as_list
1304  */
1305 static inline int as_list_prepend_integer(as_list * list, as_integer * value)
1306 {
1307  return as_list_prepend(list, (as_val *) value);
1308 }
1309 
1310 /**
1311  * Prepend an as_double to the list.
1312  *
1313  * @param list The list.
1314  * @param value The value to prepend to the list.
1315  *
1316  * @return 0 on success. Otherwise an error occurred.
1317  * @relatesalso as_list
1318  */
1319 static inline int as_list_prepend_as_double(as_list * list, as_double * value)
1320 {
1321  return as_list_prepend(list, (as_val *) value);
1322 }
1323 
1324 /**
1325  * Prepend an as_string to the list.
1326  *
1327  * @param list The list.
1328  * @param value The value to prepend to the list.
1329  *
1330  * @return 0 on success. Otherwise an error occurred.
1331  * @relatesalso as_list
1332  */
1333 static inline int as_list_prepend_string(as_list * list, as_string * value)
1334 {
1335  return as_list_prepend(list, (as_val *) value);
1336 }
1337 
1338 /**
1339  * Prepend an as_bytes to the list.
1340  *
1341  * @param list The list.
1342  * @param value The value to prepend to the list.
1343  *
1344  * @return 0 on success. Otherwise an error occurred.
1345  * @relatesalso as_list
1346  */
1347 static inline int as_list_prepend_bytes(as_list * list, as_bytes * value)
1348 {
1349  return as_list_prepend(list, (as_val *) value);
1350 }
1351 
1352 /**
1353  * Prepend an as_list to the list.
1354  *
1355  * @param list The list.
1356  * @param value The value to prepend to the list.
1357  *
1358  * @return 0 on success. Otherwise an error occurred.
1359  * @relatesalso as_list
1360  */
1361 static inline int as_list_prepend_list(as_list * list, as_list * value)
1362 {
1363  return as_list_prepend(list, (as_val *) value);
1364 }
1365 
1366 /**
1367  * Prepend an as_map to the list.
1368  *
1369  * @param list The list.
1370  * @param value The value to prepend to the list.
1371  *
1372  * @return 0 on success. Otherwise an error occurred.
1373  * @relatesalso as_list
1374  */
1375 static inline int as_list_prepend_map(as_list * list, struct as_map_s * value)
1376 {
1377  return as_list_prepend(list, (as_val *) value);
1378 }
1379 
1380 /******************************************************************************
1381  * REMOVE FUNCTION
1382  *****************************************************************************/
1383 
1384 /**
1385  * Remove element at specified index.
1386  *
1387  * Any elements beyond specified index will be shifted so their indexes
1388  * decrease by 1. The element at specified index will be destroyed.
1389  *
1390  * @param list The list.
1391  * @param index The index of the element to remove.
1392  *
1393  * @return 0 on success. Otherwise an error occurred.
1394  * @relatesalso as_list
1395  */
1396 static inline int as_list_remove(as_list * list, uint32_t index)
1397 {
1398  return as_util_hook(remove, 1, list, index);
1399 }
1400 
1401 /******************************************************************************
1402  * ITERATION FUNCTIONS
1403  *****************************************************************************/
1404 
1405 /**
1406  * Call the callback function for each element in the list..
1407  *
1408  * @param list The list to iterate over.
1409  * @param callback The callback function call for each element.
1410  * @param udata User-data to send to the callback.
1411  *
1412  * @return true if iteration completes fully. false if iteration was aborted.
1413  *
1414  * @relatesalso as_list
1415  */
1416 static inline bool as_list_foreach(const as_list * list, as_list_foreach_callback callback, void * udata)
1417 {
1418  return as_util_hook(foreach, false, list, callback, udata);
1419 }
1420 
1421 /**
1422  * Creates and initializes a new heap allocated iterator over the given list.
1423  *
1424  * @param list The list to iterate.
1425  *
1426  * @return On success, a new as_iterator. Otherwise NULL.
1427  * @relatesalso as_list
1428  */
1429 static inline union as_list_iterator_u * as_list_iterator_new(const as_list * list)
1430 {
1431  return as_util_hook(iterator_new, NULL, list);
1432 }
1433 
1434 
1435 /**
1436  * Initializes a stack allocated iterator over the given list.
1437  *
1438  * @param list The list to iterate.
1439  * @param it The iterator to initialize.
1440  *
1441  * @return On success, the initializes as_iterator. Otherwise NULL.
1442  * @relatesalso as_list
1443  */
1444 static inline union as_list_iterator_u * as_list_iterator_init(union as_list_iterator_u * it, const as_list * list)
1445 {
1446  return as_util_hook(iterator_init, NULL, list, it);
1447 }
1448 
1449 /******************************************************************************
1450  * CONVERSION FUNCTIONS
1451  *****************************************************************************/
1452 
1453 /**
1454  * Convert to an as_val.
1455  * @relatesalso as_list
1456  */
1457 static inline as_val * as_list_toval(as_list * list)
1458 {
1459  return (as_val *) list;
1460 }
1461 
1462 /**
1463  * Convert from an as_val.
1464  * @relatesalso as_list
1465  */
1466 static inline as_list * as_list_fromval(as_val * v)
1467 {
1468  return as_util_fromval(v, AS_LIST, as_list);
1469 }
1470 
1471 /******************************************************************************
1472  * as_val FUNCTIONS
1473  *****************************************************************************/
1474 
1475 /**
1476  * @private
1477  * Internal helper function for destroying an as_val.
1478  */
1479 void as_list_val_destroy(as_val * v);
1480 
1481 /**
1482  * @private
1483  * Internal helper function for getting the hashcode of an as_val.
1484  */
1485 uint32_t as_list_val_hashcode(const as_val * v);
1486 
1487 /**
1488  * @private
1489  * Internal helper function for getting the string representation of an as_val.
1490  */
1491 char * as_list_val_tostring(const as_val * v);
1492 
1493 #ifdef __cplusplus
1494 } // end extern "C"
1495 #endif