/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * Guido Tack * * Copyright: * Christian Schulte, 2003 * Guido Tack, 2004 * * Last modified: * $Date: 2007-11-13 21:33:44 +0100 (Tue, 13 Nov 2007) $ by $Author: schulte $ * $Revision: 5290 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ #include #include namespace Gecode { /** * \brief Shared array with arbitrary number of elements * * Sharing is implemented by reference counting: the same elements * are shared among several objects. * */ template class SharedArray : public SharedHandle { protected: /// Implementation of object for shared arrays class SAO : public SharedHandle::Object { private: /// Elements T* a; /// Number of elements int n; public: /// Allocate for \a n elements SAO(int n); /// Create copy of elements virtual SharedHandle::Object* copy(void) const; /// Delete object virtual ~SAO(void); /// Access element at position \a i T& operator[](int i); /// Access element at position \a i const T& operator[](int i) const; /// Return number of elements int size(void) const; }; public: /** * \brief Construct as not yet intialized * * The only member functions that can be used on a constructed but not * yet initialized shared array is init and the assignment operator. * */ SharedArray(void); /// Initialize as array with \a n elements SharedArray(int n); /** * \brief Initialize as array with \a n elements * * This member function can only be used once and only if the shared * array has been constructed with the default constructor. * */ void init(int n); /// Initialize from shared array \a a (share elements) SharedArray(const SharedArray& a); /// Access element at position \a i T& operator[](int i); /// Access element at position \a i const T& operator[](int i) const; /// Return number of elements int size(void) const; /// Return specification for reflection Reflection::Arg* spec(Reflection::VarMap& vm) const; /// Recreate from specification SharedArray(Reflection::VarMap& vm, Reflection::Arg* arg); }; } /** * \brief Print array elements enclosed in curly brackets * \relates SharedArray */ template std::ostream& operator<<(std::ostream& os, const Gecode::SharedArray& x); namespace Gecode { /* * Implementation * */ /* * Shared arrays * */ template forceinline SharedArray::SAO::SAO(int n0) : n(n0) { a = (n>0) ? static_cast(Memory::malloc(sizeof(T)*n)) : NULL; } template SharedHandle::Object* SharedArray::SAO::copy(void) const { SAO* o = new SAO(n); for (int i=n; i--;) new (&(o->a[i])) T(a[i]); return o; } template SharedArray::SAO::~SAO(void) { if (n>0) { for (int i=n; i--;) a[i].~T(); Memory::free(a); } } template forceinline T& SharedArray::SAO::operator[](int i) { assert((i>=0) && (i forceinline const T& SharedArray::SAO::operator[](int i) const { assert((i>=0) && (i forceinline int SharedArray::SAO::size(void) const { return n; } template forceinline SharedArray::SharedArray(void) {} template forceinline SharedArray::SharedArray(int n) : SharedHandle(new SAO(n)) {} template forceinline SharedArray::SharedArray(const SharedArray& sa) : SharedHandle(sa) {} template forceinline void SharedArray::init(int n) { assert(object() == NULL); object(new SAO(n)); } template forceinline T& SharedArray::operator[](int i) { assert(object() != NULL); return (*static_cast(object()))[i]; } template forceinline const T& SharedArray::operator[](int i) const { assert(object() != NULL); return (*static_cast(object()))[i]; } template forceinline int SharedArray::size(void) const { assert(object() != NULL); return static_cast(object())->size(); } /// Serialization helper for SharedArray objects template class SharedArraySerialization { public: static Reflection::Arg* t(const T& t) { return NULL; } static T t(Reflection::Arg* arg) { T tt; return tt; } }; /// Serialization helper for SharedArray objects template <> class SharedArraySerialization { public: static Reflection::Arg* t(const int& t) { return Reflection::Arg::newInt(t); } static int t(Reflection::Arg* arg) { return arg->toInt(); } }; template Reflection::Arg* SharedArray::spec(Reflection::VarMap& vm) const { int sharedIndex = vm.getSharedIndex(object()); if (sharedIndex >= 0) return Reflection::Arg::newSharedReference(sharedIndex); Reflection::ArrayArg* a = Reflection::Arg::newArray(size()); for (int i=size(); i--; ) (*a)[i] = SharedArraySerialization::t((*this)[i]); vm.putMasterObject(object()); return Reflection::Arg::newSharedObject(a); } template SharedArray::SharedArray(Reflection::VarMap& vm, Reflection::Arg* arg) { if (arg->isSharedReference()) { object(static_cast(vm.getSharedObject(arg->toSharedReference()))); } else { Reflection::ArrayArg* a = arg->toSharedObject()->toArray(); object(new SAO(a->size())); for (int i=a->size(); i--; ) { (*this)[i] = SharedArraySerialization::t((*a)[i]); } vm.putMasterObject(object()); } } } template std::ostream& operator<<(std::ostream& os, const Gecode::SharedArray& x) { os << '{'; if (x.size() > 0) { os << x[0]; for (int i=1; i