Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //// Include files added here to make code a single file
- // mallocheap.h
- #ifndef _MALLOCHEAP_H_
- #define _MALLOCHEAP_H_
- #include <stdlib.h>
- #if defined(__SVR4)
- extern "C" size_t malloc_usable_size (void *);
- #else
- extern "C" size_t malloc_usable_size (void *) throw ();
- #endif
- #if defined(_WIN32) || defined(linux)
- #include <malloc.h>
- #elif defined(__APPLE__)
- #include <malloc/malloc.h>
- #endif
- /**
- * @class mallocHeap
- * @brief A "source heap" that uses malloc and free.
- */
- namespace HL {
- class mallocHeap {
- public:
- ~mallocHeap (void) {}
- inline void * malloc (size_t sz) {
- return ::malloc (sz);
- }
- inline void free (void * ptr) {
- ::free (ptr);
- }
- #if defined(_MSC_VER)
- inline size_t getSize (void * ptr) {
- return ::_msize (ptr);
- }
- #elif defined(__GNUC__) && !defined(__SVR4)
- inline size_t getSize (void * ptr) {
- return ::malloc_usable_size (ptr);
- }
- #elif defined(__APPLE__)
- inline size_t getSize (void * ptr) {
- return ::malloc_size (ptr);
- }
- #endif
- };
- };
- #endif
- // end mallocheap.h
- //// hash.h
- #ifndef _HL_HASH_H_
- #define _HL_HASH_H_
- #include <stdlib.h>
- namespace HL {
- template <typename Key>
- extern size_t hash (Key k);
- template <>
- inline size_t hash (void * v) {
- return (size_t) v;
- }
- template <>
- inline size_t hash (const void * v) {
- return (size_t) ((size_t) v);
- }
- template <>
- inline size_t hash (int v) {
- return (size_t) v;
- }
- }
- #endif
- // end hash.h
- #ifndef _HL_MYHASHMAP_H_
- #define _HL_MYHASHMAP_H_
- #include <assert.h>
- #include <new>
- //#include "mallocheap.h"
- //#include "hash.h"
- namespace HL {
- template <typename Key,
- typename Value,
- class Allocator = mallocHeap>
- class MyHashMap {
- public:
- MyHashMap (int size = INITIAL_NUM_BINS)
- : num_bins (size)
- {
- bins = new (alloc.malloc (sizeof(ListNodePtr) * num_bins)) ListNodePtr;
- for (int i = 0 ; i < num_bins; i++) {
- bins[i] = NULL;
- }
- }
- private:
- class ListNode {
- public:
- ListNode (void)
- : next (NULL)
- {}
- Key key;
- Value value;
- ListNode * next;
- };
- public:
- class iterator {
- public:
- iterator (void)
- : _theNode (NULL),
- _index (0)
- {}
- explicit iterator (ListNode * l, int index)
- : _theNode (l),
- _index (index)
- {}
- Value operator*(void) {
- return _theNode->value;
- }
- bool operator!=(iterator other) const {
- return (this->_theNode != other._theNode);
- }
- bool operator==(iterator other) const {
- return (this->_theNode == other._theNode);
- }
- iterator& operator++ (void) {
- if (_theNode) {
- if (_theNode->next) {
- _theNode = _theNode->next;
- } else {
- _theNode = NULL;
- for (_index++; (_index < num_bins) && (bins[_index] == NULL); _index++)
- ;
- if (_index < num_bins) {
- _theNode = bins[_index];
- }
- }
- }
- return *this;
- }
- iterator& operator=(iterator& it) {
- this->_theNode = it->_theNode;
- this->_index = it->_index;
- return *this;
- }
- private:
- ListNode * _theNode;
- int _index;
- };
- iterator begin (void) {
- iterator it (bins[0], 0);
- return it;
- }
- iterator end (void) {
- iterator it (NULL, num_bins - 1);
- return it;
- }
- iterator find (Key k) {
- int binIndex = (unsigned int) hash(k) % num_bins;
- ListNode * l = bins[binIndex];
- while (l != NULL) {
- if (l->key == k) {
- return iterator (l, binIndex);
- }
- l = l->next;
- }
- // Didn't find it.
- return end();
- }
- private:
- void insert (Key k, Value v) {
- int binIndex = (unsigned int) hash(k) % num_bins;
- ListNode * l = new (alloc.malloc (sizeof(ListNode))) ListNode;
- l->key = k;
- l->value = v;
- l->next = bins[binIndex];
- bins[binIndex] = l;
- }
- enum { INITIAL_NUM_BINS = 511 };
- const int num_bins;
- typedef ListNode * ListNodePtr;
- ListNodePtr * bins;
- Allocator alloc;
- };
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement