Whiteboard
An interface and tools for visualizing large and complex datasets
SVector.h
Go to the documentation of this file.
1 #ifndef SVECTOR_H_
2 #define SVECTOR_H_
3 
4 using namespace std;
5 
6 #include <vector>
7 #include <iterator>
8 #include <assert.h>
9 #include <algorithm>
10 #include "base/ErrorHandling.h"
11 
12 //#define FORCE_DEBUG
13 
14 template <class T>
15 class svec : public vector<T>
16 {
17  public:
18 
19  int isize() const {return (int)vector<T>::size();}
20  long long lsize() const {return vector<T>::size();}
21 
22 #ifdef FORCE_DEBUG
23  const T & operator[] (long long i) const {
24  if (i>= lsize()) {
25  cout << "ERROR: i=" << i << " lsize=" << lsize() << endl;
26  ThrowError("i>=lsize()", "svec");
27  }
28  if (i<0) {
29  cout << "ERROR: i=" << i << " lsize=" << lsize() << endl;
30  ThrowError("i<0", "svec");
31  }
32  //assert(i<lsize());
33  //assert(i>=0);
34  return vector<T>::operator[](i);
35  }
36 
37  T & operator[] (long long i) {
38 
39  if (i>= lsize()) {
40  cout << "ERROR: i=" << i << " lsize=" << lsize() << endl;
41  ThrowError("i>=lsize()", "svec");
42  }
43  if (i<0) {
44  cout << "ERROR: i=" << i << " lsize=" << lsize() << endl;
45  ThrowError("i<0", "svec");
46  }
47  //assert(i<lsize());
48  //assert(i>=0);
49  return vector<T>::operator[](i);
50  }
51 #endif
52 
53 
54 };
55 
56 
57 template <class T>
58 class vec : public svec<T>
59 {
60 };
61 
62 template<class T>
63 void Sort(svec<T>& v)
64 {
65  sort(v.begin(), v.end());
66 }
67 
68 
69 template<class T>
71 {
72  sort(v.begin(), v.end());
73 
74  long long i;
75  long long k = 0;
76  for (i=0; i<v.lsize(); i++) {
77  v[k] = v[i];
78  while (i+1<v.lsize() && !(v[k] < v[i+1]))
79  i++;
80 
81  k++;
82  }
83  v.resize(k);
84 }
85 
86 template<class T>
87 long long BinSearch(svec<T> & v, const T & item)
88 {
89  typename svec<T>::iterator iter;
90  typename svec<T>::iterator begin = v.begin();
91  typename svec<T>::iterator end = v.end();
92  iter = lower_bound(begin, end, item);
93  long long pos = distance(begin, iter);
94 
95  if (pos < 0)
96  return -1;
97 
98  if (pos == v.isize())
99  return -1;
100 
101  if (v[pos] < item || item < v[pos])
102  return -1;
103 
104  return pos;
105 
106 }
107 
108 template<class T>
109 long long BinSearchFuzzy(svec<T> & v, const T & item)
110 {
111  typename svec<T>::iterator iter;
112  typename svec<T>::iterator begin = v.begin();
113  typename svec<T>::iterator end = v.end();
114  iter = lower_bound(begin, end, item);
115  long pos = distance(begin, iter);
116 
117  if (pos < 0)
118  return -1;
119 
120  return pos;
121 
122 }
123 
124 
125 
126 
127 #endif //SVECTOR_H_
128 
129 
long long lsize() const
Definition: SVector.h:20
long long BinSearch(svec< T > &v, const T &item)
Definition: SVector.h:87
void Sort(svec< T > &v)
Definition: SVector.h:63
Definition: SVector.h:15
Definition: SVector.h:58
long long BinSearchFuzzy(svec< T > &v, const T &item)
Definition: SVector.h:109
void ThrowError(const string &info, const string &type)
Definition: ErrorHandling.h:29
int isize() const
Definition: SVector.h:19
void UniqueSort(svec< T > &v)
Definition: SVector.h:70