Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////////////////////////
  2. // File: maphash.hpp
  3. // Language: C++
  4. // Version: 1
  5. // Author: culb ( nightfrog )
  6. // Contact: the01culb@gmail.com
  7. //
  8. // Purpose: This "library" supplies class template that is similiar to the std::map container
  9. //
  10. // ToDo: Create a vector like class template so there isn't a dependency on std::vector
  11. // Basically just remove the reliance on the STL ( Except for types )
  12. //
  13. // License: If this code is used in any way
  14. // then be courteous and include all of my information and notes
  15. /////////////////////////////////////////////////////////////////////////////////////////////
  16.  
  17. #ifndef MAPHASH_HPP
  18. #define MAPHASH_HPP
  19.  
  20. #include <vector>
  21.  
  22. template <class K, class T>
  23. class maphash
  24. {
  25. public:
  26. /// Return the number of key, value pairs
  27. std::uint32_t item_size() const
  28. {
  29. return hash.size();
  30. }
  31.  
  32. /// Check if a key, value pair exists
  33. /// boolean so it can be checked for success or failure
  34. bool item_exists( const K key )
  35. {
  36. for( std::size_t i = 0; i < item_size(); ++i )
  37. {
  38. if( hash[i].key == key )
  39. return true;
  40. }
  41. return false;
  42. }
  43.  
  44. /// Add a key, value pair
  45. /// boolean so it can be checked for success or failure
  46. bool item_add( const K key, const T value )
  47. {
  48. if( item_exists( key ) )
  49. return false;
  50. kvStruct d;
  51. d.key = key;
  52. d.value = value;
  53. hash.push_back( d );
  54. return true;
  55. }
  56.  
  57. /// Delete a key, value pair
  58. /// boolean so it can be checked for success or failure
  59. bool item_delete( const K key ) const
  60. {
  61. for( std::size_t i = 0; i < item_size(); ++i )
  62. {
  63. if( hash[i].key == key )
  64. {
  65. hash.erase( hash.begin() + i );
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71.  
  72. /// Add a key, value pair with a bracket operator
  73. T& operator [] ( const K key )
  74. {
  75. for( std::size_t i = 0; i < item_size(); ++i )
  76. {
  77. if( hash[i].key == key )
  78. return hash[i].value;
  79. }
  80. // Declare and define this before the struct object or there is a segfault
  81. // Visual Studio 2015, "Expression: vector subscript out of range"
  82. // Rasbian 4.4.26-v7+ - "clang 3.5.0-10+rpi1" and GCC "Raspbian 4.9.2-10", malloc error
  83. // FreeBSD and other Linux distros are fine
  84. const std::uint32_t sizeOfItem = item_size();
  85. kvStruct d;
  86. d.key = key;
  87. hash.push_back(d);
  88. return hash[sizeOfItem].value;
  89. }
  90.  
  91. /// FIXME, Needs a return type if the key isn't found.
  92. /// A function to get the value name of a key that's past in.
  93. T& item_value( const K key )
  94. {
  95. for( std::size_t i = 0; i < item_size(); ++i )
  96. {
  97. if( hash[i].key == key )
  98. return hash[i].value;
  99. }
  100. // There needs to be a return here.
  101. }
  102.  
  103. private:
  104. struct kvStruct
  105. {
  106. T value;
  107. K key;
  108. };
  109. std::vector<kvStruct> hash;
  110. };
  111. #endif // MAPHASH_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement