ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
Vec.h
Go to the documentation of this file.
1/*******************************************************************************************[Vec.h]
2Copyright (c) 2003-2007, Niklas Een, Niklas Sorensson
3Copyright (c) 2007-2010, Niklas Sorensson
4
5Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6associated documentation files (the "Software"), to deal in the Software without restriction,
7including without limitation the rights to use, copy, modify, merge, publish, distribute,
8sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all copies or
12substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
18OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19**************************************************************************************************/
20
21#ifndef Glucose_Vec_h
22#define Glucose_Vec_h
23
24#include <assert.h>
25#include <new>
26
28#include "sat/glucose2/XAlloc.h"
29
31
32namespace Gluco2 {
33
34//=================================================================================================
35// Automatically resizable arrays
36//
37// NOTE! Don't use this vector on datatypes that cannot be re-located in memory (with realloc)
38
39template<class T>
40class vec {
41 T* data;
42 int sz;
43 int cap;
44
45 // Don't allow copying (error prone):
46 vec<T>& operator = (vec<T>& other) { assert(0); return *this; }
47 vec (vec<T>& other) { assert(0); }
48
49 // Helpers for calculating next capacity:
50 static inline int imax (int x, int y) { int mask = (y-x) >> (sizeof(int)*8-1); return (x&mask) + (y&(~mask)); }
51 //static inline void nextCap(int& cap){ cap += ((cap >> 1) + 2) & ~1; }
52 static inline void nextCap(int& cap){ cap += ((cap >> 1) + 2) & ~1; }
53
54public:
55 // Constructors:
56 vec() : data(NULL) , sz(0) , cap(0) { }
57 explicit vec(int size) : data(NULL) , sz(0) , cap(0) { growTo(size); }
58 vec(int size, const T& pad) : data(NULL) , sz(0) , cap(0) { growTo(size, pad); }
59 ~vec() { clear(true); }
60
61 // Pointer to first element:
62 operator T* (void) { return data; }
63
64 // Size operations:
65 int size (void) const { return sz; }
66 void shrink (int nelems) { assert(nelems <= sz); for (int i = 0; i < nelems; i++) sz--, data[sz].~T(); }
67 void shrink_ (int nelems) { assert(nelems <= sz); sz -= nelems; }
68 int capacity (void) const { return cap; }
69 void capacity (int min_cap);
70 void prelocate(int ext_cap);
71 void growTo (int size);
72 void growTo_ (int size);
73 void growTo (int size, const T& pad);
74 void clear (bool dealloc = false);
75
76 // Stack interface:
77 void push (void) { if (sz == cap) capacity(sz+1); new (&data[sz]) T(); sz++; }
78 void push (const T& elem) { if (sz == cap) capacity(sz+1); data[sz++] = elem; }
79 void push_ (const T& elem) { assert(sz < cap); data[sz++] = elem; }
80 void pop (void) { assert(sz > 0); sz--, data[sz].~T(); }
81 // NOTE: it seems possible that overflow can happen in the 'sz+1' expression of 'push()', but
82 // in fact it can not since it requires that 'cap' is equal to INT_MAX. This in turn can not
83 // happen given the way capacities are calculated (below). Essentially, all capacities are
84 // even, but INT_MAX is odd.
85
86 const T& last (void) const { return data[sz-1]; }
87 T& last (void) { return data[sz-1]; }
88
89 // Vector interface:
90 const T& operator [] (int index) const { return data[index]; }
91 T& operator [] (int index) { return data[index]; }
92
93 // Duplicatation (preferred instead):
94 void copyTo (vec<T>& copy) const { copy.clear(); copy.growTo(sz); for (int i = 0; i < sz; i++) copy[i] = data[i]; }
95 void copyTo_(vec<T>& copy) const { copy.shrink_(copy.size()); copy.growTo_(sz); for (int i = 0; i < sz; i++) copy[i] = data[i]; }
96 void moveTo(vec<T>& dest) { dest.clear(true); dest.data = data; dest.sz = sz; dest.cap = cap; data = NULL; sz = 0; cap = 0; }
97};
98
99
100template<class T>
101void vec<T>::capacity(int min_cap) {
102 if (cap >= min_cap) return;
103 int add = imax((min_cap - cap + 1) & ~1, ((cap >> 1) + 2) & ~1); // NOTE: grow by approximately 3/2
104 if (add > INT_MAX - cap || (((data = (T*)::realloc((void*)data, (cap += add) * sizeof(T))) == NULL) && errno == ENOMEM))
105 fatal_out_of_memory();
106 }
107
108template<class T>
109void vec<T>::prelocate(int ext_cap) {
110 if (cap >= ext_cap) return;
111 if (ext_cap > INT_MAX || (((data = (T*)::realloc((void*)data, ext_cap * sizeof(T))) == NULL) && errno == ENOMEM))
112 fatal_out_of_memory();;
113 cap = ext_cap;
114 }
115
116
117template<class T>
118void vec<T>::growTo(int size, const T& pad) {
119 if (sz >= size) return;
120 capacity(size);
121 for (int i = sz; i < size; i++) data[i] = pad;
122 sz = size; }
123
124
125template<class T>
127 if (sz >= size) return;
128 capacity(size);
129 for (int i = sz; i < size; i++) new (&data[i]) T();
130 sz = size; }
131
132
133template<class T>
135 if (sz >= size) return;
136 capacity(size);
137 sz = size; }
138
139
140template<class T>
141void vec<T>::clear(bool dealloc) {
142 if (data != NULL){
143 for (int i = 0; i < sz; i++) data[i].~T();
144 sz = 0;
145 if (dealloc) free(data), data = NULL, cap = 0; } }
146
147//=================================================================================================
148}
149
151
152#endif
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
void shrink_(int nelems)
Definition Vec.h:67
const T & last(void) const
Definition Vec.h:86
~vec()
Definition Vec.h:59
void push(const T &elem)
Definition Vec.h:78
void moveTo(vec< T > &dest)
Definition Vec.h:96
const T & operator[](int index) const
Definition Vec.h:90
void growTo_(int size)
Definition Vec.h:134
vec()
Definition Vec.h:56
vec(int size)
Definition Vec.h:57
int capacity(void) const
Definition Vec.h:68
void capacity(int min_cap)
Definition Vec.h:101
void shrink(int nelems)
Definition Vec.h:66
void growTo(int size)
Definition Vec.h:126
void push(void)
Definition Vec.h:77
void copyTo_(vec< T > &copy) const
Definition Vec.h:95
void push_(const T &elem)
Definition Vec.h:79
T & last(void)
Definition Vec.h:87
void pop(void)
Definition Vec.h:80
void clear(bool dealloc=false)
Definition Vec.h:141
void prelocate(int ext_cap)
Definition Vec.h:109
vec(int size, const T &pad)
Definition Vec.h:58
void copyTo(vec< T > &copy) const
Definition Vec.h:94
int size(void) const
Definition Vec.h:65
void growTo(int size, const T &pad)
Definition Vec.h:118
Definition Alg.h:28
#define assert(ex)
Definition util_old.h:213
VOID_HACK free()
char * realloc()