algorithm-in-python/dataStructure/map.cc

171 lines
3.1 KiB
C++
Raw Normal View History

2018-07-08 23:28:29 +08:00
/* mbinary
#########################################################################
# File : map.cc
# Author: mbinary
# Mail: zhuheqin1@gmail.com
2019-01-31 12:09:46 +08:00
# Blog: https://mbinary.xyz
2018-07-08 23:28:29 +08:00
# Github: https://github.com/mbinary
# Created Time: 2018-04-26 10:33
# Description:
#########################################################################
*/
#include<stdio.h>
bool isZero(float a)
{
2020-04-15 12:28:20 +08:00
return a < 0.00001 && -a < 0.00001;
2018-07-08 23:28:29 +08:00
}
2020-04-15 12:28:20 +08:00
template<class, class> class map;
//notice that if you declare a class template,declare the class first like this.
template<class t1, class t2>
2018-07-08 23:28:29 +08:00
class pair
{
2020-04-15 12:28:20 +08:00
friend class map<t1, t2>;
pair<t1, t2> *next;
public:
2018-07-08 23:28:29 +08:00
t1 first;
t2 second;
};
2020-04-15 12:28:20 +08:00
template<class t1, class t2>
2018-07-08 23:28:29 +08:00
class map
{
int n;
2020-04-15 12:28:20 +08:00
pair<t1, t2> head;
2018-07-08 23:28:29 +08:00
int cur;
2020-04-15 12:28:20 +08:00
pair<t1, t2> *last_visit;
public:
2018-07-08 23:28:29 +08:00
map();
~map();
bool has(t1);
void erase(t1);
t2& operator[](t1);
2020-04-15 12:28:20 +08:00
pair<t1, t2> &locate(int index = -1);
2018-07-08 23:28:29 +08:00
int size();
};
2020-04-15 12:28:20 +08:00
template<class t1, class t2>
map<t1, t2>::map()
{
n = 0;
cur = -1;
last_visit = &head;
head.next = NULL;
2018-07-08 23:28:29 +08:00
head.first = head.second = 0;
}
2020-04-15 12:28:20 +08:00
template<class t1, class t2>
map<t1, t2>::~map()
2018-07-08 23:28:29 +08:00
{
2020-04-15 12:28:20 +08:00
pair<t1, t2> *p, *q = &head;
while (q != NULL) {
p = q->next;
2018-07-08 23:28:29 +08:00
delete q;
2020-04-15 12:28:20 +08:00
q = p;
2018-07-08 23:28:29 +08:00
}
}
2020-04-15 12:28:20 +08:00
template<class t1, class t2>
bool map<t1, t2>::has(t1 key)
2018-07-08 23:28:29 +08:00
{
2020-04-15 12:28:20 +08:00
pair<t1, t2> *p = head.next;
for (int i = 0; i < n && p->first <= key; ++i) {
if (isZero(p->first - key)) return 1;
p = p->next;
2018-07-08 23:28:29 +08:00
}
2020-04-15 12:28:20 +08:00
2018-07-08 23:28:29 +08:00
return 0;
}
2020-04-15 12:28:20 +08:00
template<class t1, class t2>
pair<t1, t2>& map<t1, t2>::locate(int index)
2018-07-08 23:28:29 +08:00
{
2020-04-15 12:28:20 +08:00
if (index >= n || index < 0) {
2018-07-08 23:28:29 +08:00
printf("the index is out of range\n");
return head;
}
2020-04-15 12:28:20 +08:00
if (cur > index) {
2018-07-08 23:28:29 +08:00
last_visit = &head;
cur = -1;
}
2020-04-15 12:28:20 +08:00
while (cur < index) {
2018-07-08 23:28:29 +08:00
last_visit = last_visit->next;
++cur;
}
2020-04-15 12:28:20 +08:00
2018-07-08 23:28:29 +08:00
return *last_visit;
}
2020-04-15 12:28:20 +08:00
template<class t1, class t2>
int map<t1, t2>::size()
2018-07-08 23:28:29 +08:00
{
return n;
}
2020-04-15 12:28:20 +08:00
template<class t1, class t2>
t2& map<t1, t2>::operator[](t1 key)
2018-07-08 23:28:29 +08:00
{
2020-04-15 12:28:20 +08:00
pair<t1, t2> * p = &head;
while (p->next != NULL) {
if (isZero(p->next->first - key)) return p->next->second;
else if (p->next->first > key) {
break;
}
p = p->next;
2018-07-08 23:28:29 +08:00
}
2020-04-15 12:28:20 +08:00
cur = -1;
last_visit = &head;
pair<t1, t2> *tmp = new pair<t1, t2>;
2018-07-08 23:28:29 +08:00
tmp ->next = p->next;
tmp->first = key;
p->next = tmp;
++n;
return tmp->second;
}
2020-04-15 12:28:20 +08:00
template<class t1, class t2>
void map<t1, t2>::erase(t1 key)
2018-07-08 23:28:29 +08:00
{
2020-04-15 12:28:20 +08:00
pair<t1, t2> *p = &head;
while (p->next != NULL) {
if (isZero(p->next->first - key)) {
pair<t1, t2> *q = p->next;
2018-07-08 23:28:29 +08:00
p->next = p->next->next;
delete q;
--n;
break;
}
2020-04-15 12:28:20 +08:00
p = p->next;
2018-07-08 23:28:29 +08:00
}
2020-04-15 12:28:20 +08:00
cur = -1;
last_visit = &head;
2018-07-08 23:28:29 +08:00
}
int main()
{
2020-04-15 12:28:20 +08:00
map<double, float> b;
for (int i = 0; i < 40; ++i) {
2018-07-08 23:28:29 +08:00
b[i] = i;
2020-04-15 12:28:20 +08:00
if (i % 3) {
2018-07-08 23:28:29 +08:00
b[i] = 1;
}
2020-04-15 12:28:20 +08:00
if (i % 2) {
2018-07-08 23:28:29 +08:00
b.erase(i);
}
}
2020-04-15 12:28:20 +08:00
for (int i = 0; i < b.size(); ++i) {
printf("item %d %g:%g\n", i, b.locate(i).first, b.locate(i).second);
2018-07-08 23:28:29 +08:00
}
2020-04-15 12:28:20 +08:00
2018-07-08 23:28:29 +08:00
return 0;
}