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_ASTSTRING_HH__ 13#define __MINIZINC_ASTSTRING_HH__ 14 15#include <minizinc/gc.hh> 16 17#include <cstring> 18#include <functional> 19#include <iostream> 20#include <string> 21 22namespace MiniZinc { 23 24class ASTStringO; 25 26/** 27 * \brief Handler for ASTStringO objects 28 */ 29class ASTString { 30protected: 31 /// String 32 ASTStringO* _s; 33 34public: 35 /// Default constructor 36 ASTString(void) : _s(NULL) {} 37 /// Constructor 38 ASTString(ASTStringO* s) : _s(s) {} 39 /// Constructor 40 ASTString(const std::string& s); 41 /// Copy constructor 42 ASTString(const ASTString& s); 43 /// Assignment operator 44 ASTString& operator=(const ASTString& s); 45 46 /// Size of the string 47 unsigned int size(void) const; 48 /// Underlying C string object 49 const char* c_str(void) const; 50 /// Conversion to STL string 51 std::string str(void) const; 52 /// Underlying string implementation 53 ASTStringO* aststr(void) const { return _s; } 54 55 /// Return if string is equal to \a s 56 bool operator==(const ASTString& s) const; 57 /// Return if string is not equal to \a s 58 bool operator!=(const ASTString& s) const; 59 60 /// Return if string is equal to \a s 61 bool operator==(const std::string& s) const; 62 /// Return if string is not equal to \a s 63 bool operator!=(const std::string& s) const; 64 65 /// Return if string ends with \a s 66 bool endsWith(const std::string& s) const; 67 68 /// Return if string begins with \a s 69 bool beginsWith(const std::string& s) const; 70 71 /// Compute hash value of string 72 size_t hash(void) const; 73 74 /// Mark string during garbage collection 75 void mark(void) const; 76}; 77 78/// Hash map from strings to \a T 79template <typename T> 80struct ASTStringMap { 81 /// The map type specialised for ASTString 82 typedef std::unordered_map<ASTString, T> t; 83}; 84 85/** 86 * \brief Print integer set \a s 87 * \relates Gecode::IntSet 88 */ 89template <class Char, class Traits> 90std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, 91 const ASTString& s) { 92 return s.size() == 0 ? os : (os << s.c_str()); 93} 94 95} // namespace MiniZinc 96 97namespace std { 98template <> 99struct hash<MiniZinc::ASTString> { 100public: 101 size_t operator()(const MiniZinc::ASTString& s) const; 102}; 103} // namespace std 104 105namespace std { 106template <> 107struct equal_to<MiniZinc::ASTString> { 108public: 109 bool operator()(const MiniZinc::ASTString& s0, const MiniZinc::ASTString& s1) const; 110}; 111} // namespace std 112 113namespace MiniZinc { 114 115/** 116 * \brief Garbage collected string 117 */ 118class ASTStringO : public ASTChunk { 119protected: 120 /// Constructor 121 ASTStringO(const std::string& s); 122 123public: 124 /// Allocate and initialise as \a s 125 static ASTStringO* a(const std::string& s); 126 /// Return underlying C-style string 127 const char* c_str(void) const { return _data + sizeof(size_t); } 128 /// Conversion to STL string 129 std::string str(void) const { return std::string(c_str()); } 130 /// Return size of string 131 unsigned int size(void) const { 132 return static_cast<unsigned int>(_size) - static_cast<unsigned int>(sizeof(size_t)) - 1; 133 } 134 /// Access character at position \a i 135 char operator[](unsigned int i) { 136 assert(i < size()); 137 return _data[sizeof(size_t) + i]; 138 } 139 /// Return hash value of string 140 size_t hash(void) const { return reinterpret_cast<const size_t*>(_data)[0]; } 141 /// Mark for garbage collection 142 void mark(void) const { _gc_mark = 1; } 143}; 144 145inline ASTString::ASTString(const std::string& s) : _s(ASTStringO::a(s)) {} 146inline ASTString::ASTString(const ASTString& s) : _s(s._s) {} 147inline ASTString& ASTString::operator=(const ASTString& s) { 148 _s = s._s; 149 return *this; 150} 151inline unsigned int ASTString::size(void) const { return _s ? _s->size() : 0; } 152inline const char* ASTString::c_str(void) const { return _s ? _s->c_str() : NULL; } 153inline std::string ASTString::str(void) const { return _s ? _s->str() : std::string(""); } 154inline void ASTString::mark(void) const { 155 if (_s) _s->mark(); 156} 157 158inline bool ASTString::operator==(const ASTString& s) const { 159 return size() == s.size() && (size() == 0 || strncmp(_s->c_str(), s._s->c_str(), size()) == 0); 160} 161inline bool ASTString::operator!=(const ASTString& s) const { return !(*this == s); } 162inline bool ASTString::operator==(const std::string& s) const { 163 return size() == s.size() && (size() == 0 || strncmp(_s->c_str(), s.c_str(), size()) == 0); 164} 165inline bool ASTString::operator!=(const std::string& s) const { return !(*this == s); } 166inline bool ASTString::endsWith(const std::string& s) const { 167 return size() >= s.size() && 168 (size() == 0 || strncmp(_s->c_str() + size() - s.size(), s.c_str(), s.size()) == 0); 169} 170inline bool ASTString::beginsWith(const std::string& s) const { 171 return size() >= s.size() && (size() == 0 || strncmp(_s->c_str(), s.c_str(), s.size()) == 0); 172} 173 174inline size_t ASTString::hash(void) const { return _s ? _s->hash() : 0; } 175 176} // namespace MiniZinc 177 178namespace std { 179inline size_t hash<MiniZinc::ASTString>::operator()(const MiniZinc::ASTString& s) const { 180 return s.hash(); 181} 182} // namespace std 183 184namespace std { 185inline bool equal_to<MiniZinc::ASTString>::operator()(const MiniZinc::ASTString& s0, 186 const MiniZinc::ASTString& s1) const { 187 return s0 == s1; 188} 189} // namespace std 190 191#endif