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;
	}
	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 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;
}
	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;
	}
	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;
	}
Example #7
0
template<class T,class Cont> void Database::ReadArray(const ID& lid, const std::string& key, Cont& cont) const
{
  //get the plain value
  std::string value = Read(lid,key);
  Debug(value);

  std::insert_iterator<Cont> it(cont,cont.end());
  int lastpos=0;
  int pos=0;
  T tmp;
  //split it with '|' and push the parts to value_vector
  while((pos = value.find('|',lastpos)) != std::string::npos)
  {
    std::stringstream ss;
    ss << value.substr(lastpos,pos-lastpos);
    ss >> tmp;
    it = tmp;
    lastpos=pos+1;
  }
  //the last value has to be handled seperate
  if(value.substr(lastpos,value.length()).length())
  {
    //value_vector.push_back(value.substr(lastpos,value.length()-lastpos));
    std::stringstream ss;
    ss << value.substr(lastpos,value.length()-lastpos);
    ss >> tmp;
    it = tmp;
  }
Example #8
0
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;
}
		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;
		}
Example #10
0
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 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++;
     }
 }
Example #12
0
void dump(Cont & container)
{
    for (ContIter iter = container.begin(); iter != container.end(); iter++)
    {
        cout << *iter << " ";
    }
    cout << endl;
}
Example #13
0
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++;
    }
}
		const_iterator(const Cont& elements, ContConstIter it)
		  : elements(&elements)
		  , it(it)
		  , t(T())
		{
			if (it != elements.end()) {
				t = it->lb;
			}
		}
Example #15
0
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;
}
Example #16
0
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;
}
Example #17
0
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;
	}
}
		const_iterator& operator++() {
			if (it == elements->end()) {
				t = T();
				return *this;
			}
			if (t == it->ub) {
				++it;
				if (it == elements->end()) {
					t = T();
				}
				else {
					t = it->lb;
				}
			}
			else {
				++t;
			}
			return *this;
		}
Example #19
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);
}
Example #20
0
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);
} 
		inline
		bool replace_key (Cont& c,
						  const typename Cont::key_type& old_key,
						  const typename Cont::key_type& new_key)
		{
			typename Cont::iterator pos;
			pos = c.find(old_key);
			if (pos != c.end()) {
				// insert new element with value of old element
				c.insert(typename Cont::value_type(new_key,
												   pos->second));
				// remove old element
				c.erase(pos);
				return true;
			}
			else {
				// key not found
				return false;
			}
		}
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);
} 
Example #23
0
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);
    } 
}
	bool ParseData(const vector<string>::iterator & s_itr, const string search, int start, const Cont & cont, string & cmd)
	{
		int offset=0;
		
		if (s_itr == cont.end())
		{
			return false;
		}

		cmd.clear();
		string cmdline = *s_itr;
		offset=cmdline.find(search,start);
		if (offset == -1)
		{
			//Search for the end of line
			offset=cmdline.find("\r\n",start);
			if (offset == -1)
			{
				return false;
			}
		}
		cmd=cmdline.substr(start+1, cmdline.size()-start+1);
		return true;
	}
Example #25
0
inline bool index_matches(const Cont& index, const VT& entry) {
	return (index.find(entry) != index.end());
}
Example #26
0
void assert_contents(const Cont &c, std::initializer_list<T> il)
{ return assert_range(c.begin(), c.end(), il.begin(), il.end()); }
	bool is_last(Iter iter, const Cont& cont)
	{
		return (iter != cont.end()) && (next(iter) == cont.end());
	}
	const_iterator end() const {
		return const_iterator(elements, elements.end());
	}
Example #29
0
inline void erase(Cont& cont, const T& val) {
	cont.erase(std::remove(cont.begin(), cont.end(), val), cont.end());
}
Example #30
0
 LowLevelItrType
 get_end(Cont& container)
 {
   return LowLevelItrType(container.end());
 }