22 template <
typename SearchPo
intType,
typename DataSetType>
23 BaseSearchSpace<SearchPointType, DataSetType>::BaseSearchSpace(
const Filter* filter,
bool ownsFilter)
24 : _dataset(0), _filter(filter), _ownsFilter(ownsFilter), _sortedUpTo(0), _filteredUpTo(0) {
28 template <
typename SearchPo
intType,
typename DataSetType>
29 BaseSearchSpace<SearchPointType, DataSetType>::BaseSearchSpace(
const BaseSearchSpace<SearchPointType, DataSetType>& other)
30 : _dataset(0), _filter(0), _ownsFilter(false), _sortedUpTo(0), _filteredUpTo(0) {
32 copyPointsFrom(other);
36 template <
typename SearchPo
intType,
typename DataSetType>
40 int size = other.size();
42 memcpy(&(*
this)[0], &other[0], size*
sizeof(SearchPointType));
43 this->setDataSet(other.dataSet());
46 template <
typename SearchPo
intType,
typename DataSetType>
47 BaseSearchSpace<SearchPointType, DataSetType>::~BaseSearchSpace() {
48 if (_ownsFilter)
delete _filter;
51 template <
typename SearchPo
intType,
typename DataSetType>
53 BaseSearchSpace<SearchPointType, DataSetType>* space =
const_cast<BaseSearchSpace<SearchPointType, DataSetType>*
>(
this);
56 return unfilteredSize();
59 template <
typename SearchPo
intType,
typename DataSetType>
61 return (
int)Vector<SearchPointType>::size();
64 template <
typename SearchPo
intType,
typename DataSetType>
65 void BaseSearchSpace<SearchPointType, DataSetType>::cleanSearchSpace() {
67 if (_ownsFilter)
delete _filter;
74 template <
typename SearchPo
intType,
typename DataSetType>
76 if (offset + n > validUpTo()) {
83 offset = qMin(offset, (
int)size());
84 int end = qMin(offset+n, (
int)size());
85 for (
int i=offset; i<end; i++) {
86 result << qMakePair(this->pointName(this->at(i)), this->at(i).dist);
92 template <
typename SearchPo
intType,
typename DataSetType>
93 inline void BaseSearchSpace<SearchPointType, DataSetType>::setFilter(
const Filter* filter,
bool ownsFilter) {
94 if (_ownsFilter)
delete _filter;
97 _ownsFilter = ownsFilter;
103 template <
typename SearchPo
intType,
typename DataSetType>
105 int size = unfilteredSize();
108 if (!_filter || _filter->isAlwaysTrue()) _filteredUpTo = size;
109 if (_filteredUpTo == size)
return;
111 Q_ASSERT(_filteredUpTo <= size);
112 Q_ASSERT(_sortedUpTo <= size);
114 int lastAccepted = _filteredUpTo;
116 for (
int i=_filteredUpTo; i<size; i++) {
117 if (this->validPoint(this->at(i))) {
118 this->at(lastAccepted) = this->at(i);
123 else if (_sortedUpTo > i) {
128 this->resize(lastAccepted);
129 _filteredUpTo = lastAccepted;
133 template <
typename SearchPo
intType,
typename DataSetType>
134 void BaseSearchSpace<SearchPointType, DataSetType>::sortAll() {
136 Q_ASSERT(_filteredUpTo == unfilteredSize());
137 gaia2::sort(this->begin() + _sortedUpTo, this->end());
138 _sortedUpTo = (int)Vector<SearchPointType>::size();
143 template <
typename SearchPo
intType,
typename DataSetType>
145 Q_ASSERT(_filteredUpTo <= unfilteredSize());
146 Q_ASSERT(_sortedUpTo <= unfilteredSize());
148 Vector<SearchPointType>::resize(qMin(n, unfilteredSize()));
152 template <
typename SearchPo
intType,
typename DataSetType>
155 Vector<SearchPointType>::clear();
158 template <
typename SearchPo
intType,
typename DataSetType>
160 Q_ASSERT(_filteredUpTo <= unfilteredSize());
161 Q_ASSERT(_sortedUpTo <= unfilteredSize());
163 float maxValidDist = -1;
164 int validIdx = validUpTo();
165 int start = 0, end = 0;
167 if (validIdx > 0) maxValidDist = this->at(validIdx-1).dist;
168 if (maxValidDist < maxDist) {
171 end = unfilteredSize();
177 if (end == 0)
return;
180 if (maxDist < this->at(start).dist) { clear();
return; }
181 if (maxDist > this->at(end-1).dist) {
return; }
183 while ((end - start) > 1) {
184 int pivotIdx = (start + end) / 2;
185 float distm = this->at(pivotIdx).dist;
186 if (distm > maxDist) end = pivotIdx;
187 else start = pivotIdx;
191 int finalSize = start;
195 this->resize(finalSize);
196 _filteredUpTo = qMin(_filteredUpTo, finalSize);
197 _sortedUpTo = qMin(_sortedUpTo, finalSize);
200 template <
typename SearchPo
intType,
typename DataSetType>
202 int size = unfilteredSize();
204 Q_ASSERT(_filteredUpTo <= size);
205 Q_ASSERT(_sortedUpTo <= size);
207 if (n < 0 || n > size) n = size;
209 if (_filteredUpTo >= n && _sortedUpTo >= n)
return;
212 n = qMin(n, unfilteredSize());
214 std::partial_sort(this->begin(), this->begin() + n, this->end());
219 template <
typename SearchPo
intType,
typename DataSetType>
221 foreach (
const QString&
id, ids) {
222 this->push_back(SearchPointType(dataset->point(
id),
223 dataset->referenceDataSet()->point(
id)));
227 template <
typename SearchPo
intType,
typename DataSetType>
229 QList<int> positionsToRemove;
230 QSet<QString> idSet = QSet<QString>::fromList(ids);
233 for (
typename BaseSearchSpace<SearchPointType, DataSetType>::const_iterator it = this->begin(); it != this->end(); ++it) {
234 if (idSet.contains(it->ptr->name())) {
235 positionsToRemove << (it - this->begin());
240 sort(positionsToRemove.begin(), positionsToRemove.end());
243 for (
int i=(
int)positionsToRemove.size()-1; i>=0; i--) {
244 this->erase(this->begin() + positionsToRemove[i]);
249 int size = unfilteredSize();
250 _sortedUpTo = qMin(_sortedUpTo, size);
251 _filteredUpTo = qMin(_filteredUpTo, size);
255 template <
typename SearchPo
intType>
256 inline bool pointerOrderCompare(
const SearchPointType& p1,
const SearchPointType& p2);
260 inline bool pointerOrderCompare(
const SearchPoint& p1,
const SearchPoint& p2) {
261 return p1.ptr < p2.ptr;
265 inline bool pointerOrderCompare(
const FrozenSearchPoint& p1,
const FrozenSearchPoint& p2) {
266 return p1.idx < p2.idx;
269 template <
typename SearchPo
intType,
typename DataSetType>
271 gaia2::sort(range(*
this), pointerOrderCompare<SearchPointType>);
275 template <
typename SearchPo
intType,
typename DataSetType>
277 Q_ASSERT(_filteredUpTo == 0);
278 Q_ASSERT(_sortedUpTo == 0);
280 typename Vector<SearchPointType>::iterator end = std::set_intersection(range(*
this),
282 this->begin(), pointerOrderCompare<SearchPointType>);
283 this->resize(end - this->begin());
286 template <
typename SearchPo
intType,
typename DataSetType>
288 Q_ASSERT(_filteredUpTo == 0);
289 Q_ASSERT(_sortedUpTo == 0);
291 typename Vector<SearchPointType>::iterator end = std::set_difference(range(*
this),
293 this->begin(), pointerOrderCompare<SearchPointType>);
294 this->resize(end - this->begin());
297 template <
typename SearchPo
intType,
typename DataSetType>
299 Q_ASSERT(_filteredUpTo == 0);
300 Q_ASSERT(_sortedUpTo == 0);
302 Vector<SearchPointType> tmp;
303 int size = unfilteredSize();
306 memcpy(&tmp[0], &(*
this)[0], size *
sizeof(SearchPointType));
308 this->resize(size +
static_cast<const Vector<SearchPointType>*
>(other)->size());
309 typename Vector<SearchPointType>::iterator end = std::set_union(range(tmp),
311 this->begin(), pointerOrderCompare<SearchPointType>);
312 this->resize(end - this->begin());
314 Q_ASSERT(_filteredUpTo == 0);
315 Q_ASSERT(_sortedUpTo == 0);
320 template <
typename SearchPo
intType,
typename DataSetType>
321 BaseResultSet<SearchPointType, DataSetType>::BaseResultSet() {
322 _d =
new SearchSpaceWrapper<SearchPointType, DataSetType>(
new BaseSearchSpace<SearchPointType, DataSetType>());
325 template <
typename SearchPo
intType,
typename DataSetType>
326 BaseResultSet<SearchPointType, DataSetType>::BaseResultSet(BaseSearchSpace<SearchPointType, DataSetType>* sspace) {
327 _d =
new SearchSpaceWrapper<SearchPointType, DataSetType>(sspace);
330 template <
typename SearchPo
intType,
typename DataSetType>
332 return _d->sspace->get(n, offset);
335 template <
typename SearchPo
intType,
typename DataSetType>
336 void BaseResultSet<SearchPointType, DataSetType>::addPoints(
const DataSetType* dataset,
const QList<QString>& ids) {
337 _d->sspace->addPoints(dataset, ids);
340 template <
typename SearchPo
intType,
typename DataSetType>
341 void BaseResultSet<SearchPointType, DataSetType>::removePoints(
const QList<QString>& ids) {
342 _d->sspace->removePoints(ids);
int unfilteredSize() const
Returns the size of this SearchSpace, before filtering the points.
Definition: searchspace.h:61
void filterAndSort(int n=1000)
Filters the points in this SearchSpace using the given Filter and sort them by increasing distance...
Definition: searchspace.h:202
int size() const
Returns the total number of points contained in this SearchSpace.
Definition: searchspace.h:53
void filterAll()
Filters all points so that the remaining points at the end all comply to the Filter.
Definition: searchspace.h:105
void setUnion(const BaseSearchSpace< SearchPointType, DataSetType > *other)
This computes the union of this SearchSpace with the other one.
Definition: searchspace.h:299
void copyPointsFrom(const BaseSearchSpace< SearchPointType, DataSetType > &other)
Copy all the points from the other SearchSpace into this one.
Definition: searchspace.h:38
void removePoints(const QList< QString > &ids)
Remove the points with the given IDs from this SearchSpace.
Definition: searchspace.h:229
void addPoints(const DataSetType *dataset, const QList< QString > &ids)
Add the points from the dataset with the given IDs to this SearchSpace.
Definition: searchspace.h:221
void clear()
Clears the whole SearchSpace.
Definition: searchspace.h:154
void pointerSort()
Sorts the SearchPoints contained into this SearchSpace by order of their pointer address.
Definition: searchspace.h:271
void thresholdLimit(float maxDist)
This method limits the bymber of results contained in this SearchSpace.
Definition: searchspace.h:160
SearchResults get(int n, int offset=0)
Returns the list of search results, which are pairs of (pointName, distance).
Definition: searchspace.h:332
void limit(int n)
This method limits the number of results contained in this SearchSpace.
Definition: searchspace.h:145
SearchResults get(int n, int offset=0)
Returns the list of search results, which are pairs of (pointName, distance).
Definition: searchspace.h:76
void setDifference(const BaseSearchSpace< SearchPointType, DataSetType > *other)
This computes the difference of this SearchSpace and the other one (ie: this - other).
Definition: searchspace.h:288
void setIntersection(const BaseSearchSpace< SearchPointType, DataSetType > *other)
This computes the intersection of this SearchSpace with the other one.
Definition: searchspace.h:277