#include #include #include #include #include "JTools/JHashCollection.hh" #include "Jeep/JPrint.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" namespace { /** * Example class to get value for hash key. */ struct get_value { /** * Constructor. * * \param precision precision */ get_value(const double precision) { this->precision = precision; } /* * Function. * * \param key key * \return hash value */ inline int operator()(const double key) const { return (int) ((key + 0.5*precision) / precision); } double precision; }; } /** * \file * * Example program to test JTOOLS::JHashCollection class. * \author mdejong */ int main(int argc, char **argv) { using namespace std; double precision; int debug; try { JParser<> zap("Example program to test hash collection."); zap['e'] = make_field(precision) = 1.0e-3; zap['d'] = make_field(debug) = 3; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } using namespace JPP; typedef JHashCollection hash_collection; hash_collection buffer(precision); set input = { 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0 }; set rm = { 1.5, 1.1 }; for (set::const_reverse_iterator x = input.rbegin(); x != input.rend(); ++x) { buffer.insert(*x); buffer.insert(*x); } for (hash_collection::const_iterator i = buffer.begin(); i != buffer.end(); ++i) { DEBUG(FIXED(5,2) << *i << endl); } ASSERT(buffer.size() == input.size(), "Test of buffer size with multiple inserts of same element."); for (set::const_iterator x = input.begin(); x != input.end(); ++x) { DEBUG(FIXED(5,2) << *x << ' ' << buffer.getIndex(*x) << endl); ASSERT(buffer.has(*x), "Test of buffer content."); } { hash_collection out(precision); out = buffer; for (set::const_iterator x = input.begin(); x != input.end(); ++x) { DEBUG(FIXED(5,2) << *x << ' ' << out.getIndex(*x) << endl); ASSERT(out.has(*x), "Test of buffer content after assignment."); } } for (set::const_iterator x = rm.begin(); x != rm.end(); ++x) { hash_collection::iterator p = buffer.find(*x); DEBUG("find " << FIXED(5,2) << *x << " at position " << distance(buffer.begin(),p) << ' ' << buffer.getIndex(*x) << endl); buffer.erase(p); } for (set::const_iterator x = input.begin(); x != input.end(); ++x) { ASSERT(buffer.has(*x) == (rm.count(*x) == 0), "Test of buffer content after erase."); } return 0; }