void test_serialization_helper() { Cont vec; set_capacity<Cont>()( vec ); add( vec, new Base( -1 ), 0u ); add( vec, new Derived( 1 ), 1u ); BOOST_CHECK_EQUAL( vec.size(), 2u ); std::ofstream ofs("filename"); OArchive oa(ofs); oa << boost::serialization::make_nvp( "container", as_const(vec) ); ofs.close(); std::ifstream ifs("filename", std::ios::binary); IArchive ia(ifs); Cont vec2; ia >> boost::serialization::make_nvp( "container", vec2 ); ifs.close(); BOOST_CHECK_EQUAL( vec.size(), vec2.size() ); BOOST_CHECK_EQUAL( (*vec2.begin()).i, -1 ); BOOST_CHECK_EQUAL( (*++vec2.begin()).i, 0 ); typename Cont::iterator i = vec2.begin(); ++i; Derived* d = dynamic_cast<Derived*>( &*i ); BOOST_CHECK_EQUAL( d->i2, 1 ); }
auto zip_var(LastCont lastContainer) { Cont<tuple<typename LastCont::value_type>, allocator<tuple<typename LastCont::value_type>>> result; for_each(lastContainer.begin(), lastContainer.end(), [&](auto &content) { result.insert(result.end(), make_tuple(content)); }); return result; }
iterator insert(value_type&& val) { if (container.empty()) { return container.insert(cend(), val); } auto elementgreaterthanorequalto = std::lower_bound(cbegin(), cend(), val, std::ref(compare_predicate)); return container.insert(elementgreaterthanorequalto, std::move(val)); }
iterator emplace(Tn&&... argn) { if (container.empty()) { return container.emplace(cend(), std::forward<Tn>(argn)...); } auto elementgreaterthanorequalto = std::lower_bound(cbegin(), cend(), val, std::ref(compare_predicate)); return container.emplace(elementgreaterthanorequalto, std::forward<Tn>(argn)...); }
void test_serialization_helper() { Cont vec; add( vec, new Base( -1 ), 0u ); add( vec, new Derived( 1 ), 1u ); std::ofstream ofs("filename"); boost::archive::text_oarchive oa(ofs); oa << as_const(vec); ofs.close(); std::ifstream ifs("filename", std::ios::binary); boost::archive::text_iarchive ia(ifs); Cont vec2; ia >> vec2; ifs.close(); BOOST_CHECK_EQUAL( vec.size(), vec2.size() ); BOOST_CHECK_EQUAL( (*vec2.begin()).i, -1 ); BOOST_CHECK_EQUAL( (*--vec2.end()).i, 0 ); typename Cont::iterator i = vec2.end(); --i; Derived* d = dynamic_cast<Derived*>( &*i ); BOOST_CHECK_EQUAL( d->i2, 1 ); }
bool insert(T t) { ContIter it = std::lower_bound(elements.begin(), elements.end(), t); if (it != elements.end() && t >= it->lb && t <= it->ub) { return false; } ContIter pr = it - 1; if (it != elements.begin() && pr->ub + 1 == t) { ++pr->ub; if (it != elements.end() && pr->ub + 1 == it->lb) { pr->ub = it->ub; elements.erase(it); } } else if (it != elements.end() && it->lb == t + 1) { --it->lb; if (it != elements.begin() && pr->ub + 1 == it->lb) { pr->ub = it->ub; elements.erase(it); } } else { elements.insert(it, interval(t)); } ++_size; return true; }
iterator emplace(Tn&&... argn) { if (container.empty()) { return container.insert(detail::adl_cend(container), std::forward<Tn>(argn)...); } auto elementgreaterthanorequalto = std::lower_bound(detail::adl_cbegin(container), detail::adl_cend(container), val, std::ref(predicate)); return container.emplace(elementgreaterthanorequalto, std::forward<Tn>(argn)...); }
//--------------------------------------------------------------------------- void Graph::Visit(int From, Cont & C) { if (From >= 0 && From < _Size) { int V = From; bool * Labelled = new bool[_Size]; for(int i = 0; i < _Size; i++) Labelled[i] = false; C.Push(V); Labelled[V] = true; do { V = C.Pop(); printf("%d ", V); for(int U = 0; U < _Size; U++) { if(!Labelled[U] && _m[V][U] != MAXDOUBLE) { C.Push(U); Labelled[U] = true; } } } while(!C.Empty()); delete [] Labelled; printf("\n"); } }
void insert_back(Cont& c, long& avg, const int& num_op) { cout << "\nCounting..." << "time of " << num_op << " back_insert"; clock_t t = 0; for(int j = 0; j < REPEAT_TEST; ++j) { c.push_back(0); c.push_back(0); auto it = c.end(); t = clock(); for (int i = 1; i <= num_op ; ++i) { it = c.end(); c.insert(it, i); } t = clock() - t; avg += t; c.clear(); } avg /= REPEAT_TEST; }
interval_vector intersect(const interval_vector& o) const { interval_vector rv; if (!empty() && !o.empty()) { ContConstIter a = elements.begin(); ContConstIter b = o.elements.begin(); while (a != elements.end() && b != o.elements.end()) { while (a != elements.end() && b != o.elements.end() && a->ub < b->lb) { ++a; } while (a != elements.end() && b != o.elements.end() && b->ub < a->lb) { ++b; } while (a != elements.end() && b != o.elements.end() && a->ub >= b->lb && b->ub >= a->lb) { const T lb = std::max(a->lb, b->lb); const T ub = std::min(a->ub, b->ub); if (!rv.elements.empty() && rv.elements.back().ub + 1 == lb) { rv.elements.back().ub = ub; } else { rv.elements.push_back(interval(lb, ub)); } rv._size += ub - lb + 1; if (a->ub < b->ub) { ++a; } else { ++b; } } } } return rv; }
iterator insert(value_type&& val) { if (container.empty()) { return container.insert(detail::adl_cend(container), val); } auto elementgreaterthanorequalto = std::lower_bound(detail::adl_cbegin(container), detail::adl_cend(container), val, std::ref(predicate)); return container.insert(elementgreaterthanorequalto, std::move(val)); }
bool erase(T t) { ContIter it = std::lower_bound(elements.begin(), elements.end(), t); if (it == elements.end()) { return false; } if (it->ub < t || it->lb > t) { return false; } if (it->lb == t && it->ub == t) { elements.erase(it); --_size; return true; } if (it->ub == t) { --it->ub; --_size; return true; } if (it->lb == t) { ++it->lb; --_size; return true; } if (it->lb < t && it->ub > t) { elements.insert(it + 1, interval(t + 1, it->ub)); it->ub = t - 1; --_size; return true; } assert(false && "interval_vector.erase() should never reach this place..."); return false; }
void operator()(Cont& c, long count) { long cnt = count / 100; for (long i = 0; i < cnt; i++) { typename Cont::iterator it = c.begin(), end = c.end(); while (it != end) it++; } }
template<class Cont> void backInsertion(Cont& ci) { copy(a, a + sizeof(a)/sizeof(Cont::value_type), back_inserter(ci)); copy(ci.begin(), ci.end(), ostream_iterator<typename Cont::value_type>( cout, " ")); cout << endl; }
void dump(Cont & container) { for (ContIter iter = container.begin(); iter != container.end(); iter++) { cout << *iter << " "; } cout << endl; }
void apply(Cont& c, PtrMemFun f) { typename Cont::iterator it = c.begin(); while (it != c.end()) { //(it->*f)(); // Compact form //! gcc-4.4 gcc-4.6 ±àÒ벻ͨ¹ý ((*it).*f)(); // Alternate form it++; } }
inline std::enable_if_t<std::is_base_of<T, std::decay_t<U>>::value> push(U&& value) { std::unique_lock<std::mutex> lock(mut); // only add the value on the stack if there is room data_cond.wait(lock, [this]{ return (data_queue.size() < capacity) || shutdownFlag; }); data_queue.emplace(std::make_shared< std::decay_t<U>> (std::forward<U>(value))); data_cond.notify_one(); }
// return shared pointer to value - which if set to nullptr, // indicates container was empty at the time of the call. inline std::shared_ptr<T> try_pop() { std::lock_guard<std::mutex> lock(mut); if (data_queue.empty()) { return std::shared_ptr<T>(); } else { auto res = std::move(front_or_top(data_queue)); data_queue.pop(); return std::move(res); } }
void operator()(Cont& c, long count) { long cnt = count * 10; if (cnt > c.size()) { cout << "RemoveBack: not enough elements" << endl; return; } for (long i = 0; i < cnt; i++) c.pop_back(); }
typename std::enable_if<is_hashable<typename Cont::value_type>::value, void>::type hash_container_object(const Cont & cont, Hasher & hasher) { // some containers don't have size() (ie, forward_list) size_t d = static_cast<size_t>(std::distance(cont.begin(), cont.end())); hasher(d); for(const auto & it : cont) hasher(it); }
void midInsertion(Cont& ci) { typename Cont::iterator it = ci.begin(); it++; it++; it++; copy(a, a + sizeof(a)/(sizeof(int)*2), inserter(ci, it)); copy(ci.begin(), ci.end(), ostream_iterator<int>(cout, " ")); cout << endl; }
bool contains(T t) const { ContConstIter it = std::lower_bound(elements.begin(), elements.end(), t); if (it == elements.end()) { return false; } if (it->ub < t || it->lb > t) { return false; } return true; }
template<class Cont> void midInsertion(Cont& ci) { typename Cont::iterator it = ci.begin(); ++it; ++it; ++it; copy(a, a + sizeof(a)/(sizeof(Cont::value_type) * 2), inserter(ci, it)); copy(ci.begin(), ci.end(), ostream_iterator<typename Cont::value_type>( cout, " ")); cout << endl; }
const_iterator find(T t) const { ContConstIter it = std::lower_bound(elements.begin(), elements.end(), t); if (it == elements.end()) { return end(); } if (it->ub < t || it->lb > t) { return end(); } return const_iterator(elements, it, t); }
// return value in specified reference indicating whether // value successfully returned or not inline bool try_pop(std::shared_ptr<T>& rValue) { std::lock_guard<std::mutex> lock(mut); if (data_queue.empty()) { rValue.reset(); return false; } else { rValue = std::move(front_or_top(data_queue)); data_queue.pop(); return true; } }
void operator()(Cont& c, long count) { int middle = c.size() / 2; typename Cont::iterator it = c.begin(), mid = c.begin(); it++; // Put it in the middle for (int x = 0; x < middle + 1; x++) mid++; long cnt = count * 10; for (long i = 0; i < cnt; i++) swap(*it, *mid); }
// wait for non empty lambda condition before returning shared pointer to value inline std::shared_ptr<T> wait_and_pop() { std::unique_lock<std::mutex> lock(mut); data_cond.wait(lock, [this]{ return !data_queue.empty() || shutdownFlag; }); if (!shutdownFlag) { auto res = std::move(front_or_top(data_queue)); data_queue.pop(); return std::move(res); } return nullptr; }
inline std::enable_if_t<std::is_base_of<T, U>::value> push(std::shared_ptr<U>&& ptr) { std::unique_lock<std::mutex> lock(mut); // only add the value on the stack if there is room data_cond.wait(lock, [this]{ return (data_queue.size() < capacity) || shutdownFlag; }); if (!shutdownFlag) { data_queue.emplace(std::move(ptr)); // move the pointer } data_cond.notify_one(); }
void setCheck(E e) { ASSERT_TRUE((fis_same<typename SetterHelper<M, m>::type>( SetterHelper<M, m>::function))); F set = SetterHelper<M, m>::function; // Build invalid objet Cont<E> cont; // Sets valid value ASSERT_TRUE(set(cont, e)); // Check that value is set ASSERT_TRUE(e == cont.getConst()); }
Cont keep_if(Pred pred, const Cont& xs) { Cont ys; for (const auto x : xs) { if (pred(x)) { ys.push_back(x); } } return ys; }