vector< IteratorSeq<T>* > ParallelArrayRDD<T>::slice(IteratorSeq<T> *seq) { vector< IteratorSeq<T>* > slices; if (numSlices < 1) { // logError("ParallelArray: slice number should be positive integer!"); return slices; } long seqSize = seq->size(); long num_group = seqSize / numSlices; if (seq->getType() == 0) { T step = seq->at(1) - seq->at(0); for (int i = 0; i < numSlices - 1; i++) { T start = seq->at(0) + i * num_group * step; T end = start + (num_group - 1) * step; IteratorSeq<T> *it = new RangeIteratorSeq<T>(start, end , step); slices.push_back(it); } T last_start = seq->at(0) + (numSlices - 1) * num_group * step; T end = seq->at(seqSize-1); IteratorSeq<T> *last = new RangeIteratorSeq<T>(last_start, end, step); slices.push_back(last); } else { // type == 1 for (int i = 0; i < numSlices - 1; i++) { vector<T> group; VectorIteratorSeq<T> *it = new VectorIteratorSeq<T>(group); for (long j = 0; j < num_group; j++) { it->push_back(seq->at(i * num_group + j)); } slices.push_back(it); } vector<T> last; VectorIteratorSeq<T> *it = new VectorIteratorSeq<T>(last); for (size_t i = (numSlices - 1) * num_group; i < seq->size(); i++) { it->push_back(seq->at(i)); } slices.push_back(it); } return slices; }
template <class U> IteratorSeq<U> * IteratorSeq<T>::flatMap(vector<U> (*f)(T&)) { VectorIteratorSeq<U> *ret = new VectorIteratorSeq<U>(); for(size_t i = 0; i < size(); i++) { T t = this->at(i); vector<U> u = f(t); ret->push_back(u); } return ret; }