Skip to content

Instantly share code, notes, and snippets.

@chenshuo
Created July 6, 2012 09:02
Show Gist options
  • Save chenshuo/3059083 to your computer and use it in GitHub Desktop.
Save chenshuo/3059083 to your computer and use it in GitHub Desktop.
find expired timer entries with lower_bound()
#include <map>
#include <set>
#include <stdio.h>
typedef std::pair<int64_t, uint32_t> Entry;
typedef std::set<Entry> TimerList;
TimerList timers;
void test(int64_t now)
{
Entry sentry(now, 0xFFFFFFFF);
TimerList::iterator end = timers.lower_bound(sentry);
assert(end == timers.end() || now < end->first);
if (end == timers.end() || now < end->first)
printf("test %zd pass\n", now);
else
printf("test %zd FAILED\n", now);
for (TimerList::iterator it = timers.begin();
it != end; ++it)
{
printf(" expired entry %zd\n", it->first);
}
}
int main()
{
timers.insert(Entry(10, 100));
timers.insert(Entry(20, 200));
timers.insert(Entry(20, 300));
timers.insert(Entry(30, 400));
test(5);
test(10);
test(15);
test(20);
test(25);
test(30);
test(35);
}
test 5 pass
test 10 pass
expired entry 10
test 15 pass
expired entry 10
test 20 pass
expired entry 10
expired entry 20
expired entry 20
test 25 pass
expired entry 10
expired entry 20
expired entry 20
test 30 pass
expired entry 10
expired entry 20
expired entry 20
expired entry 30
test 35 pass
expired entry 10
expired entry 20
expired entry 20
expired entry 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment