ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
vec_char.h
Go to the documentation of this file.
1//===--- vec_char.h ---------------------------------------------------------===
2//
3// satoko: Satisfiability solver
4//
5// This file is distributed under the BSD 2-Clause License.
6// See LICENSE for details.
7//
8//===------------------------------------------------------------------------===
9#ifndef satoko__utils__vec__vec_char_h
10#define satoko__utils__vec__vec_char_h
11
12#include <assert.h>
13#include <stdio.h>
14#include <string.h>
15
16#include "../mem.h"
17
20
21typedef struct vec_char_t_ vec_char_t;
23 unsigned cap;
24 unsigned size;
25 char *data;
26};
27
28//===------------------------------------------------------------------------===
29// Vector Macros
30//===------------------------------------------------------------------------===
31#define vec_char_foreach(vec, entry, i) \
32 for (i = 0; (i < vec_char_size(vec)) && (((entry) = vec_char_at(vec, i)), 1); i++)
33
34#define vec_char_foreach_start(vec, entry, i, start) \
35 for (i = start; (i < vec_char_size(vec)) && (((entry) = vec_char_at(vec, i)), 1); i++)
36
37#define vec_char_foreach_stop(vec, entry, i, stop) \
38 for (i = 0; (i < stop) && (((entry) = vec_char_at(vec, i)), 1); i++)
39
40//===------------------------------------------------------------------------===
41// Vector API
42//===------------------------------------------------------------------------===
43static inline vec_char_t * vec_char_alloc(unsigned cap)
44{
46
47 if (cap > 0 && cap < 16)
48 cap = 16;
49 p->size = 0;
50 p->cap = cap;
51 p->data = p->cap ? satoko_alloc(char, p->cap) : NULL;
52 return p;
53}
54
55static inline vec_char_t * vec_char_alloc_exact(unsigned cap)
56{
58
59 cap = 0;
60 p->size = 0;
61 p->cap = cap;
62 p->data = p->cap ? satoko_alloc(char, p->cap ) : NULL;
63 return p;
64}
65
66static inline vec_char_t * vec_char_init(unsigned size, char value)
67{
69
70 p->cap = size;
71 p->size = size;
72 p->data = p->cap ? satoko_alloc(char, p->cap) : NULL;
73 memset(p->data, value, sizeof(char) * p->size);
74 return p;
75}
76
77static inline void vec_char_free(vec_char_t *p)
78{
79 if (p->data != NULL)
80 satoko_free(p->data);
82}
83
84static inline unsigned vec_char_size(vec_char_t *p)
85{
86 return p->size;
87}
88
89static inline void vec_char_resize(vec_char_t *p, unsigned new_size)
90{
91 p->size = new_size;
92 if (p->cap >= new_size)
93 return;
94 p->data = satoko_realloc(char, p->data, new_size);
95 assert(p->data != NULL);
96 p->cap = new_size;
97}
98
99static inline void vec_char_shrink(vec_char_t *p, unsigned new_size)
100{
101 assert(p->cap > new_size);
102 p->size = new_size;
103}
104
105static inline void vec_char_reserve(vec_char_t *p, unsigned new_cap)
106{
107 if (p->cap >= new_cap)
108 return;
109 p->data = satoko_realloc(char, p->data, new_cap);
110 assert(p->data != NULL);
111 p->cap = new_cap;
112}
113
114static inline unsigned vec_char_capacity(vec_char_t *p)
115{
116 return p->cap;
117}
118
119static inline int vec_char_empty(vec_char_t *p)
120{
121 return p->size ? 0 : 1;
122}
123
124static inline void vec_char_erase(vec_char_t *p)
125{
126 satoko_free(p->data);
127 p->size = 0;
128 p->cap = 0;
129}
130
131static inline char vec_char_at(vec_char_t *p, unsigned idx)
132{
133 assert(idx >= 0 && idx < p->size);
134 return p->data[idx];
135}
136
137static inline char * vec_char_at_ptr(vec_char_t *p, unsigned idx)
138{
139 assert(idx >= 0 && idx < p->size);
140 return p->data + idx;
141}
142
143static inline char * vec_char_data(vec_char_t *p)
144{
145 assert(p);
146 return p->data;
147}
148
149static inline void vec_char_duplicate(vec_char_t *dest, const vec_char_t *src)
150{
151 assert(dest != NULL && src != NULL);
152 vec_char_resize(dest, src->cap);
153 memcpy(dest->data, src->data, sizeof(char) * src->cap);
154 dest->size = src->size;
155}
156
157static inline void vec_char_copy(vec_char_t *dest, const vec_char_t *src)
158{
159 assert(dest != NULL && src != NULL);
160 vec_char_resize(dest, src->size);
161 memcpy(dest->data, src->data, sizeof(char) * src->size);
162 dest->size = src->size;
163}
164
165static inline void vec_char_push_back(vec_char_t *p, char value)
166{
167 if (p->size == p->cap) {
168 if (p->cap < 16)
169 vec_char_reserve(p, 16);
170 else
171 vec_char_reserve(p, 2 * p->cap);
172 }
173 p->data[p->size] = value;
174 p->size++;
175}
176
177static inline char vec_char_pop_back(vec_char_t *p)
178{
179 assert(p && p->size);
180 return p->data[--p->size];
181}
182
183static inline void vec_char_assign(vec_char_t *p, unsigned idx, char value)
184{
185 assert((idx >= 0) && (idx < vec_char_size(p)));
186 p->data[idx] = value;
187}
188
189static inline void vec_char_insert(vec_char_t *p, unsigned idx, char value)
190{
191 assert((idx >= 0) && (idx < vec_char_size(p)));
192 vec_char_push_back(p, 0);
193 memmove(p->data + idx + 1, p->data + idx, (p->size - idx - 2) * sizeof(char));
194 p->data[idx] = value;
195}
196
197static inline void vec_char_drop(vec_char_t *p, unsigned idx)
198{
199 assert((idx >= 0) && (idx < vec_char_size(p)));
200 memmove(p->data + idx, p->data + idx + 1, (p->size - idx - 1) * sizeof(char));
201 p->size -= 1;
202}
203
204static inline void vec_char_clear(vec_char_t *p)
205{
206 p->size = 0;
207}
208
209static inline int vec_char_asc_compare(const void *p1, const void *p2)
210{
211 const char *pp1 = (const char *)p1;
212 const char *pp2 = (const char *)p2;
213
214 if (*pp1 < *pp2)
215 return -1;
216 if (*pp1 > *pp2)
217 return 1;
218 return 0;
219}
220
221static inline int vec_char_desc_compare(const void *p1, const void *p2)
222{
223 const char *pp1 = (const char *)p1;
224 const char *pp2 = (const char *)p2;
225
226 if (*pp1 > *pp2)
227 return -1;
228 if (*pp1 < *pp2)
229 return 1;
230 return 0;
231}
232
233static inline void vec_char_sort(vec_char_t *p, int ascending)
234{
235 if (ascending)
236 qsort((void *) p->data, (size_t)p->size, sizeof(char),
237 (int (*)(const void *, const void *)) vec_char_asc_compare);
238 else
239 qsort((void*) p->data, (size_t)p->size, sizeof(char),
240 (int (*)(const void *, const void *)) vec_char_desc_compare);
241}
242
243
244static inline long vec_char_memory(vec_char_t *p)
245{
246 return p == NULL ? 0 : sizeof(char) * p->cap + sizeof(vec_char_t);
247}
248
249static inline void vec_char_print(vec_char_t* p)
250{
251 unsigned i;
252 assert(p != NULL);
253 fprintf(stdout, "Vector has %u(%u) entries: {", p->size, p->cap);
254 for (i = 0; i < p->size; i++)
255 fprintf(stdout, " %d", p->data[i]);
256 fprintf(stdout, " }\n");
257}
258
260#endif /* satoko__utils__vec__vec_char_h */
#define ABC_NAMESPACE_HEADER_END
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
ABC_NAMESPACE_IMPL_START typedef signed char value
Cube * p
Definition exorList.c:222
unsigned long long size
Definition giaNewBdd.h:39
#define satoko_free(p)
Definition mem.h:20
#define satoko_realloc(type, ptr, n_elements)
Definition mem.h:19
#define satoko_alloc(type, n_elements)
Definition mem.h:17
char * data
Definition vec_char.h:25
unsigned size
Definition vec_char.h:24
unsigned cap
Definition vec_char.h:23
#define assert(ex)
Definition util_old.h:213
char * memcpy()
char * memset()
char * memmove()
typedefABC_NAMESPACE_HEADER_START struct vec_char_t_ vec_char_t
Definition vec_char.h:21