int32 Vector::scan(char* key, uint32 sz, uint32* index, Comparator* c) { Comparator* cUse = NULL; int32 ret = 0; if (!c && !m_comp) return FAILURE; if (!c) cUse = m_comp; else cUse = c; for (uint32 j = 0; j < getCount(); j++) { VectorSlot* vs = (VectorSlot*) m_pvDB[j]; if (vs) { ret = cUse->compare(vs->dbData.toString(), vs->dbData.getCount(), key, sz); if (ret == SUCCESS) { if (index) *index = j; return SUCCESS; } } } return FAILURE; }
int partition(vector<string> &nuts, vector<string> &bolts, int s, int e, Comparator compare) { for(int i = s; i <= e; i++) { if(compare.cmp(nuts[s], bolts[i]) == 0) { swap(bolts[s], bolts[i]); break; } } int p = s+1, i = s+1; while(i <= e) { if(compare.cmp(nuts[s], bolts[i]) == 1) { swap(bolts[p], bolts[i]); p++; } i++; } swap(bolts[s], bolts[--p]); swap(nuts[s], nuts[p]); i = s; int j = p+1; while(i <= p-1 && j <= e) { if(compare.cmp(nuts[i], bolts[p]) == 1) { swap(nuts[i], nuts[j]); j++; } else { i++; } } return p; }
int main (void) { //int x = 123, y = 456; double x = 1.23, y = 4.56; //Comparator<int> comp (x, y); Comparator<double> comp (x, y); cout << comp.max () << ' ' << comp.min () << endl; cout << ::max (x, y) << ' ' << ::min (x, y) << endl; return 0; }
/** * Compares two solutions. * @param solution1 Object representing the first <code>Solution</code>. * @param solution2 Object representing the second <code>Solution</code>. * @return -1, or 0, or 1, or 2 if solution1 is dominates solution2, solution1 * and solution2 are equals, or solution1 is greater than solution2, * respectively. */ int EqualSolutions::compare(Solution * solution1, Solution * solution2) { if (solution1==NULL) return 1; else if (solution2 == NULL) return -1; int dominate1 ; // dominate1 indicates if some objective of solution1 // dominates the same objective in solution2. dominate2 int dominate2 ; // is the complementary of dominate1. dominate1 = 0 ; dominate2 = 0 ; int flag; double value1, value2; for (int i = 0; i < solution1->getNumberOfObjectives(); i++) { Comparator * c = new ObjectiveComparator(i); flag = c->compare(solution1,solution2); delete c; value1 = solution1->getObjective(i); value2 = solution2->getObjective(i); if (value1 < value2) { flag = -1; } else if (value1 > value2) { flag = 1; } else { flag = 0; } if (flag == -1) { dominate1 = 1; } if (flag == 1) { dominate2 = 1; } } if (dominate1== 0 && dominate2 ==0) { return 0; //No one dominate the other } if (dominate1 == 1) { return -1; // solution1 dominate } else if (dominate2 == 1) { return 1; // solution2 dominate } return 2; } // compare
int main(){ Comparator comp(3,4); Comparator comp1(25.1f,45.6f); Comparator comp2('v','c'); Comparator comp3("happy","sad"); cout<< "\nUse of copy constructor" << endl; cout << "-----------------------" << endl; Comparator compCopy = comp; compCopy.check(); return 0; }
void sort(vector<string> &nuts, vector<string> &bolts, int left, int right, Comparator compare) { if (left >= right) return; fprintf(stderr, "%d %d ", left, right); int ni, nj, bi, bj, m; ni = bi = left; nj = bj = right; m = (left + right) >> 1; // recover bolts int bp = 0; while (bi < bj) { while (bi < bj && compare.cmp(nuts[m], bolts[bi]) < 0) bi ++; while (bi < bj && compare.cmp(nuts[m], bolts[bj]) > 0) bj --; if (compare.cmp(nuts[m], bolts[bi]) == 0) bp = bj; else if (compare.cmp(nuts[m], bolts[bj]) == 0) bp = bi; if (bi < bj) { swap(bolts, bi, bj); bi ++; bj --; } // print(bolts); } // printf("%d %d\n", bi, bp); while (compare.cmp(nuts[m], bolts[bi]) < 0) bi ++; fprintf(stderr, "%d %d ", bi, bp); if (bp < bi) { swap(bolts, bp, bi - 1); bp = bi - 1; } else if (bp > bi) { swap(bolts, bp, bi); bp = bi; } // print(bolts); // recover nuts swap(nuts, m, bp); m = bp; int np = 0; while (ni < m) { while (ni < m && compare.cmp(nuts[ni], bolts[m]) > 0) ni ++; while (ni < m && compare.cmp(nuts[nj], bolts[m]) < 0) nj --; if (ni < m) { swap(nuts, ni, nj); ni ++; nj --; } } // print(nuts); fprintf(stderr, "%d|", m); sort(nuts, bolts, left, m - 1, compare); sort(nuts, bolts, m + 1, right, compare); }
/* template<typename T> Comparator<T>::Comparator (T x, T y) : m_x (x), m_y (y) {} template<typename T> T Comparator<T>::max (void) const { return m_x < m_y ? m_y : m_x; } template<typename T> T Comparator<T>::min (void) const { return m_x < m_y ? m_x : m_y; } */ int main (void) { Comparator<int> ci (123, 456); cout << ci.max() << ' ' << ci.min() << endl; Comparator<double> cd (1.23, 4.56); cout << cd.max() << ' ' << cd.min() << endl; Comparator<string> cs ("hello", "world"); cout << cs.max() << ' ' << cs.min() << endl; Comparator<char const*> cp("hello","world"); cout << cp.max() << ' ' << cp.min() << endl; return 0; }
void ContainerTest::outputExpectedSmaller() { std::stringstream out; std::vector<int> a{1, 2, 3, 4}; std::vector<int> b{1, 2, 3}; { Error e(&out); Comparator<Compare::Container<std::vector<int>>> compare; CORRADE_VERIFY(!compare(a, b)); compare.printErrorMessage(e, "a", "b"); } CORRADE_COMPARE(out.str(), "Containers a and b have different size, actual 4 but 3 expected. Actual has 4 on position 3.\n"); }
void ContainerTest::output() { std::stringstream out; std::vector<int> a{1, 9, 3, 4}; std::vector<int> b{1, 2, 3, 4}; { Error e(&out); Comparator<Compare::Container<std::vector<int>>> compare; CORRADE_VERIFY(!compare(a, b)); compare.printErrorMessage(e, "a", "b"); } CORRADE_COMPARE(out.str(), "Containers a and b have different contents. Actual 9 but 2 expected on position 1.\n"); }
void sort_template(xptr_sequence *xs, int off, int len) { Comparator comp; // Insertion sort on smallest arrays if (len < 7) { for (int i=off; i<len+off; i++) for (int j=i; j>off && comp.on_less(xs, j,j-1)<0; j--) xs->swap(j, j-1); return; } // Choose a partition element, v int m = off + (len >> 1); // Small arrays, middle element if (len > 7) { int l = off; int n = off + len - 1; if (len > 40) { // Big arrays, pseudomedian of 9 int s = len/8; l = comp.med3(xs, l, l+s, l+2*s); m = comp.med3(xs, m-s, m, m+s); n = comp.med3(xs, n-2*s, n-s, n); } m = comp.med3(xs, l, m, n); // Mid-size, med of 3 } xptr v = xs->get(m); // Establish Invariant: v* (<v)* (>v)* v* int a = off, b = a, c = off + len - 1, d = c; while(true) { while (b <= c && comp.on_less_lt(xs, b, v)<=0) { if (xs->get(b) == v) xs->swap(a++, b); b++; } while (c >= b && comp.on_less_rt(xs,v,c)<=0) { if (xs->get(c) == v) xs->swap(c, d--); c--; } if (b > c) break; xs->swap(b++, c--); } // Swap partition elements back to middle int s, n = off + len; s = s_min(a-off, b-a ); xs->vecswap( off, b-s, s); s = s_min(d-c, n-d-1); xs->vecswap( b, n-s, s); // Recursively sort non-partition-elements if ((s = b-a) > 1) sort_template<Comparator>(xs, off, s); if ((s = d-c) > 1) sort_template<Comparator>(xs, n-s, s); }
void quicksort(vector<string>& nuts, vector<string>& bolts, int begin, int end, Comparator cmp) { if (begin >= end) return; int pivot_pos; int left = 0; for (int i = begin; i <= end; ++i) { int res = cmp.cmp(nuts[begin], bolts[i]); if (res == 0) { // find the corresponding place for bolt pivot_pos = i; } else if (res == 1) { // find number of smaller bolts ++left; } } // put pivot into position swap(nuts, begin, begin+left); swap(bolts, pivot_pos, begin+left); pivot_pos = begin + left; // do mutual quicksort int i = begin, j = end; while (i < pivot_pos && pivot_pos < j) { while (i < pivot_pos && cmp.cmp(nuts[pivot_pos], bolts[i]) == 1) ++i; while (j > pivot_pos && cmp.cmp(nuts[pivot_pos], bolts[j]) == -1) --j; if (i < j) { swap(bolts, i, j); ++i; --j; } } i = begin, j = end; while (i < pivot_pos && pivot_pos < j) { while (i < pivot_pos && cmp.cmp(nuts[i], bolts[pivot_pos]) == -1) ++i; while (j > pivot_pos && cmp.cmp(nuts[j], bolts[pivot_pos]) == 1) --j; if (i < j) { swap(nuts, i, j); ++i; --j; } } quicksort(nuts, bolts, begin, pivot_pos-1, cmp); quicksort(nuts, bolts, pivot_pos+1, end, cmp); }
// All the smaller elements should be in the left side of the pivot, // and all the bigger elements should in the right side of the pivot. int partition(vector<string>& arr, int left, int right, const string& pivot, Comparator& compare) { for (int i = left; i < right; ) { if (compare.cmp(arr[i], pivot) == SMALLER || // Smaller. (compare.cmp(arr[i], pivot) == REVERSE && compare.cmp(pivot, arr[i]) == BIGGER)) { swap(arr[left++], arr[i++]); } else if (compare.cmp(arr[i], pivot) == BIGGER || // Bigger. (compare.cmp(arr[i], pivot) == REVERSE && compare.cmp(pivot, arr[i]) == SMALLER)) { ++i; } else { // Equal. swap(arr[i], arr[right]); } } // Put the pivot to the partition index. swap(arr[left], arr[right]); // Return the partition index of an array. return left; }
int partition(vector<string> &str, string pivot, Comparator compare, int l, int u){ int mid = l; // middle for (int i = l + 1; i <= u; i++) { if (compare.cmp(str[i], pivot) == -1 || compare.cmp(pivot, str[i]) == 1) { // str[i] smaller than pivot mid++; swap(str, i, mid); } else if (compare.cmp(str[i], pivot) == 0 || compare.cmp(pivot, str[i]) == 0) { // swap nuts[l]/bolts[l] with pivot // l位置放pivot swap(str, i, l); i--; } } // move pivot to proper index swap(str, mid, l); return mid; }
int main(int ac, char** av) { // checking args if (ac != 3) { std::cerr << "Usage: " << av[0] << " <file1> <file2>" << std::endl; std::cerr << "<file1>: list of hashes" << std::endl; std::cerr << "<file2>: list of words" << std::endl; return -1; } // getting datas Parser parser; std::string file1 = av[1], file2 = av[2]; std::vector<std::string>& hashes = parser.GetLines(file1); std::vector<std::string>& words = parser.GetLines(file2); // get matches with time Comparator comparator; clock_t begin, end; begin = clock(); comparator.ShowMatches(hashes, words); end = clock(); double elapsed = double(end - begin) / CLOCKS_PER_SEC; std::cout.precision(5); std::cout << "time elapsed: " << elapsed << " second(s)" << std::endl; // cleaning hashes.clear(); delete &hashes; words.clear(); delete &words; return 0; }
/** * @param nuts: a vector of integers * @param bolts: a vector of integers * @param compare: a instance of Comparator * @return: nothing */ void sortNutsAndBoltsN2(vector<string> &nuts, vector<string> &bolts, Comparator compare) { int n = nuts.size(); int m = bolts.size(); for (int i = 0; i < n - 1; i ++) { int j = i; while (j < m && compare.cmp(nuts[i], bolts[j])) j ++; string temp = bolts[j]; bolts[j] = bolts[i]; bolts[i] = temp; } for (int i = 0; i < n; i ++) { printf("%s ", bolts[i].c_str()); } }
long binarySearch(const S searched, const std::size_t searchLength, const T& searchedFor, const Comparator<T>& comp) { std::size_t low(0); std::size_t high(searchedLength); while( low < high ) { std::size_t mid(low + ((high - low) >> 1)); switch(comp.compare(searched[mid], searcedFor)) { case ORDER_INCREASING: low = mid + 1; break; case ORDER_DECREASING: high = mid; break; case ORDER_SAME: return static_cast<long>(mid); } } return -1 - static_cast<long>(low); }
TEST(Comparator, CompareTwoNonEqualFiles) { Comparator comparator; ASSERT_FALSE(comparator.compareFiles(FILES_NON_EQUAL_1, FILES_NON_EQUAL_2)); }
int main(int argc, char *argv[]) { const char *filename = "serialized.bin"; /* ============================================================= */ /* WRITING */ /* ============================================================= */ std::cout << "-------------------------------------" << std::endl; std::cout << " Binary file writer test" << std::endl; std::cout << "-------------------------------------" << std::endl; std::cout << "Opening " << filename << " for writing" << std::endl; BinaryFileWriter bfw(filename); //FlippingBinaryFileWriter bfw(filename); // Create and initialize the two objects SimpleObject so1(10, 20.0); ListObject lo1; lo1.init(); std::cout << std::endl << "Writing SimpleObject:" << std::endl; so1.print(); if (AS_FAILED(bfw.write(&so1))) { std::cout << "Could not write SimpleObject in file " << filename << std::endl; return 1; } std::cout << std::endl << "Writing ListObject:" << std::endl; lo1.print(); if (AS_FAILED(bfw.write(&lo1))) { std::cout << "Could not write ListObject in file " << filename << std::endl; return 1; } std::cout << "Serialization successful" << std::endl << std::endl; /* ============================================================= */ /* READING */ /* ============================================================= */ std::cout << "-------------------------------------" << std::endl; std::cout << " Binary file reader test" << std::endl; std::cout << "-------------------------------------" << std::endl; std::cout << "Opening " << filename << " for reading" << std::endl; BinaryFileReader bfr(filename); //FlippingBinaryFileReader bfr(filename); SimpleObject *so2; ListObject *lo2; std::cout << std::endl << "Reading SimpleObject:" << std::endl; if (AS_FAILED(bfr.read((ISerializable**)&so2))) { std::cout << "Could not read SimpleObject from file " << filename << std::endl; return 1; }; so2->print(); std::cout << std::endl << "Reading ListObject:" << std::endl; if (AS_FAILED(bfr.read((ISerializable**)&lo2))) { std::cout << "Could not read SimpleObject from file " << filename << std::endl; return 1; }; lo2->print(); //if (lo2->isSubclassOf<ISerializable>()) // std::cout << "all your base are belong to use" << std::endl; std::cout << "Deserialization successful" << std::endl; // Checking that written and read objects are equal if (so1.equals(so2)) std::cout << "Simple objects are identical" << std::endl; else { Comparator c; c.printMembersEquality(&so1, so2); } if (lo1.equals(lo2)) std::cout << "List objects are identical" << std::endl; else { Comparator c; c.printMembersEquality(&lo1, lo2); } return 0; }
int main(void){ Comparator<int> ci (123,456); cout << ci.max() << endl; return 0; }
TEST(Comparator, CompareTwoEqualFiles) { Comparator comparator; ASSERT_TRUE(comparator.compareFiles(FILES_EQUAL_1, FILES_EQUAL_2)); }
virtual void SetUp() { comparator.addUnitIncrementListener(&listener); comparator.setThreshold(50); comparator.setHysteresis(5); }
/* * Runs the GDE3 algorithm. * @return a <code>SolutionSet</code> that is a set of non dominated solutions * as a result of the algorithm execution */ SolutionSet * GDE3::execute() { int populationSize; int maxIterations; int evaluations; int iterations; SolutionSet * population; SolutionSet * offspringPopulation; Distance * distance; Comparator * dominance; Operator * crossoverOperator; Operator * selectionOperator; distance = new Distance(); dominance = new DominanceComparator(); Solution ** parent; //Read the parameters populationSize = *(int *) getInputParameter("populationSize"); maxIterations = *(int *) getInputParameter("maxIterations"); //Initialize the variables population = new SolutionSet(populationSize); evaluations = 0; iterations = 0; //Read the operators crossoverOperator = operators_["crossover"]; selectionOperator = operators_["selection"]; // Create the initial solutionSet Solution * newSolution; for (int i = 0; i < populationSize; i++) { newSolution = new Solution(problem_); problem_->evaluate(newSolution); problem_->evaluateConstraints(newSolution); evaluations++; population->add(newSolution); } //for // Generations ... while (iterations < maxIterations) { // Create the offSpring solutionSet offspringPopulation = new SolutionSet(populationSize * 2); for (int i = 0; i < populationSize; i++){ // Obtain parents. Two parameters are required: the population and the // index of the current individual void ** object1 = new void*[2]; object1[0] = population; object1[1] = &i; parent = (Solution **) (selectionOperator->execute(object1)); delete[] object1; Solution * child ; // Crossover. Two parameters are required: the current individual and the // array of parents void ** object2 = new void*[2]; object2[0] = population->get(i); object2[1] = parent; child = (Solution *) (crossoverOperator->execute(object2)); delete[] object2; delete[] parent; problem_->evaluate(child) ; problem_->evaluateConstraints(child); evaluations++ ; // Dominance test int result ; result = dominance->compare(population->get(i), child) ; if (result == -1) { // Solution i dominates child offspringPopulation->add(new Solution(population->get(i))); delete child; } // if else if (result == 1) { // child dominates offspringPopulation->add(child) ; } // else if else { // the two solutions are non-dominated offspringPopulation->add(child) ; offspringPopulation->add(new Solution(population->get(i))); } // else } // for // Ranking the offspring population Ranking * ranking = new Ranking(offspringPopulation); int remain = populationSize; int index = 0; SolutionSet * front = NULL; for (int i = 0; i < populationSize; i++) { delete population->get(i); } population->clear(); // Obtain the next front front = ranking->getSubfront(index); while ((remain > 0) && (remain >= front->size())){ //Assign crowding distance to individuals distance->crowdingDistanceAssignment(front,problem_->getNumberOfObjectives()); //Add the individuals of this front for (int k = 0; k < front->size(); k++ ) { population->add(new Solution(front->get(k))); } // for //Decrement remain remain = remain - front->size(); //Obtain the next front index++; if (remain > 0) { front = ranking->getSubfront(index); } // if } // while // remain is less than front(index).size, insert only the best one if (remain > 0) { // front contains individuals to insert while (front->size() > remain) { distance->crowdingDistanceAssignment(front,problem_->getNumberOfObjectives()); Comparator * crowdingComparator = new CrowdingComparator(); int indexWorst = front->indexWorst(crowdingComparator); delete crowdingComparator; delete front->get(indexWorst); front->remove(indexWorst); } for (int k = 0; k < front->size(); k++) { population->add(new Solution(front->get(k))); } remain = 0; } // if delete ranking; delete offspringPopulation; iterations ++ ; } // while delete dominance; delete distance; // Return the first non-dominated front Ranking * ranking = new Ranking(population); SolutionSet * result = new SolutionSet(ranking->getSubfront(0)->size()); for (int i=0;i<ranking->getSubfront(0)->size();i++) { result->add(new Solution(ranking->getSubfront(0)->get(i))); } delete ranking; delete population; return result; } // execute
//const char* file2 = "DQM_V0001_R000000001__RelValQCD__FlatPt_15_3000_Fast__ParticleFlow.root"; const char* file1 = "DQM_V0001_R000000001__A__B__C.root"; const char* file2 = "DQM_V0001_R000000001__A__B__C.root"; float ptMin = 0; float ptMax = 9999; Styles styles; Style* style1 = styles.spred; Style* style2 = styles.spblue; Comparator::Mode mode = Comparator::SCALE; string outdir = "Plots"; Comparator comp(file1, dir1.c_str(), file2, dir2.c_str()); comp.SetStyles(style1, style2, "Particle Flow Met", "Calo Met"); TCanvas c0("c0", "legend", 400, 200); Styles::FormatPad( &c0, false ); comp.Legend().Draw(); Styles::SavePlot("legend", outdir.c_str() ); comp.SetAxis(1, -200,200); TCanvas c1a("c1a", "DeltaMET"); Styles::FormatPad( &c1a, false); comp.DrawSlice("delta_et_VS_et_", 0, 1000, mode); Styles::SavePlot("deltaMET", outdir.c_str() ); comp.SetAxis(1, -500,500);