-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository_in_memory.cpp
100 lines (86 loc) · 3.12 KB
/
repository_in_memory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "repository_in_memory.h"
#include "domain.h"
#include <algorithm>
#include <exception>
#include <typeinfo>
#include "setDebugNew.h"
#define new DEBUG_NEW
using namespace std;
/// Adds a new tower to the repo
/// Returns a 1 if the location is not unique (not a success), 0 otherwise
void RepoInMemory::add(const Tower& tower)
{
auto it = std::find_if(this->elements.begin(), this->elements.end(), [tower](const Tower& mytower) {return mytower.get_location() == tower.get_location(); });
if (it != this->elements.end())
throw std::exception("Element already exists!");
elements.push_back(tower);
}
/// Removes a tower from the repo
/// Returns a 1 if the location does not exist (not a success), 0 otherwise
void RepoInMemory::remove(const std::string& location)
{
int oldSize = this->elements.size();
this->elements.erase(std::remove_if(this->elements.begin(), this->elements.end(), [location](const Tower& tower) { return tower.get_location() == location; }), this->elements.end());
if (this->elements.size() == oldSize)
throw std::exception("Tried to remove a non existing element!");
}
/// Updates a new tower from the repo
/// Returns a 1 if the location does not exist (not a success), 0 otherwise
void RepoInMemory::update(const Tower& tower)
{
auto it = std::find_if(this->elements.begin(), this->elements.end(), [tower](const Tower& mytower) {return mytower.get_location() == tower.get_location(); });
if (it != this->elements.end())
{
*it = tower;
return;
}
throw std::exception("Tried to update a non existing element!");
}
Tower RepoInMemory::search(const std::string& location) const
{
auto it = std::find_if(this->elements.begin(), this->elements.end(), [location](const Tower& mytower) {return mytower.get_location() == location; });
if (it != this->elements.end())
return *it;
return Tower();
}
unique_ptr<RepoInterface::IteratorInterface> RepoInMemory::begin() const
{
// TODO: insert return statement here
//this->first = iterator(elements.begin(), *this);
return make_unique<InMemoryIterator>(this, this->elements.cbegin());
}
unique_ptr<RepoInterface::IteratorInterface> RepoInMemory::end() const
{
// TODO: insert return statement here
//this->last = iterator(elements.end(), *this);
return make_unique<InMemoryIterator>(this, this->elements.cend());
}
RepoInMemory::InMemoryIterator::InMemoryIterator(const InMemoryIterator& other): IteratorInterface{other.container}
{
this->ptr = other.ptr;
}
void RepoInMemory::InMemoryIterator::first()
{
this->ptr = ((RepoInMemory*)this->container)->elements.begin();
}
const Tower& RepoInMemory::InMemoryIterator::getTower() const
{
// TODO: insert return statement here
return *(this->ptr);
}
bool RepoInMemory::InMemoryIterator::Equals(const unique_ptr<RepoInterface::IteratorInterface> it) const
{
auto ptr = dynamic_cast<RepoInMemory::InMemoryIterator*>(it.get());
if (ptr == nullptr)
return false;
return this->getTower() != it->getTower();
}
bool RepoInMemory::InMemoryIterator::valid() const
{
return this->ptr != ((RepoInMemory*)this->container)->elements.end();
}
void RepoInMemory::InMemoryIterator::next()
{
// TODO: insert return statement here
this->ptr++;
}