diff options
Diffstat (limited to 'algo1w1/NAWArray.cpp')
-rw-r--r-- | algo1w1/NAWArray.cpp | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/algo1w1/NAWArray.cpp b/algo1w1/NAWArray.cpp new file mode 100644 index 0000000..51742f1 --- /dev/null +++ b/algo1w1/NAWArray.cpp @@ -0,0 +1,121 @@ +#include "NAWArray.h" +#include "NAW.h" + +#include <sstream> +#include <iostream> + +NAWArray::NAWArray() { } +NAWArray::~NAWArray() { } + +void NAWArray::add(const NAW& newNAW) { + _nawCollection[_nawCollectionSize++] = new NAW(newNAW); +} + +int NAWArray::zoekOpEersteNaam(const std::string& naam) const { + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (!_nawCollection[i]->heeftNaam(naam)) continue; + return i; + } + return -1; +} + +int NAWArray::zoekOpEersteAdres(const std::string& adres) const { + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (!_nawCollection[i]->heeftAdres(adres)) continue; + return i; + } + return -1; +} + +int NAWArray::zoekOpEerstePlaats(const std::string& plaats) const { + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (!_nawCollection[i]->heeftPlaats(plaats)) continue; + return i; + } + return -1; +} + +int NAWArray::zoekOpEersteAdresEnPlaats(const std::string& adres, const std::string& plaats) const { + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (!_nawCollection[i]->heeftAdres(adres)) continue; + if (!_nawCollection[i]->heeftPlaats(plaats)) continue; + return i; + } + return -1; +} + +int NAWArray::verwijderEersteMetNaam(const std::string& naam) { + int x = -1; // deleted element index + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (x == -1) { + if (!_nawCollection[i]->heeftNaam(naam)) continue; + x = i; // set deleted element index + _nawCollectionSize--; // decrease container size + i++; // skip deleted element + } + if (i == 0) continue; // check if i-1 >= 0 + _nawCollection[i-1] = _nawCollection[i]; // shift elements back + } + return x; +} + +int NAWArray::verwijderLaatsteMetNaam(const std::string& naam) { + int x = -1; // deleted element index + for (unsigned _i = 0; _i < _nawCollectionSize; _i++) { + unsigned i = _nawCollectionSize - (_i + 1); // calculate index from back + if (x == -1) { + if (!_nawCollection[i]->heeftNaam(naam)) continue; + x = i; // set deleted element index + _nawCollectionSize--; // decrease container size + _i = i; // set loop index to deleted element + i++; // skip deleted element + } + i = _i; // move forward through array shifting elements back + if (i == 0) continue; // check if i-1 >= 0 + _nawCollection[i-1] = _nawCollection[i]; // shift elements back + } + return x; +} + +int NAWArray::verwijderAllenMetNaam(const std::string& naam) { + int c = 0; // deleted element count + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (_nawCollection[i]->heeftNaam(naam)) { + c++; // pun unintended + _nawCollectionSize--; // decrease container size + } + if (i + c >= _nawCollectionSize) break; + _nawCollection[i] = _nawCollection[i + c]; // shift elements back + } + return c; +} + +int NAWArray::verwijderEersteMetAdresEnPlaats(const std::string& adres, const std::string& plaats) { + int x = -1; // deleted element index + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (x == -1) { + if (!_nawCollection[i]->heeftAdres(adres)) continue; + if (!_nawCollection[i]->heeftPlaats(plaats)) continue; + x = i; // set deleted element index + _nawCollectionSize--; // decrease container size + i++; // skip deleted element + } + if (i == 0) continue; // check if i-1 >= 0 + _nawCollection[i-1] = _nawCollection[i]; // shift elements back + } + return x; +} + +int NAWArray::verwijderAllenMetAdresEnPlaats(const std::string& adres, const std::string& plaats) { + int c = 0; // deleted element count + for (unsigned i = 0; i < _nawCollectionSize; i++) { + if (_nawCollection[i]->heeftAdres(adres) && + _nawCollection[i]->heeftPlaats(plaats)) { + c++; // pun unintended + _nawCollectionSize--; // decrease container size + } + if (i + c >= _nawCollectionSize) break; + _nawCollection[i] = _nawCollection[i + c]; // shift elements back + } + return c; +} |