Vowpal Wabbit
stable_unique.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <set>
4 
5 // Performs unique operation over collection without requiring the collection to be sorted first.
6 // Returns pointer to first element after unique elements to erase from until end of collection.
7 // Original ordering of elements is preserved.
8 template <typename ForwardIterator>
9 ForwardIterator stable_unique(ForwardIterator begin, ForwardIterator end)
10 {
11  using value_t = typename std::iterator_traits<ForwardIterator>::value_type;
12 
13  std::set<value_t> unique_set;
14 
15  auto current_head = begin;
16  for (auto current_check = begin; current_check != end; current_check++)
17  {
18  if (unique_set.find(*current_check) == unique_set.end())
19  {
20  unique_set.insert(*current_check);
21  *current_head = *current_check;
22  current_head++;
23  }
24  }
25 
26  return current_head;
27 }
ForwardIterator stable_unique(ForwardIterator begin, ForwardIterator end)
Definition: stable_unique.h:9