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 Minisat_Vec_h
22#define Minisat_Vec_h
23
24#include <assert.h>
25#include <new>
26
27#include "sat/bsat2/IntTypes.h"
28#include "sat/bsat2/XAlloc.h"
29
31
32namespace Minisat {
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 growTo (int size);
71 void growTo (int size, const T& pad);
72 void clear (bool dealloc = false);
73
74 // Stack interface:
75 void push (void) { if (sz == cap) capacity(sz+1); new (&data[sz]) T(); sz++; }
76 void push (const T& elem) { if (sz == cap) capacity(sz+1); data[sz++] = elem; }
77 void push_ (const T& elem) { assert(sz < cap); data[sz++] = elem; }
78 void pop (void) { assert(sz > 0); sz--, data[sz].~T(); }
79 // NOTE: it seems possible that overflow can happen in the 'sz+1' expression of 'push()', but
80 // in fact it can not since it requires that 'cap' is equal to INT_MAX. This in turn can not
81 // happen given the way capacities are calculated (below). Essentially, all capacities are
82 // even, but INT_MAX is odd.
83
84 const T& last (void) const { return data[sz-1]; }
85 T& last (void) { return data[sz-1]; }
86
87 // Vector interface:
88 const T& operator [] (int index) const { return data[index]; }
89 T& operator [] (int index) { return data[index]; }
90
91 // Duplicatation (preferred instead):
92 void copyTo(vec<T>& copy) const { copy.clear(); copy.growTo(sz); for (int i = 0; i < sz; i++) copy[i] = data[i]; }
93 void moveTo(vec<T>& dest) { dest.clear(true); dest.data = data; dest.sz = sz; dest.cap = cap; data = NULL; sz = 0; cap = 0; }
94};
95
96template<class T>
97void vec<T>::capacity(int min_cap) {
98 if (cap >= min_cap) return;
99 int add = imax((min_cap - cap + 1) & ~1, ((cap >> 1) + 2) & ~1); // NOTE: grow by approximately 3/2
100 if (add > INT_MAX - cap || (((data = (T*)::realloc(data, (cap += add) * sizeof(T))) == NULL) && errno == ENOMEM))
101#ifdef __wasm
102 abort();
103#else
104 throw OutOfMemoryException();
105#endif
106 }
107
108
109template<class T>
110void vec<T>::growTo(int size, const T& pad) {
111 if (sz >= size) return;
112 capacity(size);
113 for (int i = sz; i < size; i++) data[i] = pad;
114 sz = size; }
115
116
117template<class T>
119 if (sz >= size) return;
120 capacity(size);
121 for (int i = sz; i < size; i++) new (&data[i]) T();
122 sz = size; }
123
124
125template<class T>
126void vec<T>::clear(bool dealloc) {
127 if (data != NULL){
128 for (int i = 0; i < sz; i++) data[i].~T();
129 sz = 0;
130 if (dealloc) free(data), data = NULL, cap = 0; } }
131
132//=================================================================================================
133}
134
136
137#endif
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
void shrink(int nelems)
Definition Vec.h:66
void growTo(int size)
Definition Vec.h:118
void push_(const T &elem)
Definition Vec.h:77
void copyTo(vec< T > &copy) const
Definition Vec.h:92
void moveTo(vec< T > &dest)
Definition Vec.h:93
int capacity(void) const
Definition Vec.h:68
const T & operator[](int index) const
Definition Vec.h:88
int size(void) const
Definition Vec.h:65
void push(const T &elem)
Definition Vec.h:76
vec(int size, const T &pad)
Definition Vec.h:58
void shrink_(int nelems)
Definition Vec.h:67
void clear(bool dealloc=false)
Definition Vec.h:126
void capacity(int min_cap)
Definition Vec.h:97
void push(void)
Definition Vec.h:75
T & last(void)
Definition Vec.h:85
const T & last(void) const
Definition Vec.h:84
void pop(void)
Definition Vec.h:78
void growTo(int size, const T &pad)
Definition Vec.h:110
vec(int size)
Definition Vec.h:57
Definition Alg.h:28
#define assert(ex)
Definition util_old.h:213
VOID_HACK abort()
VOID_HACK free()
char * realloc()