Advertisement
Guest User

Untitled

a guest
Jan 21st, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.36 KB | None | 0 0
  1. #ifndef MAP_H_
  2. #define MAP_H_
  3.  
  4. #include <iostream>
  5. #include "pair.h"
  6.  
  7. namespace mtm {
  8.  
  9. // exception definitions
  10. class MappingDoesntExist : public std::exception {
  11. public:
  12.     virtual const char* what() const throw() {
  13.         return "Mapping for key was not found!\n";
  14.     }
  15. };
  16. class MappingAlreadyExists : public std::exception {
  17. public:
  18.     virtual const char* what() const throw() {
  19.         return "Key already exists";
  20.     }
  21. };
  22.  
  23.  
  24. template <class KeyType, class DataType>
  25. class Map {
  26. private:
  27.  
  28.     //Node
  29.     template <class S, class T>
  30.     class Node {
  31.    
  32.     public:
  33.         const S key;
  34.         T data;
  35.         Node<S, T>* next;
  36.  
  37.         //C'tor
  38.         Node(const KeyType& sKey, const DataType& sData) :
  39.             key(sKey), data(sData), next(NULL) {
  40.         }
  41.  
  42.         //Copy C'tor
  43.         Node(const Node<S, T>& sNode):
  44.             key(sNode.key), data(sNode.data), next(NULL) {
  45.         }
  46.  
  47.         friend class map;
  48.         friend class iterator;
  49.         friend class const_iterator;
  50.     };
  51.     //End of Node
  52.  
  53.     //Map structure
  54.     Node<KeyType, DataType>* head;
  55.     Node<KeyType, DataType>* tail;
  56.     int mSize;
  57.     //End of Map stracture
  58.  
  59. public:
  60.  
  61.     /*
  62.     Class declarations
  63.     */
  64.     class iterator;
  65.     class const_iterator;
  66.  
  67.  
  68.     //Beginning of iterator
  69.     class iterator {
  70.     private:
  71.         Node<KeyType, DataType>* current;
  72.         Map<KeyType, DataType>* map;
  73.  
  74.     public:
  75.  
  76.         //C'tor
  77.         iterator() :
  78.             current(NULL), map(NULL) {}
  79.  
  80.         iterator(Map<KeyType, DataType>* mapPtr, Node<KeyType, DataType>* nodePtr):
  81.             current(nodePtr), map(mapPtr) {}
  82.  
  83.         iterator(Map<KeyType, DataType>::const_iterator& sIterator):
  84.             current(sIterator.current), map(sIterator.map) {
  85.         }
  86.  
  87.  
  88.         iterator& operator=(const iterator& sIterator);
  89.         iterator& operator++();
  90.         iterator operator++(int);
  91.         bool operator==(const iterator& sIterator) const;
  92.         bool operator!=(const iterator& sIterator) const;
  93.         const Pair<KeyType,DataType> operator*() const;
  94.         const KeyType& getKey() const;
  95.         DataType& getData();
  96.  
  97.         friend class Map;
  98.     };
  99.     //End of iterator
  100.  
  101.  
  102.     //Beginning of const_iterator
  103.     class const_iterator {
  104.     private:
  105.         Node<KeyType, DataType>* current;
  106.         const Map<KeyType, DataType>* map;
  107.  
  108.     public:
  109.  
  110.         //C'tor
  111.         const_iterator() :
  112.             current(NULL), map(NULL) {}
  113.  
  114.         const_iterator(const Map<KeyType, DataType>* mapPtr, Node<KeyType, DataType>* nodePtr):
  115.             current(nodePtr), map(mapPtr) {}
  116.  
  117.         const_iterator(const Map<KeyType, DataType>::const_iterator& sIterator):
  118.             current(sIterator.current), map(sIterator.map) {}
  119.  
  120.         const_iterator(const Map<KeyType, DataType>::iterator& sIterator):
  121.             current(sIterator.current), map(sIterator.map) {}
  122.  
  123.        
  124.         const_iterator& operator=(const const_iterator& sIterator);
  125.         const_iterator& operator++();
  126.         const_iterator operator++(int);
  127.         bool operator==(const const_iterator& sIterator) const;
  128.         bool operator!=(const const_iterator& sIterator) const;
  129.         const Pair<KeyType,DataType> operator*() const;
  130.         const KeyType& getKey() const;
  131.         const DataType& getData() const;
  132.  
  133.  
  134.         friend class Map;
  135.     };
  136.     //End of const_iterator
  137.  
  138.  
  139.     //C`tor
  140.     Map() : head(NULL), tail(NULL), mSize(0) {}
  141.  
  142.     //D`tor
  143.     ~Map() {
  144.         while (head != NULL)
  145.         {
  146.             Node<KeyType, DataType>* tempNode = head;
  147.             head = head->next;
  148.             delete(tempNode);
  149.         }
  150.         mSize = 0;
  151.     }
  152.  
  153.     //Copy C'tor
  154.     Map(const Map<KeyType, DataType>& sMap) : head(NULL), tail(NULL), mSize(0) {
  155.         for(Map::const_iterator it = sMap.begin(); it != sMap.end(); it++ ){
  156.             insert(it.getKey(), it.getData());
  157.         }
  158.  
  159.     }
  160.  
  161.     Map::const_iterator begin() const;
  162.     Map::const_iterator end() const;
  163.     Map::iterator begin();
  164.     Map::iterator end();
  165.     void insert(const KeyType& key, const DataType& data);
  166.     bool isIn(const KeyType& key) const;
  167.     void remove(const KeyType key);
  168.     void clear();
  169.     bool isEmpty();
  170.     int size() const;
  171.     const DataType& operator[](const KeyType key) const;
  172.     DataType& operator[](const KeyType key);
  173.  
  174. };
  175.  
  176. /*
  177. Map methods and operators
  178. */
  179. template<class S, class T>
  180. typename Map<S,T>::const_iterator Map<S,T>::begin() const {
  181.     Map::const_iterator iterator;
  182.     iterator.current = head;
  183.     iterator.map = this;
  184.     return iterator;
  185. }
  186.  
  187. template<class S, class T>
  188. typename Map<S,T>::const_iterator Map<S, T>::end() const {
  189.     return Map<S,T>::const_iterator();
  190. }
  191.  
  192. template <class S, class T>
  193. typename Map<S,T>::iterator Map<S, T>::begin() {
  194.     Map::iterator iterator;
  195.     iterator.current = head;
  196.     iterator.map = this;
  197.     return iterator;
  198. }
  199.  
  200. template <class S, class T>
  201. typename Map<S,T>::iterator Map<S, T>::end() {
  202.     return Map<S,T>::iterator();
  203. }
  204.  
  205. template <class S, class T>
  206. void Map<S,T>::insert(const S& key, const T& data) {
  207.     Node<S,T>* newNode = new Node<S,T>(key, data);
  208.     if (mSize == 0){
  209.         head = newNode;
  210.         tail = newNode;
  211.         mSize++;
  212.         return;
  213.     }
  214.  
  215.     if (isIn(key)){
  216.         throw MappingAlreadyExists();
  217.         return;
  218.     }
  219.  
  220.     tail->next = newNode;
  221.     tail = newNode;
  222.     mSize++;
  223.     return;
  224. }
  225.  
  226. template <class S, class T>
  227. bool Map<S,T>::isIn(const S& key) const {
  228.     Map<S,T>::const_iterator it = begin();
  229.     while (it != end()){
  230.         if (it.current->key == key){
  231.             return true;
  232.         }
  233.         it++;
  234.     }
  235.     return false;
  236. }
  237.  
  238. template <class S, class T>
  239. void Map<S,T>::remove(const S key) {
  240.     Node<S,T>* nodeToRemove;
  241.  
  242.     //If the node to be removed is the first one.
  243.     if (head->key == key){
  244.         nodeToRemove = head;
  245.         head = head->next;
  246.         delete(nodeToRemove);
  247.         mSize--;
  248.         return;
  249.     }
  250.  
  251.     //Find a node and remove it.
  252.     Node<S,T>* currentNode = head;
  253.     Node<S,T>* nextNode;
  254.     while (currentNode != NULL)
  255.     {  
  256.         nextNode = currentNode->next;
  257.         if (nextNode != NULL && nextNode->key == key)
  258.         {
  259.             nodeToRemove = nextNode;
  260.             currentNode->next = nodeToRemove->next;
  261.             delete(nodeToRemove);
  262.             mSize--;
  263.             return;
  264.         }
  265.  
  266.         currentNode = currentNode->next;
  267.     }
  268.  
  269.     throw MappingDoesntExist();
  270.     return;
  271. }
  272.  
  273. template <class S, class T>
  274. void Map<S,T>::clear() {
  275.     Node<S,T>* tempNode;
  276.     while(head != NULL){
  277.         tempNode = head;
  278.         head=head->next;
  279.         delete (tempNode);
  280.     }
  281.     mSize = 0;
  282. }
  283.  
  284. template <class S, class T>
  285. bool Map<S,T>::isEmpty() {
  286.     return (mSize == 0);
  287. }
  288.  
  289. template <class S, class T>
  290. int Map<S,T>::size() const {
  291.     return mSize;
  292. }
  293.  
  294. template <class S, class T>
  295. const T& Map<S,T>::operator[](const S key) const {
  296.     for (Map::const_iterator it = begin(); it != end(); ++it){
  297.         if (it.current->key == key){
  298.             return it.current->data;
  299.         }
  300.     }
  301.     throw MappingDoesntExist();
  302. }
  303.  
  304. template <class S, class T>
  305. T& Map<S,T>::operator[](const S key) {
  306.     for (Map::iterator it = begin(); it != end(); ++it){
  307.         if (it.current->key == key){
  308.             return it.current->data;
  309.         }
  310.     }
  311.     throw MappingDoesntExist();
  312. }
  313. /* End of Map methods and operators */
  314.  
  315.  
  316. /* iterator methods and operators */
  317. template <class S, class T>
  318. typename Map<S,T>::iterator& Map<S,T>::iterator::operator=(const Map<S,T>::iterator& sIterator) {
  319.     current = sIterator.current;
  320.     map = sIterator.map;
  321. }
  322.  
  323. template <class S, class T>
  324. typename Map<S,T>::iterator& Map<S,T>::iterator::operator++() {
  325.     current = current->next;
  326.     return *this;
  327. }
  328.  
  329. template <class S, class T>
  330. typename Map<S,T>::iterator Map<S,T>::iterator::operator++(int) {
  331.     Map<S,T>::iterator tmpIt(*this);
  332.     current = current->next;
  333.     return tmpIt;
  334. }
  335.  
  336. template <class S, class T>
  337. bool Map<S,T>::iterator::operator==(const Map<S,T>::iterator& sIterator) const {
  338.     return (current == sIterator.current);
  339. }
  340.  
  341. template <class S, class T>
  342. bool Map<S,T>::iterator::operator!=(const Map<S,T>::iterator& sIterator) const {
  343.     return !(*this == sIterator);
  344. }
  345.  
  346. template <class S, class T>
  347. const Pair<S, T> Map<S,T>::iterator::operator*() const {
  348.     return Pair<S,T>(current->key, current->data);
  349. }
  350.  
  351. template <class S, class T>
  352. const S& Map<S,T>::iterator::getKey() const {
  353.     return current->key;
  354. }
  355.  
  356. template <class S, class T>
  357. T& Map<S,T>::iterator::getData() {
  358.     return current->data;
  359. }
  360.  
  361.  
  362. /* End of iterator methods and operators */
  363.  
  364.  
  365. /* iterator methods and operators */
  366. template <class S, class T>
  367. typename Map<S,T>::const_iterator& Map<S,T>::const_iterator::operator=(const Map<S,T>::const_iterator& sIterator) {
  368.     current = sIterator.current;
  369.     map = sIterator.map;
  370. }
  371.  
  372. template <class S, class T>
  373. typename Map<S,T>::const_iterator& Map<S,T>::const_iterator::operator++() {
  374.     current = current->next;
  375.     return *this;
  376. }
  377.  
  378. template <class S, class T>
  379. typename Map<S,T>::const_iterator Map<S,T>::const_iterator::operator++(int) {
  380.     Map<S,T>::const_iterator tmpIt(*this);
  381.     current = current->next;
  382.     return tmpIt;
  383. }
  384.  
  385. template <class S, class T>
  386. bool Map<S,T>::const_iterator::operator==(const Map<S,T>::const_iterator& sIterator) const {
  387.     return (current == sIterator.current);
  388. }
  389.  
  390. template <class S, class T>
  391. bool Map<S,T>::const_iterator::operator!=(const Map<S,T>::const_iterator& sIterator) const {
  392.     return !(*this == sIterator);
  393. }
  394.  
  395. template <class S, class T>
  396. const Pair<S, T> Map<S,T>::const_iterator::operator*() const {
  397.     return Pair<S,T>(current->key, current->data);
  398. }
  399.  
  400. template <class S, class T>
  401. const S& Map<S,T>::const_iterator::getKey() const {
  402.     return current->key;
  403. }
  404.  
  405. template <class S, class T>
  406. const T& Map<S,T>::const_iterator::getData() const {
  407.     return current->data;
  408. }
  409.  
  410. /* End of const_iterator methods and operators */
  411.  
  412.  
  413. template<class T, class V, class Predicate, class Action>
  414. void MapIf(Map<T,V>& map, Predicate predicate, Action action){
  415.     for (typename Map<T, V>::iterator it = map.begin(); it != map.end(); it++){
  416.         if (predicate(it.getKey())){
  417.             action(it.getData());
  418.         }
  419.     }
  420. }
  421.  
  422.  
  423. } /* End of mtm namespace */
  424.  
  425. #endif /* MAP_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement