Dictionary.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCOREDICTIONARY_H
  28. #define ROCKETCOREDICTIONARY_H
  29. #include "Header.h"
  30. #include "Variant.h"
  31. namespace Rocket {
  32. namespace Core {
  33. /**
  34. A dictionay is a container of variants.
  35. It uses a hash table to maintain a string key to variant mapping.
  36. @author Lloyd Weehuizen
  37. */
  38. class ROCKETCORE_API Dictionary
  39. {
  40. public:
  41. Dictionary();
  42. Dictionary(const Dictionary &dict);
  43. ~Dictionary();
  44. /// Store an item in the dictionary
  45. void Set(const String& key, const Variant &value);
  46. /// Templated set eases setting of values
  47. template <typename T>
  48. inline void Set(const String& key, const T& value);
  49. /// Get an item from the dictionary
  50. Variant* Get(const String& key) const;
  51. Variant* operator[](const String& key) const;
  52. /// Get a value from the dictionary, if it doesn't exist
  53. /// use the supplied default value
  54. template <typename T>
  55. inline T Get(const String& key, const T& default_val) const;
  56. /// Get a value from the dictionary, returns if the
  57. /// value was found or not.
  58. template <typename T>
  59. inline bool GetInto(const String& key, T& value) const;
  60. /// Remove an item from the dictionary
  61. bool Remove(const String& key);
  62. /// Iterate through a dictionary
  63. bool Iterate(int &pos, String& key, Variant* &value) const;
  64. template <typename T>
  65. bool Iterate(int &pos, String& key, T& value) const;
  66. /// Reserve the specified number of entries in the dictionary
  67. bool Reserve(int size);
  68. /// Empty the dictionary
  69. void Clear();
  70. /// Is the dictionary empty?
  71. bool IsEmpty() const;
  72. /// Items in the dict
  73. int Size() const;
  74. /// Merges another dictionary into this one. Any existing values stored against similar keys will be updated.
  75. void Merge(const Dictionary& dict);
  76. // Copy
  77. void operator=(const Dictionary &dict);
  78. private:
  79. unsigned int num_full; // Active + # Dummy
  80. unsigned int num_used; // Active
  81. /* DICTIONARY_MINSIZE is the minimum size of a dictionary. This many slots are
  82. * allocated directly in the dict object (in the small_table member).
  83. * It must be a power of 2, and at least 4. 8 allows dicts with no more
  84. * than 5 active entries to live in small_table (and so avoid an
  85. * additional malloc); instrumentation suggested this suffices for the
  86. * majority of dicts (consisting mostly of usually-small instance dicts and
  87. * usually-small dicts created to pass keyword arguments).
  88. */
  89. static const int DICTIONARY_MINSIZE = 8;
  90. // Individual entry in a dictionary
  91. struct DictionaryEntry
  92. {
  93. DictionaryEntry() : hash(0) {}
  94. Hash hash; // Cached hash of key
  95. String key; // key in plain text
  96. Variant value; // Value for this entry
  97. };
  98. /* The table contains mask + 1 slots, and that's a power of 2.
  99. * We store the mask instead of the size because the mask is more
  100. * frequently needed.
  101. */
  102. unsigned int mask;
  103. // Small dictionaries just use this, saves mallocs for small tables
  104. DictionaryEntry small_table[DICTIONARY_MINSIZE];
  105. /// Pointer to table in use, may be malloc'd or may point to smallTable
  106. DictionaryEntry* table;
  107. /// Insert an item
  108. void Insert(const String& key, Hash hash, const Variant& value);
  109. /// Retrieve an item
  110. DictionaryEntry* Retrieve(const String& key, Hash hash) const;
  111. /// Reset to small dictionary
  112. void ResetToMinimumSize();
  113. // Copy another dict
  114. void Copy(const Dictionary &dict);
  115. };
  116. #include "Dictionary.inl"
  117. }
  118. }
  119. #endif