Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
struct.h@0:1a89e28dea91, 2012-05-30 (annotated)
- Committer:
- yusufx
- Date:
- Wed May 30 21:13:01 2012 +0000
- Revision:
- 0:1a89e28dea91
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| yusufx | 0:1a89e28dea91 | 1 | /* struct.h |
| yusufx | 0:1a89e28dea91 | 2 | * |
| yusufx | 0:1a89e28dea91 | 3 | * APIs for array, byte_array, [f|l]ifo and map |
| yusufx | 0:1a89e28dea91 | 4 | */ |
| yusufx | 0:1a89e28dea91 | 5 | |
| yusufx | 0:1a89e28dea91 | 6 | #ifndef STRUCT_H |
| yusufx | 0:1a89e28dea91 | 7 | #define STRUCT_H |
| yusufx | 0:1a89e28dea91 | 8 | |
| yusufx | 0:1a89e28dea91 | 9 | |
| yusufx | 0:1a89e28dea91 | 10 | #include <inttypes.h> |
| yusufx | 0:1a89e28dea91 | 11 | #include <stdbool.h> |
| yusufx | 0:1a89e28dea91 | 12 | #include <stdlib.h> |
| yusufx | 0:1a89e28dea91 | 13 | |
| yusufx | 0:1a89e28dea91 | 14 | |
| yusufx | 0:1a89e28dea91 | 15 | #define ERROR_INDEX "index out of bounds" |
| yusufx | 0:1a89e28dea91 | 16 | #define ERROR_NULL "null pointer" |
| yusufx | 0:1a89e28dea91 | 17 | |
| yusufx | 0:1a89e28dea91 | 18 | |
| yusufx | 0:1a89e28dea91 | 19 | // array /////////////////////////////////////////////////////////////////// |
| yusufx | 0:1a89e28dea91 | 20 | |
| yusufx | 0:1a89e28dea91 | 21 | struct array { |
| yusufx | 0:1a89e28dea91 | 22 | void **data, **current; |
| yusufx | 0:1a89e28dea91 | 23 | uint32_t length; |
| yusufx | 0:1a89e28dea91 | 24 | }; |
| yusufx | 0:1a89e28dea91 | 25 | |
| yusufx | 0:1a89e28dea91 | 26 | struct array *array_new(); |
| yusufx | 0:1a89e28dea91 | 27 | void array_del(struct array *a); |
| yusufx | 0:1a89e28dea91 | 28 | struct array *array_new_size(uint32_t size); |
| yusufx | 0:1a89e28dea91 | 29 | void array_resize(struct array *a, uint32_t length); |
| yusufx | 0:1a89e28dea91 | 30 | uint32_t array_add(struct array *a , void *datum); |
| yusufx | 0:1a89e28dea91 | 31 | void array_insert(struct array *a, uint32_t index, void *datam); |
| yusufx | 0:1a89e28dea91 | 32 | void* array_get(const struct array *a, uint32_t index); |
| yusufx | 0:1a89e28dea91 | 33 | void array_set(struct array *a, uint32_t index, void *datum); |
| yusufx | 0:1a89e28dea91 | 34 | void array_remove(struct array *a, uint32_t start, int32_t length); |
| yusufx | 0:1a89e28dea91 | 35 | struct array *array_part(struct array *within, uint32_t start, uint32_t length); |
| yusufx | 0:1a89e28dea91 | 36 | void array_append(struct array *a, const struct array* b); |
| yusufx | 0:1a89e28dea91 | 37 | |
| yusufx | 0:1a89e28dea91 | 38 | // byte_array /////////////////////////////////////////////////////////////// |
| yusufx | 0:1a89e28dea91 | 39 | |
| yusufx | 0:1a89e28dea91 | 40 | struct byte_array { |
| yusufx | 0:1a89e28dea91 | 41 | uint8_t *data, *current; |
| yusufx | 0:1a89e28dea91 | 42 | uint32_t length; |
| yusufx | 0:1a89e28dea91 | 43 | }; |
| yusufx | 0:1a89e28dea91 | 44 | |
| yusufx | 0:1a89e28dea91 | 45 | struct byte_array *byte_array_new(); |
| yusufx | 0:1a89e28dea91 | 46 | struct byte_array *byte_array_new_size(uint32_t size); |
| yusufx | 0:1a89e28dea91 | 47 | void byte_array_append(struct byte_array *a, const struct byte_array* b); |
| yusufx | 0:1a89e28dea91 | 48 | struct byte_array *byte_array_from_string(const char* str); |
| yusufx | 0:1a89e28dea91 | 49 | char* byte_array_to_string(const struct byte_array* ba); |
| yusufx | 0:1a89e28dea91 | 50 | void byte_array_del(struct byte_array* ba); |
| yusufx | 0:1a89e28dea91 | 51 | struct byte_array *byte_array_copy(const struct byte_array* original); |
| yusufx | 0:1a89e28dea91 | 52 | struct byte_array *byte_array_add_byte(struct byte_array *a, uint8_t b); |
| yusufx | 0:1a89e28dea91 | 53 | void byte_array_reset(struct byte_array* ba); |
| yusufx | 0:1a89e28dea91 | 54 | void byte_array_resize(struct byte_array* ba, uint32_t size); |
| yusufx | 0:1a89e28dea91 | 55 | bool byte_array_equals(const struct byte_array *a, const struct byte_array* b); |
| yusufx | 0:1a89e28dea91 | 56 | struct byte_array *byte_array_concatenate(int n, const struct byte_array* ba, ...); |
| yusufx | 0:1a89e28dea91 | 57 | void byte_array_print(const char* text, const struct byte_array* ba); |
| yusufx | 0:1a89e28dea91 | 58 | int32_t byte_array_find(struct byte_array *within, struct byte_array *sought, uint32_t start); |
| yusufx | 0:1a89e28dea91 | 59 | struct byte_array *byte_array_part(struct byte_array *within, uint32_t start, uint32_t length); |
| yusufx | 0:1a89e28dea91 | 60 | void byte_array_remove(struct byte_array *within, uint32_t start, int32_t length); |
| yusufx | 0:1a89e28dea91 | 61 | struct byte_array *byte_array_replace(struct byte_array *within, struct byte_array *replacement, uint32_t start, int32_t length); |
| yusufx | 0:1a89e28dea91 | 62 | |
| yusufx | 0:1a89e28dea91 | 63 | // stack //////////////////////////////////////////////////////////////////// |
| yusufx | 0:1a89e28dea91 | 64 | |
| yusufx | 0:1a89e28dea91 | 65 | struct stack_node { |
| yusufx | 0:1a89e28dea91 | 66 | void* data; |
| yusufx | 0:1a89e28dea91 | 67 | struct stack_node* next; |
| yusufx | 0:1a89e28dea91 | 68 | }; |
| yusufx | 0:1a89e28dea91 | 69 | |
| yusufx | 0:1a89e28dea91 | 70 | struct stack { |
| yusufx | 0:1a89e28dea91 | 71 | struct stack_node* head; |
| yusufx | 0:1a89e28dea91 | 72 | struct stack_node* tail; |
| yusufx | 0:1a89e28dea91 | 73 | }; |
| yusufx | 0:1a89e28dea91 | 74 | |
| yusufx | 0:1a89e28dea91 | 75 | struct stack* stack_new(); |
| yusufx | 0:1a89e28dea91 | 76 | struct stack_node* stack_node_new(); |
| yusufx | 0:1a89e28dea91 | 77 | void fifo_push(struct stack* fifo, void* data); |
| yusufx | 0:1a89e28dea91 | 78 | void stack_push(struct stack* stack, void* data); |
| yusufx | 0:1a89e28dea91 | 79 | void* stack_pop(struct stack* stack); |
| yusufx | 0:1a89e28dea91 | 80 | void* stack_peek(const struct stack* stack, uint8_t index); |
| yusufx | 0:1a89e28dea91 | 81 | bool stack_empty(const struct stack* stack); |
| yusufx | 0:1a89e28dea91 | 82 | |
| yusufx | 0:1a89e28dea91 | 83 | // map ///////////////////////////////////////////////////////////////////// |
| yusufx | 0:1a89e28dea91 | 84 | |
| yusufx | 0:1a89e28dea91 | 85 | struct hash_node { |
| yusufx | 0:1a89e28dea91 | 86 | struct byte_array *key; |
| yusufx | 0:1a89e28dea91 | 87 | void *data; |
| yusufx | 0:1a89e28dea91 | 88 | struct hash_node *next; |
| yusufx | 0:1a89e28dea91 | 89 | }; |
| yusufx | 0:1a89e28dea91 | 90 | |
| yusufx | 0:1a89e28dea91 | 91 | struct map { |
| yusufx | 0:1a89e28dea91 | 92 | size_t size; |
| yusufx | 0:1a89e28dea91 | 93 | struct hash_node **nodes; |
| yusufx | 0:1a89e28dea91 | 94 | size_t (*hash_func)(const struct byte_array*); |
| yusufx | 0:1a89e28dea91 | 95 | }; |
| yusufx | 0:1a89e28dea91 | 96 | |
| yusufx | 0:1a89e28dea91 | 97 | struct map* map_new(); |
| yusufx | 0:1a89e28dea91 | 98 | void map_del(struct map* map); |
| yusufx | 0:1a89e28dea91 | 99 | int map_insert(struct map* map, const struct byte_array *key, void *data); |
| yusufx | 0:1a89e28dea91 | 100 | int map_remove(struct map* map, const struct byte_array *key); |
| yusufx | 0:1a89e28dea91 | 101 | void *map_get(const struct map* map, const struct byte_array *key); |
| yusufx | 0:1a89e28dea91 | 102 | bool map_has(const struct map* map, const struct byte_array *key); |
| yusufx | 0:1a89e28dea91 | 103 | int map_resize(struct map* map, size_t size); |
| yusufx | 0:1a89e28dea91 | 104 | struct array* map_keys(const struct map* m); |
| yusufx | 0:1a89e28dea91 | 105 | struct array* map_values(const struct map* m); |
| yusufx | 0:1a89e28dea91 | 106 | void map_update(struct map *a, const struct map *b); |
| yusufx | 0:1a89e28dea91 | 107 | |
| yusufx | 0:1a89e28dea91 | 108 | #endif // STRUCT_H |