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