Advertisement
Guest User

non-static nested question

a guest
Jan 14th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. //// Include files added here to make code a single file
  2.  
  3.  
  4.  
  5. // mallocheap.h
  6. #ifndef _MALLOCHEAP_H_
  7. #define _MALLOCHEAP_H_
  8.  
  9. #include <stdlib.h>
  10.  
  11. #if defined(__SVR4)
  12. extern "C" size_t malloc_usable_size (void *);
  13. #else
  14. extern "C" size_t malloc_usable_size (void *) throw ();
  15. #endif
  16.  
  17. #if defined(_WIN32) || defined(linux)
  18. #include <malloc.h>
  19. #elif defined(__APPLE__)
  20. #include <malloc/malloc.h>
  21. #endif
  22.  
  23. /**
  24.  * @class mallocHeap
  25.  * @brief A "source heap" that uses malloc and free.
  26.  */
  27.  
  28. namespace HL {
  29.  
  30. class mallocHeap {
  31. public:
  32.  
  33.   ~mallocHeap (void) {}
  34.  
  35.   inline void * malloc (size_t sz) {
  36.     return ::malloc (sz);
  37.   }
  38.  
  39.  
  40.   inline void free (void * ptr) {
  41.     ::free (ptr);
  42.   }
  43.  
  44. #if defined(_MSC_VER)
  45.   inline size_t getSize (void * ptr) {
  46.     return ::_msize (ptr);
  47.   }
  48. #elif defined(__GNUC__) && !defined(__SVR4)
  49.   inline size_t getSize (void * ptr) {
  50.     return ::malloc_usable_size (ptr);
  51.   }
  52. #elif defined(__APPLE__)
  53.   inline size_t getSize (void * ptr) {
  54.     return ::malloc_size (ptr);
  55.   }
  56. #endif
  57.  
  58. };
  59.  
  60. };
  61.  
  62. #endif
  63. // end mallocheap.h
  64.  
  65.  
  66. //// hash.h
  67. #ifndef _HL_HASH_H_
  68. #define _HL_HASH_H_
  69.  
  70. #include <stdlib.h>
  71.  
  72. namespace HL {
  73.   template <typename Key>
  74.     extern size_t hash (Key k);
  75.  
  76.   template <>
  77.     inline size_t hash (void * v) {
  78.     return (size_t) v;
  79.   }
  80.  
  81.   template <>
  82.     inline size_t hash (const void * v) {
  83.     return (size_t) ((size_t) v);
  84.   }
  85.  
  86.   template <>
  87.     inline size_t hash (int v) {
  88.     return (size_t) v;
  89.   }
  90. }
  91.  
  92. #endif
  93. // end hash.h
  94.  
  95.  
  96. #ifndef _HL_MYHASHMAP_H_
  97. #define _HL_MYHASHMAP_H_
  98.  
  99. #include <assert.h>
  100. #include <new>
  101.  
  102. //#include "mallocheap.h"
  103. //#include "hash.h"
  104.  
  105. namespace HL {
  106.  
  107. template <typename Key,
  108.       typename Value,
  109.       class Allocator = mallocHeap>
  110. class MyHashMap {
  111.  
  112. public:
  113.  
  114.   MyHashMap (int size = INITIAL_NUM_BINS)
  115.     : num_bins (size)
  116.   {
  117.     bins = new (alloc.malloc (sizeof(ListNodePtr) * num_bins)) ListNodePtr;
  118.     for (int i = 0 ; i < num_bins; i++) {
  119.       bins[i] = NULL;
  120.     }
  121.   }
  122.  
  123.  
  124. private:
  125.  
  126.   class ListNode {
  127.   public:
  128.     ListNode (void)
  129.       : next (NULL)
  130.     {}
  131.     Key key;
  132.     Value value;
  133.     ListNode * next;
  134.   };
  135.  
  136. public:
  137.  
  138.   class iterator {
  139.   public:
  140.  
  141.     iterator (void)
  142.       : _theNode (NULL),
  143.     _index (0)
  144.     {}
  145.    
  146.     explicit iterator (ListNode * l, int index)
  147.       : _theNode (l),
  148.     _index (index)
  149.     {}
  150.  
  151.     Value operator*(void) {
  152.       return _theNode->value;
  153.     }
  154.    
  155.     bool operator!=(iterator other) const {
  156.       return (this->_theNode != other._theNode);
  157.     }
  158.    
  159.     bool operator==(iterator other) const {
  160.       return (this->_theNode == other._theNode);
  161.     }
  162.    
  163.     iterator& operator++ (void) {
  164.       if (_theNode) {
  165.     if (_theNode->next) {
  166.       _theNode = _theNode->next;
  167.     } else {
  168.       _theNode = NULL;
  169.       for (_index++; (_index < num_bins) && (bins[_index] == NULL); _index++)
  170.         ;
  171.       if (_index < num_bins) {
  172.         _theNode = bins[_index];
  173.       }
  174.     }
  175.       }
  176.       return *this;
  177.     }
  178.    
  179.     iterator& operator=(iterator& it) {
  180.       this->_theNode = it->_theNode;
  181.       this->_index = it->_index;
  182.       return *this;
  183.     }
  184.  
  185.   private:
  186.     ListNode * _theNode;
  187.     int _index;
  188.   };
  189.  
  190.   iterator begin (void) {
  191.     iterator it (bins[0], 0);
  192.     return it;
  193.   }
  194.  
  195.   iterator end (void) {
  196.     iterator it (NULL, num_bins - 1);
  197.     return it;
  198.   }
  199.  
  200.   iterator find (Key k) {
  201.     int binIndex = (unsigned int) hash(k) % num_bins;
  202.     ListNode * l = bins[binIndex];
  203.     while (l != NULL) {
  204.       if (l->key == k) {
  205.     return iterator (l, binIndex);
  206.       }
  207.       l = l->next;
  208.     }
  209.     // Didn't find it.
  210.     return end();
  211.   }
  212.  
  213.  
  214. private:
  215.  
  216.   void insert (Key k, Value v) {
  217.     int binIndex = (unsigned int) hash(k) % num_bins;
  218.     ListNode * l = new (alloc.malloc (sizeof(ListNode))) ListNode;
  219.     l->key = k;
  220.     l->value = v;
  221.     l->next = bins[binIndex];
  222.     bins[binIndex] = l;
  223.   }
  224.  
  225.   enum { INITIAL_NUM_BINS = 511 };
  226.  
  227.   const int num_bins;
  228.  
  229.   typedef ListNode * ListNodePtr;
  230.   ListNodePtr * bins;
  231.   Allocator alloc;
  232. };
  233.  
  234. };
  235.  
  236. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement