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 using std::deque;
10 
11 template<typename T>
12 inline T myth_deque_init(const T */*unused*/) { return nullptr; }
13 template<>
14 inline int myth_deque_init(const int * /*unused*/) { return 0; }
15 template<>
16 inline uint myth_deque_init(const uint * /*unused*/) { return 0; }
17 template<>
18 inline QString myth_deque_init(const QString * /*unused*/) { return QString(); }
19 template<>
20 inline QStringList myth_deque_init(const QStringList * /*unused*/) { return QStringList(); }
21 
27 template<typename T>
28 class MythDeque : public deque<T>
29 {
30  public:
32  T dequeue()
33  {
34  T *dummy = nullptr;
35  if (deque<T>::empty())
36  return myth_deque_init(dummy);
37  T item = deque<T>::front();
38  deque<T>::pop_front();
39  return item;
40  }
42  void enqueue(T d) { deque<T>::push_back(d); }
43 
44  using iterator = typename deque<T>::iterator;
45  using const_iterator = typename deque<T>::const_iterator;
46  using size_type = typename deque<T>::size_type;
47 
49  iterator find(T const item)
50  {
51  for (auto it = deque<T>::begin(); it != deque<T>::end(); ++it)
52  if (*it == item)
53  return it;
54  return deque<T>::end();
55  }
56 
58  const_iterator find(T const item) const
59  {
60  for (auto it = deque<T>::begin(); it != deque<T>::end(); ++it)
61  if (*it == item)
62  return it;
63  return deque<T>::end();
64  }
65 
67  void remove(T const item)
68  {
69  auto it = find(item);
70  if (it != deque<T>::end())
71  deque<T>::erase(it);
72  }
73 
75  // cppcheck-suppress constParameter
76  bool contains(T const &item) const
77  { return find(item) != deque<T>::end(); }
78 
80  size_type count() const { return deque<T>::size(); }
81 
83  T head()
84  { if (!deque<T>::empty()) return deque<T>::front();
85  T *dummy = nullptr; return myth_deque_init(dummy); }
87  T head() const
88  { if (!deque<T>::empty()) return deque<T>::front();
89  T *dummy = NULL; return myth_deque_init(dummy); }
90 
92  T tail()
93  { if (!deque<T>::empty()) return deque<T>::back();
94  T *dummy = nullptr; return myth_deque_init(dummy); }
96  T tail() const
97  { if (!deque<T>::empty()) return deque<T>::back();
98  T *dummy = NULL; return myth_deque_init(dummy); }
99 };
100 
101 #endif // MYTH_DEQUE_H
MythDeque::remove
void remove(T const item)
Removes any item from list. O(n).
Definition: mythdeque.h:67
MythDeque::find
iterator find(T const item)
Finds an item in the list via linear search O(n).
Definition: mythdeque.h:49
back
static guint32 * back
Definition: goom_core.cpp:29
MythDeque::head
T head() const
Returns item at head of list. O(1).
Definition: mythdeque.h:87
d
static const uint16_t * d
Definition: iso6937tables.cpp:1025
MythDeque::tail
T tail()
Returns item at tail of list. O(1).
Definition: mythdeque.h:92
NULL
#define NULL
Definition: H2645Parser.h:67
myth_deque_init
T myth_deque_init(const T *)
Definition: mythdeque.h:12
MythDeque< QStringList >::const_iterator
typename deque< QStringList >::const_iterator const_iterator
Definition: mythdeque.h:45
MythDeque< QStringList >::size_type
typename deque< QStringList >::size_type size_type
Definition: mythdeque.h:46
MythDeque::tail
T tail() const
Returns item at tail of list. O(1).
Definition: mythdeque.h:96
uint
unsigned int uint
Definition: compat.h:140
MythDeque
MythDeque is similar to QPtrQueue, while being based off deque, this allows that items that are not a...
Definition: mythdeque.h:29
MythDeque::head
T head()
Returns item at head of list. O(1).
Definition: mythdeque.h:83
MythDeque::contains
bool contains(T const &item) const
Returns true if item is in list. O(n).
Definition: mythdeque.h:76
MythDeque::enqueue
void enqueue(T d)
Adds item to the back of the list. O(1).
Definition: mythdeque.h:42
MythDeque::dequeue
T dequeue()
Removes item from front of list and returns a copy. O(1).
Definition: mythdeque.h:32
MythDeque::count
size_type count() const
Returns size of list. O(1).
Definition: mythdeque.h:80
MythDeque::find
const_iterator find(T const item) const
Finds an item in the list via linear search O(n).
Definition: mythdeque.h:58
MythDeque< QStringList >::iterator
typename deque< QStringList >::iterator iterator
Definition: mythdeque.h:44