1 | #ifndef XC_VECTOR_H_0957AC4E1A44E838C7B8DBECFF9C4B3B |
---|
2 | #define XC_VECTOR_H_0957AC4E1A44E838C7B8DBECFF9C4B3B |
---|
3 | |
---|
4 | #if _MSC_VER > 1000 |
---|
5 | #pragma once |
---|
6 | #endif /* _MSC_VER > 1000 */ |
---|
7 | |
---|
8 | #include <string.h> |
---|
9 | #include <stdlib.h> |
---|
10 | #include <assert.h> |
---|
11 | |
---|
12 | typedef struct { |
---|
13 | size_t size; |
---|
14 | void *data; |
---|
15 | |
---|
16 | size_t capacity; |
---|
17 | size_t data_size; |
---|
18 | int persistent; |
---|
19 | } xc_vector_t; |
---|
20 | |
---|
21 | #define xc_vector_initializer(type, persistent_) { \ |
---|
22 | 0, \ |
---|
23 | NULL, \ |
---|
24 | \ |
---|
25 | 0, \ |
---|
26 | sizeof(type), \ |
---|
27 | persistent_, \ |
---|
28 | } |
---|
29 | |
---|
30 | #define xc_vector_init(type, vector, persistent_) do { \ |
---|
31 | (vector)->size = 0; \ |
---|
32 | (vector)->data = NULL; \ |
---|
33 | \ |
---|
34 | (vector)->capacity = 0; \ |
---|
35 | (vector)->data_size = sizeof(type); \ |
---|
36 | (vector)->persistent = persistent_; \ |
---|
37 | } while (0) |
---|
38 | |
---|
39 | static inline void xc_vector_destroy_impl(xc_vector_t *vector TSRMLS_DC) |
---|
40 | { |
---|
41 | vector->size = 0; |
---|
42 | if (vector->data) { |
---|
43 | pefree(vector->data, vector->persistent); |
---|
44 | vector->data = NULL; |
---|
45 | } |
---|
46 | vector->capacity = 0; |
---|
47 | vector->data_size = 0; |
---|
48 | } |
---|
49 | |
---|
50 | #define xc_vector_destroy(vector) xc_vector_destroy_impl(vector TSRMLS_CC) |
---|
51 | #define xc_vector_clear(vector) do { (vector)->size = 0; } while (0) |
---|
52 | |
---|
53 | #define xc_vector_size(vector) ((vector)->size) |
---|
54 | #define xc_vector_initialized(vector) ((vector)->data_size != 0) |
---|
55 | #define xc_vector_element_ptr_(vector, index) ( \ |
---|
56 | (void *) ( \ |
---|
57 | ((char *) (vector)->data) + (index) * (vector)->data_size \ |
---|
58 | ) \ |
---|
59 | ) |
---|
60 | |
---|
61 | static inline xc_vector_t *xc_vector_check_type_(xc_vector_t *vector, size_t data_size) |
---|
62 | { |
---|
63 | assert(vector->data_size = data_size); |
---|
64 | return vector; |
---|
65 | } |
---|
66 | |
---|
67 | #define xc_vector_data(type, vector) ((type *) xc_vector_check_type_(vector, sizeof(type))->data) |
---|
68 | |
---|
69 | static inline void xc_vector_check_reserve_(xc_vector_t *vector TSRMLS_DC) |
---|
70 | { |
---|
71 | if (vector->size == vector->capacity) { |
---|
72 | if (vector->capacity) { |
---|
73 | vector->capacity <<= 1; |
---|
74 | vector->data = perealloc(vector->data, vector->data_size * vector->capacity, vector->persistent); |
---|
75 | } |
---|
76 | else { |
---|
77 | vector->capacity = 8; |
---|
78 | vector->data = pemalloc(vector->data_size * vector->capacity, vector->persistent); |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | #define xc_vector_push_back(vector, value_ptr) do { \ |
---|
84 | xc_vector_check_reserve_(vector TSRMLS_CC); \ |
---|
85 | memcpy(xc_vector_element_ptr_(vector, (vector)->size++), value_ptr, (vector)->data_size); \ |
---|
86 | } while (0) |
---|
87 | |
---|
88 | static inline void *xc_vector_detach_impl(xc_vector_t *vector) |
---|
89 | { |
---|
90 | void *data = vector->data; |
---|
91 | |
---|
92 | vector->data = NULL; |
---|
93 | vector->capacity = 0; |
---|
94 | vector->size = 0; |
---|
95 | return data; |
---|
96 | } |
---|
97 | |
---|
98 | #define xc_vector_detach(type, vector) ((type *) xc_vector_detach_impl(xc_vector_check_type_(vector, sizeof(type)))) |
---|
99 | |
---|
100 | static inline xc_vector_t *xc_vector_pop_back_check_(xc_vector_t *vector, size_t data_size) |
---|
101 | { |
---|
102 | assert(vector); |
---|
103 | assert(vector->data_size == data_size); |
---|
104 | assert(vector->capacity > 0); |
---|
105 | return vector; |
---|
106 | } |
---|
107 | |
---|
108 | #define xc_vector_pop_back(type, vector) xc_vector_data(type, \ |
---|
109 | xc_vector_pop_back_check_(vector, sizeof(type)) \ |
---|
110 | )[--(vector)->size] |
---|
111 | |
---|
112 | static inline void xc_vector_reverse(xc_vector_t *vector) |
---|
113 | { |
---|
114 | char *left, *right; |
---|
115 | void *tmp; |
---|
116 | |
---|
117 | assert(vector); |
---|
118 | tmp = alloca(vector->data_size); |
---|
119 | for (left = vector->data, right = xc_vector_element_ptr_(vector, vector->size - 1); left < right; left += vector->data_size, right -= vector->data_size) { |
---|
120 | memcpy(tmp, left, vector->data_size); |
---|
121 | memcpy(left, right, vector->data_size); |
---|
122 | memcpy(right, tmp, vector->data_size); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | #endif /* XC_VECTOR_H_0957AC4E1A44E838C7B8DBECFF9C4B3B */ |
---|