NAMD
ResizeArray.h
Go to the documentation of this file.
1 
7 /*
8  ResizeArray template
9  Uses simple contingous array allocation in a hidden manner
10  so that array object can have items added without limit
11  Suffers from memory fragmentation during resizing
12 
13  Copy construction and assignment are forbidden to avoid
14  confusion with old reference-counted implementation.
15  Use copy() and swap() to explicitly transfer storage.
16 */
17 
18 #ifndef RESIZEARRAY_H
19 #define RESIZEARRAY_H
20 
21 #include "ResizeArrayRaw.h"
22 
23 // Need this juju to use templated friend below
24 template <class Type> class ResizeArrayIter;
25 template <class Type> class ResizeArrayRaw;
26 
27 template <class Elem> class ResizeArray {
28  friend class ResizeArrayIter<Elem>;
29 
30  protected:
32 
33  public:
34  // STL style iterators
35  typedef Elem* iterator;
36  iterator begin(void) { return rep.array; }
37  iterator end(void) { return rep.array + rep.arraySize; }
38  typedef const Elem* const_iterator;
39  const_iterator const_begin(void) const { return rep.array; }
40  const_iterator const_end(void) const { return rep.array + rep.arraySize; }
41 
42  // Various Constructors
43  ResizeArray(void) { }
44 
45  // Constructor make ResizeArray of predefined size
46  ResizeArray(int s) {
47  rep.resize(s);
48  }
49 
50 private:
51  inline ResizeArray(ResizeArray<Elem> &ra);
52 
53  inline ResizeArray(const ResizeArray<Elem>* ra);
54 
56 
57 public:
58  // Make copy of ResizeArrayRaw (for use in messages)
59  void copy(ResizeArray<Elem> &ra) {
60  rep.copy(ra.rep);
61  }
62 
63  // Swap ResizeArrayRaw (for use in messages to avoid copy)
64  void swap(ResizeArray<Elem> &ra) {
65  // uses synthesized copy constructor and assignment operator
67  rep = ra.rep;
68  ra.rep = tmp;
69  }
70 
71  // does some other ResizeArray have a handle to our data
72  bool shared() const {
73  return 0;
74  }
75 
76  // Constructor to take-in pre-existing array
77  ResizeArray(Elem * * array, int arraySize, int allocSize=0) :
78  rep(array, arraySize, allocSize) { }
79 
80  ~ResizeArray(void) { rep.free(); }
81 
82  // If array is expanded - new elements are default constructed
83  // if array is reduced, removed elements have ~Elem() run
84  void resize(int i) { rep.resize(i); }
85 
86  // Allows users to request additional memory to avoid
87  // reallocation later
88  void reserve(int i) { rep.resizeRaw(i); }
89 
90  // destruct elements, free storage, set size to 0
91  void clear() { rep.clear(); }
92 
93  // Set all elements to a given value (like 0).
94  void setall(const Elem &elem) {
95  iterator i = begin();
96  iterator e = end();
97  for ( ; i != e; ++i ) *i = elem;
98  }
99 
100  // Add element to end of array
101  int add (const Elem &elem) {
102  int end=rep.size();
103  rep.ins(elem, end);
104  return(end);
105  }
106 
107  // delete num elements from current index
108  void del(int index, int num=1) {
109  rep.del(index,num);
110  }
111 
112  // insert element at index
113  int insert (const Elem& elem, int index) {
114  rep.ins(elem,index);
115  return (index);
116  }
117 
118  // array member access (can be lvalue) that grows array.
119  inline Elem & item(int i) {
120  i = ( i < 0 ? 0 : i );
121  if ((i+1) > size())
122  resize(i+1);
123  return rep.array[i];
124  }
125 
126  // array member access (can be lvalue) no checks.
127  inline Elem & operator[](int index) { return rep.array[index]; }
128  inline const Elem & operator[](int index) const { return rep.array[index]; }
129 
130  // returns size of ResizeArray
131  inline int size(void) const { return rep.size(); }
132 
133  // DMK - MIC Support - Allow us to see the buffer's size, not just how much of it is used
134  #if NAMD_MIC != 0
135  inline int bufSize(void) const { return rep.bufSize(); }
136  #endif
137 
138  // reduce storage size
139  // void reduce(void) { rep.reduce(); }
140 
141  inline int find(const Elem &e) const { return rep.find(e); }
142 
143 };
144 
145 #endif
ResizeArray(int s)
Definition: ResizeArray.h:46
int insert(const Elem &elem, int index)
Definition: ResizeArray.h:113
ResizeArrayRaw< Elem > rep
Definition: ResizeArray.h:31
void copy(ResizeArray< Elem > &ra)
Definition: ResizeArray.h:59
int size(void) const
Definition: ResizeArray.h:131
Elem * iterator
Definition: ResizeArray.h:35
void reserve(int i)
Definition: ResizeArray.h:88
const Elem & operator[](int index) const
Definition: ResizeArray.h:128
ResizeArray(void)
Definition: ResizeArray.h:43
ResizeArrayIter< T > & operator=(const ResizeArrayIter< T > &iter)
~ResizeArray(void)
Definition: ResizeArray.h:80
void clear()
Definition: ResizeArray.h:91
int add(const Elem &elem)
Definition: ResizeArray.h:101
void resize(int i)
Definition: ResizeArray.h:84
void setall(const Elem &elem)
Definition: ResizeArray.h:94
const Elem * const_iterator
Definition: ResizeArray.h:38
const_iterator const_begin(void) const
Definition: ResizeArray.h:39
Elem & item(int i)
Definition: ResizeArray.h:119
iterator begin(void)
Definition: ResizeArray.h:36
iterator end(void)
Definition: ResizeArray.h:37
int find(const Elem &e) const
Definition: ResizeArray.h:141
void del(int index, int num=1)
Definition: ResizeArray.h:108
const_iterator const_end(void) const
Definition: ResizeArray.h:40
bool shared() const
Definition: ResizeArray.h:72
void swap(ResizeArray< Elem > &ra)
Definition: ResizeArray.h:64
ResizeArray(Elem **array, int arraySize, int allocSize=0)
Definition: ResizeArray.h:77
Elem & operator[](int index)
Definition: ResizeArray.h:127