Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 KB | None | 0 0
  1. /***************************************************************
  2. Steven Gallagher
  3. BasicSymbolTable.hpp
  4. Program 2
  5. Provides implementation for SymbolTable.hpp.
  6. ***************************************************************/
  7.  
  8. #ifndef BASIC_SYMBOL_TABLE_H
  9. #define BASIC_SYMBOL_TABLE_H
  10.  
  11. #include <algorithm>
  12. #include <deque>
  13. #include <iostream>
  14. #include "SymbolTable.hpp"
  15.  
  16. template <typename Key, typename Value>
  17. class BasicSymbolTable : public SymbolTable<Key, Value>
  18. {
  19. protected:
  20.  
  21. struct NodePair
  22. {
  23. Key _key;
  24. Value _value;
  25.  
  26. NodePair(const Key& key = Key{}, const Value& value = Value{}) : _key{ key }, _value{ value } {}
  27. };
  28.  
  29. // The container of the <key, value> pairs
  30. std::deque<NodePair> _pairs;
  31.  
  32. // Key value comparison (less than)
  33. bool keyLessThan(const NodePair& lhs, const NodePair& rhs) const { return lhs._key < rhs._key; }
  34. bool keyLessThan(const Key& lhs, const Key& rhs) const { return lhs < rhs; }
  35.  
  36. // Equality of key values
  37. bool keyEquals(const NodePair& lhs, const NodePair& rhs) const { return lhs._key == rhs._key; }
  38. bool keyEquals(const Key& lhs, const Key& rhs) const { return lhs == rhs; }
  39.  
  40. // Equality of key values
  41. bool keyLessThanOrEquals(const NodePair& lhs, const NodePair& rhs) const
  42. {
  43. return keyEquals(lhs, rhs) || keyLessThan(lhs, rhs);
  44. }
  45. bool keyLessThanOrEquals(const Key& lhs, const Key& rhs) const
  46. {
  47. return keyEquals(lhs, rhs) || keyLessThan(lhs, rhs);
  48. }
  49.  
  50. public:
  51.  
  52. BasicSymbolTable() {};
  53. virtual ~BasicSymbolTable() { clear(); };
  54.  
  55. //insert a key,value NodePair into the Symbol Table
  56. void put(const Key& key, const Value& val = Value{}) {
  57. NodePair pairToPut(key, val);
  58. if(!empty() && key <= _pairs.back()._key) {
  59. for(auto itr = _pairs.begin(); itr != _pairs.end(); ++itr) {
  60. if(keyLessThanOrEquals(key,itr -> _key)) {
  61. //overwrite value if key exsist
  62. if(keyEquals(key,itr->_key)) itr->_value = val;
  63. else _pairs.insert(itr,pairToPut);
  64. break;
  65.  
  66. }
  67. }
  68. }
  69. else _pairs.push_back(pairToPut);
  70. }
  71.  
  72.  
  73.  
  74. // acquire the value paired with key
  75. bool get(const Key& key, Value& val = Value{}) const {
  76. if (empty()) return false;
  77. for(NodePair pair : _pairs) {
  78. if (keyLessThan(key, pair._key)) break;
  79. if(keyEquals(pair._key,key)) {
  80. val = pair._value;
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86.  
  87. // Number of key-value pairs.
  88. int size() const { return _pairs.size(); }
  89.  
  90. // Is the table empty?
  91. bool empty() const { return size()==0; }
  92.  
  93. // Removes all elements from the table
  94. void clear() { _pairs.clear(); }
  95.  
  96. // remove key (and its value) from table
  97. void remove(const Key& key) {
  98. if (!empty()) {
  99. for (auto itr = _pairs.begin(); itr != _pairs.end(); ++itr) {
  100. if (keyLessThan(key, itr->_key)) break;
  101. if (keyEquals(itr->_key, key)) {
  102. _pairs.erase(itr);
  103. break;
  104. }
  105. }
  106. }
  107. }
  108.  
  109. // Is there a value paired with key?
  110. bool contains(const Key& key) const {
  111. if(empty()) return false;
  112. for (NodePair pair : _pairs) {
  113. if (keyLessThan(key, pair._key)) break;
  114. if (keyEquals(pair._key, key)) return true;
  115. }
  116. return false;
  117. }
  118.  
  119. //return smallest key
  120. bool min(Key& key = Key{}) const {
  121. if(empty()) return false;
  122. key = _pairs.front()._key;
  123. return true;
  124. }
  125.  
  126. //return largest key
  127. bool max(Key& key = Key{}) const {
  128. if(empty()) return false;
  129. key = _pairs.back()._key;
  130. return true;
  131. }
  132.  
  133. // Largest key less than or equal to key
  134. bool floor(const Key& key, Key& floorKey) const {
  135. if(empty()) return false;
  136. for (auto itr = _pairs.end()-1; itr != _pairs.begin(); --itr) {
  137. if (keyLessThanOrEquals(itr->_key, key)) {
  138. floorKey = itr -> _key;
  139. return true;
  140. }
  141. }
  142. //compare key against first NodePair in _pairs
  143. if (keyLessThanOrEquals(_pairs.front()._key, key)) {
  144. floorKey = _pairs.front()._key;
  145. return true;
  146. }
  147. //if there is no key less than or equal to the key
  148. return false;
  149. }
  150.  
  151. // Smallest key greater than or equal to key
  152. bool ceiling(const Key& key, Key& ceilingKey) const {
  153. if (empty()) return false;
  154. for (NodePair pair : _pairs) {
  155. if (keyLessThanOrEquals(key,pair._key)) {
  156. ceilingKey = pair._key;
  157. return true;
  158. }
  159. }
  160. //if there is no key greater than or equal to the key
  161. return false;
  162. }
  163.  
  164. // Number of keys less than key
  165. int rank(const Key& key) const {
  166. int count = 0;
  167. for (NodePair pair : _pairs) {
  168. if (keyLessThan(pair._key, key)) count++;
  169. else break;
  170. }
  171. return count;
  172. }
  173.  
  174. // Delete the smallest key
  175. bool deleteMin() {
  176. if(empty()) return false;
  177. remove(_pairs.front()._key);
  178. return true;
  179. }
  180.  
  181. // Delete the largest key
  182. bool deleteMax() {
  183. if(empty()) return false;
  184. remove(_pairs.back()._key);
  185. return true;
  186. }
  187.  
  188. // all keys in the table, in sorted order
  189. std::vector<Key> keys() const {
  190. std::vector<Key> result;
  191. for(NodePair pair : _pairs)
  192. result.push_back(pair._key);
  193. return result;
  194. }
  195.  
  196. // number of keys in [low, high] (including low, high)
  197. int size(const Key& low, const Key& high) const {
  198. int count = 0;
  199. for (NodePair pair : _pairs) {
  200. if (keyLessThanOrEquals(pair._key, high) && keyLessThanOrEquals(low, pair._key))
  201. count++;
  202. else if (keyLessThan(high, pair._key)) break;
  203. }
  204. return count;
  205. }
  206.  
  207. //keys in [low, high] (including low, high), in sorted order
  208. std::vector<Key> keys(const Key& low, const Key& high) const {
  209. std::vector<Key> result;
  210. for (NodePair pair : _pairs) {
  211. if (keyLessThanOrEquals(pair._key, high) && keyLessThanOrEquals(low, pair._key))
  212. result.push_back(pair._key);
  213. else if (keyLessThan(high, pair._key)) break;
  214. }
  215. return result;
  216. }
  217.  
  218. //key of rank k
  219. bool select(int k = 0, Key& key = Key{}) const {
  220. if(empty()) return false;
  221. key = _pairs.at(k)._key;
  222. return true;
  223. }
  224.  
  225. //
  226. ///////////////////////////////////////////////////////////////////////////////
  227. // Check integrity of the vector data structure.
  228. ///////////////////////////////////////////////////////////////////////////////
  229. //
  230. bool check() const
  231. {
  232. bool ordered = isOrdered();
  233. bool rankConsistent = isRankConsistent();
  234.  
  235. if (!ordered) std::cout << "Not in symmetric order" << std::endl;
  236. if (!rankConsistent) std::cout << "Ranks inconsistent" << std::endl;
  237.  
  238. return ordered && rankConsistent;
  239. }
  240.  
  241. private:
  242. //
  243. // does this container satisfy symmetric order?
  244. //
  245. bool isOrdered() const
  246. {
  247. if (size() <= 1) return true;
  248.  
  249. for (unsigned index = 0; index < _pairs.size() - 1; index++)
  250. {
  251. if (keyLessThan(_pairs[index + 1], _pairs[index])) return false;
  252. }
  253. return true;
  254. }
  255.  
  256. // check that ranks are consistent
  257. bool isRankConsistent() const
  258. {
  259. // The i th node should be rank i
  260. for (int i = 0; i < size(); i++)
  261. {
  262. Key key;
  263. select(i, key);
  264. if (i != rank(key)) return false;
  265. }
  266. // All keys must equate to the key acquired at its rank
  267. for (Key key : keys())
  268. {
  269. Key acquired;
  270. select(rank(key), acquired);
  271.  
  272. if (!keyEquals(key, acquired)) return false;
  273. }
  274.  
  275. return true;
  276.  
  277. }
  278.  
  279. };
  280.  
  281. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement