Changeset 461 for trunk/poppler/mypoppler/goo/GooVector.h
 Timestamp:
 Aug 27, 2010, 11:00:52 AM (11 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/poppler/mypoppler/goo/GooVector.h
r257 r461 1 1 //======================================================================== 2 2 // 3 // This file comes from pdftohtml project 4 // http://pdftohtml.sourceforge.net 3 // GooVector.h 5 4 // 6 // Copyright from:7 // Gueorgui Ovtcharov8 // Rainer Dorsch <http://www.ra.informatik.unistuttgart.de/~rainer/>9 // Mikhail Kruk <meshko@cs.brandeis.edu>5 // This file is licensed under the GPLv2 or later 6 // 7 // Copyright 2010 David Benjamin <davidben@mit.edu> 8 // Copyright 2010 Albert Astals Cid <aacid@kde.org> 10 9 // 11 10 //======================================================================== 12 11 13 #ifndef GOO_VECTOR_H14 #define GOO_VECTOR_H15 #include "goo/gtypes.h"16 12 17 13 18 template<class T> 19 class GooVector{ 20 private: 21 22 int _size; 23 T* last; 24 T* storage; 25 26 void resize(){ 27 if (_size==0) _size=2;else _size=2*_size; 28 T *tmp=new T[_size]; 29 if (storage){ 30 last=copy(storage,last,tmp); 31 delete [] storage; 32 } 33 else last=tmp; 34 storage=tmp; 35 } 14 #ifndef GOO_GOOVECTOR_H 15 #define GOO_GOOVECTOR_H 36 16 37 T* copy(T* src1,T* src2,T* dest){ 38 T* tmp=src1; 39 T* d=dest; 40 while(tmp!=src2){ 41 *d=*tmp; 42 d++;tmp++; 43 } 44 return d; 45 } 46 47 public: 48 typedef T* iterator; 49 50 GooVector(){ 51 _size=0; 52 last=0; 53 storage=0; 54 } 55 56 57 58 virtual ~GooVector(){ 59 delete[] storage ; 60 } 61 62 void reset(){ 63 last=storage; 64 } 65 66 int size(){ 67 return (laststorage); 68 } 69 void push_back(const T& elem){ 70 if (!storage(size() >=_size)) resize(); 71 *last=elem; 72 last++; 73 74 75 } 76 77 78 T pop_back() { 79 if (last!=storage) last; 80 81 return *last; 82 } 83 84 85 T operator[](unsigned int i){ 86 return *(storage+i); 87 } 88 89 90 GBool isEmpty() const{ 91 return !_size  (last==storage) ; 92 } 93 94 95 96 iterator begin() const{ 97 return storage; 98 } 99 100 iterator end() const { 101 return last; 102 } 103 }; 17 #ifdef USE_GCC_PRAGMAS 18 #pragma interface 104 19 #endif 105 20 21 #include <new> // vector implementations need placementnew 106 22 23 #include <assert.h> 24 #include <stdlib.h> 107 25 108 26 /* Mostly STLcompatible vector class. Should correctly call constructors and 27 * destructors, but does not carefully handle alignment requirements. */ 28 29 template<class T> class GooVector { 30 public: 31 /* various STLcompatible typedefs */ 32 typedef T value_type; 33 typedef T* pointer; 34 typedef T& reference; 35 typedef const T& const_reference; 36 typedef size_t size_type; 37 typedef int difference_type; 38 typedef T* iterator; 39 typedef const T* const_iterator; 40 // TODO: reverse_iterator, if we feel like it 109 41 110 42 GooVector() : m_data(NULL), m_capacity(0), m_size(0) {} 43 explicit GooVector(size_type n) : m_data(NULL), m_capacity(0), m_size(0) { 44 resize(n); 45 } 46 explicit GooVector(size_type n, const T& t) : m_data(NULL), m_capacity(0), m_size(0) { 47 resize(n, t); 48 } 49 explicit GooVector(const GooVector& gv) : m_data(NULL), m_capacity(0), m_size(0) { 50 reserve(gv.size()); 51 for (size_type i = 0; i < m_size; i++) { 52 push_back(gv[i]); 53 } 54 } 111 55 56 ~GooVector() { 57 clear(); 58 } 112 59 60 iterator begin() { return m_data; } 61 const_iterator begin() const { return m_data; } 62 iterator end() { return m_data + m_size; } 63 const_iterator end() const { return m_data + m_size; } 113 64 65 size_type size() const { return m_size; } 66 size_type capacity() const { return m_capacity; } 67 68 bool empty() const { return m_size == 0; } 69 70 reference operator[] (size_type n) { return m_data[n]; } 71 const_reference operator[] (size_type n) const { return m_data[n]; } 72 73 reference at(size_type n) { 74 assert(n < m_size); 75 return m_data[n]; 76 } 77 const_reference at(size_type n) const { 78 assert(n < m_size); 79 return m_data[n]; 80 } 81 82 reference front() { assert(!empty()); return m_data[0]; } 83 const_reference front() const { assert(!empty()); return m_data[0]; } 84 85 reference back() { assert(!empty()); return m_data[m_size1]; } 86 const_reference back() const { assert(!empty()); return m_data[m_size1]; } 87 88 void push_back(const T& v) { 89 reserve(m_size + 1); 90 place_new(m_data + m_size, v); 91 m_size++; 92 } 93 void pop_back() { 94 assert(!empty()); 95 m_size; 96 destruct(m_data + m_size); 97 } 98 99 void clear() { 100 for (size_t i = 0; i < m_size; i++) { 101 destruct(m_data + i); 102 } 103 m_size = 0; 104 free(m_data); 105 m_data = NULL; 106 m_capacity = 0; 107 } 108 109 void reserve(size_type cap) { 110 if (m_capacity >= cap) return; 111 // make sure we always at least double 112 if (m_capacity*2 > cap) 113 cap = m_capacity*2; 114 resize_internal(cap); 115 } 116 117 void resize(size_type n) { resize(n, T()); } 118 void resize(size_type n, const T& t) { 119 reserve(n); 120 while (m_size < n) 121 push_back(t); 122 while (m_size > n) 123 pop_back(); 124 } 125 126 private: 127 T *m_data; 128 size_type m_capacity; 129 size_type m_size; 130 131 inline void destruct(T *obj) { 132 obj>~T(); 133 } 134 inline void place_new(T *loc, const T& v) { 135 new (loc) T(v); 136 } 137 138 inline void resize_internal(size_type new_cap) { 139 assert(new_cap >= m_capacity); 140 // To be correct with ctors and dtors, we do not use realloc and friends. 141 // A more efficient implementation would specialize for POD types and just 142 // realloc() or something. Meh, if we care, we ought to use just STL's 143 T *new_data = (T*) malloc(sizeof(T) * new_cap); 144 assert(new_data); 145 // Move over old data 146 if (m_data) { 147 for (size_type i = 0; i < m_size; i++) { 148 place_new(new_data + i, m_data[i]); 149 destruct(m_data + i); 150 } 151 free(m_data); 152 } 153 // And set the new values 154 m_data = new_data; 155 m_capacity = new_cap; 156 } 157 }; 158 159 #endif
Note: See TracChangeset
for help on using the changeset viewer.