Evoplex  0.2.1
attributes.h
1 /* Evoplex <https://evoplex.org>
2  * Copyright (C) 2016-present - Marcos Cardinot <marcos@cardinot.net>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ATTRIBUTES_H
18 #define ATTRIBUTES_H
19 
20 #include <QPair>
21 #include <QString>
22 #include <algorithm>
23 #include <stdexcept>
24 #include <vector>
25 
26 #include "value.h"
27 #include "utils.h"
28 
29 namespace evoplex {
30 
31 class Attributes;
32 using SetOfAttributes = std::vector<Attributes>;
33 
40 {
41 public:
49 
52 
62  inline void resize(int size);
63 
74  inline void reserve(int size);
75 
79  inline int size() const;
83  inline bool isEmpty() const;
85  inline bool empty() const;
86 
93  inline int indexOf(const QString& name) const;
99  inline bool contains(const QString& name) const;
100 
109  inline void replace(int id, QString newName, Value newValue);
110 
116  inline void push_back(QString name, Value value);
117 
121  inline const std::vector<QString>& names() const;
126  inline const QString& name(int id) const;
127 
131  inline const std::vector<Value>& values() const;
137  inline const Value& value(int id) const;
143  inline Value value(const QString& name, Value defaultValue=Value()) const;
144 
151  inline void setValue(int id, const Value& value);
152 
153 private:
154  std::vector<QString> m_names;
155  std::vector<Value> m_values;
156 };
157 
158 
159 /************************************************************************
160  Attributes: Inline member functions
161  ************************************************************************/
162 
163 inline void Attributes::resize(int size) {
164  size_t s = size < 0 ? 0 : static_cast<size_t>(size);
165  m_names.resize(s);
166  m_values.resize(s);
167 }
168 
169 inline void Attributes::reserve(int size) {
170  size_t s = size < 0 ? 0 : static_cast<size_t>(size);
171  m_names.reserve(s);
172  m_values.reserve(s);
173 }
174 
175 inline int Attributes::size() const
176 { return static_cast<int>(m_values.size()); }
177 
178 inline bool Attributes::isEmpty() const
179 { return m_names.empty(); }
180 
181 inline bool Attributes::empty() const
182 { return m_names.empty(); }
183 
184 inline int Attributes::indexOf(const QString& name) const
185 { return Utils::indexOf(m_names, name); }
186 
187 inline bool Attributes::contains(const QString& name) const
188 { return indexOf(name) > -1; }
189 
190 inline void Attributes::replace(int id, QString newName, Value newValue) {
191  if (id < 0) throw std::out_of_range("id must be positive!");
192  size_t _id = static_cast<size_t>(id);
193  m_names.at(_id) = newName;
194  m_values.at(_id) = newValue;
195 }
196 
197 inline void Attributes::push_back(QString name, Value value) {
198  m_names.emplace_back(name);
199  m_values.emplace_back(value);
200  if (m_names.size() >= INT32_MAX)
201  throw std::length_error("too many attributes");
202 }
203 
204 inline const std::vector<QString>& Attributes::names() const
205 { return m_names; }
206 
207 inline const QString& Attributes::name(int id) const
208 { return m_names.at(id); }
209 
210 inline const std::vector<Value>& Attributes::values() const
211 { return m_values; }
212 
213 inline const Value& Attributes::value(int id) const
214 { return m_values.at(id); }
215 
216 inline Value Attributes::value(const QString& name, Value defaultValue) const {
217  const int idx = indexOf(name);
218  return idx < 0 ? defaultValue : m_values.at(static_cast<size_t>(idx));
219 }
220 
221 inline void Attributes::setValue(int id, const Value& value) {
222  if (id < 0) throw std::out_of_range("id must be positive!");
223  m_values.at(static_cast<size_t>(id)) = value;
224 }
225 
226 } // evoplex
227 #endif // ATTRIBUTES_H
A class for variant data types (tagged union).
Definition: value.h:56
void reserve(int size)
Attempt to preallocate enough memory for specified number of elements.
Definition: attributes.h:169
const std::vector< QString > & names() const
Gets the name of all attributes.
Definition: attributes.h:204
Attributes(int size)
Constructor.
Definition: attributes.h:46
bool contains(const QString &name) const
Checks if the container contains name.
Definition: attributes.h:187
bool isEmpty() const
Checks if the container is empty.
Definition: attributes.h:178
const QString & name(int id) const
Gets the name of the attribute at id.
Definition: attributes.h:207
int indexOf(const QString &name) const
Returns the index position of name in the container.
Definition: attributes.h:184
int size() const
Gets the number of attributes in the container.
Definition: attributes.h:175
void setValue(int id, const Value &value)
Sets the value at id.
Definition: attributes.h:221
const Value & value(int id) const
Gets the value of the attribute at id.
Definition: attributes.h:213
void replace(int id, QString newName, Value newValue)
Replaces the item at index position id with newName and newValue.
Definition: attributes.h:190
Attributes()
Constructor.
Definition: attributes.h:48
bool empty() const
Checks if the container is empty.
Definition: attributes.h:181
const std::vector< Value > & values() const
Gets the values of all attributes.
Definition: attributes.h:210
~Attributes()
Destructor.
Definition: attributes.h:51
Definition: abstractgraph.h:29
A container of labeled values.
Definition: attributes.h:39
void resize(int size)
Resizes the container to the specified number of elements.
Definition: attributes.h:163
void push_back(QString name, Value value)
Appends the attribute name with the value value.
Definition: attributes.h:197