26 #ifndef TG_TAGGABLES_H
27 #define TG_TAGGABLES_H
43 assert(m_elements.empty());
52 tgTaggables(std::vector<T>& elements) : m_elements(elements) {
54 assertUniqueElements(
"All elements must be unique.");
64 std::vector<T*>
find(std::string tags)
66 std::vector<T*> result;
67 for(
int i = 0; i < m_elements.size(); i++) {
68 if(_taggable(&m_elements[i])->hasAllTags(tags)) {
69 result.push_back(&(m_elements[i]));
77 return m_elements.size();
80 std::vector<T*> findAll()
82 std::vector<T*> result;
83 for(
int i = 0; i < m_elements.size(); i++) {
84 result.push_back(&(m_elements[i]));
89 std::vector<T*> findUntagged()
91 std::vector<T*> result;
92 for(
int i = 0; i < m_elements.size(); i++) {
95 result.push_back(&(m_elements[i]));
101 static bool contains(std::vector<T*> haystack,
const T* needle)
103 return std::find(haystack.begin(), haystack.end(), needle) != haystack.end();
106 bool contains(
const T& needle)
const
109 for(
typename std::vector<T>::const_iterator it = m_elements.begin(); it != m_elements.end(); it++) {
110 if(&(*it) == &needle)
124 assertKeyExists(key);
125 return m_elements[key];
129 assertKeyExists(key);
130 return m_elements[key];
137 this->removeElements(other.getElements());
142 this->removeElements(other);
146 T& operator+=(
const T& other) {
147 this->addElement(other);
151 T& operator+=(
const std::vector<T*> other) {
152 this->addElements(other);
160 int addElement(T element)
163 assert(!elementExists(element));
164 assertUnique(element);
165 m_elements.push_back(element);
166 return m_elements.size();
169 void addElements(std::vector<T*> elements)
171 for(
int i = 0; i < elements.size(); i++) {
172 this->addElement(elements[i]);
176 void setElement(
int key, T element) {
177 assert((0 <= key) && (key <= m_elements.size()));
178 m_elements[key] = element;
181 std::vector<T>& getElements()
186 const std::vector<T>& getElements()
const
191 void removeElement(
const T& element) {
192 m_elements.erase(std::remove(m_elements.begin(), m_elements.end(), element), m_elements.end());
195 void removeElement(
const T* element) {
196 m_elements.erase(std::remove(m_elements.begin(), m_elements.end(), *element), m_elements.end());
199 void removeElements(
const std::vector<T>& elements) {
200 for(
int i = 0; i < elements.size(); i++) {
201 removeElement(elements[i]);
205 void removeElements(
const std::vector<T*>& elements) {
206 for(
int i = 0; i < elements.size(); i++) {
207 removeElement(elements[i]);
212 T& getElement(
int key)
214 return m_elements[key];
218 const T& getElement(
int key)
const
220 return m_elements[key];
231 return (0 <= key) && (key < m_elements.size());
235 bool elementExists(
const T& element)
const
238 for(
int i = 0; i < m_elements.size(); i++) {
240 const T* elem =& m_elements[i];
241 if (elem == &element) {
248 void assertKeyExists(
int key, std::string message =
"Element at index does not exist")
const
251 std::stringstream ss;
253 throw std::out_of_range(message +
" (index "+ ss.str() +
").");
257 void assertUnique(T& element, std::string message =
"Taggable elements must be unique.") {
258 if(elementExists(element)) {
259 throw std::logic_error(message);
263 void assertUniqueElements(std::string message =
"Taggable elements must be unique.")
const
269 if(! std::set<T>(m_elements.begin(), m_elements.end()).size() ==
271 throw std::logic_error(message);
283 std::vector<T> m_elements;
T & operator-=(const T &other)
std::vector< T * > find(std::string tags)
Contains the definition of class tgTaggable.
tgTaggables(std::vector< T > &elements)
bool keyExists(int key) const