void insert_front(Cont& c, long& avg, const int& num_op) { cout << "\nCounting..." << "time of " << num_op << " front_insert"; clock_t t = 0; for(int j = 0; j < REPEAT_TEST; ++j) { c.push_back(0); c.push_back(0); auto it = c.begin(); t = clock(); for (int i = 1; i <= num_op ; ++i) { it = c.begin(); c.insert(it, i); } t = clock() - t; avg += t; c.clear(); } avg /= REPEAT_TEST; }
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; }
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; }
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; }
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); }
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; }
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; }
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++; } }
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; }
static void do_move(Cont& styls, int type, int& first, int& last, bool storage) { auto begin = styls.begin(); // Move up if (type == 0) { if (first == 0) return; rotate(begin + first - 1, begin + first, begin + last + 1); first--; last--; } // Move to top else if (type == 1) { rotate(begin, begin + first, begin + last + 1); last = last - first; first = 0; } // Move down else if (type == 2) { if (last + 1 == (int)styls.size()) return; rotate(begin + first, begin + last + 1, begin + last + 2); first++; last++; } // Move to bottom else if (type == 3) { rotate(begin + first, begin + last + 1, styls.end()); first = styls.size() - (last - first + 1); last = styls.size() - 1; } // Sort else if (type == 4) { // Get confirmation if (storage) { int res = wxMessageBox(_("Are you sure? This cannot be undone!"), _("Sort styles"), wxYES_NO | wxCENTER); if (res == wxNO) return; } sort(styls.begin(), styls.end(), cmp_name()); first = 0; last = 0; } }
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); }
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); }
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; }
void operator()(Cont& c, long count) { typename Cont::iterator it; long cnt = count / 10; for (long i = 0; i < cnt; i++) { // Must get the iterator every time to keep // from causing an access violation with // vector. Increment it to put it in the // middle of the container: it = c.begin(); it++; c.insert(it, fs); } }
void operator()(Cont& c, long count) { long cnt = count / 10; if (cnt > c.size()) { cout << "RemoveMiddle: not enough elements" << endl; return; } for (long i = 0; i < cnt; i++) { typename Cont::iterator it = c.begin(); it++; c.erase(it); } }
const_iterator& operator--() { if (it == elements->end() || t == it->lb) { if (it == elements->begin()) { t = T(); it = elements->end(); } else { --it; t = it->ub; } } else { --t; } return *this; }
void test_unordered_interface() { Cont c; T* t = new T; c.insert( t ); typename Cont::local_iterator i = c.begin( 0 ); typename Cont::const_local_iterator ci = i; ci = c.cbegin( 0 ); i = c.end( 0 ); ci = c.cend( 0 ); typename Cont::size_type s = c.bucket_count(); s = c.max_bucket_count(); s = c.bucket_size( 0 ); s = c.bucket( *t ); float f = c.load_factor(); f = c.max_load_factor(); c.max_load_factor(f); c.rehash(1000); }
void test_unordered_interface() { Cont c; T* t = new T; Key key = get_next_key( key ); c.insert( key, t ); typename Cont::local_iterator i = c.begin( 0 ); typename Cont::const_local_iterator ci = i; ci = c.cbegin( 0 ); i = c.end( 0 ); ci = c.cend( 0 ); typename Cont::size_type s = c.bucket_count(); hide_warning(s); s = c.max_bucket_count(); s = c.bucket_size( 0 ); s = c.bucket( key ); float f = c.load_factor(); f = c.max_load_factor(); c.max_load_factor(f); c.rehash(1000); }
void strategy1() { int numElems; cin >> numElems; Cont container; for (int ii = 0; ii < numElems; ii++) { container.push_back(0); cin >> container.back(); } for (int day = 0; ; day++) { list<ContIter> toDelete; ContIter iThis(container.begin()); ContIter iPrev(iThis); iThis++; while (iThis != container.end()) { if (*iThis > *iPrev) { toDelete.push_back(iThis); } iThis++; iPrev++; } if (toDelete.empty()) { cout << day << endl; break; } // cout << "toDelete "; // dump(toDelete); for (list<ContIter>::reverse_iterator iDel = toDelete.rbegin(); iDel != toDelete.rend(); iDel++) { container.erase(*iDel); } // dump(container); } }
template <class Cont> inline outputter<typename Cont::iterator> make_outputter_cont(Cont &container) { return outputter<typename Cont::iterator>(container.begin()); }
void test(const Cont &) { // Testing if all types are provided. typename Cont::value_type t0; typename Cont::reference t1 = t0; CGAL_USE(t1); typename Cont::const_reference t2 = t0; CGAL_USE(t2); typename Cont::pointer t3 = &t0; typename Cont::const_pointer t4 = &t0; CGAL_USE(t4); typename Cont::size_type t5 = 0; CGAL_USE(t5); typename Cont::difference_type t6 = t3-t3; CGAL_USE(t6); typename Cont::iterator t7; CGAL_USE(t7); typename Cont::const_iterator t8; CGAL_USE(t8); typename Cont::reverse_iterator t9; CGAL_USE(t9); typename Cont::const_reverse_iterator t10; CGAL_USE(t10); typename Cont::allocator_type t15; std::cout << "Testing empty containers." << std::endl; Cont c0, c1; Cont c2(t15); Cont c3(c2); Cont c4; c4 = c2; typedef std::vector<typename Cont::value_type> Vect; Vect v0; const Cont c5(v0.begin(), v0.end()); Cont c6(c5.begin(), c5.end()); typename Cont::allocator_type Al; Cont c7(c0.begin(), c0.end(), Al); Cont c8; c8.insert(c0.rbegin(), c0.rend()); // test conversion iterator-> const_iterator. typename Cont::const_iterator t16 = c5.begin(); CGAL_USE(t16); assert(t16 == c5.begin()); assert(c0 == c1); assert(! (c0 < c1)); assert(check_empty(c0)); assert(check_empty(c1)); assert(check_empty(c2)); assert(check_empty(c3)); assert(check_empty(c4)); assert(check_empty(c5)); assert(check_empty(c6)); assert(check_empty(c7)); assert(check_empty(c8)); c1.swap(c0); assert(check_empty(c0)); assert(check_empty(c1)); c1.merge(c0); assert(check_empty(c0)); assert(check_empty(c1)); typename Cont::allocator_type t20 = c0.get_allocator(); std::cout << "Now filling some containers" << std::endl; Vect v1(10000); Cont c9(v1.begin(), v1.end()); assert(c9.size() == v1.size()); assert(c9.max_size() >= v1.size()); assert(c9.capacity() >= c9.size()); Cont c10 = c9; assert(c10 == c9); assert(c10.size() == v1.size()); assert(c10.max_size() >= v1.size()); assert(c10.capacity() >= c10.size()); c9.clear(); assert(check_empty(c9)); assert(c9.capacity() >= c9.size()); assert(c0 == c9); c9.merge(c10); c10.swap(c9); assert(check_empty(c9)); assert(c9.capacity() >= c9.size()); assert(c10.size() == v1.size()); assert(c10.max_size() >= v1.size()); assert(c10.capacity() >= c10.size()); std::cout << "Testing insertion methods" << std::endl; c9.assign(c10.begin(), c10.end()); assert(c9 == c10); c10.assign(c9.begin(), c9.end()); assert(c9 == c10); c9.insert(c10.begin(), c10.end()); assert(c9.size() == 2*v1.size()); c9.clear(); assert(c9 != c10); c9.insert(c10.begin(), c10.end()); assert(c9.size() == v1.size()); assert(c9 == c10); typename Cont::iterator it = c9.iterator_to(*c9.begin()); assert(it == c9.begin()); typename Cont::const_iterator cit = c9.iterator_to(const_cast<typename Cont::const_reference>(*c9.begin())); assert(cit == c9.begin()); typename Cont::iterator s_it = Cont::s_iterator_to(*c9.begin()); assert(s_it == c9.begin()); typename Cont::const_iterator s_cit = Cont::s_iterator_to(const_cast<typename Cont::const_reference>(*c9.begin())); assert(s_cit == c9.begin()); c10 = Cont(); assert(check_empty(c10)); for(typename Vect::const_iterator it = v1.begin(); it != v1.end(); ++it) c10.insert(*it); assert(c10.size() == v1.size()); assert(c9 == c10); c9.erase(c9.begin()); c9.erase(c9.begin()); assert(c9.size() == v1.size() - 2); // test reserve /*Cont c11; c11.reserve(v1.size()); for(typename Vect::const_iterator it = v1.begin(); it != v1.end(); ++it) c11.insert(*it); assert(c11.size() == v1.size()); assert(c10 == c11);*/ // owns() and owns_dereferencable(). for(typename Cont::const_iterator it = c9.begin(), end = c9.end(); it != end; ++it) { assert(c9.owns(it)); assert(c9.owns_dereferencable(it)); assert(! c10.owns(it)); assert(! c10.owns_dereferencable(it)); } assert(c9.owns(c9.end())); assert(! c9.owns_dereferencable(c9.end())); c9.erase(c9.begin(), c9.end()); assert(check_empty(c9)); std::cout << "Testing parallel insertion" << std::endl; { Cont c11; Vect v11(1000000); std::vector<typename Cont::iterator> iterators(v11.size()); tbb::parallel_for( tbb::blocked_range<size_t>( 0, v11.size() ), Insert_in_CCC_functor<Vect, Cont>(v11, c11, iterators) ); assert(c11.size() == v11.size()); std::cout << "Testing parallel erasure" << std::endl; tbb::parallel_for( tbb::blocked_range<size_t>( 0, v11.size() ), Erase_in_CCC_functor<Cont>(c11, iterators) ); assert(c11.empty()); } std::cout << "Testing parallel insertion AND erasure" << std::endl; { Cont c12; Vect v12(1000000); std::vector<tbb::atomic<bool> > free_elements(v12.size()); for(typename std::vector<tbb::atomic<bool> >::iterator it = free_elements.begin(), end = free_elements.end(); it != end; ++it) { *it = true; } tbb::atomic<unsigned int> num_erasures; num_erasures = 0; std::vector<typename Cont::iterator> iterators(v12.size()); tbb::parallel_for( tbb::blocked_range<size_t>( 0, v12.size() ), Insert_and_erase_in_CCC_functor<Vect, Cont>( v12, c12, iterators, free_elements, num_erasures) ); assert(c12.size() == v12.size() - num_erasures); } }
void assert_range(InputIterator b, InputIterator e, const Cont &c) { return assert_range(b, e, c.begin(), c.end()); }
void assert_contents(const Cont &c, std::initializer_list<T> il) { return assert_range(c.begin(), c.end(), il.begin(), il.end()); }
inline bool check_empty(const Cont &c) { return c.empty() && c.size() == 0 && c.begin() == c.end(); }
void drop(Cont &cont, Pred pred) { cont.erase(std::remove_if(cont.begin(), cont.end(), pred), cont.end()); }
LowLevelItrType get_begin(Cont& container) { return LowLevelItrType(container.begin()); }
inline void erase(Cont& cont, const T& val) { cont.erase(std::remove(cont.begin(), cont.end(), val), cont.end()); }
const_iterator begin() const { return const_iterator(elements, elements.begin()); }