New create widen region algorithm.
[xy_vsfilter.git] / src / subtitles / xy_circular_array_queue.h
blob56a75cdc340c3e7eb27122069c602075d2740221
1 #ifndef __XY_CIRCULAR_ARRAY_QUEUE_HPP_47831C93_EDAA_44B9_94F2_6FF36839D1A6__
2 #define __XY_CIRCULAR_ARRAY_QUEUE_HPP_47831C93_EDAA_44B9_94F2_6FF36839D1A6__
4 template <typename ElementType>
5 class XYCircularArrayQueue
7 public:
8 typedef ElementType value_type;
9 typedef ElementType& reference;
10 typedef const ElementType& const_reference;
11 public:
12 /***
13 * @brief Default constructor creates no elements.
14 **/
15 explicit XYCircularArrayQueue()
16 :_elements(NULL),
17 _head(0),
18 _tail(0),
19 _CAPACITY(0)
23 ~XYCircularArrayQueue()
25 delete[] _elements;
28 inline int init(size_t capacity)
30 _CAPACITY = capacity;
31 _elements = new value_type[_CAPACITY];
32 return _elements ? 0 : -1;
35 inline size_t capacity()
37 return _CAPACITY;
40 inline bool full() const
42 return size() >= (_CAPACITY -1);
45 inline bool empty() const
47 return _head == _tail;
50 /***
51 * @return: the number of elements in the @queue.
52 **/
53 inline size_t size() const
55 return (_CAPACITY -_head+_tail) %_CAPACITY;
58 /***
59 * @return: the number of free elements in the @queue.
60 **/
61 inline size_t free() const
63 return _CAPACITY - (_CAPACITY -_head+_tail) %_CAPACITY;
66 /***
67 * @return: a read/write reference to the i-th element.
68 **/
69 inline reference get_at(int i)
71 i += _head;
72 i %= _CAPACITY;
73 return _elements[i];
76 /***
77 * @return: a read-only reference to the i-th element.
78 **/
79 inline const_reference get_at(int i) const
81 i += _head;
82 i %= _CAPACITY;
83 return _elements[i];
86 /***
87 * @return: a read/write reference to the last element
88 **/
89 inline reference back()
91 return _elements[(_CAPACITY +_tail -1) % _CAPACITY];
94 /***
95 * @return: a read-only reference to the last element
96 **/
97 inline const_reference back() const
99 return _elements[(_CAPACITY +_tail -1) % _CAPACITY];
102 /***
103 * @return: 0 if succeeded, -1 if failed
105 inline int push_back(const_reference value)
107 if(size() == (_CAPACITY -1))
109 return -1;
111 else
113 _elements[_tail] = value;
114 _tail = (_tail+1) % _CAPACITY;
115 return 0;
119 inline reference inc_1_at_tail()
121 _tail++;
122 _tail += _CAPACITY;
123 _tail %= _CAPACITY;
124 return _elements[(_CAPACITY +_tail -1) % _CAPACITY];
127 inline void pop_front()
129 if(empty())
131 return;
133 else
135 _head = (_head + 1) % _CAPACITY;
139 inline void pop_back()
141 if(empty())
143 return;
145 else
147 _tail = (_tail - 1 + _CAPACITY) % _CAPACITY;
151 inline void pop_last_n(int n)
153 if (size()<n)
155 _head = _tail = 0;
157 else
159 _tail -= n;
160 _tail += _CAPACITY;
161 _tail %= _CAPACITY;
164 private:
165 value_type* _elements;
166 volatile uint32_t _head;
167 volatile uint32_t _tail;
168 size_t _CAPACITY;
172 #endif /* #ifndef __XY_CIRCULAR_ARRAY_QUEUE_HPP_47831C93_EDAA_44B9_94F2_6FF36839D1A6__ */