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