Bites
A small library of miscellaneous C++ codes.
 All Classes Namespaces Functions Friends Pages
SortedList.hpp
1 #ifndef BITES_SORTEDLIST_HPP_INCLUDED
2 #define BITES_SORTEDLIST_HPP_INCLUDED
3 
4 #include <list>
5 
6 namespace bites {
7 
11 template<typename T>
13 {
14 public:
15 
19  SortedList(const std::list<T>& donor = std::list<T>())
20  : m_data(donor)
21  {
22  m_data.sort();
23  }
24 
28  void add(const T& item)
29  {
30  m_data.push_back(item);
31  m_data.sort();
32  }
33 
37  unsigned int getCountLT(const T& item) const
38  {
39  auto lower = lower_bound(m_data.begin(), m_data.end(), item);
40  return distance(m_data.begin(), lower);
41  }
42 
46  unsigned int getCountGT(const T& item) const
47  {
48  auto upper = lower_bound(m_data.begin(), m_data.end(), item);
49  return distance(++upper, m_data.end());
50  }
51 
56  unsigned int removeLT(const T& item)
57  {
58  auto lower = lower_bound(m_data.begin(), m_data.end(), item);
59  unsigned int count = distance(m_data.begin(), lower);
60  m_data.erase(m_data.begin(), lower);
61  return count;
62  }
63 
64 private:
65  std::list<T> m_data;
66 };
67 
68 } // namespace bites.
69 
70 #endif // BITES_SORTEDLIST_HPP_INCLUDED
Definition: SortedList.hpp:12
unsigned int removeLT(const T &item)
Definition: SortedList.hpp:56
unsigned int getCountGT(const T &item) const
Definition: SortedList.hpp:46
SortedList(const std::list< T > &donor=std::list< T >())
Definition: SortedList.hpp:19
unsigned int getCountLT(const T &item) const
Definition: SortedList.hpp:37
void add(const T &item)
Definition: SortedList.hpp:28