Permalink
Browse files

Add copy constructors to fix assignment operator.

  • Loading branch information...
1 parent 3685dd4 commit 46c55fa03aa8ed0e81a6ef01d8031ec469a2693f @cfobel committed Apr 25, 2012
Showing with 59 additions and 6 deletions.
  1. +51 −0 blitz_array_slices.hpp
  2. +8 −6 test_blitz_array_slices.cpp
View
@@ -14,6 +14,8 @@ class ArraySlices {
std::vector<Array<T, 1> > blitz_slices_;
Array<T, 1> slice_sizes_;
+ ArraySlices() {}
+
ArraySlices(Array<T, 1> blitz_array)
: blitz_array_(blitz_array) {
slice_by_step_size(blitz_array.size());
@@ -29,6 +31,29 @@ class ArraySlices {
slice_by_sizes(slice_sizes);
}
+ ArraySlices(ArraySlices const &other)
+ : blitz_array_(other) {
+ Array<int, 1> slice_sizes(other.blitz_slices_.size());
+ for(int i = 0; i < slice_sizes.size(); i++) {
+ slice_sizes(i) = other.blitz_slices_[i].size();
+ }
+ slice_by_sizes(slice_sizes);
+ }
+
+ ArraySlices &operator=(ArraySlices const &other) {
+ // protect against invalid self-assignment
+ if(this != &other) {
+ this->blitz_array_.reference(other.blitz_array_);
+ Array<int, 1> slice_sizes(other.blitz_slices_.size());
+ for(int i = 0; i < slice_sizes.size(); i++) {
+ slice_sizes(i) = other.blitz_slices_[i].size();
+ }
+ this->slice_by_sizes(slice_sizes);
+ }
+ // by convention, always return *this
+ return *this;
+ }
+
void slice_by_sizes(Array<int, 1> const &slice_sizes) {
assert(blitz::sum(slice_sizes) == blitz_array_.size());
int running_count = 0;
@@ -124,6 +149,8 @@ class SortableArraySlices : public ArraySlices<T> {
}
}
+ SortableArraySlices() {}
+
SortableArraySlices(Array<T, 1> blitz_array) : ArraySlices<T>(blitz_array) {
set_default_slice_order();
}
@@ -157,6 +184,30 @@ class SortableArraySlices : public ArraySlices<T> {
}
this->reorder(slice_order_);
}
+
+ SortableArraySlices(SortableArraySlices const &other)
+ : ArraySlices<T>(other), slice_order_(other.slice_order_),
+ pre_sort_slice_order_(other.pre_sort_slice_order_) {}
+
+ SortableArraySlices &operator=(SortableArraySlices const &other) {
+ // protect against invalid self-assignment
+ if(this != &other) {
+ this->blitz_array_.reference(other.blitz_array_);
+ Array<int, 1> slice_sizes(other.blitz_slices_.size());
+ for(int i = 0; i < slice_sizes.size(); i++) {
+ slice_sizes(i) = other.blitz_slices_[i].size();
+ }
+ this->slice_by_sizes(slice_sizes);
+ this->slice_order_ = other.slice_order_;
+ this->pre_sort_slice_order_ = other.pre_sort_slice_order_;
+ }
+ // by convention, always return *this
+ return *this;
+ }
+
+
+
+
};
}
@@ -16,8 +16,7 @@ int main(int argc, char** argv) {
duration_map_t duration_map;
timespec start, end, delta;
- if(argc != 3) {
- cerr << _f("usage: %s <array size> <step size>") % argv[0] << endl;
+ if(argc != 3) { cerr << _f("usage: %s <array size> <step size>") % argv[0] << endl;
exit(-1);
}
const int N = atoi(argv[1]);
@@ -39,17 +38,19 @@ int main(int argc, char** argv) {
cout << test.blitz_slices_[i] << endl;
}
- Array<int, 1> slice_sizes((float)N / step_size);
+ Array<int, 1> slice_sizes((float)N / step_size + 1);
slice_sizes = 0;
- for(int i = 0; i < slice_sizes.size(); i++) {
+ for(int i = 0; i < slice_sizes.size() - 1; i++) {
int slice_size = rand() % step_size + 1;
slice_sizes(i) = slice_size;
}
int slice_total = blitz::sum(slice_sizes);
if(slice_total < N) {
- slice_sizes(slice_sizes.size() - 1) += N - slice_total;
+ slice_sizes(slice_sizes.size() - 2) += N - slice_total;
}
+ /* Add an empty slice to show they can be handled. */
+ slice_sizes(slice_sizes.size() - 1) = 0;
ArraySlices<int> test2(test_array.copy(), slice_sizes);
@@ -75,7 +76,8 @@ int main(int argc, char** argv) {
cout << test.blitz_slices_[i] << endl;
}
- SortableArraySlices<int> sortable_test(test_array.copy(), slice_sizes);
+ SortableArraySlices<int> sortable_test;
+ sortable_test = SortableArraySlices<int>(test_array.copy(), slice_sizes);
sortable_test.sort();
cout << "Sorted ascending array:" << endl;
cout << sortable_test.blitz_array_ << endl;

0 comments on commit 46c55fa

Please sign in to comment.