Revision 1025,
1.6 KB
checked in by gumbau, 18 years ago
(diff) |
namespace simplif
|
Rev | Line | |
---|
[774] | 1 | #ifndef GFXTOOLS_BUFFER_INCLUDED // -*- C++ -*-
|
---|
| 2 | #define GFXTOOLS_BUFFER_INCLUDED
|
---|
| 3 |
|
---|
| 4 | #include <gfx/tools/Array.h>
|
---|
| 5 |
|
---|
[1025] | 6 | namespace simplif
|
---|
[774] | 7 | {
|
---|
| 8 | template<class T>
|
---|
| 9 | class buffer : public array<T> {
|
---|
| 10 | protected:
|
---|
| 11 | int fill;
|
---|
| 12 | public:
|
---|
| 13 | buffer() { init(8); }
|
---|
| 14 | buffer(int l) { init(l); }
|
---|
| 15 |
|
---|
| 16 | inline void init(int l) { array<T>::init(l); fill=0; }
|
---|
| 17 |
|
---|
| 18 | inline int add(const T& t);
|
---|
| 19 | inline void reset();
|
---|
| 20 | inline int find(const T&);
|
---|
| 21 | inline T remove(int i);
|
---|
| 22 | inline int addAll(const buffer<T>& buf);
|
---|
| 23 | inline void removeDuplicates();
|
---|
| 24 |
|
---|
| 25 | inline int length() const { return fill; }
|
---|
| 26 | inline int maxLength() const { return len; }
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | template<class T>
|
---|
| 31 | inline int buffer<T>::add(const T& t)
|
---|
| 32 | {
|
---|
| 33 | if( fill == len )
|
---|
| 34 | resize( len*2 );
|
---|
| 35 |
|
---|
| 36 | data[fill] = t;
|
---|
| 37 |
|
---|
| 38 | return fill++;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | template<class T>
|
---|
| 42 | inline void buffer<T>::reset()
|
---|
| 43 | {
|
---|
| 44 | fill = 0;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | template<class T>
|
---|
| 48 | inline int buffer<T>::find(const T& t)
|
---|
| 49 | {
|
---|
| 50 | for(int i=0;i<fill;i++)
|
---|
| 51 | if( data[i] == t )
|
---|
| 52 | return i;
|
---|
| 53 |
|
---|
| 54 | return -1;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | template<class T>
|
---|
| 58 | inline T buffer<T>::remove(int i)
|
---|
| 59 | {
|
---|
| 60 | #ifdef SAFETY
|
---|
| 61 | assert( i>=0 );
|
---|
| 62 | assert( i<fill );
|
---|
| 63 | #endif
|
---|
| 64 |
|
---|
| 65 | fill--;
|
---|
| 66 | T temp = data[i];
|
---|
| 67 | data[i] = data[fill];
|
---|
| 68 |
|
---|
| 69 | return temp;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | template<class T>
|
---|
| 73 | inline int buffer<T>::addAll(const buffer<T>& buf)
|
---|
| 74 | {
|
---|
| 75 | for(int i=0; i<buf.fill; i++)
|
---|
| 76 | add(buf(i));
|
---|
| 77 |
|
---|
| 78 | return fill;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | template<class T>
|
---|
| 82 | inline void buffer<T>::removeDuplicates()
|
---|
| 83 | {
|
---|
| 84 | for(int i=0; i<fill; i++)
|
---|
| 85 | {
|
---|
| 86 | for(int j=i+1; j<fill; )
|
---|
| 87 | {
|
---|
| 88 | if( data[j] == data[i] )
|
---|
| 89 | remove(j);
|
---|
| 90 | else
|
---|
| 91 | j++;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | // GFXTOOLS_BUFFER_INCLUDED
|
---|
| 98 | #endif
|
---|
Note: See
TracBrowser
for help on using the repository browser.