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/glucose/XAlloc.h"
29
31
32namespace Gluco {
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 copyTo_(vec<T>& copy) const { copy.shrink_(copy.size()); copy.growTo(sz); for (int i = 0; i < sz; i++) copy[i] = data[i]; }
94 void moveTo(vec<T>& dest) { dest.clear(true); dest.data = data; dest.sz = sz; dest.cap = cap; data = NULL; sz = 0; cap = 0; }
95};
96
97
98template<class T>
99void vec<T>::capacity(int min_cap) {
100 if (cap >= min_cap) return;
101 int add = imax((min_cap - cap + 1) & ~1, ((cap >> 1) + 2) & ~1); // NOTE: grow by approximately 3/2
102 if (add > INT_MAX - cap || (((data = (T*)::realloc((void*)data, (cap += add) * sizeof(T))) == NULL) && errno == ENOMEM))
103 fatal_out_of_memory();
104 }
105
106
107template<class T>
108void vec<T>::growTo(int size, const T& pad) {
109 if (sz >= size) return;
110 capacity(size);
111 for (int i = sz; i < size; i++) data[i] = pad;
112 sz = size; }
113
114
115template<class T>
117 if (sz >= size) return;
118 capacity(size);
119 for (int i = sz; i < size; i++) new (&data[i]) T();
120 sz = size; }
121
122
123template<class T>
124void vec<T>::clear(bool dealloc) {
125 if (data != NULL){
126 for (int i = 0; i < sz; i++) data[i].~T();
127 sz = 0;
128 if (dealloc) free(data), data = NULL, cap = 0; } }
129
130//=================================================================================================
131}
132
134
135#endif
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
const T & last(void) const
Definition Vec.h:84
int size(void) const
Definition Vec.h:65
~vec()
Definition Vec.h:59
const T & operator[](int index) const
Definition Vec.h:88
int capacity(void) const
Definition Vec.h:68
vec(int size, const T &pad)
Definition Vec.h:58
void copyTo(vec< T > &copy) const
Definition Vec.h:92
void shrink_(int nelems)
Definition Vec.h:67
void pop(void)
Definition Vec.h:78
void push_(const T &elem)
Definition Vec.h:77
void shrink(int nelems)
Definition Vec.h:66
vec(int size)
Definition Vec.h:57
void growTo(int size)
Definition Vec.h:116
void growTo(int size, const T &pad)
Definition Vec.h:108
vec()
Definition Vec.h:56
void capacity(int min_cap)
Definition Vec.h:99
void moveTo(vec< T > &dest)
Definition Vec.h:94
void copyTo_(vec< T > &copy) const
Definition Vec.h:93
void clear(bool dealloc=false)
Definition Vec.h:124
void push(const T &elem)
Definition Vec.h:76
T & last(void)
Definition Vec.h:85
void push(void)
Definition Vec.h:75
Definition Alg.h:28
#define assert(ex)
Definition util_old.h:213
VOID_HACK free()
char * realloc()