shared-array.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
* Guido Tack <tack@gecode.org>
*
* Contributing authors:
* Gregory Crosswhite <gcross@phys.washington.edu>
*
* Copyright:
* Gregory Crosswhite, 2011
* Christian Schulte, 2003
* Guido Tack, 2004
*
* 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 <iostream>
#include <sstream>
namespace Gecode {
template<class T>
class SharedArray : public SharedHandle {
protected:
class SAO : public SharedHandle::Object {
private:
T* a;
int n;
public:
SAO(int n);
virtual ~SAO(void);
T& operator [](int i);
const T& operator [](int i) const;
int size(void) const;
T* begin(void);
const T* begin(void) const;
T* end(void);
const T* end(void) const;
};
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef T* iterator;
typedef const T* const_iterator;
typedef std::reverse_iterator<T*> reverse_iterator;
typedef std::reverse_iterator<const T*> const_reverse_iterator;
SharedArray(void);
SharedArray(int n);
void init(int n);
SharedArray(const SharedArray& a);
SharedArray(const ArgArrayBase<T>& a);
T& operator [](int i);
const T& operator [](int i) const;
int size(void) const;
bool operator ==(const SharedArray<T>& sa) const;
iterator begin(void);
const_iterator begin(void) const;
iterator end(void);
const_iterator end(void) const;
reverse_iterator rbegin(void);
const_reverse_iterator rbegin(void) const;
reverse_iterator rend(void);
const_reverse_iterator rend(void) const;
};
template<class Char, class Traits, class T>
std::basic_ostream<Char,Traits>&
operator <<(std::basic_ostream<Char,Traits>& os,
const SharedArray<T>& x);
/*
* Implementation
*
*/
/*
* Shared arrays
*
*/
template<class T>
forceinline
SharedArray<T>::SAO::SAO(int n0) : n(n0) {
a = (n>0) ? heap.alloc<T>(n) : NULL;
}
template<class T>
SharedArray<T>::SAO::~SAO(void) {
if (n>0) {
heap.free<T>(a,n);
}
}
template<class T>
forceinline T&
SharedArray<T>::SAO::operator [](int i) {
assert((i>=0) && (i<n));
return a[i];
}
template<class T>
forceinline const T&
SharedArray<T>::SAO::operator [](int i) const {
assert((i>=0) && (i<n));
return a[i];
}
template<class T>
forceinline int
SharedArray<T>::SAO::size(void) const {
return n;
}
template<class T>
forceinline T*
SharedArray<T>::SAO::begin(void) {
return a;
}
template<class T>
forceinline const T*
SharedArray<T>::SAO::begin(void) const {
return a;
}
template<class T>
forceinline T*
SharedArray<T>::SAO::end(void) {
return a+n;
}
template<class T>
forceinline const T*
SharedArray<T>::SAO::end(void) const {
return a+n;
}
template<class T>
forceinline
SharedArray<T>::SharedArray(void) {}
template<class T>
forceinline
SharedArray<T>::SharedArray(int n)
: SharedHandle(new SAO(n)) {}
template<class T>
forceinline
SharedArray<T>::SharedArray(const SharedArray<T>& sa)
: SharedHandle(sa) {}
template<class T>
forceinline void
SharedArray<T>::init(int n) {
assert(object() == NULL);
object(new SAO(n));
}
template<class T>
forceinline T&
SharedArray<T>::operator [](int i) {
assert(object() != NULL);
return (*static_cast<SAO*>(object()))[i];
}
template<class T>
forceinline const T&
SharedArray<T>::operator [](int i) const {
assert(object() != NULL);
return (*static_cast<SAO*>(object()))[i];
}
template<class T>
inline bool
SharedArray<T>::operator ==(const SharedArray<T>& sa) const {
if (size() != sa.size())
return false;
if (object()==sa.object())
return true;
for (int i=0; i<size(); i++) {
if ((*this)[i] != sa[i])
return false;
}
return true;
}
template<class T>
forceinline
SharedArray<T>::SharedArray(const ArgArrayBase<T>& a)
: SharedHandle(new SAO(a.size())) {
for (int i=0; i<a.size(); i++)
operator [](i)=a[i];
}
template<class T>
forceinline int
SharedArray<T>::size(void) const {
assert(object() != NULL);
return static_cast<SAO*>(object())->size();
}
template<class T>
forceinline typename SharedArray<T>::iterator
SharedArray<T>::begin(void) {
assert(object() != NULL);
return static_cast<SAO*>(object())->begin();
}
template<class T>
forceinline typename SharedArray<T>::const_iterator
SharedArray<T>::begin(void) const {
assert(object() != NULL);
return static_cast<SAO*>(object())->begin();
}
template<class T>
forceinline typename SharedArray<T>::iterator
SharedArray<T>::end(void) {
assert(object() != NULL);
return static_cast<SAO*>(object())->end();
}
template<class T>
forceinline typename SharedArray<T>::const_iterator
SharedArray<T>::end(void) const {
assert(object() != NULL);
return static_cast<SAO*>(object())->end();
}
template<class T>
forceinline typename SharedArray<T>::reverse_iterator
SharedArray<T>::rbegin(void) {
assert(object() != NULL);
return reverse_iterator(static_cast<SAO*>(object())->end());
}
template<class T>
forceinline typename SharedArray<T>::const_reverse_iterator
SharedArray<T>::rbegin(void) const {
assert(object() != NULL);
return const_reverse_iterator(static_cast<SAO*>(object())->end());
}
template<class T>
forceinline typename SharedArray<T>::reverse_iterator
SharedArray<T>::rend(void) {
assert(object() != NULL);
return reverse_iterator(static_cast<SAO*>(object())->begin());
}
template<class T>
forceinline typename SharedArray<T>::const_reverse_iterator
SharedArray<T>::rend(void) const {
assert(object() != NULL);
return const_reverse_iterator(static_cast<SAO*>(object())->begin());
}
template<class Char, class Traits, class T>
std::basic_ostream<Char,Traits>&
operator <<(std::basic_ostream<Char,Traits>& os,
const SharedArray<T>& x) {
std::basic_ostringstream<Char,Traits> s;
s.copyfmt(os); s.width(0);
s << '{';
if (x.size() > 0) {
s << x[0];
for (int i=1; i<x.size(); i++)
s << ", " << x[i];
}
s << '}';
return os << s.str();
}
}
// STATISTICS: kernel-other
Gecode::ArgArrayBase
Base-class for argument arrays.
Definition array.hpp:537
Gecode::ArgArrayBase::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1607
Gecode::FloatVal::size
FloatNum size(void) const
Return size of float value (distance between maximum and minimum).
Definition val.hpp:78
Gecode::SharedArray::SAO
Implementation of object for shared arrays.
Definition shared-array.hpp:56
Gecode::SharedArray::SAO::end
T * end(void)
Return end of array (for iterators).
Definition shared-array.hpp:229
Gecode::SharedArray::SAO::begin
T * begin(void)
Return beginning of array (for iterators).
Definition shared-array.hpp:217
Gecode::SharedArray::SAO::SAO
SAO(int n)
Allocate for n elements.
Definition shared-array.hpp:184
Gecode::SharedArray::SAO::size
int size(void) const
Return number of elements.
Definition shared-array.hpp:211
Gecode::SharedArray::SAO::operator[]
T & operator[](int i)
Access element at position i.
Definition shared-array.hpp:197
Gecode::SharedArray::SAO::~SAO
virtual ~SAO(void)
Delete object.
Definition shared-array.hpp:189
Gecode::SharedArray
Shared array with arbitrary number of elements.
Definition shared-array.hpp:53
Gecode::SharedArray::begin
const_iterator begin(void) const
Return a read-only iterator at the beginning of the array.
Definition shared-array.hpp:313
Gecode::SharedArray::rend
const_reverse_iterator rend(void) const
Return a reverse and read-only iterator past the beginning of the array.
Definition shared-array.hpp:355
Gecode::SharedArray< int >::const_reverse_iterator
std::reverse_iterator< const int * > const_reverse_iterator
Definition shared-array.hpp:105
Gecode::SharedArray::rend
reverse_iterator rend(void)
Return a reverse iterator past the beginning of the array.
Definition shared-array.hpp:348
Gecode::SharedArray::end
const_iterator end(void) const
Return a read-only iterator past the end of the array.
Definition shared-array.hpp:327
Gecode::SharedArray::operator==
bool operator==(const SharedArray< T > &sa) const
Test equality with sa.
Definition shared-array.hpp:277
Gecode::SharedArray::SharedArray
SharedArray(const ArgArrayBase< T > &a)
Initialize from argument array a.
Definition shared-array.hpp:291
Gecode::SharedArray::SharedArray
SharedArray(int n)
Initialize as array with n elements.
Definition shared-array.hpp:246
Gecode::SharedArray::rbegin
reverse_iterator rbegin(void)
Return a reverse iterator at the end of the array.
Definition shared-array.hpp:334
Gecode::SharedArray::begin
iterator begin(void)
Return an iterator at the beginning of the array.
Definition shared-array.hpp:306
Gecode::SharedArray::end
iterator end(void)
Return an iterator past the end of the array.
Definition shared-array.hpp:320
Gecode::SharedArray< int >::pointer
int * pointer
Definition shared-array.hpp:95
Gecode::SharedArray::SharedArray
SharedArray(const SharedArray &a)
Initialize from shared array a (share elements).
Definition shared-array.hpp:251
Gecode::SharedArray< int >::const_pointer
const int * const_pointer
Definition shared-array.hpp:97
Gecode::SharedArray< int >::const_iterator
const int * const_iterator
Definition shared-array.hpp:101
Gecode::SharedArray< int >::reverse_iterator
std::reverse_iterator< int * > reverse_iterator
Definition shared-array.hpp:103
Gecode::SharedArray< int >::value_type
int value_type
Definition shared-array.hpp:89
Gecode::SharedArray::SharedArray
SharedArray(void)
Construct as not yet intialized.
Definition shared-array.hpp:242
Gecode::SharedArray::size
int size(void) const
Return number of elements.
Definition shared-array.hpp:299
Gecode::SharedArray< int >::iterator
int * iterator
Definition shared-array.hpp:99
Gecode::SharedArray< int >::const_reference
const int & const_reference
Definition shared-array.hpp:93
Gecode::SharedArray::rbegin
const_reverse_iterator rbegin(void) const
Return a reverse and read-only iterator at the end of the array.
Definition shared-array.hpp:341
Gecode::SharedArray< int >::reference
int & reference
Definition shared-array.hpp:91
Gecode::SharedArray::operator[]
T & operator[](int i)
Access element at position i.
Definition shared-array.hpp:263
Gecode::SharedArray::init
void init(int n)
Initialize as array with n elements.
Definition shared-array.hpp:256
Gecode::SharedHandle::Object
The shared object.
Definition shared-object.hpp:53
Gecode::SharedHandle::SharedHandle
SharedHandle(void)
Create shared handle with no object pointing to.
Definition shared-object.hpp:122
Gecode::SharedHandle::object
SharedHandle::Object * object(void) const
Access to the shared object.
Definition shared-object.hpp:102
Gecode::heap
Heap heap
The single global heap.
Definition heap.cpp:44
Gecode
Gecode toplevel namespace
Gecode::operator<<
Archive & operator<<(Archive &e, FloatNumBranch nl)
Definition val-sel.hpp:39
forceinline
#define forceinline
Definition config.hpp:187