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*) { return nullptr; }
13 template<>
14 inline int myth_deque_init(const int*) { return 0; }
15 template<>
16 inline uint myth_deque_init(const uint*) { return 0; }
17 template<>
18 inline QString myth_deque_init(const QString*) { return QString(); }
19 template<>
20 inline QStringList myth_deque_init(const QStringList*) { 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  typedef typename deque<T>::iterator iterator;
45  typedef typename deque<T>::const_iterator const_iterator;
46  typedef typename deque<T>::size_type size_type;
47 
49  iterator find(T const item)
50  {
51  for (iterator 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 (const_iterator 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  iterator it = find(item);
70  if (it != deque<T>::end())
71  deque<T>::erase(it);
72  }
73 
75  bool contains(T const &item) const
76  { return find(item) != deque<T>::end(); }
77 
79  size_type count() const { return deque<T>::size(); }
80 
82  T head()
83  { if (!deque<T>::empty()) return deque<T>::front();
84  T *dummy = nullptr; return myth_deque_init(dummy); }
86  const T head() const
87  { if (!deque<T>::empty()) return deque<T>::front();
88  T *dummy = NULL; return myth_deque_init(dummy); }
89 
91  T tail()
92  { if (!deque<T>::empty()) return deque<T>::back();
93  T *dummy = nullptr; return myth_deque_init(dummy); }
95  const T tail() const
96  { if (!deque<T>::empty()) return deque<T>::back();
97  T *dummy = NULL; return myth_deque_init(dummy); }
98 };
99 
100 #endif // __MYTH_DEQUE_H__
#define NULL
Definition: H264Parser.h:62
T tail()
Returns item at tail of list. O(1).
Definition: mythdeque.h:91
iterator find(T const item)
Finds an item in the list via linear search O(n).
Definition: mythdeque.h:49
unsigned int uint
Definition: compat.h:140
T head()
Returns item at head of list. O(1).
Definition: mythdeque.h:82
size_type count() const
Returns size of list. O(1).
Definition: mythdeque.h:79
void enqueue(T d)
Adds item to the back of the list. O(1).
Definition: mythdeque.h:42
const_iterator find(T const item) const
Finds an item in the list via linear search O(n).
Definition: mythdeque.h:58
T myth_deque_init(const T *)
Definition: mythdeque.h:12
deque< T >::iterator iterator
Definition: mythdeque.h:44
static const uint16_t * d
const T head() const
Returns item at head of list. O(1).
Definition: mythdeque.h:86
T dequeue()
Removes item from front of list and returns a copy. O(1).
Definition: mythdeque.h:32
bool contains(T const &item) const
Returns true if item is in list. O(n).
Definition: mythdeque.h:75
deque< T >::size_type size_type
Definition: mythdeque.h:46
deque< T >::const_iterator const_iterator
Definition: mythdeque.h:45
const T tail() const
Returns item at tail of list. O(1).
Definition: mythdeque.h:95
static guint32 * back
Definition: goom_core.c:34
void remove(T const item)
Removes any item from list. O(n).
Definition: mythdeque.h:67
MythDeque is similar to QPtrQueue, while being based off deque, this allows that items that are not a...
Definition: mythdeque.h:28