-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneBookMap.cpp
More file actions
182 lines (136 loc) · 5.4 KB
/
PhoneBookMap.cpp
File metadata and controls
182 lines (136 loc) · 5.4 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// ===============================================================================
// PhoneBookMap.h
// ===============================================================================
#include "PhoneBookMap.h"
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <sstream>
namespace PhoneBook
{
std::size_t PhoneBookMap::size() const
{
return m_map.size();
}
bool PhoneBookMap::insert(const std::string& first, const std::string& last, std::size_t number)
{
const std::string& key = getKeyFromName(first, last);
std::pair<std::string, std::size_t> entry(key, number);
std::pair<std::unordered_map<std::string, std::size_t>::iterator, bool> result = m_map.insert(entry);
// returns a bool value set to true if and only if the insertion took place
bool insertionSucceeded = result.second;
return insertionSucceeded;
}
bool PhoneBookMap::update(const std::string& first, const std::string& last, std::size_t number)
{
const std::string& key = getKeyFromName(first, last);
std::unordered_map<std::string, std::size_t>::iterator pos = m_map.find(key);
if (pos == m_map.end()) {
return false;
}
else {
(*pos).second = number;
return true;
}
}
bool PhoneBookMap::search(const std::string& first, const std::string& last, std::size_t& number) const
{
const std::string& key = getKeyFromName(first, last);
std::unordered_map<std::string, std::size_t>::const_iterator pos = m_map.find(key);
if (pos == m_map.end()) {
std::cout << first << " " << last << " not found!" << std::endl;
return false;
}
else {
std::pair<std::string, std::size_t> result = *pos;
number = result.second;
return true;
}
}
bool PhoneBookMap::contains(const std::string& first, const std::string& last) const
{
const std::string& key = getKeyFromName(first, last);
std::unordered_map<std::string, std::size_t>::const_iterator pos = m_map.find(key);
return pos != m_map.end();
}
bool PhoneBookMap::remove(const std::string& first, const std::string& last)
{
const std::string& key = getKeyFromName(first, last);
std::size_t numErased = m_map.erase(key);
return numErased == 1;
}
// ---------------------------------------------------------------------------
std::string PhoneBookMap::pairToString(const std::pair<std::string, std::size_t>& entry)
{
const std::string& key = entry.first;
std::pair<std::string, std::string> fullName = getNameFromKey(key);
std::string name(fullName.first + " " + fullName.second);
return name;
}
std::forward_list<std::string> PhoneBookMap::getNames() const
{
std::forward_list<std::string> list;
std::transform(
m_map.begin(),
m_map.end(),
std::front_insert_iterator<std::forward_list<std::string>>(list),
pairToString
);
return list;
}
// ---------------------------------------------------------------------------
std::string PhoneBookMap::append(const std::string& first, const std::pair<std::string, std::size_t>& next)
{
static int counter = 0;
counter++;
const std::string& key = next.first;
std::pair<std::string, std::string> fullName = getNameFromKey(key);
std::string name = fullName.first + " " + fullName.second;
std::ostringstream ss;
ss << std::setfill('0') << std::setw(2) << counter
<< ": " << std::setfill(' ') << std::setw(16) << std::left << name << " Phone: "
<< next.second << std::endl;
return first + ss.str();
}
std::string PhoneBookMap::toString() const {
std::string s = std::accumulate(
m_map.begin(),
m_map.end(),
std::string(),
append
);
return s;
};
// ---------------------------------------------------------------------------
void PhoneBookMap::printEntry(const std::pair<std::string, std::size_t>& entry)
{
const std::string& key = entry.first;
std::pair<std::string, std::string> fullName = getNameFromKey(key);
std::size_t number = entry.second;
std::cout << fullName.first << " " << fullName.second << ": " << number << std::endl;
}
void PhoneBookMap::print() const
{
std::for_each(
m_map.begin(),
m_map.end(),
printEntry
);
}
// ---------------------------------------------------------------------------
std::pair<std::string, std::string> PhoneBookMap::getNameFromKey(const std::string& key)
{
std::size_t pos = key.find("_");
const std::string& first = key.substr(0, pos);
const std::string& last = key.substr(pos + 1);
return std::pair<std::string, std::string>(first, last);
}
std::string PhoneBookMap::getKeyFromName(const std::string& first, const std::string& last)
{
return first + "_" + last; // "Hubert", "Mueller" ==> "Hubert_Mueller"
}
}
// ===============================================================================
// End-of-File
// ===============================================================================