Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.66 KB | None | 0 0
  1. //HashTable.h
  2. #ifndef HASHTABLE_H
  3. #define HASHTABLE_H
  4. #include "../../List/include/List.h"
  5.  
  6. /**
  7. * Implementation of a Hashtable based on bucket lists made with Linkedlist
  8. */
  9.  
  10. template<typename K, typename V> class HashList;
  11.  
  12. template<typename K, typename V>
  13. class HashPair
  14. {
  15. private:
  16.  
  17. friend class HashList<K,V>;
  18. K key;
  19. V value;
  20.  
  21. public:
  22.  
  23. HashPair();
  24. // Default constructor
  25.  
  26. HashPair(const K key,const V value);
  27. // constructs a new hash pair given a key and a value
  28.  
  29. K getKey() const;
  30. // returns the key
  31.  
  32. void setKey(const K key);
  33. // sets the key
  34.  
  35. V getValue() const;
  36. // returns the value
  37.  
  38. void setValue(const V v);
  39. // sets the value
  40.  
  41. };
  42.  
  43. template<typename K, typename V>
  44. class HashList
  45. {
  46. private:
  47.  
  48. List<HashPair<K,V>> l;
  49.  
  50. public:
  51.  
  52. List_iterator<HashPair<K,V>> find(const K key) const;
  53. // Returns an HashPair given a key if present, null if absent
  54.  
  55. void insert(const K key,const V value) const;
  56. // Inserts a key-value pair in the HashList
  57.  
  58. V lookup(const K key) const;
  59. // Returns a reference to an HashPair value given a key if present; null otherwise
  60.  
  61. void remove(const K key) const;
  62. // Removes an element given a key
  63.  
  64. bool empty() const;
  65. // Returns true if the list is empty, false otherwise
  66.  
  67. List_iterator<HashPair<K,V>> begin() const;
  68.  
  69. List_iterator<HashPair<K,V>> end() const;
  70.  
  71. bool finished(List_iterator<HashPair<K,V>> const p) const;
  72.  
  73. };
  74.  
  75. template<typename K, typename V>
  76. class HashTable;
  77.  
  78. template<typename K, typename V>
  79. class hash_iterator;
  80.  
  81. template<typename K, typename V>
  82. bool operator ==(const hash_iterator<K,V> it, const hash_iterator<K,V> it2);
  83.  
  84. template<typename K, typename V>
  85. bool operator !=(const hash_iterator<K,V> it, const hash_iterator<K,V> it2);
  86.  
  87. template<typename K, typename V>
  88. class hash_iterator
  89. {
  90. private:
  91.  
  92. HashTable<K,V>* baseTable;
  93. int i;
  94. List_iterator<HashPair<K,V>> it;
  95. List_iterator<HashPair<K,V>> nextOccurrence();
  96.  
  97. public:
  98.  
  99. hash_iterator();
  100.  
  101. hash_iterator(HashTable<K,V>* table);
  102.  
  103. hash_iterator(const hash_iterator& it2);
  104.  
  105. friend bool operator == <>(const hash_iterator it, const hash_iterator it2);
  106.  
  107. friend bool operator != <>(const hash_iterator it, const hash_iterator it2);
  108.  
  109. hash_iterator begin();
  110.  
  111. hash_iterator end();
  112.  
  113. hash_iterator operator ++(); //prefix
  114.  
  115. hash_iterator operator ++( int ); //postfix
  116.  
  117. HashPair<K,V> operator *() const;
  118. };
  119.  
  120. template<typename K, typename V>
  121. class HashTable
  122. {
  123. protected:
  124.  
  125. HashList<K,V>* entries;
  126. int m; //table dimension
  127. friend class hash_iterator<K,V>;
  128.  
  129. public:
  130.  
  131. HashTable(const int capacity);
  132. //Creates a new hash table with given dimension
  133.  
  134. ~HashTable();
  135. //Destructor
  136.  
  137. bool contains(const K k) const;
  138. //Returns true if the hashtable contains k
  139.  
  140. V lookup(const K k) const;
  141. //returns the value being searched if present, nil otherwise
  142.  
  143. V operator [](const K k) const;
  144. // same as lookup, with a array like notation
  145.  
  146. void insert(const K key,const V value) const;
  147. //Inserts the key-value pair into the table
  148.  
  149. void remove(const K key) const;
  150. //Given a key, it removes the key-pair value, if present
  151.  
  152. int Hash(const long int key) const;
  153. //Hash function
  154.  
  155. hash_iterator<K,V> begin();
  156.  
  157. hash_iterator<K,V> end();
  158. };
  159.  
  160. namespace keyOnly
  161. {
  162.  
  163. template<typename K>
  164. class HashList
  165. {
  166. private:
  167.  
  168. List<K> l;
  169.  
  170. public:
  171.  
  172. List_iterator<K> find(const K key) const;
  173. // Returns an HashPair given a key if present, null if absent
  174.  
  175. void insert(const K key) const;
  176. // Inserts a key-value pair in the HashList
  177.  
  178. void remove(const K key) const;
  179. // Removes an element given a key
  180.  
  181. bool empty() const;
  182. // Returns true if the list is empty, false otherwise
  183.  
  184. List_iterator<K> begin() const;
  185.  
  186. List_iterator<K> end() const;
  187.  
  188. bool finished(List_iterator<K> const p) const;
  189.  
  190. };
  191.  
  192. template<typename K>
  193. class HashTable;
  194.  
  195. template<typename K>
  196. class hash_iterator;
  197.  
  198. template<typename K>
  199. bool operator ==(const hash_iterator<K> it, const hash_iterator<K> it2);
  200.  
  201. template<typename K>
  202. bool operator !=(const hash_iterator<K> it, const hash_iterator<K> it2);
  203.  
  204. template<typename K>
  205. class hash_iterator
  206. {
  207. protected:
  208.  
  209. HashTable<K>* baseTable;
  210. int i;
  211. List_iterator<K> it;
  212. List_iterator<K> nextOccurrence();
  213.  
  214. public:
  215.  
  216. hash_iterator();
  217.  
  218. hash_iterator(HashTable<K>* table);
  219.  
  220. hash_iterator(const hash_iterator& it2);
  221.  
  222. friend bool operator == <>(const hash_iterator it, const hash_iterator it2);
  223.  
  224. friend bool operator != <>(const hash_iterator it, const hash_iterator it2);
  225.  
  226. hash_iterator begin();
  227.  
  228. hash_iterator end() const;
  229.  
  230. hash_iterator operator ++(); //prefix
  231.  
  232. hash_iterator operator ++( int ); //postfix
  233.  
  234. K operator *() const;
  235. };
  236.  
  237. template<typename K>
  238. class HashTable
  239. {
  240. protected:
  241.  
  242. HashList<K>* entries;
  243. int m; //table dimension
  244. friend class hash_iterator<K>;
  245.  
  246. public:
  247.  
  248. HashTable();
  249. //Default constructor
  250.  
  251. HashTable(const int capacity);
  252. //Creates a new hash table with given dimension
  253.  
  254. ~HashTable();
  255. //Destructor
  256.  
  257. bool contains(const K k) const;
  258. // Returns true if the table contains k
  259.  
  260. void insert(const K key) const;
  261. //Inserts the key-value pair into the table
  262.  
  263. void remove(const K key) const;
  264. //Given a key, it removes the key-pair value, if present
  265.  
  266. int Hash(const long int key) const;
  267. //Hash function
  268. };
  269.  
  270. }
  271.  
  272. #include "../src/HashTable.cpp"
  273. #endif
  274.  
  275. // HashTable.cpp
  276. #ifndef HASHTABLE_CPP
  277. #define HASHTABLE_CPP
  278. #include "../include/HashTable.h"
  279. #include <cmath>
  280.  
  281. using namespace std;
  282.  
  283. template<typename K, typename V>
  284. HashPair<K,V>::HashPair()
  285. {
  286. key = K();
  287. value = V();
  288. }
  289.  
  290. template<typename K, typename V>
  291. HashPair<K,V>::HashPair(const K key,const V value):key(key), value(value)
  292. {
  293. }
  294.  
  295. template<typename K, typename V>
  296. K HashPair<K,V>::getKey() const
  297. {
  298. return key;
  299. }
  300. // returns the key
  301.  
  302. template<typename K, typename V>
  303. void HashPair<K,V>::setKey(const K key)
  304. {
  305. this->key = key;
  306. }
  307. // sets the key
  308.  
  309. template<typename K, typename V>
  310. V HashPair<K,V>::getValue() const
  311. {
  312. return value;
  313. }
  314. // returns the value
  315.  
  316. template<typename K, typename V>
  317. void HashPair<K,V>::setValue(const V v)
  318. {
  319. this->value = value;
  320. }
  321. // sets the value
  322.  
  323. template<typename K, typename V>
  324. List_iterator<HashPair<K,V>> HashList<K,V>::find(const K key) const
  325. {
  326. bool found = false;
  327. List_iterator<HashPair<K,V>> e(nullptr);
  328. List_iterator<HashPair<K,V>> i = l.begin();
  329.  
  330. while(!l.finished(i) && !found)
  331. {
  332. if((*i).key == key)
  333. {
  334. e = i;
  335. found = true;
  336. }
  337.  
  338. i++;
  339. }
  340.  
  341. return e;
  342. }
  343. // Returns an HashPair given a key if present, null if absent
  344.  
  345. template<typename K, typename V>
  346. void HashList<K,V>::insert(const K key,const V value) const
  347. {
  348. List_iterator<HashPair<K,V>> kv = find(key);
  349. HashPair<K,V> k(key,value);
  350.  
  351. if (kv != List_iterator<HashPair<K,V>>(nullptr))
  352. {
  353. l.write(kv,k);
  354. }
  355. else
  356. {
  357. l.insert(k);
  358. }
  359. }
  360. // Inserts a key-value pair in the HashList
  361.  
  362. template<typename K, typename V>
  363. V HashList<K,V>::lookup(const K key) const
  364. {
  365. List_iterator<HashPair<K,V>> kv = find(key);
  366. V e = V();
  367.  
  368. if (kv != List_iterator<HashPair<K,V>>(nullptr))
  369. {
  370. e = (*kv).value;
  371. }
  372.  
  373. return e;
  374. }
  375. // Returns a reference to an HashPair value given a key if present; null otherwise
  376.  
  377. template<typename K, typename V>
  378. void HashList<K,V>::remove(const K key) const
  379. {
  380. List_iterator<HashPair<K,V>> item = find(key);
  381. if(item != List_iterator<HashPair<K,V>>(nullptr))
  382. l.remove(item);
  383. }
  384.  
  385. template<typename K, typename V>
  386. bool HashList<K,V>::empty() const
  387. {
  388. return l.empty();
  389. }
  390.  
  391. template<typename K, typename V>
  392. List_iterator<HashPair<K,V>> HashList<K,V>::begin() const
  393. {
  394. return l.begin();
  395. }
  396.  
  397. template<typename K, typename V>
  398. List_iterator<HashPair<K,V>> HashList<K,V>::end() const
  399. {
  400. return l.end();
  401. }
  402.  
  403. template<typename K, typename V>
  404. bool HashList<K,V>::finished(List_iterator<HashPair<K,V>> const p) const
  405. {
  406. return l.finished(p);
  407. }
  408.  
  409. template<typename K, typename V>
  410. List_iterator<HashPair<K,V>> hash_iterator<K,V>::nextOccurrence()
  411. {
  412. i++;
  413.  
  414. it = List_iterator<HashPair<K,V>>(nullptr);
  415.  
  416. while(i < baseTable->m && it == List_iterator<HashPair<K,V>>(nullptr))
  417. {
  418. if(baseTable->entries[i].empty())
  419. i++;
  420. else
  421. it = baseTable->entries[i].begin();
  422. }
  423.  
  424. return it;
  425. }
  426.  
  427. template<typename K, typename V>
  428. hash_iterator<K,V>::hash_iterator()
  429. {
  430. baseTable = nullptr;
  431. i = -1;
  432. it = List_iterator<HashPair<K,V>>(nullptr);
  433. }
  434.  
  435. template<typename K, typename V>
  436. hash_iterator<K,V>::hash_iterator(HashTable<K,V>* table)
  437. {
  438. baseTable = table;
  439. i = -1;
  440. it = List_iterator<HashPair<K,V>>(nullptr);
  441. }
  442.  
  443. template<typename K, typename V>
  444. hash_iterator<K,V>::hash_iterator(const hash_iterator& it2)
  445. {
  446. baseTable = it2.baseTable;
  447. i = it2.i;
  448. it = it2.it;
  449. }
  450.  
  451. template<typename K, typename V>
  452. bool operator ==(const hash_iterator<K,V> it, const hash_iterator<K,V> it2)
  453. {
  454. return (it.baseTable == it2.baseTable && it.it == it2.it);
  455. }
  456.  
  457. template<typename K, typename V>
  458. bool operator !=(const hash_iterator<K,V> it, const hash_iterator<K,V> it2)
  459. {
  460. return !(it == it2);
  461. }
  462.  
  463. template<typename K, typename V>
  464. hash_iterator<K,V> hash_iterator<K,V>::begin()
  465. {
  466. if(i != 0)
  467. i = -1;
  468.  
  469. hash_iterator<K,V> ret(*this);
  470.  
  471. ret.nextOccurrence();
  472.  
  473. return ret;
  474. }
  475.  
  476. template<typename K, typename V>
  477. hash_iterator<K,V> hash_iterator<K,V>::end()
  478. {
  479. hash_iterator<K,V> ret(*this);
  480. ret.i = baseTable->m;
  481. ret.it = List_iterator<HashPair<K,V>>(nullptr);
  482. return ret;
  483. }
  484.  
  485. template<typename K, typename V>
  486. hash_iterator<K,V> hash_iterator<K,V>::operator ++() //prefix
  487. {
  488. it++;
  489.  
  490. if (baseTable->entries[i].finished(it))
  491. {
  492. it = nextOccurrence();
  493. }
  494.  
  495. return *this;
  496. }
  497.  
  498. template<typename K, typename V>
  499. hash_iterator<K,V> hash_iterator<K,V>::operator ++( int ) //postfix
  500. {
  501. hash_iterator<K,V> oldit(*this);
  502.  
  503. ++(*this);
  504.  
  505. return oldit;
  506. }
  507.  
  508. template<typename K, typename V>
  509. HashPair<K,V> hash_iterator<K,V>::operator *() const
  510. {
  511. return *it;
  512. }
  513.  
  514. template<typename K, typename V>
  515. HashTable<K,V>::HashTable(const int capacity)
  516. {
  517. entries = new HashList<K,V> [capacity];
  518. m = capacity;
  519. }
  520. //Creates a new hash table with given dimension
  521.  
  522. template<typename K, typename V>
  523. HashTable<K,V>::~HashTable()
  524. {
  525. delete [] entries;
  526. }
  527. //Destructor
  528.  
  529. template<typename K, typename V>
  530. V HashTable<K,V>::lookup(const K k) const
  531. {
  532. int i = Hash(hash<K>()(k));
  533. V value = V();
  534.  
  535. if (!entries[i].empty())
  536. value = entries[i].lookup(k);
  537.  
  538. return value;
  539. }
  540. //returns the value being searched if present, nil otherwise
  541.  
  542. template<typename K,typename V>
  543. bool HashTable<K,V>::contains(const K k) const
  544. {
  545. int i = Hash(hash<K>()(k));
  546. if(entries[i].empty())
  547. return false;
  548. else
  549. {
  550. if(entries[i].find(k) == List_iterator<HashPair<K,V>>(nullptr))
  551. return false;
  552. else
  553. return true;
  554. }
  555. }
  556. //
  557.  
  558. template<typename K,typename V>
  559. V HashTable<K,V>::operator [](const K k) const
  560. {
  561. return lookup(k);
  562. }
  563.  
  564. template<typename K, typename V>
  565. void HashTable<K,V>::insert(const K key,const V value) const
  566. {
  567. int i = Hash(hash<K>()(key));
  568.  
  569. entries[i].insert(key,value);
  570. }
  571. //Inserts the key-value pair into the table
  572.  
  573. template<typename K, typename V>
  574. void HashTable<K,V>::remove(const K key) const
  575. {
  576. int k = Hash(hash<K>()(key));
  577.  
  578. if (!entries[k].empty())
  579. entries[k].remove(key);
  580.  
  581. }
  582. //Given a key, it removes the key-pair value, if present
  583.  
  584. template<typename K, typename V>
  585. int HashTable<K,V>::Hash(const long int key) const
  586. {
  587. return abs(key) % m;
  588. }
  589. //Hash function
  590.  
  591. template<typename K, typename V>
  592. hash_iterator<K,V> HashTable<K,V>::begin()
  593. {
  594. hash_iterator<K,V> ret(this);
  595.  
  596. return ret.begin();
  597. }
  598.  
  599. template<typename K, typename V>
  600. hash_iterator<K,V> HashTable<K,V>::end()
  601. {
  602. hash_iterator<K,V> ret(this);
  603.  
  604. return ret.end();
  605. }
  606.  
  607. namespace keyOnly
  608. {
  609.  
  610. template<typename K>
  611. List_iterator<K> HashList<K>::find(const K key) const
  612. {
  613. bool found = false;
  614. List_iterator<K> e = List_iterator<K>(nullptr);
  615.  
  616. if(!l.empty())
  617. {
  618. List_iterator<K> i = l.begin();
  619.  
  620. while(!l.finished(i) && !found)
  621. {
  622. if(*i == key)
  623. {
  624. e = i;
  625. found = true;
  626. }
  627.  
  628. i++;
  629. }
  630. }
  631.  
  632. return e;
  633. }
  634. // Returns an HashPair given a key if present, null if absent
  635.  
  636. template<typename K>
  637. void HashList<K>::insert(const K key) const
  638. {
  639. List_iterator<K> k = find(key);
  640.  
  641. if (k == List_iterator<K>(nullptr))
  642. {
  643. l.insert(key);
  644. }
  645. else
  646. {
  647. l.write(k,key);
  648. }
  649.  
  650. }
  651. // Inserts a key-value pair in the HashList
  652.  
  653. /*
  654. template<typename K>
  655. K HashList<K>::lookup(K key)
  656. {
  657. List_iterator<K> k = find(key);
  658. K e;
  659.  
  660. if (k != List_iterator<K>(nullptr))
  661. e = *k;
  662.  
  663. return e;
  664. }
  665. // Returns a reference to an HashPair value given a key if present; null otherwise
  666. */
  667. template<typename K>
  668. void HashList<K>::remove(const K key) const
  669. {
  670. List_iterator<K> item = find(key);
  671. if(item != List_iterator<K>(nullptr))
  672. l.remove(item);
  673. }
  674.  
  675. template<typename K>
  676. bool HashList<K>::empty() const
  677. {
  678. return l.empty();
  679. }
  680.  
  681. template<typename K>
  682. List_iterator<K> HashList<K>::begin() const
  683. {
  684. return l.begin();
  685. }
  686.  
  687. template<typename K>
  688. List_iterator<K> HashList<K>::end() const
  689. {
  690. return l.end();
  691. }
  692.  
  693. template<typename K>
  694. bool HashList<K>::finished(const List_iterator<K> p) const
  695. {
  696. return l.finished(p);
  697. }
  698.  
  699. template<typename K>
  700. List_iterator<K> hash_iterator<K>::nextOccurrence()
  701. {
  702. i++;
  703.  
  704. it = List_iterator<K>(nullptr);
  705.  
  706. while(i < baseTable->m && it == List_iterator<K>(nullptr))
  707. {
  708. if(baseTable->entries[i].empty())
  709. i++;
  710. else
  711. it = baseTable->entries[i].begin();
  712. }
  713.  
  714. return it;
  715. }
  716.  
  717. template<typename K>
  718. hash_iterator<K>::hash_iterator()
  719. {
  720. baseTable = nullptr;
  721. i = -1;
  722. it = List_iterator<K>(nullptr);
  723. }
  724.  
  725. template<typename K>
  726. hash_iterator<K>::hash_iterator(HashTable<K>* table)
  727. {
  728. baseTable = table;
  729. i = -1;
  730. it = List_iterator<K>(nullptr);
  731. }
  732.  
  733. template<typename K>
  734. hash_iterator<K>::hash_iterator(const hash_iterator& it2)
  735. {
  736. baseTable = it2.baseTable;
  737. i = it2.i;
  738. it = it2.it;
  739. }
  740.  
  741. template<typename K>
  742. bool operator ==(const hash_iterator<K> it, const hash_iterator<K> it2)
  743. {
  744. return (it.baseTable == it2.baseTable && it.it == it2.it);
  745. }
  746.  
  747. template<typename K>
  748. bool operator !=(const hash_iterator<K> it, const hash_iterator<K> it2)
  749. {
  750. return !(it == it2);
  751. }
  752.  
  753. template<typename K>
  754. hash_iterator<K> hash_iterator<K>::begin()
  755. {
  756. if(i != 0)
  757. i = -1;
  758.  
  759. hash_iterator<K> ret(*this);
  760.  
  761. ret.nextOccurrence();
  762.  
  763. return ret;
  764. }
  765.  
  766. template<typename K>
  767. hash_iterator<K> hash_iterator<K>::end() const
  768. {
  769. hash_iterator<K> ret(*this);
  770. ret.i = baseTable->m;
  771. ret.it = List_iterator<K>(nullptr);
  772. return ret;
  773. }
  774.  
  775. template<typename K>
  776. hash_iterator<K> hash_iterator<K>::operator ++() //prefix
  777. {
  778. it++;
  779.  
  780. if (baseTable->entries[i].finished(it))
  781. {
  782. it = nextOccurrence();
  783. }
  784.  
  785. return *this;
  786. }
  787.  
  788. template<typename K>
  789. hash_iterator<K> hash_iterator<K>::operator ++( int ) //postfix
  790. {
  791. hash_iterator<K> oldit(*this);
  792.  
  793. ++(*this);
  794.  
  795. return oldit;
  796. }
  797.  
  798. template<typename K>
  799. K hash_iterator<K>::operator *() const
  800. {
  801.  
  802. return *it;
  803. }
  804.  
  805. template<typename K>
  806. HashTable<K>::HashTable(const int capacity)
  807. {
  808. entries = new HashList<K> [capacity];
  809. m = capacity;
  810. }
  811. //Creates a new hash table with given dimension
  812.  
  813. template<typename K>
  814. HashTable<K>::~HashTable()
  815. {
  816. delete [] entries;
  817. }
  818. //Destructor
  819.  
  820. template<typename K>
  821. HashTable<K>::HashTable()
  822. {
  823. entries = nullptr;
  824. m = -1;
  825. }
  826.  
  827. /*
  828. template<typename K>
  829. K HashTable<K>::lookup(K k)
  830. {
  831. K key = K();
  832. int i = Hash(hash<K>()(k));
  833.  
  834. if (!entries[i].empty())
  835. key = entries[i].lookup(k);
  836.  
  837. return key;
  838. }
  839. //returns the value being searched if present, nil otherwise
  840. */
  841.  
  842. template<typename K>
  843. bool HashTable<K>::contains(const K k) const
  844. {
  845. int i = Hash(hash<K>()(k));
  846. if(entries[i].empty())
  847. return false;
  848. else
  849. {
  850. if(entries[i].find(k) == List_iterator<K>(nullptr))
  851. return false;
  852. else
  853. return true;
  854. }
  855. }
  856.  
  857. template<typename K>
  858. void HashTable<K>::insert(const K key) const
  859. {
  860. int i = Hash(hash<K>()(key));
  861.  
  862. entries[i].insert(key);
  863. }
  864. //Inserts the key-value pair into the table
  865.  
  866. template<typename K>
  867. void HashTable<K>::remove(const K key) const
  868. {
  869. int k = Hash(hash<K>()(key));
  870.  
  871. if (!entries[k].empty())
  872. entries[k].remove(key);
  873.  
  874. }
  875. //Given a key, it removes the key-pair value, if present
  876.  
  877. template<typename K>
  878. int HashTable<K>::Hash(const long int key) const
  879. {
  880. return abs(key) % m;
  881. }
  882. //Hash function
  883. }
  884.  
  885. #endif
  886.  
  887. HashPair => std::pair
  888. List => std::list
  889. HashTable => std::unordered_map
  890.  
  891. template<typename K, typename V>
  892. HashPair<K,V>::HashPair(const K key,const V value)
  893. ^^^^^^ ^^^^^^^ Copy to parameters
  894. : key(key) // Copy from parameter to key
  895. , value(value) // Copy from parameter to value
  896. {}
  897.  
  898. template<typename K, typename V>
  899. HashPair<K,V>::HashPair(K const& key, V const& value)
  900. : key(key) // Copy from parameter to key
  901. , value(value) // Copy from parameter to value
  902. {}
  903.  
  904. template<typename K, typename V>
  905. HashPair<K,V>::HashPair(K&& key, V&& value)
  906. : key(std::move(key))
  907. , value(std::move(value))
  908. {}
  909.  
  910. template<typename K, typename... Args>
  911. HashPair<K,V>::HashPair(K const& key, Args&&... value)
  912. : key(key)
  913. , value(std::forward<Args>(value)...)
  914. {}
  915.  
  916. template<typename K, typename V>
  917. V HashPair<K,V>::getValue() const
  918. {
  919. return value; // here you are returning by value and thus
  920. // causing a copy.
  921. }
  922.  
  923. template<typename K, typename V>
  924. V const& HashPair<K,V>::getValue() const
  925. {
  926. return value; // here you are returning by "const" reference and thus
  927. // thus allowing users to read the value.
  928. }
  929.  
  930. List_iterator<HashPair<K,V>> begin() const;
  931. List_iterator<HashPair<K,V>> end() const;
  932.  
  933. Using ValueType = std::pair<K, V>;
  934. Using Container = std::list<ValueType>;
  935. Using iterator = typename Container::iterator;
  936. Using const_iterator = typename Container::const_iterator;
  937.  
  938. Container list;
  939. const_iterator cbegin() const;
  940. const_iterator cend() const;
  941. const_iterator begin() const;
  942. const_iterator end() const;
  943. iterator begin();
  944. iterator end();
  945.  
  946. bool finished(List_iterator<HashPair<K,V>> const p) const;
  947.  
  948. for(auto const& val: container)
  949. {
  950. std::cout << value << "n";
  951. }
  952.  
  953. for(auto loop = std::begin(container); loop != std::end(container); ++loop)
  954. {
  955. auto const& value = *loop;
  956. std::cout << value << "n";
  957. }
  958.  
  959. while(!l.finished(i) && !found)
  960. {
  961. if((*i).key == key)
  962. {
  963. e = i;
  964. found = true;
  965. }
  966.  
  967. i++;
  968. }
  969.  
  970. while(!l.finished(i))
  971. {
  972. if((*i).key == key)
  973. {
  974. e = i;
  975. break; // Note the break here.
  976. }
  977. i++;
  978. }
  979.  
  980. if((*i).key == key)
  981.  
  982. if (i->key == key)
  983.  
  984. i++;
  985.  
  986. HashList<K,V>* entries;
  987.  
  988. {
  989. HashTable<int, int> x;
  990. HashTable<int, int> y(x); // Works even though you
  991. // Did not define a copy constructor
  992. }
  993. // Problem is here. You get a double delete
  994. // As both the destructrs try and destroies `entries` which points
  995. // at the same thing.
  996.  
  997. HashList<K,V>* entries;
  998.  
  999. HashList<K>* entries;
  1000.  
  1001. List<HashPair<K,V>> l;
  1002.  
  1003. List<K> l;
  1004.  
  1005. // Here is the template we are going to specialize.
  1006. template<typename K, typename V>
  1007. struct HashTableType
  1008. {
  1009. using ValueType = HashPair<K,V>;
  1010. };
  1011. // Here is the specialization when the value type is void.
  1012. template<typename K>
  1013. struct HashTableType<K, void>
  1014. {
  1015. using ValueType = K;
  1016. };
  1017.  
  1018. // Now in our main class we can use the above two classes to get
  1019. // the correct types.
  1020. template<typename K, typename V>
  1021. class HashTable
  1022. {
  1023. using ValueType = typename HashTableType<K, V>::ValueType;
  1024. using ListType = typename HashList<ValueType>;
  1025.  
  1026. ListType entries;
  1027. };
  1028. template<typename K>
  1029. using HashSet = HashTable<K, void>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement