libRocket
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Dictionary.h
Go to the documentation of this file.
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 
28 #ifndef ROCKETCOREDICTIONARY_H
29 #define ROCKETCOREDICTIONARY_H
30 
31 #include "Header.h"
32 #include "Variant.h"
33 
34 namespace Rocket {
35 namespace Core {
36 
45 {
46 public:
48  Dictionary(const Dictionary &dict);
49  ~Dictionary();
50 
52  void Set(const String& key, const Variant &value);
53 
55  template <typename T>
56  inline void Set(const String& key, const T& value);
57 
59  Variant* Get(const String& key) const;
60  Variant* operator[](const String& key) const;
61 
64  template <typename T>
65  inline T Get(const String& key, const T& default_val) const;
66 
69  template <typename T>
70  inline bool GetInto(const String& key, T& value) const;
71 
73  bool Remove(const String& key);
74 
76  bool Iterate(int &pos, String& key, Variant* &value) const;
77  template <typename T>
78  bool Iterate(int &pos, String& key, T& value) const;
79 
81  bool Reserve(int size);
82 
84  void Clear();
85 
87  bool IsEmpty() const;
88 
90  int Size() const;
91 
93  void Merge(const Dictionary& dict);
94 
95  // Copy
96  void operator=(const Dictionary &dict);
97 
98 private:
99  unsigned int num_full; // Active + # Dummy
100  unsigned int num_used; // Active
101 
102  /* DICTIONARY_MINSIZE is the minimum size of a dictionary. This many slots are
103  * allocated directly in the dict object (in the small_table member).
104  * It must be a power of 2, and at least 4. 8 allows dicts with no more
105  * than 5 active entries to live in small_table (and so avoid an
106  * additional malloc); instrumentation suggested this suffices for the
107  * majority of dicts (consisting mostly of usually-small instance dicts and
108  * usually-small dicts created to pass keyword arguments).
109  */
110  static const int DICTIONARY_MINSIZE = 8;
111 
112  // Individual entry in a dictionary
113  struct DictionaryEntry
114  {
115  DictionaryEntry() : hash(0) {}
116  Hash hash; // Cached hash of key
117  String key; // key in plain text
118  Variant value; // Value for this entry
119  };
120 
121  /* The table contains mask + 1 slots, and that's a power of 2.
122  * We store the mask instead of the size because the mask is more
123  * frequently needed.
124  */
125  unsigned int mask;
126 
127  // Small dictionaries just use this, saves mallocs for small tables
128  DictionaryEntry small_table[DICTIONARY_MINSIZE];
129 
131  DictionaryEntry* table;
132 
134  void Insert(const String& key, Hash hash, const Variant& value);
135 
137  DictionaryEntry* Retrieve(const String& key, Hash hash) const;
138 
140  void ResetToMinimumSize();
141 
142  // Copy another dict
143  void Copy(const Dictionary &dict);
144 };
145 
146 #include "Dictionary.inl"
147 
148 }
149 }
150 
151 #endif
#define ROCKETCORE_API
Definition: Header.h:73
Definition: Clipboard.h:33
Definition: Variant.h:48
StringBase< char > String
Definition: String.h:40
unsigned int Hash
Definition: Types.h:54
Definition: Dictionary.h:44
bool Iterate(int &pos, String &key, T &value) const
Definition: Dictionary.h:57