#include <iostream>
class HashMap {
int c;
int s;
struct Node {
char x;
Node* next;
};
Node* a;
public:
HashMap() {
c = 10;
s = 0;
a = new Node[c];
for(int i = 0; i != c; ++i) {
a[i].x = 0;
a[i].next = NULL;
}
}
HashMap(int n) {
c = n;
s = 0;
a = new Node[c];
for(int i = 0; i != c; ++i) {
a[i].x = 0;
a[i].next = NULL;
}
}
~HashMap() {
clear();
Node* tmp_n;
for(int i = 0; i != c; ++i) {
tmp_n = a[i].next;
delete tmp_n;
}
c = 0;
Node* tmp_a = a;
delete[] tmp_a;
a = NULL;
}
HashMap(const HashMap& src) {
c = src.c;
s = src.s;
a = new Node[c];
for(int i = 0; i != c; ++i) {
a[i].x = src.a[i].x;
a[i].next = src.a[i].next;
}
}
HashMap& operator=(const HashMap& src) {
if(&src==this) {
return *this;
}
else {
c = src.c;
s = src.s;
a = new Node[c];
for(int i = 0; i != c; ++i) {
a[i].x = src.a[i].x;
a[i].next = src.a[i].next;
}
return *this;
}
}
int hash(int key) { return (key % c); }
bool insert(int key, char value);
bool remove(int key, char& value);
bool search(int key, char& value);
void clear();
bool is_empty() { return (s==0) ? true : false; }
std::size_t capacity() { return c; }
std::size_t size() { return s; }
double load();
std::ostream& print(std::ostream& out);
};
bool HashMap::insert(int key, char value) {
if(s==c)
return false;
int k = hash(key);
if(a[k].x == 0) {
++s;
}
a[k].x = value;
}
bool HashMap::remove(int key, char& value) {
int k = hash(key);
if(is_empty() || (a[k].x == 0)) {
return false;
}
value = a[k].x;
--s;
a[k].x = 0;
return true;
}
bool HashMap::search(int key, char& value) {
int k = hash(key);
if(a[k].x != 0) {
value = a[k].x;
return true;
}
return false;
}
void HashMap::clear() {
for(int i = 0; i != c; ++i) {
a[i].x = 0;
}
s = 0;
}
double HashMap::load() {
double l = 0;
int r = 0;
for(int i = 0; i != c; ++i) {
if(a[i].next != NULL) {
++r;
}
}
l = (r + s)/c;
return l;
}
std::ostream& HashMap::print(std::ostream& out) {
if(is_empty()) {
out << "<empty list>";
}
else {
out << "[ ";
for(int i = 0; i != (c-1); ++i) {
if(a[i].x == 0) {
out << "--";
}
if(a[i].x != 0) {
out << a[i].x;
}
out << " : ";
}
out << " ]";
}
out << std::endl;
return out;
}
int main() {
HashMap h = HashMap();
std::cout << std::boolalpha << h.is_empty() << std::endl;
h.insert(3,'x');
h.print(std::cout);
std::cout << std::boolalpha << h.is_empty() << std::endl;
std::cout << h.size() << std::endl;
return 0;
}