Gaia
searchspace.h
1 /*
2  * Copyright (C) 2006-2013 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Gaia
5  *
6  * Gaia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef GAIA_SEARCHSPACE_H
21 #define GAIA_SEARCHSPACE_H
22 
23 #include <vector>
24 #include "dataset.h"
25 #include "frozendataset.h"
26 #include "parser/filter.h"
27 
28 
29 namespace gaia2 {
30 
31 
32 typedef QPair<QString, Real> Result;
33 typedef QList<Result> SearchResults;
34 
35 yaml::Node toYaml(const SearchResults& results);
36 
37 // fwd-declaration
39 class FrozenDistance;
40 
41 #define Vector std::vector
42 
43 
44 class SearchPoint {
45 public:
46  float dist;
47  const Point* ptr;
48  const Point* ref;
49 
50  // should never use the default constructor, it is defined because std::vector::resize() needs it
51  SearchPoint() {}
52  SearchPoint(const Point* pptr, const Point* rref) : ptr(pptr), ref(rref) {}
53  bool operator<(const SearchPoint& x) const { return dist < x.dist; }
54 };
55 
56 
58 public:
59  float dist;
60  int idx;
61 
62  // should never use the default constructor, it is defined because std::vector::resize() needs it
64  FrozenSearchPoint(int iidx) : idx(iidx) {}
65  bool operator<(const FrozenSearchPoint& x) const { return dist < x.dist; }
66 };
67 
68 
90 template <typename SearchPointType, typename DataSetType>
91 class BaseSearchSpace : public Vector<SearchPointType> {
92 
93  public:
94  BaseSearchSpace(const Filter* filter = 0, bool ownsFilter = false);
95 
101 
102  ~BaseSearchSpace();
103 
109  void copyPointsFrom(const BaseSearchSpace<SearchPointType, DataSetType>& other);
110 
114  int size() const;
115 
120  int unfilteredSize() const;
121 
125  void clear();
126 
132  SearchResults get(int n, int offset = 0);
133 
139  void limit(int n);
140 
146  void thresholdLimit(float maxDist);
147 
148  const Filter* filter() { return _filter; }
149  void setFilter(const Filter* filter, bool ownsFilter = false);
150 
154  void invalidate() { _sortedUpTo = _filteredUpTo = 0; }
155 
160  void filterAll();
161 
166  void pointerSort();
167 
171  void addPoints(const DataSetType* dataset, const QList<QString>& ids);
172 
176  void removePoints(const QList<QString>& ids);
177 
183  void setIntersection(const BaseSearchSpace<SearchPointType, DataSetType>* other);
184 
190  void setUnion(const BaseSearchSpace<SearchPointType, DataSetType>* other);
191 
197  void setDifference(const BaseSearchSpace<SearchPointType, DataSetType>* other);
198 
201  QString pointName(const SearchPointType& p) const;
202  const Point* refPoint(const SearchPointType& p) const;
203 
204  bool validPoint(const SearchPointType& p) const {
205  return _filter->isTrue(refPoint(p));
206  }
207 
208  static BaseSearchSpace<SearchPointType, DataSetType>* createFromDataSet(const DataSetType* dataset);
209 
210  static BaseSearchSpace<SearchPointType, DataSetType>* createFromPoints(const DataSetType* dataset,
211  const QList<QString>& pointNames);
212 
218  template <typename PointType, typename DistanceType>
219  void computeDistance(const PointType& query, const DistanceType* dist);
220 
221  //void computeDistance(const FrozenPoint& query, const FrozenDistance* dist);
222 
224 
225  void setDataSet(const DataSetType* dataset) { _dataset = dataset; }
226  const DataSetType* dataSet() const { return _dataset; }
227 
235  void filterAndSort(int n = 1000);
236 
237  void cleanSearchSpace();
238 
239  protected:
240  const DataSetType* _dataset;
241  const Filter* _filter;
242  bool _ownsFilter;
243  int _sortedUpTo, _filteredUpTo;
244 
245  // FIXME: use the typenames from the search space, not any ones like here
246  template <typename SearchPointType2, typename DataSetType2>
247  friend class BaseQueryOptimizer;
248 
253  int validUpTo() const { return qMin(_sortedUpTo, _filteredUpTo); }
254 
255  void sortAll();
256 
257 };
258 
259 
260 
263 
264 template <>
265 inline const Point* SearchSpace::refPoint(const SearchPoint& p) const {
266  return p.ref;
267 }
268 
269 template <>
270 inline QString SearchSpace::pointName(const SearchPoint& p) const {
271  return p.ptr->name();
272 }
273 
276 
277 template <>
278 inline const Point* FrozenSearchSpace::refPoint(const FrozenSearchPoint& p) const {
279  return _dataset->referenceDataSet()->at(p.idx);
280 }
281 
282 template <>
283 inline QString FrozenSearchSpace::pointName(const FrozenSearchPoint& p) const {
284  return _dataset->pointName(p.idx);
285 }
287 
289 
290 template <typename SearchPointType, typename DataSetType>
291 class SearchSpaceWrapper : public QSharedData {
292  public:
294 
296  ~SearchSpaceWrapper(); // defined after SearchSpacePool is defined, at the end of SearchSpacePool_impl.h
297 };
298 
305 template <typename SearchPointType, typename DataSetType>
307 
308  public:
309  BaseResultSet();
311 
317  SearchResults get(int n, int offset = 0);
318 
319  int size() const { return _d->sspace->size(); }
320 
327  BaseResultSet limit(int n) { _d->sspace->limit(n); return *this; }
328 
335  BaseResultSet thresholdLimit(float maxDist) { _d->sspace->thresholdLimit(maxDist); return *this; }
336 
337 #ifndef SWIG
338  void addPoints(const DataSetType* dataset, const QList<QString>& ids);
339  void removePoints(const QList<QString>& ids);
340 #endif
341 
342  BaseSearchSpace<SearchPointType, DataSetType>* searchSpace() { return _d->sspace; }
343 
344  protected:
345  QExplicitlySharedDataPointer<SearchSpaceWrapper<SearchPointType, DataSetType> > _d;
346 };
347 
348 
349 
350 // as SearchSpace* is a template, we have to import its definition here
351 #include "searchspace_impl.h"
352 
353 
354 
356 
357 // Useful typedefs, because sometimes we might want to create such a beast from
358 // scratch, in which case the name ResultSet is a bit misleading
359 typedef ResultSet InputSpace;
360 
361 } // namespace gaia2
362 
363 
364 #endif // GAIA_SEARCHSPACE_H
Definition: frozendistance.h:33
Definition: searchspace.h:57
The QueryOptimizer class tries to optimize a query by reducing the SearchSpace on which it is suppose...
Definition: parsertypes.h:30
void invalidate()
Mark this SearchSpace as neither sorted nor filtered.
Definition: searchspace.h:154
The Filter class allows to check whether a predicate is true for a given Point.
Definition: filter.h:73
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
QString name() const
Returns the point name, which also acts as its unique identifier.
Definition: point.h:115
Definition: distancefunction.h:37
Definition: searchspace.h:291
A SearchSpace is a structure dedicated to the task of storing pointers to Point with an associated di...
Definition: searchspace.h:91
Definition: searchspace.h:44
This class serves as a ref-counted wrapper for a SearchSpace, which is much more convenient to deal w...
Definition: searchspace.h:306
Definition: yamlcpp.h:77
int validUpTo() const
Index in the search space up to which points are actually filtered and sorted.
Definition: searchspace.h:253
BaseResultSet thresholdLimit(float maxDist)
This method limits the number of results contained in this ResultSet.
Definition: searchspace.h:335
Definition: point.h:106
BaseResultSet limit(int n)
This function limits the number of results contained in this ResultSet.
Definition: searchspace.h:327