summaryrefslogtreecommitdiffstats
path: root/variablearray.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'variablearray.cpp')
-rw-r--r--variablearray.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/variablearray.cpp b/variablearray.cpp
new file mode 100644
index 0000000..c1d0503
--- /dev/null
+++ b/variablearray.cpp
@@ -0,0 +1,81 @@
+#include "variablearray.h"
+#include <algorithm> /* for find() */
+
+VariableArray::VariableArray() : arr(/* ARR_STARTING_SIZE */)
+{
+}
+
+VariableArray::VariableArray(const VariableArray& array) : Collection(array), arr(array.arr) /* ha! */
+{
+}
+
+VariableArray& VariableArray::operator=(const Collection& rhs)
+{
+ this->arr = dynamic_cast<const VariableArray *>(&rhs)->arr; /* use vector's copy ctor */
+ this->size_ = rhs.get_size();
+ return *this;
+}
+
+VariableArray::~VariableArray()
+{
+}
+
+void VariableArray::add(int n)
+{
+ this->arr.push_back(n);
+ this->size_++;
+}
+
+bool VariableArray::remove(int n)
+{
+ std::vector<int>::iterator pos = std::find(this->arr.begin(), this->arr.end(), n);
+ if (pos != this->arr.end())
+ {
+ this->arr.erase(pos); /* kind of slow, since elements will get shifted */
+ this->size_--;
+ return true;
+ }
+ else
+ return false;
+}
+
+int VariableArray::operator[](const int i)
+{
+ /* invalid accesses */
+ if (!this->size_ || i < 0 || i+1 > this->size_)
+ return -1;
+
+ return this->arr[i];
+}
+
+VariableArray *VariableArray::copy(void)
+{
+ VariableArray *ret = new VariableArray(*this);
+ return ret;
+}
+
+void VariableArray::iterate(void (*callback)(int *))
+{
+ std::vector<int>::size_type i;
+ for (i = 0; i != this->arr.size(); ++i)
+ callback(&this->arr[i]);
+}
+
+bool VariableArray::contains(int n) const
+{
+ return (std::find(this->arr.begin(), this->arr.end(), n) != this->arr.end());
+}
+
+std::string VariableArray::print(void) const
+{
+ std::stringstream sstm;
+ std::vector<int>::const_iterator iter = this->arr.begin();
+ while (iter != this->arr.end())
+ {
+ sstm << *iter << " ";
+ iter++;
+ }
+
+ return sstm.str();
+}
+