Gaia
dataset.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_DATASET_H
21 #define GAIA_DATASET_H
22 
23 #include <QVector>
24 #include <QReadWriteLock>
25 #include "point.h"
26 #include "pointlayout.h"
27 #include "transformation.h"
28 
29 namespace gaia2 {
30 
31 // forward-declaration of View, a bit complex as everything is now templated...
32 class SearchPoint;
33 class DistanceFunction;
34 class DataSet;
35 
36 template <typename DataSetType, typename PointType, typename SearchPointType, typename DistanceType>
37 class BaseView;
38 
40 
41 
48 class PointArray : public QVector<Point*> {
49  protected:
50  bool _ownsMemory;
51 
52  public:
53 
54  PointArray(int n = 0, bool ownsMemory = true) : QVector<Point*>(n, 0), _ownsMemory(ownsMemory) {}
55  PointArray(bool ownsMemory) : _ownsMemory(ownsMemory) {}
56  ~PointArray();
57 
62  void clear();
63 
68  int totalSegments() const;
69 
73  const Point* samplePoint() const;
74 };
75 
76 
77 
91 class DataSet : public PointArray, public QObject {
92 
93  public:
94  DataSet();
95  ~DataSet();
96 
101  QReadWriteLock lock;
102 
106  const QString& name() const { return _name; }
107 
111  void setName(const QString& name) { _name = name; }
112 
118  const Point* point(const QString& id) const;
119 
121  Point* point(const QString& id);
122 
126  bool contains(const QString& id) const;
127 
131  QStringList pointNames() const;
132 
136  const PointLayout& layout() const { return _layout; }
137 
141  const PointLayout& originalLayout() const;
142 
148  void checkAllPointsShareSameLayout(const QVector<Point*>* points = 0) const;
149 
153  const TransfoChain& history() const { return _history; }
154 
163  void setHistory(const TransfoChain& history);
164 
168  void forgetHistory();
169 
178  void simplifyHistory();
179 
180 
181 
191  void setReferenceDataSet(DataSet* dataset = 0, bool checkOriginalLayout = true);
192 
199  const DataSet* referenceDataSet() const;
200 
210  void addPoint(const Point* point);
211 
220  void addPoints(const QVector<Point*>& points);
221 
230  void appendDataSet(const DataSet* dataset);
231 
237  void removePoint(const QString& id);
238 
246  void removePoints(const QList<QString>& ids);
247 
248 
249 
250 
255  void addView(View* view);
256 
260  void removeView(View* view);
261 
262 
263 
264 
269  static DataSet* mergeFiles(const QMap<QString, QString>& sigfiles,
270  const QStringList& descsSelect = QStringList() << "*",
271  const QStringList& descsExclude = QStringList(),
272  int start = 0, int end = 10000000,
273  PointLayout* reflayout = 0);
274 
278  DataSet* copy() const;
279 
288  void load(const QString& filename, int start = 0, int end = -1);
289 
297  void loadNthPart(const QString& filename, int idx = 0, int total = 1);
298 
302  void save(const QString& filename) const;
303 
307  void fromBase64(const std::string& data);
308 
312  void fromBase64(const QByteArray& data);
313 
317  std::string toBase64() const;
318 
319 
320 
321  protected:
322 
328  QString _name;
329 
338 
346 
357  QList<DataSet*> _linkedDataSets;
358 
365  QList<View*> _linkedViews;
366 
367  bool _isDataSorted;
368 
372  int binarySearch(const QString& id, int start, int end) const;
373 
379  int pointIndex(const QString& id) const;
380 
381  void clear();
382 
386  int load(QDataStream& in, int start = 0, int end = -1, bool readAllPointsFromStream = false);
387 
388  void setLayoutIfEmpty(const Point* point);
389 
390  void invalidateViews();
391 
392  void modify();
393 
394  void setHistoryNoCheck(const TransfoChain& history) { _history = history; }
395 
396  void addTransformation(const Transformation& transfo);
397 
398  bool consistentLinks() const;
399 
400  void forceUnlinkReferringDataSets();
401 
405  void unifyLayout();
406 
414  void checkUniqueIDs();
415 
424  void checkUniqueIDsFrom(const QVector<Point*>& v);
425 
426 
455  void addPoints(const QVector<Point*>& points,
456  bool layoutCheck,
457  bool transformPoint,
458  bool checkUnique = true,
459  bool takeOwnership = false,
460  bool relaySignal = true);
461 
462  void removePoints(const QList<QString>& ids, bool relaySignal);
463 
464  void sortPoints(int pivotIdx = -1);
465 
466  // make both the copy-constructor and assignment operator protected, so we
467  // can ensure that no copy of a dataset can be made unintentionally (which could
468  // lead to either memory problems or terrible performance)
469  // people wanting to make a copy of a DataSet should use the DataSet::copy() method.
470  DataSet(const DataSet& rhs);
471  DataSet& operator=(const DataSet& rhs);
472 
473  // those functions from the parent class should also not be callable
474  void resize(int n);
475 
476  public:
477 
478  friend class Applier; // to access the transformation history
479 
480  // mergeDataSets needs to have direct access to a DataSet to work properly
481  // because it needs to call the setHistoryNoCheck() method.
482  friend DataSet* mergeDataSets(const DataSet* ds1, const DataSet* ds2);
483 
484 
485  // serialization methods
486  friend QDataStream& operator<<(QDataStream& out, const DataSet& dataset);
487  friend QDataStream& operator>>(QDataStream& in, DataSet& dataset);
488 
489 };
490 
491 
492 } // namespace gaia2
493 
494 #endif // GAIA_DATASET_H
const TransfoChain & history() const
Return the history of this dataset (the list of all transformations that have been applied)...
Definition: dataset.h:153
A PointArray is a simple subclass of a QVector<Point*> that owns the memory to the points (hence will...
Definition: dataset.h:48
Class containing all info pertaining to a specific transformation, ie: the name of the analyzer class...
Definition: transformation.h:41
This class represents a dataset and all related information.
Definition: dataset.h:91
const PointLayout & layout() const
Return the layout of this dataset.
Definition: dataset.h:136
const Point * samplePoint() const
Returns any single point from the PointArray.
Definition: dataset.cpp:61
This class describes the layout of a point.
Definition: pointlayout.h:60
int binarySearch(const Container< T > &v, T value)
Iterative function for binary search (more efficient than recursive one).
Definition: gaiamath.h:270
TransfoChain _history
This represents the history of transformations that have been applied to this dataset, and also contains all the parameters to allow mapping a point from the original dataset space into the space this dataset is in.
Definition: dataset.h:337
void setName(const QString &name)
Set the name for this dataset.
Definition: dataset.h:111
const QString & name() const
Return the name of this dataset.
Definition: dataset.h:106
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
void clear()
Delete the points this array contains (if it owns them), then resize the array to 0...
Definition: dataset.cpp:45
DataSet * mergeDataSets(const DataSet *ds1, const DataSet *ds2)
Merges two datasets together, provided that their layout don&#39;t overlap, and return the resulting data...
Definition: utils.cpp:421
QReadWriteLock lock
A lock available for users to take, if the dataset is to be used in a multi-threaded context...
Definition: dataset.h:101
The Applier abstract base class.
Definition: applier.h:44
QString _name
Represents the name of the dataset, which should be a short way to describe its function or where it ...
Definition: dataset.h:328
The View class contains a specific view on a dataset.
Definition: dataset.h:37
int totalSegments() const
Returns the total number of segments in this PointArray (the sum of the number of segments for each p...
Definition: dataset.cpp:53
PointLayout _layout
This represents the common layout of all points contained in this dataset and provides functions for ...
Definition: dataset.h:345
QList< View * > _linkedViews
This list contains all Views linked to this DataSet, ie: the Views using points from this dataset to ...
Definition: dataset.h:365
Definition: transformation.h:106
Definition: point.h:106
QList< DataSet * > _linkedDataSets
This list contains all datasets linked to this one, like when a dataset is referencing another one fo...
Definition: dataset.h:357