NAMD
SortedArray.h
Go to the documentation of this file.
1 
7 #ifndef SRTARRAY_H
8 #define SRTARRAY_H
9 
10 #include "SortableResizeArray.h"
11 
12 template <class Elem> class SortedArray: public SortableResizeArray<Elem> {
13 
14  protected:
15 
16  int isSorted;
17 
18  public:
19 
20  SortedArray(void) : SortableResizeArray<Elem>() {
21  isSorted = 1;
22  }
23 
24  SortedArray(int s) : SortableResizeArray<Elem>(s) {
25  isSorted = 1;
26  }
27 
29  if(!(isSorted = sa.isSorted)) sort();
30  isSorted = 1;
31  }
32 
34  SortableResizeArray<Elem>(ra) {
35  sort(); isSorted = 1;
36  }
37 
40  isSorted = sa.isSorted;
41  return(*this);
42  }
43 
46  sort(); isSorted = 1;
47  return(*this);
48  }
49 
50  int load(const Elem& elem) {
51  isSorted = 0;
52  return(ResizeArray<Elem>::add(elem));
53  }
54 
55  int add(const Elem& elem) {
56  return(insert(elem));
57  }
58 
59  void del(const Elem & elem) {
60  int found = bsearch(elem);
61  if (this->size() != 0 && this->rep[found] == elem) {
63  }
64  }
65 
67 
68  int bsearch(const Elem& elem) {
69  if (!isSorted) sort();
71  }
72 
73  inline int insert(const Elem& elem);
74 
75  int index(const Elem& elem) { return bsearch(elem); }
76 
77  inline Elem *find(const Elem& elem);
78 };
79 
80 template <class Elem>
81 inline int SortedArray<Elem>::insert(const Elem& elem) {
82  int found = bsearch(elem);
83  if (found == -1) {
84  return (ResizeArray<Elem>::insert(elem, 0));
85  }
86  if (found == (this->size()-1) && this->rep[found] < elem) {
87  return (ResizeArray<Elem>::insert(elem, this->size()));
88  } else {
89  return (ResizeArray<Elem>::insert(elem, found));
90  }
91 }
92 
93 template <class Elem>
94 inline Elem * SortedArray<Elem>::find(const Elem& elem) {
95  int found = bsearch(elem);
96  if ( found < 0 || found == this->size() )
97  return ((Elem *)NULL);
98  if (this->rep[found] == elem) {
99  return (&(this->rep[found]));
100  } else {
101  return ((Elem *)NULL);
102  }
103 }
104 
105 #endif
ResizeArrayRaw< Elem > rep
Definition: ResizeArray.h:31
void del(int index, int num=1)
Definition: ResizeArray.h:104
int add(const Elem &elem)
Definition: SortedArray.h:55
int insert(const Elem &elem)
SortedArray(int s)
Definition: SortedArray.h:24
void sort(void)
Definition: SortedArray.h:66
int bsearch(const Elem &elem)
Definition: SortedArray.h:68
SortedArray< Elem > & operator=(SortedArray< Elem > &sa)
Definition: SortedArray.h:38
SortableResizeArray< Elem > & operator=(SortableResizeArray< Elem > &sa)
int index(const Elem &elem)
Definition: SortedArray.h:75
SortedArray(SortableResizeArray< Elem > &ra)
Definition: SortedArray.h:33
SortedArray(SortedArray< Elem > &sa)
Definition: SortedArray.h:28
void del(const Elem &elem)
Definition: SortedArray.h:59
SortedArray(void)
Definition: SortedArray.h:20
Elem * find(const Elem &elem)
int size(void) const
Definition: ResizeArray.h:127
int load(const Elem &elem)
Definition: SortedArray.h:50