Xmipp  v3.23.11-Nereus
Public Member Functions | Static Public Member Functions | Friends | List of all members
MDRowVec Class Reference

#include <metadata_row_vec.h>

Inheritance diagram for MDRowVec:
Inheritance graph
[legend]
Collaboration diagram for MDRowVec:
Collaboration graph
[legend]

Public Member Functions

 MDRowVec ()
 
 MDRowVec (std::vector< MDObject > &row, size_t rowi, std::array< int, MDL_LAST_LABEL > &label_to_col, std::vector< MDLabel > &col_to_label, size_t &no_columns)
 
 MDRowVec (const std::vector< MDObject > &row, size_t rowi, const std::array< int, MDL_LAST_LABEL > &label_to_col, const std::vector< MDLabel > &col_to_label, const size_t &no_columns)
 
 MDRowVec (const MDRowVec &)
 
MDRowVecoperator= (const MDRowVec &)
 
MDRowoperator= (const MDRow &row)
 
virtual ~MDRowVec ()
 
void detach () override
 
bool empty () const override
 
int size () const override
 
void clear () override
 
bool inMetadata () const
 
bool containsLabel (MDLabel label) const override
 
std::vector< MDLabellabels () const override
 
void addLabel (MDLabel label) override
 
MDObjectgetObject (MDLabel label) override
 
const MDObjectgetObject (MDLabel label) const override
 
void setValue (const MDObject &object) override
 
template<typename T >
T & getValue (MDLabel label)
 
template<typename T >
const T & getValue (MDLabel label) const
 
template<typename T >
bool getValue (MDLabel label, T &d) const
 
template<typename T >
const T & getValueOrDefault (MDLabel label, const T &def) const
 
template<typename T >
T & getValueOrDefault (MDLabel label, const T &def)
 
template<typename T , typename T1 >
void getValueOrDefault (MDLabel label, T &d, T1 def) const
 
template<typename T >
void setValue (MDLabel label, const T &d, bool addLabel=true)
 
- Public Member Functions inherited from MDRow
virtual ~MDRow ()=default
 
virtual size_t id () const
 
virtual void resetGeo (bool addLabels=true)
 
template<typename T >
T & getValue (MDLabel label)
 
template<typename T >
const T & getValue (MDLabel label) const
 
template<typename T >
bool getValue (MDLabel label, T &d) const
 
bool getValue (MDObject &object) const
 
template<typename T >
const T & getValueOrDefault (MDLabel label, const T &def) const
 
template<typename T >
T & getValueOrDefault (MDLabel label, const T &def)
 
template<typename T , typename T1 >
void getValueOrDefault (MDLabel label, T &d, T1 def) const
 
template<typename T >
void setValue (MDLabel label, const T &d, bool addLabel=true)
 
virtual void setValueFromStr (MDLabel label, const String &value)
 
iterator begin ()
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 

Static Public Member Functions

static MDRowVec deepCopy (const MDRowVec &)
 

Friends

class MetaDataVec
 
std::ostream & operator<< (std::ostream &out, const MDRowVec &row)
 

Additional Inherited Members

- Public Types inherited from MDRow
using iterator = iterator_ptr< false >
 
using const_iterator = iterator_ptr< true >
 

Detailed Description

Class for holding an entire row of MDObject in MetaDataVec. Row could be attached to OR detached from metadata

Notes

  1. It's fast to create MDRowVec from MetaDataVec row, because only pointers are initialized. No data are copied.
  2. Adding MDRowVec to MetaDataVec requires copying whole row.

Definition at line 46 of file metadata_row_vec.h.

Constructor & Destructor Documentation

◆ MDRowVec() [1/4]

MDRowVec::MDRowVec ( )

Definition at line 31 of file metadata_row_vec.cpp.

32  : _col_to_label(nullptr), _no_columns(nullptr), _in_metadata(false) {
33  _row = new std::vector<MDObject>();
34  _label_to_col = new std::array<int, MDL_LAST_LABEL>();
35  std::fill(_label_to_col->begin(), _label_to_col->end(), -1);
36 }

◆ MDRowVec() [2/4]

MDRowVec::MDRowVec ( std::vector< MDObject > &  row,
size_t  rowi,
std::array< int, MDL_LAST_LABEL > &  label_to_col,
std::vector< MDLabel > &  col_to_label,
size_t &  no_columns 
)

Definition at line 38 of file metadata_row_vec.cpp.

40  : _row(&row), _rowi(rowi), _label_to_col(&label_to_col), _col_to_label(&col_to_label),
41  _no_columns(&no_columns), _in_metadata(true)
42  {}

◆ MDRowVec() [3/4]

MDRowVec::MDRowVec ( const std::vector< MDObject > &  row,
size_t  rowi,
const std::array< int, MDL_LAST_LABEL > &  label_to_col,
const std::vector< MDLabel > &  col_to_label,
const size_t &  no_columns 
)

Definition at line 44 of file metadata_row_vec.cpp.

46  : _row(const_cast<std::vector<MDObject>*>(&row)),
47  _rowi(rowi),
48  _label_to_col(const_cast<std::array<int, MDL_LAST_LABEL>*>(&label_to_col)),
49  _col_to_label(const_cast<std::vector<MDLabel>*>(&col_to_label)),
50  _no_columns(const_cast<size_t*>(&no_columns)),
51  _in_metadata(true)
52  // This is very nasty hack. I was unable to solve situation nicely.
53  // Creator of the object must pay close attention to create 'const MDRowVec' when instantiating from
54  // const MetaData. This should be ok as crator is only in MetaDataVec.
55  {}

◆ MDRowVec() [4/4]

MDRowVec::MDRowVec ( const MDRowVec other)

Definition at line 57 of file metadata_row_vec.cpp.

58  : _rowi(other._rowi), _col_to_label(other._col_to_label), _no_columns(other._no_columns),
59  _in_metadata(other._in_metadata)
60  {
61  if (_in_metadata) {
62  _row = other._row;
63  _label_to_col = other._label_to_col;
64  } else {
65  _row = new std::vector<MDObject>(*(other._row));
66  _label_to_col = new std::array<int, MDL_LAST_LABEL>(*(other._label_to_col));
67  }
68 }

◆ ~MDRowVec()

MDRowVec::~MDRowVec ( )
virtual

Definition at line 118 of file metadata_row_vec.cpp.

118  {
119  if (!_in_metadata) {
120  delete _row;
121  delete _label_to_col;
122  }
123 }

Member Function Documentation

◆ addLabel()

void MDRowVec::addLabel ( MDLabel  label)
overridevirtual

Implements MDRow.

Definition at line 151 of file metadata_row_vec.cpp.

151  {
152  // Warning: not adding to all rows!
153  if ((*_label_to_col)[label] < 0)
154  newCol(label);
155 }

◆ clear()

void MDRowVec::clear ( )
overridevirtual

Return number of labels present

Implements MDRow.

Definition at line 133 of file metadata_row_vec.cpp.

133  {
134  _row->clear();
135  if (!_in_metadata)
136  std::fill(_label_to_col->begin(), _label_to_col->end(), -1);
137 }

◆ containsLabel()

bool MDRowVec::containsLabel ( MDLabel  label) const
overridevirtual

Implements MDRow.

Definition at line 139 of file metadata_row_vec.cpp.

139  {
140  return (*_label_to_col)[label] >= 0;
141 }

◆ deepCopy()

MDRowVec MDRowVec::deepCopy ( const MDRowVec row)
static

Definition at line 70 of file metadata_row_vec.cpp.

70  {
71  MDRowVec newRow(row);
72  if (newRow._in_metadata)
73  newRow.detach();
74  return newRow;
75 }

◆ detach()

void MDRowVec::detach ( )
overridevirtual

Reimplemented from MDRow.

Definition at line 77 of file metadata_row_vec.cpp.

77  {
78  if (!this->_in_metadata)
79  return;
80 
81  this->_in_metadata = false;
82  this->_row = new std::vector<MDObject>(*(this->_row));
83  this->_label_to_col = new std::array<int, MDL_LAST_LABEL>(*(this->_label_to_col));
84  this->_col_to_label = nullptr;
85  this->_no_columns = nullptr;
86 }

◆ empty()

bool MDRowVec::empty ( ) const
overridevirtual

Implements MDRow.

Definition at line 125 of file metadata_row_vec.cpp.

125  {
126  return _row->size() == 0;
127 }

◆ getObject() [1/2]

MDObject * MDRowVec::getObject ( MDLabel  label)
overridevirtual

Implements MDRow.

Definition at line 183 of file metadata_row_vec.cpp.

183  {
184  if ((*_label_to_col)[label] < 0)
185  return nullptr;
186  if ((*_label_to_col)[label] >= static_cast<int>(_row->size()))
187  return nullptr;
188  return &_row->at((*_label_to_col)[label]);
189 }

◆ getObject() [2/2]

const MDObject * MDRowVec::getObject ( MDLabel  label) const
overridevirtual

Implements MDRow.

Definition at line 191 of file metadata_row_vec.cpp.

191  {
192  if ((*_label_to_col)[label] < 0)
193  return nullptr;
194  if ((*_label_to_col)[label] >= static_cast<int>(_row->size()))
195  return nullptr;
196  return &_row->at((*_label_to_col)[label]);
197 }

◆ getValue() [1/3]

template<typename T >
T& MDRowVec::getValue ( MDLabel  label)
inline

Definition at line 93 of file metadata_row_vec.h.

93 { return MDRow::getValue<T>(label); }

◆ getValue() [2/3]

template<typename T >
const T& MDRowVec::getValue ( MDLabel  label) const
inline

Definition at line 96 of file metadata_row_vec.h.

96 { return MDRow::getValue<T>(label); }

◆ getValue() [3/3]

template<typename T >
bool MDRowVec::getValue ( MDLabel  label,
T &  d 
) const
inline

Definition at line 99 of file metadata_row_vec.h.

99  { // FIXME: deprecated
100  return MDRow::getValue<T>(label, d);
101  }
doublereal * d

◆ getValueOrDefault() [1/3]

template<typename T >
const T& MDRowVec::getValueOrDefault ( MDLabel  label,
const T &  def 
) const
inline

Definition at line 104 of file metadata_row_vec.h.

104 { return MDRow::getValueOrDefault<T>(label, def); }

◆ getValueOrDefault() [2/3]

template<typename T >
T& MDRowVec::getValueOrDefault ( MDLabel  label,
const T &  def 
)
inline

Definition at line 107 of file metadata_row_vec.h.

107 { return MDRow::getValueOrDefault<T>(label, def); }

◆ getValueOrDefault() [3/3]

template<typename T , typename T1 >
void MDRowVec::getValueOrDefault ( MDLabel  label,
T &  d,
T1  def 
) const
inline

Definition at line 110 of file metadata_row_vec.h.

110  { // FIXME: deprecated
111  return MDRow::getValueOrDefault<T, T1>(label, d, def);
112  }
doublereal * d

◆ inMetadata()

bool MDRowVec::inMetadata ( ) const

Definition at line 231 of file metadata_row_vec.cpp.

231 { return _in_metadata; }

◆ labels()

std::vector< MDLabel > MDRowVec::labels ( ) const
overridevirtual

Implements MDRow.

Definition at line 143 of file metadata_row_vec.cpp.

143  {
144  std::vector<MDLabel> res;
145  res.reserve(_row->size());
146  for (const auto mdObj: *_row)
147  res.emplace_back(mdObj.label);
148  return res;
149 }

◆ operator=() [1/2]

MDRowVec & MDRowVec::operator= ( const MDRowVec other)

Definition at line 88 of file metadata_row_vec.cpp.

88  {
89  if (this == &other)
90  return *this;
91 
92  if (!_in_metadata) {
93  delete _row;
94  delete _label_to_col;
95  }
96 
97  _rowi = other._rowi;
98  _no_columns = other._no_columns;
99  _in_metadata = other._in_metadata;
100  _col_to_label = other._col_to_label;
101 
102  if (_in_metadata) {
103  _row = other._row;
104  _label_to_col = other._label_to_col;
105  } else {
106  _row = new std::vector<MDObject>(*(other._row));
107  _label_to_col = new std::array<int, MDL_LAST_LABEL>(*(other._label_to_col));
108  }
109 
110  return *this;
111 }

◆ operator=() [2/2]

MDRow & MDRowVec::operator= ( const MDRow row)
virtual

Implements MDRow.

Definition at line 113 of file metadata_row_vec.cpp.

113  {
114  *this = dynamic_cast<const MDRowVec&>(row);
115  return *this;
116 }

◆ setValue() [1/2]

void MDRowVec::setValue ( const MDObject object)
overridevirtual

Implements MDRow.

Definition at line 199 of file metadata_row_vec.cpp.

199  {
200  MDLabel _label = object.label;
201  int coli = (*_label_to_col)[_label];
202  if (coli < 0) {
203  size_t i = newCol(_label);
204  (*_row)[i] = object;
205  } else {
206  while (_row->size() <= coli) {
207  size_t j = _row->size();
208  _row->emplace_back(MDObject((*_col_to_label)[j]));
209  }
210 
211  (*_row)[(*_label_to_col)[_label]] = object;
212  }
213 }
#define i
#define j
MDLabel

◆ setValue() [2/2]

template<typename T >
void MDRowVec::setValue ( MDLabel  label,
const T &  d,
bool  addLabel = true 
)
inline

Definition at line 115 of file metadata_row_vec.h.

115 { return MDRow::setValue<T>(label, d, addLabel); }
doublereal * d
void addLabel(MDLabel label) override

◆ size()

int MDRowVec::size ( ) const
overridevirtual

Implements MDRow.

Definition at line 129 of file metadata_row_vec.cpp.

129  {
130  return _row->size();
131 }

Friends And Related Function Documentation

◆ MetaDataVec

friend class MetaDataVec
friend

Definition at line 117 of file metadata_row_vec.h.

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const MDRowVec row 
)
friend

Definition at line 223 of file metadata_row_vec.cpp.

223  {
224  for (int i = 0; i < row.size(); ++i) {
225  (*(row._row))[i].toStream(out);
226  out << " ";
227  }
228  return out;
229 }
int size() const override
#define i

The documentation for this class was generated from the following files: