A set of benchmarks to compare a new prototype MiniZinc implementation
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2 3/* 4 * Main authors: 5 * Guido Tack <guido.tack@monash.edu> 6 */ 7 8/* This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 11 12#ifndef __MINIZINC_ASTVEC_HH__ 13#define __MINIZINC_ASTVEC_HH__ 14 15#include <minizinc/gc.hh> 16 17#include <vector> 18 19namespace MiniZinc { 20 21class ASTIntVecO; 22 23/** 24 * \brief Handler for ASTIntVecO objects 25 */ 26class ASTIntVec { 27protected: 28 /// Vector 29 ASTIntVecO* _v; 30 31public: 32 /// Default constructor 33 ASTIntVec(void) : _v(NULL) {} 34 /// Constructor 35 ASTIntVec(ASTIntVecO* v) : _v(v) {} 36 /// Constructor 37 ASTIntVec(const std::vector<int>& v); 38 /// Copy constructor 39 ASTIntVec(const ASTIntVec& s); 40 /// Assignment operator 41 ASTIntVec& operator=(const ASTIntVec& s); 42 43 /// Size of vector 44 unsigned int size(void) const; 45 /// Element access 46 int& operator[](unsigned int i); 47 /// Element access 48 int operator[](unsigned int i) const; 49 /// Iterator begin 50 int* begin(void); 51 /// Iterator end 52 int* end(void); 53 /// Mark as alive for garbage collection 54 void mark(void) const; 55}; 56 57template <class> 58class ASTExprVecO; 59 60/** 61 * \brief Handler for ASTExprVecO objects 62 */ 63template <class T> 64class ASTExprVec { 65protected: 66 /// Vector 67 ASTExprVecO<T*>* _v; 68 69public: 70 /// Default constructor 71 ASTExprVec(void) : _v(NULL) {} 72 /// Constructor 73 ASTExprVec(ASTExprVecO<T*>* v) : _v(v) {} 74 /// Constructor 75 ASTExprVec(const std::vector<T*>& v); 76 /// Copy constructor 77 ASTExprVec(const ASTExprVec& v); 78 /// Assignment operator 79 ASTExprVec& operator=(const ASTExprVec& v); 80 81 /// Size of vector 82 unsigned int size(void) const; 83 /// Element access 84 T*& operator[](unsigned int i); 85 /// Element access 86 T* operator[](unsigned int i) const; 87 /// Iterator begin 88 T** begin(void); 89 /// Iterator end 90 T** end(void); 91 92 /// Return vector object 93 ASTExprVecO<T*>* vec(void) const; 94 /// Mark as alive for garbage collection 95 void mark(void) const; 96}; 97 98/// Garbage collected integer vector 99class ASTIntVecO : public ASTChunk { 100protected: 101 /// Constructor 102 ASTIntVecO(const std::vector<int>& v); 103 104public: 105 /// Allocate and initialise from \a v 106 static ASTIntVecO* a(const std::vector<int>& v); 107 /// Return size 108 unsigned int size(void) const { return static_cast<unsigned int>(_size / sizeof(int)); } 109 /// Return element at position \a i 110 int& operator[](unsigned int i) { 111 assert(i < size()); 112 return reinterpret_cast<int*>(_data)[i]; 113 } 114 /// Return element at position \a i 115 int operator[](unsigned int i) const { 116 assert(i < size()); 117 return reinterpret_cast<const int*>(_data)[i]; 118 } 119 /// Iterator begin 120 int* begin(void) { return reinterpret_cast<int*>(_data); } 121 /// Iterator end 122 int* end(void) { return begin() + size(); } 123 /// Mark as alive for garbage collection 124 void mark(void) const { _gc_mark = 1; } 125}; 126 127/// Garbage collected vector of expressions 128template <class T> 129class ASTExprVecO : public ASTVec { 130protected: 131 /// Constructor 132 ASTExprVecO(const std::vector<T>& v); 133 134public: 135 /// Allocate and initialise from \a v 136 static ASTExprVecO* a(const std::vector<T>& v); 137 unsigned int size(void) const { return static_cast<unsigned int>(_size); } 138 bool empty(void) const { return size() == 0; } 139 T& operator[](int i) { 140 assert(i < static_cast<int>(size())); 141 return reinterpret_cast<T&>(_data[i]); 142 } 143 const T operator[](int i) const { 144 assert(i < static_cast<int>(size())); 145 return reinterpret_cast<T>(_data[i]); 146 } 147 /// Iterator begin 148 T* begin(void) { return reinterpret_cast<T*>(_data); } 149 /// Iterator end 150 T* end(void) { return begin() + size(); } 151 /// Mark as alive for garbage collection 152 void mark(void) const { _gc_mark = 1; } 153 /// Check if flag is set 154 bool flag(void) const { return _flag_1; } 155 /// Set flag 156 void flag(bool f) { _flag_1 = f; } 157}; 158 159template <class T> 160ASTExprVecO<T>::ASTExprVecO(const std::vector<T>& v) : ASTVec(v.size()) { 161 _flag_1 = false; 162 for (unsigned int i = static_cast<unsigned int>(v.size()); i--;) (*this)[i] = v[i]; 163} 164template <class T> 165ASTExprVecO<T>* ASTExprVecO<T>::a(const std::vector<T>& v) { 166 ASTExprVecO<T>* ao = static_cast<ASTExprVecO<T>*>(alloc(v.size())); 167 new (ao) ASTExprVecO<T>(v); 168 return ao; 169} 170 171inline ASTIntVec::ASTIntVec(const std::vector<int>& v) : _v(ASTIntVecO::a(v)) {} 172inline ASTIntVec::ASTIntVec(const ASTIntVec& v) : _v(v._v) {} 173inline ASTIntVec& ASTIntVec::operator=(const ASTIntVec& v) { 174 _v = v._v; 175 return *this; 176} 177 178inline unsigned int ASTIntVec::size(void) const { return _v ? _v->size() : 0; } 179inline int& ASTIntVec::operator[](unsigned int i) { return (*_v)[i]; } 180inline int ASTIntVec::operator[](unsigned int i) const { return (*_v)[i]; } 181inline int* ASTIntVec::begin(void) { return _v ? _v->begin() : NULL; } 182inline int* ASTIntVec::end(void) { return _v ? _v->end() : NULL; } 183inline void ASTIntVec::mark(void) const { 184 if (_v) _v->mark(); 185} 186 187template <class T> 188ASTExprVec<T>::ASTExprVec(const std::vector<T*>& v) : _v(ASTExprVecO<T*>::a(v)) {} 189template <class T> 190inline ASTExprVec<T>::ASTExprVec(const ASTExprVec<T>& v) : _v(v._v) {} 191template <class T> 192inline ASTExprVec<T>& ASTExprVec<T>::operator=(const ASTExprVec<T>& v) { 193 _v = v._v; 194 return *this; 195} 196template <class T> 197inline unsigned int ASTExprVec<T>::size(void) const { 198 return _v ? _v->size() : 0; 199} 200template <class T> 201inline T*& ASTExprVec<T>::operator[](unsigned int i) { 202 return (*_v)[i]; 203} 204template <class T> 205inline T* ASTExprVec<T>::operator[](unsigned int i) const { 206 return (*_v)[i]; 207} 208template <class T> 209inline T** ASTExprVec<T>::begin(void) { 210 return _v ? _v->begin() : NULL; 211} 212template <class T> 213inline T** ASTExprVec<T>::end(void) { 214 return _v ? _v->end() : NULL; 215} 216template <class T> 217inline ASTExprVecO<T*>* ASTExprVec<T>::vec(void) const { 218 return _v; 219} 220template <class T> 221inline void ASTExprVec<T>::mark(void) const { 222 if (_v) _v->mark(); 223} 224 225} // namespace MiniZinc 226 227#endif