NAMD
SortableResizeArray.h
Go to the documentation of this file.
1 
7 #ifndef SRTABARRAY_H
8 #define SRTABARRAY_H
9 
10 #include <string.h>
11 #include "ResizeArray.h"
12 
13 template <class Elem> class SortableResizeArray : public ResizeArray<Elem> {
14 
15  private:
16 
17  inline void swap(int offset, int i, int j) {
18  char tmp[sizeof(Elem)];
19  register Elem *r = (this->rep.array+offset);
20  memcpy(tmp, (char *)&r[i], sizeof(Elem));
21  memcpy((char *)&(r[i]), (char *)&(r[j]), sizeof(Elem));
22  memcpy((char *)&r[j], tmp, sizeof(Elem));
23  }
24 
25 
26  inline void siftup(int offset, unsigned int i, unsigned int size) {
27  char tmp[sizeof(Elem)];
28  register unsigned int j;
29  register Elem *r = (this->rep.array+offset);
30 
31  while ((j = 2*i+1) < size) {
32  if (j+1 < size) {
33  if (r[j] < r[j+1])
34  j = j+1;
35  }
36  if (r[i] < r[j]) {
37  memcpy(tmp, (char *)&r[i], sizeof(Elem));
38  memcpy((char *)&(r[i]), (char *)&(r[j]), sizeof(Elem));
39  memcpy((char *)&r[j], tmp, sizeof(Elem));
40  i = j;
41  } else {
42  break;
43  }
44  }
45  }
46 
47  public:
48 
49  SortableResizeArray(void) : ResizeArray<Elem>() { init(); }
50 
51  SortableResizeArray(int size) : ResizeArray<Elem>(size) { init(); }
52 
54  ResizeArray<Elem>(ra) { init(); }
55 
57  ResizeArray<Elem>(ra) { init(); }
58 
60  ResizeArray<Elem>(ra) { init(); }
61 
63  ResizeArray<Elem>(ra) { init(); }
64 
65  SortableResizeArray(Elem* * const r, int numElem, int maxElem) :
66  ResizeArray<Elem>(r,numElem,maxElem) { init(); }
67 
70  return(*this);
71  }
72 
74 
75  void init(void) { }
76 
77  void sort(void) { sort(0, this->rep.size()-1); }
78 
79  // Heap Sort - worst case is O(n log n)
80  // bot = bottom element of sort range
81  // top = top element of sort range
82  void sort(int bot, int top) {
83  int index, size;
84  if (top > this->rep.size()) top = this->rep.size();
85  size = top - bot + 1;
86 
87  // Make all sub-heaps
88  for ( index = size/2-1; index > 0; index-- )
89  siftup(bot, index, size);
90 
91  // Take top element of overall heap, and put on top
92  for ( index = size; index > 1; index-- ) {
93  siftup(bot, 0, index);
94  swap(bot, 0, index-1);
95  }
96  }
97 
98  inline void uniq(void);
99 
100  // Search returns index of position where elem should be inserted.
101  // This is equal to the first position equal to elem
102  // or the first item greater than elem
103  // if elem is larger than any item, it returns
104  // the index just beyond the end of the list
105  // We stick with the < operator only
106  int bsearch(const Elem& elem) const {
107  int test;
108  int bot = -1;
109  int top = this->size();
110  if (this->size() == 0) return (-1);
111  while (top - bot > 1) {
112  if ( this->rep.array[test = (bot+top)/2] < elem )
113  bot = test;
114  else
115  top = test;
116  }
117  return(top);
118  }
119 
120 };
121 
122 template <class Elem>
124  if (this->size()) {
125  int oldIndex=0;
126  int newIndex=0;
127  while (++oldIndex < this->size()) {
128  if ( ! ( this->rep.array[oldIndex] == this->rep.array[newIndex] ) ) {
129  if (++newIndex != oldIndex)
130  memcpy((void *)&(this->rep.array[newIndex]),
131  (void *)&(this->rep.array[oldIndex]),
132  sizeof(Elem));
133  } else {
134  this->rep.array[oldIndex].~Elem();
135  }
136  }
137  this->rep.arraySize = ++newIndex;
138  }
139 }
140 
141 #endif
int bsearch(const Elem &elem) const
ResizeArrayRaw< Elem > rep
Definition: ResizeArray.h:31
SortableResizeArray(const ResizeArray< Elem > *ra)
SortableResizeArray(ResizeArray< Elem > &ra)
SortableResizeArray< Elem > & operator=(SortableResizeArray< Elem > &sa)
void sort(int bot, int top)
SortableResizeArray(const SortableResizeArray< Elem > *ra)
SortableResizeArray(Elem **const r, int numElem, int maxElem)
int size(void) const
Definition: ResizeArray.h:127
SortableResizeArray(SortableResizeArray< Elem > &ra)