1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
// Copyright (C) 2004 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_SLIDING_BUFFER_KERNEl_ABSTRACT_
#ifdef DLIB_SLIDING_BUFFER_KERNEl_ABSTRACT_
#include "../algs.h"
#include "../interfaces/enumerable.h"
#include "../serialize.h"
namespace dlib
{
template <
typename T
>
class sliding_buffer : public enumerable<T>
{
/*!
REQUIREMENTS ON T
T must have a default constructor
INITIAL VALUE
size() == 0
ENUMERATION ORDER
The enumerator will iterate over the elements of the sliding_buffer in the
order (*this)[0], (*this)[1], (*this)[2], ...
WHAT THIS OBJECT REPRESENTS
This object represents an array of T objects. The main
feature of this object is its ability to rotate its contents
left or right. An example will make it clear.
suppose we have the following buffer (assuming T is a char):
"some data!" <-- the data in the buffer
9876543210 <-- the index numbers associated with each character
applying rotate_left(2) to this buffer would give us
"me data!so"
9876543210
if instead of calling rotate_left we call rotate_right(3) instead we would have
"ta!some da"
9876543210
Also note that unless specified otherwise, no member functions
of this object throw exceptions.
!*/
public:
typedef T type;
sliding_buffer (
);
/*!
ensures
- #*this is properly initialized
throws
- std::bad_alloc or any exception thrown by T's constructor.
!*/
virtual ~sliding_buffer (
);
/*!
ensures
- any resources associated with *this have been released
!*/
void clear(
);
/*!
ensures
- #*this has its initial value
throws
- std::bad_alloc or any exception thrown by T's constructor.
if this exception is thrown then #*this is unusable
until clear() is called and succeeds
!*/
void set_size (
unsigned long exp_size
);
/*!
requires
- 0 < exp_size < 32
ensures
- #size() == 2^exp_size
- the value of all elements in the buffer are undefined
- #at_start() == true
throws
- std::bad_alloc or any exception thrown by T's constructor.
if this exception is thrown then #size() == 0
!*/
void rotate_left (
unsigned long amount
);
/*!
ensures
- for all i where 0 <= i < size():
(#*this)[i] == (*this)[(i-amount)&(size()-1)]
i.e. rotates the contents of *this left by amount spaces
- #at_start() == true
!*/
void rotate_right (
unsigned long amount
);
/*!
ensures
- for all i where 0 <= i < size():
(#*this)[i] == (*this)[(i+amount)&(size()-1)]
i.e. rotates the contents of *this right by amount spaces
- #at_start() == true
!*/
unsigned long get_element_id (
unsigned long index
) const;
/*!
requires
- index < size()
ensures
- returns an element id number that uniquely references the element at
the given index. (you can use this id to locate the new position of
an element after the buffer has been rotated)
- returned value is < size()
!*/
unsigned long get_element_index (
unsigned long element_id
) const;
/*!
require
- element_id < size()
ensures
- returns the index of the element with the given element_id.
( (*this)[get_element_index(element_id)] will always refer to the same element
no matter where it has been rotated to)
- returned value is < size()
!*/
const T& operator[] (
unsigned long index
) const;
/*!
requires
- index < size()
ensures
- returns a const reference to the element in *this at position index
!*/
T& operator[] (
unsigned long index
);
/*!
requires
- index < size()
ensures
- returns a reference to the element in *this at position index
!*/
void swap (
sliding_buffer<T>& item
);
/*!
ensures
- swaps *this and item
!*/
private:
// restricted functions
sliding_buffer(sliding_buffer<T>&); // copy constructor
sliding_buffer<T>& operator=(sliding_buffer<T>&); // assignment operator
};
template <
typename T
>
void swap (
sliding_buffer<T>& a,
sliding_buffer<T>& b
) { a.swap(b); }
/*!
provides a global swap function
!*/
template <
typename T
>
void deserialize (
sliding_buffer<T>& item,
std::istream& in
);
/*!
provides deserialization support
!*/
}
#endif // DLIB_SLIDING_BUFFER_KERNEl_ABSTRACT_
|