OpenFAST
Wind turbine multiphysics simulator
simclist.h
1 /*
2  * Copyright (c) 2007,2008 Mij <mij@bitchx.it>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 
18 /*
19  * SimCList library. See http://mij.oltrelinux.com/devel/simclist
20  */
21 
22 
23 #ifndef SIMCLIST_H
24 #define SIMCLIST_H
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /* work around lack of inttypes.h support in broken Microsoft Visual Studio compilers */
31 #if defined(_MSC_VER)
32 #include <basetsd.h>
33 typedef UINT8 uint8_t;
34 typedef UINT16 uint16_t;
35 typedef ULONG32 uint32_t;
36 typedef UINT64 uint64_t;
37 typedef INT8 int8_t;
38 typedef INT16 int16_t;
39 typedef LONG32 int32_t;
40 typedef INT64 int64_t;
41 #else
42 #include <inttypes.h> /* (u)int*_t */
43 #endif
44 #include <errno.h>
45 #include <sys/types.h>
46 
47 /* Be friend of both C90 and C99 compilers */
48 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
49  /* "inline" and "restrict" are keywords */
50 #else
51 # define inline /* inline */
52 # define restrict /* restrict */
53 #endif
54 
55 
61 typedef int32_t list_hash_t;
62 
63 #ifdef SIMCLIST_DUMPRESTORE
64 typedef struct {
65  uint16_t version; /* dump version */
66  int64_t timestamp; /* when the list has been dumped, microseconds from UNIX epoch */
67  uint32_t list_size;
68  uint32_t list_numels;
69  list_hash_t list_hash; /* hash of the list when dumped, or 0 if invalid */
70  uint32_t dumpsize;
71  int consistent; /* 1 if the dump is verified complete/consistent; 0 otherwise */
72 } list_dump_info_t;
73 #endif
74 
84 typedef int (*element_comparator)(const void *a, const void *b);
85 
97 typedef int (*element_seeker)(const void *el, const void *indicator);
98 
108 typedef size_t (*element_meter)(const void *el);
109 
119 typedef list_hash_t (*element_hash_computer)(const void *el);
120 
139 typedef void *(*element_serializer)(const void *restrict el, uint32_t *restrict serializ_len);
140 
156 typedef void *(*element_unserializer)(const void *restrict data, uint32_t *restrict data_len);
157 
158 /* [private-use] list entry -- olds actual user datum */
159 struct list_entry_s {
160  void *data;
161 
162  /* doubly-linked list service references */
163  struct list_entry_s *next;
164  struct list_entry_s *prev;
165 };
166 
167 /* [private-use] list attributes */
169  /* user-set routine for comparing list elements */
170  element_comparator comparator;
171  /* user-set routing for seeking elements */
172  element_seeker seeker;
173  /* user-set routine for determining the length of an element */
174  element_meter meter;
175  int copy_data;
176  /* user-set routine for computing the hash of an element */
177  element_hash_computer hasher;
178  /* user-set routine for serializing an element */
179  element_serializer serializer;
180  /* user-set routine for unserializing an element */
181  element_unserializer unserializer;
182 };
183 
185 typedef struct {
186  struct list_entry_s *head_sentinel;
187  struct list_entry_s *tail_sentinel;
188  struct list_entry_s *mid;
189 
190  unsigned int numels;
191 
192  /* array of spare elements */
193  struct list_entry_s **spareels;
194  unsigned int spareelsnum;
195 
196 #ifdef SIMCLIST_WITH_THREADS
197  /* how many threads are currently running */
198  unsigned int threadcount;
199 #endif
200 
201  /* service variables for list iteration */
202  int iter_active;
203  unsigned int iter_pos;
204  struct list_entry_s *iter_curentry;
205 
206  /* list attributes */
207  struct list_attributes_s attrs;
208 } list_t;
209 
216 int list_init(list_t *restrict l);
217 
227 void list_destroy(list_t *restrict l);
228 
241 int list_attributes_comparator(list_t *restrict l, element_comparator comparator_fun);
242 
255 int list_attributes_seeker(list_t *restrict l, element_seeker seeker_fun);
256 
287 int list_attributes_copy(list_t *restrict l, element_meter metric_fun, int copy_data);
288 
307 int list_attributes_hash_computer(list_t *restrict l, element_hash_computer hash_computer_fun);
308 
328 int list_attributes_serializer(list_t *restrict l, element_serializer serializer_fun);
329 
350 int list_attributes_unserializer(list_t *restrict l, element_unserializer unserializer_fun);
351 
362 int list_append(list_t *restrict l, const void *data);
363 
374 int list_prepend(list_t *restrict l, const void *restrict data);
375 
384 void *list_fetch(list_t *restrict l);
385 
393 void *list_get_at(const list_t *restrict l, unsigned int pos);
394 
407 void *list_get_max(const list_t *restrict l);
408 
421 void *list_get_min(const list_t *restrict l);
422 
430 void *list_extract_at(list_t *restrict l, unsigned int pos);
431 
440 int list_insert_at(list_t *restrict l, const void *data, unsigned int pos);
441 
457 int list_delete(list_t *restrict l, const void *data);
458 
466 int list_delete_at(list_t *restrict l, unsigned int pos);
467 
476 int list_delete_range(list_t *restrict l, unsigned int posstart, unsigned int posend);
477 
489 int list_clear(list_t *restrict l);
490 
497 unsigned int list_size(const list_t *restrict l);
498 
507 int list_empty(const list_t *restrict l);
508 
526 int list_locate(const list_t *restrict l, const void *data);
527 
541 void *list_seek(list_t *restrict l, const void *indicator);
542 
562 int list_contains(const list_t *restrict l, const void *data);
563 
581 int list_concat(const list_t *l1, const list_t *l2, list_t *restrict dest);
582 
598 int list_sort(list_t *restrict l, int versus);
599 
610 int list_iterator_start(list_t *restrict l);
611 
618 void *list_iterator_next(list_t *restrict l);
619 
626 int list_iterator_hasnext(const list_t *restrict l);
627 
634 int list_iterator_stop(list_t *restrict l);
635 
644 int list_hash(const list_t *restrict l, list_hash_t *restrict hash);
645 
646 #ifdef SIMCLIST_DUMPRESTORE
647 
662 int list_dump_getinfo_filedescriptor(int fd, list_dump_info_t *restrict info);
663 
677 int list_dump_getinfo_file(const char *restrict filename, list_dump_info_t *restrict info);
678 
713 int list_dump_filedescriptor(const list_t *restrict l, int fd, size_t *restrict len);
714 
736 int list_dump_file(const list_t *restrict l, const char *restrict filename, size_t *restrict len);
737 
756 int list_restore_filedescriptor(list_t *restrict l, int fd, size_t *restrict len);
757 
774 int list_restore_file(list_t *restrict l, const char *restrict filename, size_t *len);
775 #endif
776 
777 /* ready-made comparators, meters and hash computers */
778  /* comparator functions */
783 int list_comparator_int8_t(const void *a, const void *b);
784 
789 int list_comparator_int16_t(const void *a, const void *b);
790 
795 int list_comparator_int32_t(const void *a, const void *b);
796 
801 int list_comparator_int64_t(const void *a, const void *b);
802 
807 int list_comparator_uint8_t(const void *a, const void *b);
808 
813 int list_comparator_uint16_t(const void *a, const void *b);
814 
819 int list_comparator_uint32_t(const void *a, const void *b);
820 
825 int list_comparator_uint64_t(const void *a, const void *b);
826 
831 int list_comparator_float(const void *a, const void *b);
832 
837 int list_comparator_double(const void *a, const void *b);
838 
843 int list_comparator_string(const void *a, const void *b);
844 
845  /* metric functions */
850 size_t list_meter_int8_t(const void *el);
851 
856 size_t list_meter_int16_t(const void *el);
857 
862 size_t list_meter_int32_t(const void *el);
863 
868 size_t list_meter_int64_t(const void *el);
869 
874 size_t list_meter_uint8_t(const void *el);
875 
880 size_t list_meter_uint16_t(const void *el);
881 
886 size_t list_meter_uint32_t(const void *el);
887 
892 size_t list_meter_uint64_t(const void *el);
893 
898 size_t list_meter_float(const void *el);
899 
904 size_t list_meter_double(const void *el);
905 
910 size_t list_meter_string(const void *el);
911 
912  /* hash functions */
917 list_hash_t list_hashcomputer_int8_t(const void *el);
918 
923 list_hash_t list_hashcomputer_int16_t(const void *el);
924 
929 list_hash_t list_hashcomputer_int32_t(const void *el);
930 
935 list_hash_t list_hashcomputer_int64_t(const void *el);
936 
941 list_hash_t list_hashcomputer_uint8_t(const void *el);
942 
947 list_hash_t list_hashcomputer_uint16_t(const void *el);
948 
953 list_hash_t list_hashcomputer_uint32_t(const void *el);
954 
959 list_hash_t list_hashcomputer_uint64_t(const void *el);
960 
965 list_hash_t list_hashcomputer_float(const void *el);
966 
971 list_hash_t list_hashcomputer_double(const void *el);
972 
977 list_hash_t list_hashcomputer_string(const void *el);
978 
979 #ifdef __cplusplus
980 }
981 #endif
982 
983 #endif
984 
Definition: simclist.h:168
list object
Definition: simclist.h:185
Definition: simclist.h:159