32 #ifndef CORE_METADATA_H 33 #define CORE_METADATA_H 58 #define FILENAME_XMIPP_STAR "# XMIPP_STAR_1" 59 #define FILENAME_XMIPP_SQLITE "SQLite format 3" 60 #define DEFAULT_BLOCK_NAME "noname" 64 #define FOR_ALL_OBJECTS_IN_METADATA(__md) for (size_t objId : __md.ids()) 67 #define FOR_ALL_ROWS_IN_METADATA(__md) for (auto& row : __md) 69 #define END_OF_LINE() ((char*) memchr (iter, '\n', end-iter)) 88 #define BUFFER_CREATE(b) mdBuffer b; b.begin = nullptr; b.size = 0 89 #define BUFFER_COPY(b1, b2) mdBuffer b2; b2.begin = b1.begin; b2.size = b1.size 90 #define BUFFER_MOVE(b, n) b.begin += n; b.size -= n 91 #define BUFFER_FIND(b, str, n) (char*) _memmem(b.begin, b.size, str, n) 101 #define BLOCK_CREATE(b) mdBlock b; b.begin = b.end = b.loop = nullptr; b.nameSize = 0 102 #define BLOCK_INIT(b) b.begin = b.end = b.loop = nullptr; b.nameSize = 0 103 #define BLOCK_NAME(b, s) s.assign(b.begin, b.nameSize) 178 bool _isColumnFormat;
190 int _precision = 1000;
200 size_t _maxRows = 0, _parsedLines = 0;
203 double precision()
const;
205 virtual void readStar(
const FileName &filename,
const std::vector<MDLabel> *desiredLabels,
206 const String &blockRegExp,
bool decomposeStack);
208 virtual void _readColumns(
std::istream& is, std::vector<MDObject*> & columnValues,
209 const std::vector<MDLabel>* desiredLabels =
nullptr);
211 virtual void _readColumnsStar(
mdBlock &block,
212 std::vector<MDObject*> & columnValues,
213 const std::vector<MDLabel>* desiredLabels,
214 bool addColumns =
true,
223 virtual void _parseObjects(
std::istream &is, std::vector<MDObject*> & columnValues,
224 const std::vector<MDLabel> *desiredLabels,
bool firstTime) = 0;
231 virtual void _readRows(
std::istream& is, std::vector<MDObject*>& columnValues,
bool useCommentAsImage);
239 virtual void _readRowsStar(
mdBlock &block, std::vector<MDObject*> & columnValues,
240 const std::vector<MDLabel> *desiredLabels);
263 virtual void clear();
277 virtual void setMaxRows(
size_t maxRows=0) { _maxRows = maxRows; }
288 virtual void setPrecission(
int _precision) { this->_precision = (int)pow (10,_precision); }
303 virtual void writeText(
const FileName fn,
const std::vector<MDLabel>* desiredLabels)
const = 0;
315 virtual void setComment(
const String &newComment =
"No comment") { this->_comment = newComment; }
327 virtual std::vector<MDLabel> getActiveLabels()
const = 0;
331 virtual int getMaxStringLength(
const MDLabel thisLabel)
const = 0;
341 virtual bool setValueCol(
const MDObject &mdValueIn) = 0;
351 return setValueCol(
MDObject(label, valueIn));
368 return setValue(
MDObject(label, valueIn),
id);
371 virtual size_t addRow(
const MDRow &row) = 0;
379 for (
const auto& row : md)
390 virtual bool setValue(
const MDObject &mdValueIn,
size_t id) = 0;
391 virtual bool getValue(
MDObject &mdValueOut,
size_t id)
const = 0;
411 if (!getValue(mdValueOut,
id))
419 if (!getValue(mdValueOut,
id))
427 return getValue<T>(
label, id);
432 return getValue<T>(
label, id);
437 if (!getValue(label, valueOut,
id))
441 template <
typename T>
444 if (!getValue(mdValueOut,
id))
449 template <
typename T>
452 if (!getValue(mdValueOut,
id))
457 template <
typename T,
typename T1>
459 if (!getValue(label, valueOut,
id))
460 valueOut = (T) _default;
467 std::vector<T> result;
469 std::vector<size_t> objectsId;
470 findObjects(objectsId);
471 size_t n = objectsId.size();
473 for (
size_t i = 0;
i <
n; ++
i) {
474 if (!getValue(mdValueOut, objectsId[
i]))
475 throw std::logic_error(
"Column does not exist!");
476 result.emplace_back(mdValueOut.
getValue2(T()));
484 out = this->getColumnValues<T>(
label);
488 virtual bool getRowValues(
size_t id, std::vector<MDObject> &values)
const = 0;
493 virtual void getColumnValues(
const MDLabel label, std::vector<MDObject> &valuesOut)
const = 0;
500 if (valuesIn.size() != size())
503 for (
size_t objId : this->ids())
504 this->setValue(label, valuesIn[n++], objId);
507 virtual void setColumnValues(
const std::vector<MDObject> &valuesIn) = 0;
509 virtual std::unique_ptr<MDRow> getRow(
size_t id) = 0;
510 virtual std::unique_ptr<const MDRow> getRow(
size_t id)
const = 0;
514 virtual bool setValueFromStr(
const MDLabel label,
const String &value,
size_t id);
518 virtual bool getStrFromValue(
const MDLabel label,
String &strOut,
size_t id)
const;
522 virtual bool isEmpty()
const {
return size() == 0; }
526 virtual size_t size()
const = 0;
530 virtual bool containsLabel(
const MDLabel label)
const = 0;
537 virtual bool addLabel(
const MDLabel label,
int pos = -1) = 0;
543 virtual bool removeLabel(
const MDLabel label) = 0;
548 void keepLabels(
const std::vector<MDLabel> &labels);
556 virtual size_t addObject() = 0;
568 virtual void importObject(
const MetaData &md,
const size_t id,
bool doClear=
true) = 0;
569 virtual void importObjects(
const MetaData &md,
const std::vector<size_t> &objectsToAdd,
bool doClear=
true) = 0;
570 virtual void importObjects(
const MetaData &md,
const MDQuery &query,
bool doClear=
true) = 0;
576 virtual bool removeObject(
size_t id) = 0;
582 virtual void removeObjects(
const std::vector<size_t> &toRemove) = 0;
590 virtual int removeObjects(
const MDQuery&) = 0;
591 virtual int removeObjects() = 0;
600 virtual size_t firstRowId()
const = 0;
601 virtual size_t firstObject(
const MDQuery&)
const = 0;
604 virtual size_t lastRowId()
const = 0;
614 virtual void findObjects(std::vector<size_t> &objectsOut,
const MDQuery &query)
const = 0;
615 virtual void findObjects(std::vector<size_t> &objectsOut,
int limit = -1)
const = 0;
617 virtual size_t countObjects(
const MDQuery&)
const = 0;
618 virtual bool containsObject(
size_t objectId)
const = 0;
619 virtual bool containsObject(
const MDQuery&)
const = 0;
627 virtual void _writeRows(std::ostream &os)
const = 0;
640 void append(
const FileName &outFile)
const;
647 virtual void read(
const FileName &inFile,
const std::vector<MDLabel> *desiredLabels =
nullptr,
bool decomposeStack=
true) = 0;
656 virtual void removeDisabled();
659 virtual void selectRandomSubset(
const MetaData &mdIn,
size_t numberOfObjects,
668 virtual void selectPart(
const MetaData &mdIn,
size_t startPosition,
size_t numberOfObjects,
685 template <
bool IsConst>
688 std::unique_ptr<MDBaseRowIterator<IsConst>> impl;
693 impl = std::move(right.impl->clone());
715 template <
bool IsConst>
718 std::unique_ptr<MDBaseIdIterator<IsConst>> impl;
723 impl = std::move(right.impl->clone());
739 template <
bool IsConst>
761 virtual void fillConstant(
MDLabel label,
const String &value) = 0;
770 virtual void fillRandom(
MDLabel label,
const String &mode,
double op1,
double op2,
double op3=0.) = 0;
773 virtual void fillLinear(
MDLabel label,
double initial,
double step) = 0;
786 virtual void renameColumn(
MDLabel oldLabel,
MDLabel newLabel) = 0;
791 virtual void renameColumn(
const std::vector<MDLabel> &oldLabel,
792 const std::vector<MDLabel> &newLabel) = 0;
ColumnDoesNotExist(const std::string &msg)
const int & getValue2(int) const
void write(std::ostream &os, const datablock &db)
#define REPORT_ERROR(nerr, ErrormMsg)
ObjectDoesNotExist(size_t id, const FileName &fn)
std::vector< String > StringVector
Incorrect number of objects in Metadata.
ObjectDoesNotExist(MDLabel label, size_t id)
ColumnDoesNotExist(MDLabel label, const FileName &fn)
basic_istream< char, std::char_traits< char > > istream
static String label2Str(const MDLabel &label)
std::string to_string(bond_type bondType)
file read(std::istream &is)
NotImplemented(const std::string &str)