Xmipp  v3.23.11-Nereus
metadata_row_sql.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: J.M. De la Rosa Trevin (jmdelarosa@cnb.csic.es)
4  * Jan Horacek (xhorace4@fi.muni.cz)
5  *
6  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307 USA
22  *
23  * All comments concerning this program package may be sent to the
24  * e-mail address 'xmipp@cnb.csic.es'
25  ***************************************************************************/
26 
27 #include <algorithm>
28 #include <sstream>
29 #include "metadata_row_sql.h"
30 
32  _size = 0;
33  _objects.fill(nullptr);
34 }
35 
37  _size = 0;
38  _objects.fill(nullptr);
39  copy(row);
40 }
41 
43  copy(row);
44  return *this;
45 }
46 
48  *this = dynamic_cast<const MDRowSql&>(row);
49  return *this;
50 }
51 
52 MDRowSql::MDRowSql(const std::vector<MDObject> &values) {
53  _objects.fill(nullptr);
54  for (size_t i = 0; i < values.size(); i++) {
55  _objects[values[i].label] = new MDObject(values[i]);
56  _order[i] = values[i].label;
57  }
58 }
59 
61  for (int _label = MDL_FIRST_LABEL; _label < MDL_LAST_LABEL; ++_label) {
62  if (_objects[_label] != nullptr)
63  delete _objects[_label];
64  }
65 }
66 
67 bool MDRowSql::empty() const {
68  return _size == 0;
69 }
70 
71 int MDRowSql::size() const {
72  return _size;
73 }
74 
76  _size = 0;
77  for (int _label = MDL_FIRST_LABEL; _label < MDL_LAST_LABEL; ++_label) {
78  if (_objects[_label] != nullptr)
79  delete _objects[_label];
80  _objects[_label] = nullptr;
81  }
82 }
83 
84 bool MDRowSql::containsLabel(MDLabel label) const {
85  return _objects[label] != nullptr;
86 }
87 
88 std::vector<MDLabel> MDRowSql::labels() const {
89  std::vector<MDLabel> res;
90  res.reserve(_size);
91  for (size_t i = 0; i < _size; ++i) {
92  const MDLabel &label = _order[i];
93  if (containsLabel(label))
94  res.emplace_back(label);
95  }
96  return res;
97 }
98 
100  if (_objects[label] == nullptr) {
101  _objects[label] = new MDObject(label);
102  _order[_size] = label;
103  ++_size;
104  }
105 }
106 
107 const MDObject *MDRowSql::getObject(MDLabel label) const {
108  return _objects[label];
109 }
110 
112  return _objects[label];
113 }
114 
115 void MDRowSql::setValue(const MDObject &object) {
116  int _label = object.label;
117  if (_objects[_label] == nullptr) {
118  _objects[_label] = new MDObject(object);
119  _order[_size] = object.label;
120  ++_size;
121  } else
122  _objects[_label]->copy(object);
123 }
124 
125 void MDRowSql::copy(const MDRowSql &row) {
126  //Copy existing MDObjects from row
127  //and delete unexisting ones
128  _size = row._size;
129  MDObject ** ptrObjectsLabel=&(_objects[0]);
130  MDObject * const * ptrRowObjectsLabel=&(row._objects[0]);
131  for (int _label = MDL_FIRST_LABEL; _label < MDL_LAST_LABEL; ++_label) {
132  if (*ptrRowObjectsLabel == nullptr) {
133  delete *ptrObjectsLabel;
134  *ptrObjectsLabel = nullptr;
135  } else {
136  if (*ptrObjectsLabel == nullptr)
137  *ptrObjectsLabel = new MDObject(*(*ptrRowObjectsLabel));
138  else
139  (*ptrObjectsLabel)->copy(*(*ptrRowObjectsLabel));
140  }
141  ++ptrObjectsLabel;
142  ++ptrRowObjectsLabel;
143  }
144  //copy the order of labels
145  _order = row._order;
146 }
147 
148 MDObject* MDRowSql::iteratorValue(size_t i) {
149  return _objects[_order[i]];
150 }
151 
152 const MDObject* MDRowSql::iteratorValue(size_t i) const {
153  return _objects[_order[i]];
154 }
155 
156 std::ostream& operator << (std::ostream &out, const MDRowSql &row) {
157  for (size_t i = 0; i < row._size; ++i) {
158  row._objects[row._order[i]]->toStream(out);
159  out << " ";
160  }
161  return out;
162 }
friend std::ostream & operator<<(std::ostream &out, const MDRowSql &row)
int size() const override
void clear() override
#define i
The label MDL_OBJID is special and should not be used.
std::vector< MDLabel > labels() const override
void setValue(const MDObject &object) override
bool empty() const override
void addLabel(MDLabel label) override
bool containsLabel(MDLabel label) const override
MDRow & operator=(const MDRow &) override
MDObject * getObject(MDLabel label) override
MDLabel