ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
rewire_vec.h
Go to the documentation of this file.
1
20
21#ifndef RAR_VI_H
22#define RAR_VI_H
23
24/*************************************************************
25 vector of 32-bit integers
26**************************************************************/
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <assert.h>
31#include <string.h>
32
33#include "rewire_rng.h"
34
36
37// swapping two variables
38#define RW_SWAP(Type, a, b) \
39 { \
40 Type t = a; \
41 a = b; \
42 b = t; \
43 }
44
45typedef struct vi_ {
46 int size;
47 int cap;
48 int *ptr;
50
51// iterator through the entries in the vector
52#define Vi_ForEachEntry(v, entry, i) for (i = 0; (i < (v)->size) && (((entry) = Vi_Read((v), i)), 1); i++)
53#define Vi_ForEachEntryReverse(v, entry, i) for (i = (v)->size - 1; (i >= 0) && (((entry) = Vi_Read((v), i)), 1); i--)
54#define Vi_ForEachEntryStart(v, entry, i, start) for (i = start; (i < (v)->size) && (((entry) = Vi_Read((v), i)), 1); i++)
55#define Vi_ForEachEntryStop(v, entry, i, stop) for (i = 0; (i < stop) && (((entry) = Vi_Read((v), i)), 1); i++)
56
57static inline void Vi_Start(vi *v, int cap) {
58 v->size = 0;
59 v->cap = cap;
60 v->ptr = (int *)malloc(sizeof(int) * cap);
61}
62
63static inline vi *Vi_Alloc(int cap) {
64 vi *v = (vi *)malloc(sizeof(vi));
65 Vi_Start(v, cap);
66 return v;
67}
68
69static inline void Vi_Stop(vi *v) {
70 if (v->ptr) free(v->ptr);
71}
72
73static inline void Vi_Free(vi *v) {
74 if (v->ptr) free(v->ptr);
75 free(v);
76}
77
78static inline int Vi_Size(vi *v) {
79 return v->size;
80}
81
82static inline int Vi_Space(vi *v) {
83 return v->cap - v->size;
84}
85
86static inline int *Vi_Array(vi *v) {
87 return v->ptr;
88}
89
90static inline int Vi_Read(vi *v, int k) {
91 assert(k < v->size);
92 return v->ptr[k];
93}
94
95static inline void Vi_Write(vi *v, int k, int e) {
96 assert(k < v->size);
97 v->ptr[k] = e;
98}
99
100static inline void Vi_Shrink(vi *v, int k) {
101 assert(k <= v->size);
102 v->size = k;
103} // only safe to shrink !!
104
105static inline int Vi_Pop(vi *v) {
106 assert(v->size > 0);
107 return v->ptr[--v->size];
108}
109
110static inline void Vi_Grow(vi *v) {
111 if (v->size < v->cap)
112 return;
113 int newcap = (v->cap < 4) ? 8 : (v->cap / 2) * 3;
114 v->ptr = (int *)realloc(v->ptr, sizeof(int) * newcap);
115 if (v->ptr == NULL) {
116 printf("Failed to realloc memory from %.1f MB to %.1f MB.\n", 4.0 * v->cap / (1 << 20), (float)4.0 * newcap / (1 << 20));
117 fflush(stdout);
118 }
119 v->cap = newcap;
120}
121
122static inline vi *Vi_Dup(vi *v) {
123 vi *pNew = Vi_Alloc(v->size);
124 memcpy(pNew->ptr, v->ptr, sizeof(int) * v->size);
125 pNew->size = v->size;
126 return pNew;
127}
128
129static inline void Vi_Push(vi *v, int e) {
130 Vi_Grow(v);
131 v->ptr[v->size++] = e;
132}
133
134static inline void Vi_PushTwo(vi *v, int e1, int e2) {
135 Vi_Push(v, e1);
136 Vi_Push(v, e2);
137}
138
139static inline void Vi_PushArray(vi *v, int *p, int n) {
140 for (int i = 0; i < n; i++)
141 Vi_Push(v, p[i]);
142}
143
144static inline void Vi_PushOrder(vi *v, int e) {
145 Vi_Push(v, e);
146 if (v->size > 1)
147 for (int i = v->size - 2; i >= 0; i--) {
148 if (v->ptr[i] > v->ptr[i + 1]) {
149 RW_SWAP(int, v->ptr[i], v->ptr[i + 1])
150 } else {
151 break;
152 }
153 }
154}
155
156static inline void Vi_Fill(vi *v, int n, int fill) {
157 int i;
158 Vi_Shrink(v, 0);
159 for (i = 0; i < n; i++)
160 Vi_Push(v, fill);
161}
162
163static inline int Vi_Drop(vi *v, int i) {
164 assert(i >= 0 && i < v->size);
165 int Entry = v->ptr[i];
166 for (; i < v->size - 1; i++)
167 v->ptr[i] = v->ptr[i + 1];
168 Vi_Shrink(v, v->size - 1);
169 return Entry;
170}
171
172static inline int Vi_Find(vi *v, int e) {
173 int j;
174 for (j = 0; j < v->size; j++)
175 if (v->ptr[j] == e)
176 return j;
177 return -1;
178}
179
180static inline int Vi_Remove(vi *v, int e) {
181 int j = Vi_Find(v, e);
182 if (j == -1)
183 return 0;
184 Vi_Drop(v, j);
185 return 1;
186}
187
188static inline void Vi_Randomize(vi *v) {
189 for (int i = 0; i < v->size; i++) {
190 int iRand = Random_Num(0) % v->size;
191 RW_SWAP(int, v->ptr[iRand], v->ptr[i]);
192 }
193}
194
195static inline void Vi_Reverse(vi *v) {
196 int i, j;
197 for (i = 0, j = v->size - 1; i < j; i++, j--)
198 RW_SWAP(int, v->ptr[i], v->ptr[j]);
199}
200
201static inline void Vi_Print(vi *v) {
202 printf("Array with %d entries:", v->size);
203 int i, entry;
204 Vi_ForEachEntry(v, entry, i)
205 printf(" %d", entry);
206 printf("\n");
207}
208
209static inline void Vi_SelectSort(vi *v) {
210 int *pArray = Vi_Array(v);
211 int nSize = Vi_Size(v);
212 int temp, i, j, best_i;
213 for (i = 0; i < nSize - 1; i++) {
214 best_i = i;
215 for (j = i + 1; j < nSize; j++)
216 if (pArray[j] < pArray[best_i])
217 best_i = j;
218 temp = pArray[i];
219 pArray[i] = pArray[best_i];
220 pArray[best_i] = temp;
221 }
222}
223
225
226#endif // RAR_VI_H
#define ABC_NAMESPACE_HEADER_END
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
Cube * p
Definition exorList.c:222
unsigned Random_Num(int Seed)
Definition rewire_rng.c:43
struct vi_ vi
#define RW_SWAP(Type, a, b)
Definition rewire_vec.h:38
#define Vi_ForEachEntry(v, entry, i)
Definition rewire_vec.h:52
if(last==0)
Definition sparse_int.h:34
int cap
Definition rewire_vec.h:47
int * ptr
Definition rewire_vec.h:48
int size
Definition rewire_vec.h:46
#define assert(ex)
Definition util_old.h:213
char * memcpy()
VOID_HACK free()
char * malloc()
char * realloc()