Advertisement
Guest User

ics46

a guest
May 24th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. // HashSet.hpp
  2. //
  3. // ICS 46 Spring 2015
  4. // Project #3: Set the Controls for the Heart of the Sun
  5. //
  6. // A HashSet is an implementation of a Set that is a separately-chained
  7. // hash table, implemented as a dynamically-allocated array of linked
  8. // lists. At any given time, the HashSet has a "size" indicating
  9. // how many elements are stored within it, along with a "capacity"
  10. // indicating the size of the array.
  11. //
  12. // As elements are added to the HashSet and the proportion of the HashSet's
  13. // size to its capacity exceeds 0.8 (i.e., there are more than 80% as many
  14. // elements as there are array cells), the HashSet should be resized so
  15. // that it is twice as large as it was before.
  16. //
  17. // You are not permitted to use the containers in the C++ Standard Library
  18. // (such as std::vector, std::list, or std::array). Instead, you'll need
  19. // to use a dynamically-allocated array and your own linked list
  20. // implementation; the linked list doesn't have to be its own class,
  21. // though you can do that, if you'd like.
  22.  
  23. #ifndef HASHSET_HPP
  24. #define HASHSET_HPP
  25.  
  26. #include <functional>
  27. #include "Set.hpp"
  28.  
  29.  
  30.  
  31. template <typename T>
  32. class HashSet : public Set<T>
  33. {
  34. public:
  35. // The default capacity of the HashSet before anything has been
  36. // added to it.
  37. static constexpr unsigned int DEFAULT_CAPACITY = 10;
  38.  
  39. // A HashFunction
  40. typedef std::function<unsigned int(const T&)> HashFunction;
  41.  
  42. public:
  43. // Initializes a HashSet to be empty, so that it will use the given
  44. // hash function whenever it needs to hash an element.
  45. HashSet(HashFunction hashFunction);
  46.  
  47. // Cleans up the HashSet so that it leaks no memory.
  48. virtual ~HashSet();
  49.  
  50. // Initializes a new HashSet to be a copy of an existing one.
  51. HashSet(const HashSet& s);
  52.  
  53. // Assigns an existing HashSet into another.
  54. HashSet& operator=(const HashSet& s);
  55.  
  56.  
  57. // isImplemented() should be modified to return true if you've
  58. // decided to implement a HashSet, false otherwise.
  59. virtual bool isImplemented() const;
  60.  
  61.  
  62. // add() adds an element to the set. If the element is already in the set,
  63. // this function has no effect. This function triggers a resizing of the
  64. // array when the ratio of size to capacity would exceed 0.8. In the case
  65. // where the array is resized, this function runs in linear time (with
  66. // respect to the number of elements, assuming a good hash function);
  67. // otherwise, it runs in constant time (again, assuming a good hash
  68. // function).
  69. virtual void add(const T& element);
  70.  
  71.  
  72. // contains() returns true if the given element is already in the set,
  73. // false otherwise. This function runs in constant time (with respect
  74. // to the number of elements, assuming a good hash function).
  75. virtual bool contains(const T& element) const;
  76.  
  77.  
  78. // size() returns the number of elements in the set.
  79. virtual unsigned int size() const;
  80.  
  81. struct elementLink{
  82. T item;
  83. T name;
  84. elementLink* next;
  85. };
  86.  
  87.  
  88. private:
  89. HashFunction hashFunction;
  90. };
  91.  
  92.  
  93.  
  94. template <typename T>
  95. HashSet<T>::HashSet(HashFunction hashFunction)
  96. : hashFunction{hashFunction}
  97. {
  98. //T *hashTable;
  99. elementLink *hashTable;
  100. hashTable = new hashTable[DEFAULT_CAPACITY];
  101.  
  102. }
  103.  
  104.  
  105. template <typename T>
  106. HashSet<T>::~HashSet()
  107. {
  108. delete hashTable[];
  109. }
  110.  
  111.  
  112. template <typename T>
  113. HashSet<T>::HashSet(const HashSet& s)
  114. : hashFunction{s.hashFunction}
  115. {
  116. }
  117.  
  118.  
  119. template <typename T>
  120. HashSet<T>& HashSet<T>::operator=(const HashSet& s)
  121. {
  122. if (this != &s)
  123. {
  124. hashFunction = s.hashFunction;
  125. }
  126.  
  127. return *this;
  128. }
  129.  
  130.  
  131. template <typename T>
  132. bool HashSet<T>::isImplemented() const
  133. {
  134. return false;
  135. }
  136.  
  137.  
  138. template <typename T>
  139. void HashSet<T>::add(const T& element)
  140. {
  141.  
  142. }
  143.  
  144.  
  145. template <typename T>
  146. bool HashSet<T>::contains(const T& element) const
  147. {
  148. return false;
  149. }
  150.  
  151.  
  152. template <typename T>
  153. unsigned int HashSet<T>::size() const
  154. {
  155. return 0;
  156. }
  157.  
  158.  
  159.  
  160. #endif // HASHSET_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement