32 #ifndef SPL_Array1_hpp
33 #define SPL_Array1_hpp
42 #if defined(SPL_ARRAY1_DEBUG)
43 #define SPL_ARRAY1_INLINE
46 #define SPL_ARRAY1_INLINE inline
54 #include <SPL/config.hpp>
96 template <
class>
friend class Array1;
100 ArrayRep1(
int size,
const T& value);
102 template <
class InputIterator>
103 ArrayRep1(
int size, InputIterator data);
109 ArrayRep1(
const ArrayRep1&);
113 ArrayRep1& operator=(
const ArrayRep1&);
116 ArrayRep1* clone()
const;
119 int getRefCount()
const;
129 std::vector<T> data_;
155 typedef typename std::vector<T>::iterator
Iterator;
172 explicit Array1(
int size);
178 Array1(
int size,
const T& value);
184 template <
class InputIterator>
185 Array1(
int size, InputIterator data);
198 template<
class OtherType>
219 template<
class OtherType>
313 ConstIterator
begin()
const;
325 ConstIterator
end()
const;
351 template <
class InputIterator>
352 void resize(
int size, InputIterator data);
385 std::ostream&
output(std::ostream& out,
int fieldWidth)
const;
390 int load(
const char* fileName);
395 int save(
const char* fileName)
const;
404 void fill(
const T& value = T(0));
415 void dump(std::ostream& out)
const;
419 template <
class>
friend class Array1;
420 typedef ArrayRep1<T> Rep;
423 int getRefCount()
const;
426 void unshare()
const;
430 void copyOnWrite()
const;
457 if (--refCount_ == 0) {
470 SPL_ARRAY1_INLINE ArrayRep1<T>::ArrayRep1(
int size,
const T& value) : data_(size, value),
477 template <
class InputIterator>
479 data_(), refCount_(1)
483 SPL::copy_n(data, size, std::back_inserter(data_));
484 assert(data_.size() == size);
496 return new ArrayRep1(data_.size(), data_.begin());
509 #if defined(SPL_ARRAY1_DEBUG)
510 std::cerr <<
"Array1::default_ctor() "
522 ptr_ =
new Rep(size);
523 #if defined(SPL_ARRAY1_DEBUG)
524 std::cerr <<
"Array1::ctor(" << size <<
") "
538 #if defined(SPL_ARRAY1_DEBUG)
539 std::cerr <<
"Array1::copy_ctor(" << (&a) <<
") "
551 ptr_ =
new Rep(size, value);
552 #if defined(SPL_ARRAY1_DEBUG)
553 std::cerr <<
"Array1::ctor(" << size <<
", " << value <<
") "
563 template <
class InputIterator>
566 ptr_ =
new Rep(size, data);
567 #if defined(SPL_ARRAY1_DEBUG)
568 std::cerr <<
"Array1::ctor(" << size <<
", " <<
"InputIterator" <<
") "
580 #if defined(SPL_ARRAY1_DEBUG)
581 std::cerr <<
"Array1::dtor() "
592 template <
class OtherType>
596 #if defined(SPL_ARRAY1_DEBUG)
597 std::cerr <<
"pseudo_copy_ctor(" <<
this <<
") "
598 <<
this <<
" " << ptr_ <<
"\n";
609 #if defined(SPL_ARRAY1_DEBUG)
610 std::cerr <<
"Array1::operator=(" << (&a) <<
") "
612 <<
this <<
" " << ptr_ <<
" "
613 << (&a) <<
" " << a.ptr_ <<
""
626 template <
class OtherType>
629 #if defined(SPL_ARRAY1_DEBUG)
630 std::cerr <<
"Array1::operator= special\n";
632 if (reinterpret_cast<void*>(
this) != reinterpret_cast<const void*>(&a)) {
635 std::copy(a.ptr_->data_.begin(), a.ptr_->data_.end(),
636 ptr_->data_.begin());
648 assert(getSize() == a.
getSize());
650 std::transform(ptr_->data_.begin(), ptr_->data_.end(),
651 a.ptr_->data_.begin(), ptr_->data_.begin(), std::plus<T>());
658 assert(getSize() == a.
getSize());
660 std::transform(ptr_->data_.begin(), ptr_->data_.end(),
661 a.ptr_->data_.begin(), ptr_->data_.begin(), std::minus<T>());
668 assert(getSize() == a.
getSize());
670 std::transform(ptr_->data_.begin(), ptr_->data_.end(),
671 a.ptr_->data_.begin(), ptr_->data_.begin(), std::multiplies<T>());
678 assert(getSize() == a.
getSize());
680 std::transform(ptr_->data_.begin(), ptr_->data_.end(),
681 a.ptr_->data_.begin(), ptr_->data_.begin(), std::divides<T>());
689 std::transform(ptr_->data_.begin(), ptr_->data_.end(),
690 ptr_->data_.begin(), std::bind2nd(std::plus<T>(), value));
698 std::transform(ptr_->data_.begin(), ptr_->data_.end(),
699 ptr_->data_.begin(), std::bind2nd(std::minus<T>(), value));
707 std::transform(ptr_->data_.begin(), ptr_->data_.end(),
708 ptr_->data_.begin(), std::bind2nd(std::multiplies<T>(), value));
716 std::transform(ptr_->data_.begin(), ptr_->data_.end(),
717 ptr_->data_.begin(), std::bind2nd(std::divides<T>(), value));
728 return ptr_->data_.size();
734 return ptr_->getRefCount() > 1;
740 return ptr_ == a.ptr_;
750 assert(i >= 0 && i < getSize());
752 return ptr_->data_[i];
758 assert(i >= 0 && i < getSize());
759 return ptr_->data_[i];
770 return ptr_->data_.begin();
777 return ptr_->data_.end();
783 return ptr_->data_.begin();
789 return ptr_->data_.end();
800 if (getSize() != size) {
802 ptr_ =
new Rep(size);
807 template <
class InputIterator>
811 if (getSize() == size && ptr_->getRefCount() == 1) {
812 SPL::copy_n(data, size, ptr_->data_.begin());
815 ptr_ =
new Rep(size, data);
826 assert(getSize() > 0);
827 return *std::max_element(begin(), end());
833 assert(getSize() > 0);
834 return *std::min_element(begin(), end());
840 return std::accumulate(begin(), end(), T(0));
850 out << getSize() <<
"\n";
851 for (
int i = 0; i < getSize(); ++i) {
852 T val = operator()(i);
853 std::stringstream str;
854 str << std::setw(fieldWidth) << val;
855 std::string buf = str.str();
856 if (buf.size() > fieldWidth) {
857 buf = buf.substr(0, fieldWidth);
860 if (i < getSize() - 1) {
871 std::ifstream in(fileName);
882 std::ofstream out(fileName);
895 std::ostream& operator<<(std::ostream& out, const Array1<T>& a)
899 for (
int i = 0; i < a.
getSize(); ++i) {
922 for (
int i = 0; i < a.
getSize(); ++i) {
943 std::swap(ptr_, a.ptr_);
951 std::fill(begin(), end(), value);
983 <<
"refCount=" << ptr_->getRefCount() <<
" "
984 <<
"size=" << getSize() <<
"\n";
994 #if defined(SPL_ARRAY1_DEBUG)
995 std::cerr <<
"copyOnWrite "
999 << ptr_->getRefCount()
1002 std::cerr <<
"copied array size " << getSize() <<
"\n";
1004 assert(ptr_->getRefCount() > 1);
1007 this->ptr_ = ptr_->clone();
1013 if (ptr_->getRefCount() > 1) {
T max() const
Get the maximum of the elements in the array.
Definition: Array1.hpp:824
bool operator==(const Array1< T > &a, const Array1< T > &b)
Test two arrays for equality.
Definition: Array1.hpp:958
Array1()
Create an empty array.
Definition: Array1.hpp:506
This file contains miscellaneous code.
ConstIterator begin() const
Get a const iterator referring to the first element in the array.
Definition: Array1.hpp:781
SPL_ARRAY1_INLINE bool operator!=(const Array1< T > &a, const Array1< T > &b)
Test two arrays for inequality.
Definition: Array1.hpp:974
void fill(const T &value=T(0))
Set all elements in the array to the specified value.
Definition: Array1.hpp:948
Definition: Arcball.hpp:48
int save(const char *fileName) const
Save an array to the file with the specified name.
Definition: Array1.hpp:880
~Array1()
Destroy an array.
Definition: Array1.hpp:578
Array1< double > RealArray1
A one-dimensional array with real elements.
Definition: Array1.hpp:1023
std::vector< T >::const_iterator ConstIterator
A constant iterator for the array elements.
Definition: Array1.hpp:158
Array1 & operator/=(const Array1 &a)
Divide this array (elementwise) by another array.
Definition: Array1.hpp:676
std::istream & operator>>(std::istream &in, Array1< T > &a)
Input an array from the specified stream.
Definition: Array1.hpp:914
T sum() const
Get the sum of the elements in the array.
Definition: Array1.hpp:838
A one-dimensional array class with lazy copying and reference counting.
Definition: Array1.hpp:83
void dump(std::ostream &out) const
Output information about an array to a stream for debugging.
Definition: Array1.hpp:980
Array1< int > IntArray1
A one-dimensional array with integer elements.
Definition: Array1.hpp:1026
T & operator()(int i)
Get a mutable reference to the specified element in the array.
Definition: Array1.hpp:748
T ElemType
The type of the elements in the array.
Definition: Array1.hpp:152
std::ostream & output(std::ostream &out, int fieldWidth) const
Output an array to a stream with a particular field width to be used for each element.
Definition: Array1.hpp:848
Array1 & operator=(const Array1 &a)
Assign one array to another.
Definition: Array1.hpp:607
Array1 & operator-=(const Array1 &a)
Subtract another array (elementwise) from this array.
Definition: Array1.hpp:656
T min() const
Get the minimum of the elements in the array.
Definition: Array1.hpp:831
int getSize() const
Get the number of elements in the array.
Definition: Array1.hpp:726
bool isShared() const
Is the data for this array shared with another array?
Definition: Array1.hpp:732
Array1 & operator+=(const Array1 &a)
Add another array (elementwise) to this array.
Definition: Array1.hpp:646
void resize(int size)
Change the size of the array.
Definition: Array1.hpp:797
#define SPL_ARRAY1_INLINE
Defining this symbol will enable extra code for debugging.
Definition: Array1.hpp:47
bool isSharedWith(const Array1 &a) const
Is the data for this array shared with the specified array?
Definition: Array1.hpp:738
void swap(Array1 &a)
Swap the contents of the array with the contents of another array.
Definition: Array1.hpp:938
std::vector< T >::iterator Iterator
A mutable iterator for the array elements.
Definition: Array1.hpp:155
int load(const char *fileName)
Load an array from the file with the specified name.
Definition: Array1.hpp:869
Array1 & operator*=(const Array1 &a)
Multiply another array (elementwise) by this array.
Definition: Array1.hpp:666
ConstIterator end() const
Get a const iterator referring to one past the last element in the array.
Definition: Array1.hpp:787