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  // destruct elements, free storage, set size to 0
87  void clear() { rep.clear(); }
88 
89  // Set all elements to a given value (like 0).
90  void setall(const Elem &elem) {
91  iterator i = begin();
92  iterator e = end();
93  for ( ; i != e; ++i ) *i = elem;
94  }
95 
96  // Add element to end of array
97  int add (const Elem &elem) {
98  int end=rep.size();
99  rep.ins(elem, end);
100  return(end);
101  }
102 
103  // delete num elements from current index
104  void del(int index, int num=1) {
105  rep.del(index,num);
106  }
107 
108  // insert element at index
109  int insert (const Elem& elem, int index) {
110  rep.ins(elem,index);
111  return (index);
112  }
113 
114  // array member access (can be lvalue) that grows array.
115  inline Elem & item(int i) {
116  i = ( i < 0 ? 0 : i );
117  if ((i+1) > size())
118  resize(i+1);
119  return rep.array[i];
120  }
121 
122  // array member access (can be lvalue) no checks.
123  inline Elem & operator[](int index) { return rep.array[index]; }
124  inline const Elem & operator[](int index) const { return rep.array[index]; }
125 
126  // returns size of ResizeArray
127  inline int size(void) const { return rep.size(); }
128 
129  // DMK - MIC Support - Allow us to see the buffer's size, not just how much of it is used
130  #if NAMD_MIC != 0
131  inline int bufSize(void) const { return rep.bufSize(); }
132  #endif
133 
134  // reduce storage size
135  // void reduce(void) { rep.reduce(); }
136 
137  inline int find(const Elem &e) const { return rep.find(e); }
138 
139 };
140 
141 #endif
const Elem * const_iterator
Definition: ResizeArray.h:38
void copy(ResizeArray< Elem > &ra)
Definition: ResizeArray.h:59
ResizeArrayRaw< Elem > rep
Definition: ResizeArray.h:31
void del(int index, int num=1)
Definition: ResizeArray.h:104
int find(const Elem &e) const
Definition: ResizeArray.h:137
int insert(const Elem &elem, int index)
Definition: ResizeArray.h:109
ResizeArray(void)
Definition: ResizeArray.h:43
Elem & item(int i)
Definition: ResizeArray.h:115
Elem * iterator
Definition: ResizeArray.h:35
ResizeArrayIter< T > & operator=(const ResizeArrayIter< T > &iter)
bool shared() const
Definition: ResizeArray.h:72
Elem & operator[](int index)
Definition: ResizeArray.h:123
iterator end(void)
Definition: ResizeArray.h:37
void clear()
Definition: ResizeArray.h:87
void setall(const Elem &elem)
Definition: ResizeArray.h:90
ResizeArray(Elem **array, int arraySize, int allocSize=0)
Definition: ResizeArray.h:77
const_iterator const_end(void) const
Definition: ResizeArray.h:40
const Elem & operator[](int index) const
Definition: ResizeArray.h:124
int add(const Elem &elem)
Definition: ResizeArray.h:97
void resize(int i)
Definition: ResizeArray.h:84
void swap(ResizeArray< Elem > &ra)
Definition: ResizeArray.h:64
const_iterator const_begin(void) const
Definition: ResizeArray.h:39
int size(void) const
Definition: ResizeArray.h:127
~ResizeArray(void)
Definition: ResizeArray.h:80
ResizeArray(int s)
Definition: ResizeArray.h:46
iterator begin(void)
Definition: ResizeArray.h:36