source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Buffer.h @ 2291

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