00001 #include "SimObjectIDSortedVector.h" 00002 00003 #include <algorithm> 00004 #include <numeric> 00005 00006 using std::transform; 00007 using std::sort; 00008 using std::find_if; 00009 using std::partial_sum; 00010 00011 00012 class SimObjectIDLessThan { 00013 public: 00014 SimObjectIDLessThan(const vector<SimObject::ID::Packed> & vec ) 00015 : vec(vec) 00016 {}; 00017 00018 bool operator()(const unsigned idx1,const unsigned idx2) 00019 { 00020 if (((SimObject::ID)vec[idx1]).node == ((SimObject::ID)vec[idx2]).node) { 00021 if (((SimObject::ID)vec[idx1]).eng == ((SimObject::ID)vec[idx2]).eng) { 00022 if (((SimObject::ID)vec[idx1]).type == ((SimObject::ID)vec[idx2]).type) { 00023 return ((SimObject::ID)vec[idx1]).localid < ((SimObject::ID)vec[idx2]).localid; 00024 } 00025 return ((SimObject::ID)vec[idx1]).type < ((SimObject::ID)vec[idx2]).type; 00026 } 00027 return ((SimObject::ID)vec[idx1]).eng < ((SimObject::ID)vec[idx2]).eng; 00028 } 00029 return ((SimObject::ID)vec[idx1]).node < ((SimObject::ID)vec[idx2]).node; 00030 }; 00031 00032 protected: 00033 const vector<SimObject::ID::Packed> & vec; 00034 }; 00035 00036 00037 class IDhasNode { 00038 public: 00039 IDhasNode(nodeid_t localnode, vector< SimObject::ID::Packed > vec, bool predicate_value) : 00040 predicate_value(predicate_value), localnode(localnode), vec(vec) {}; 00041 00042 bool operator() (const unsigned idx) { 00043 if (predicate_value) 00044 return ((SimObject::ID)vec[idx]).node == localnode; 00045 else 00046 return ((SimObject::ID)vec[idx]).node != localnode; 00047 } 00048 00049 protected: 00050 bool predicate_value; 00051 nodeid_t localnode; 00052 vector<SimObject::ID::Packed> &vec; 00053 }; 00054 00055 00056 SimObjectIDSortedVector::SimObjectIDSortedVector(const SimObject::ID::Vector &v, const nodeid_t localnode, bool sorted) 00057 { 00058 init(&(*v), localnode, sorted); 00059 } 00060 00061 00062 SimObjectIDSortedVector::SimObjectIDSortedVector(const vector<SimObject::ID::Packed> &v, const nodeid_t localnode, bool sorted) 00063 { 00064 init(&v,localnode, sorted); 00065 } 00066 00067 void SimObjectIDSortedVector::init(const vector<SimObject::ID::Packed> *v, const nodeid_t localnode, bool sorted) 00068 { 00069 vec = v; 00070 index_vec.resize(v->size(), unsigned(1)); 00071 index_vec[0] = 0; 00072 partial_sum(index_vec.begin(), index_vec.end(), index_vec.begin()); 00073 if (sorted) { 00074 sort(index_vec.begin(), index_vec.end(), SimObjectIDLessThan(*v)); 00075 localids_begin_it = find_if(index_vec.begin(), index_vec.end(), IDhasNode(localnode, *vec, true)); 00076 localids_end_it = find_if(localids_begin_it, index_vec.end(), IDhasNode(localnode, *vec, false)); 00077 begin_it = index_vec.begin(); 00078 } 00079 } 00080 SimObjectIDSortedVector::~SimObjectIDSortedVector() 00081 { 00082 00083 } 00084 00085 SimObjectIDSortedVector::const_iterator SimObjectIDSortedVector::beginBlock() 00086 { 00087 return begin_it; 00088 } 00089 00090 SimObjectIDSortedVector::const_iterator SimObjectIDSortedVector::endBlock() 00091 { 00092 return end_it; 00093 } 00094 00095 void SimObjectIDSortedVector::startBlockIteration() 00096 { 00097 end_it = index_vec.begin(); 00098 } 00099 00100 void SimObjectIDSortedVector::startBlockIterationFromLocal() 00101 { 00102 end_it = localids_end_it; 00103 begin_it = localids_begin_it; 00104 } 00105 00106 00107 nodeid_t SimObjectIDSortedVector::nextBlock() 00108 { 00109 begin_it = end_it; 00110 nodeid_t prev_node = ((SimObject::ID)((*vec)[*end_it])).node; 00111 while (end_it != index_vec.end() && ((SimObject::ID)((*vec)[*end_it])).node == prev_node) 00112 ++end_it; 00113 return ((SimObject::ID)((*vec)[*begin_it])).node; 00114 } 00115 00116 SimObjectIDSortedVector::const_iterator SimObjectIDSortedVector::localIDsBegin() 00117 { 00118 return localids_begin_it; 00119 } 00120 00121 SimObjectIDSortedVector::const_iterator SimObjectIDSortedVector::localIDsEnd() 00122 { 00123 return localids_end_it; 00124 } 00125 00126 00127 bool SimObjectIDSortedVector::hasNextBlock() 00128 { 00129 return end_it != index_vec.end(); 00130 } 00131