MythTV  master
mythdeque.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 #ifndef MYTH_DEQUE_H
4 #define MYTH_DEQUE_H
5 
6 #include <QString>
7 #include <QStringList>
8 #include <deque>
9 
10 template<typename T>
11 inline T myth_deque_init(const T */*unused*/) { return nullptr; }
12 template<>
13 inline int myth_deque_init(const int * /*unused*/) { return 0; }
14 template<>
15 inline uint myth_deque_init(const uint * /*unused*/) { return 0; }
16 template<>
17 inline QString myth_deque_init(const QString * /*unused*/) { return {}; }
18 template<>
19 inline QStringList myth_deque_init(const QStringList * /*unused*/) { return {}; }
20 
26 template<typename T>
27 class MythDeque : public std::deque<T>
28 {
29  public:
31  T dequeue()
32  {
33  T *dummy = nullptr;
34  if (std::deque<T>::empty())
35  return myth_deque_init(dummy);
36  T item = std::deque<T>::front();
37  std::deque<T>::pop_front();
38  return item;
39  }
41  void enqueue(T d) { std::deque<T>::push_back(d); }
42 
43  using iterator = typename std::deque<T>::iterator;
44  using const_iterator = typename std::deque<T>::const_iterator;
45  using size_type = typename std::deque<T>::size_type;
46 
48  iterator find(T const item)
49  {
50  for (auto it = std::deque<T>::begin(); it != std::deque<T>::end(); ++it)
51  if (*it == item)
52  return it;
53  return std::deque<T>::end();
54  }
55 
57  const_iterator find(T const item) const
58  {
59  for (auto it = std::deque<T>::begin(); it != std::deque<T>::end(); ++it)
60  if (*it == item)
61  return it;
62  return std::deque<T>::end();
63  }
64 
66  void remove(T const item)
67  {
68  auto it = find(item);
69  if (it != std::deque<T>::end())
70  std::deque<T>::erase(it);
71  }
72 
74  // cppcheck-suppress constParameter
75  bool contains(T const &item) const
76  { return find(item) != std::deque<T>::end(); }
77 
79  size_type count() const { return std::deque<T>::size(); }
80 
82  T head()
83  { if (!std::deque<T>::empty()) return std::deque<T>::front();
84  T *dummy = nullptr; return myth_deque_init(dummy); }
86  T head() const
87  { if (!std::deque<T>::empty()) return std::deque<T>::front();
88  T *dummy = NULL; return myth_deque_init(dummy); }
89 
91  T tail()
92  { if (!std::deque<T>::empty()) return std::deque<T>::back();
93  T *dummy = nullptr; return myth_deque_init(dummy); }
95  T tail() const
96  { if (!std::deque<T>::empty()) return std::deque<T>::back();
97  T *dummy = NULL; return myth_deque_init(dummy); }
98 };
99 
100 #endif // MYTH_DEQUE_H
MythDeque::remove
void remove(T const item)
Removes any item from list. O(n).
Definition: mythdeque.h:66
MythDeque::find
iterator find(T const item)
Finds an item in the list via linear search O(n).
Definition: mythdeque.h:48
back
static guint32 * back
Definition: goom_core.cpp:25
MythDeque::head
T head() const
Returns item at head of list. O(1).
Definition: mythdeque.h:86
MythDeque::tail
T tail()
Returns item at tail of list. O(1).
Definition: mythdeque.h:91
myth_deque_init
T myth_deque_init(const T *)
Definition: mythdeque.h:11
MythDeque< QStringList >::iterator
typename std::deque< QStringList >::iterator iterator
Definition: mythdeque.h:43
MythDeque::tail
T tail() const
Returns item at tail of list. O(1).
Definition: mythdeque.h:95
MythDeque< QStringList >::const_iterator
typename std::deque< QStringList >::const_iterator const_iterator
Definition: mythdeque.h:44
uint
unsigned int uint
Definition: compat.h:79
MythDeque< QStringList >::size_type
typename std::deque< QStringList >::size_type size_type
Definition: mythdeque.h:45
MythDeque
MythDeque is similar to QPtrQueue, while being based off deque, this allows that items that are not a...
Definition: mythdeque.h:27
MythDeque::head
T head()
Returns item at head of list. O(1).
Definition: mythdeque.h:82
MythDeque::contains
bool contains(T const &item) const
Returns true if item is in list. O(n).
Definition: mythdeque.h:75
d
static const iso6937table * d
Definition: iso6937tables.cpp:1025
MythDeque::enqueue
void enqueue(T d)
Adds item to the back of the list. O(1).
Definition: mythdeque.h:41
MythDeque::dequeue
T dequeue()
Removes item from front of list and returns a copy. O(1).
Definition: mythdeque.h:31
MythDeque::count
size_type count() const
Returns size of list. O(1).
Definition: mythdeque.h:79
MythDeque::find
const_iterator find(T const item) const
Finds an item in the list via linear search O(n).
Definition: mythdeque.h:57