Dictionary.h

00001 /*
00002  * This source file is part of libRocket, the HTML/CSS Interface Middleware
00003  *
00004  * For the latest information, see http://www.librocket.com
00005  *
00006  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  *
00026  */
00027 
00028 #ifndef ROCKETCOREDICTIONARY_H
00029 #define ROCKETCOREDICTIONARY_H
00030 
00031 #include <Rocket/Core/Header.h>
00032 #include <Rocket/Core/Variant.h>
00033 
00034 namespace Rocket {
00035 namespace Core {
00036 
00044 class ROCKETCORE_API Dictionary
00045 {
00046 public:
00047         Dictionary();
00048         Dictionary(const Dictionary &dict);
00049         ~Dictionary();
00050 
00052         void Set(const String& key, const Variant &value);
00053   
00055         template <typename T>
00056         inline void Set(const String& key, const T& value);
00057         
00059         Variant* Get(const String& key) const;
00060         Variant* operator[](const String& key) const;
00061         
00064         template <typename T>
00065         inline T Get(const String& key, const T& default_val) const;
00066         
00069         template <typename T>
00070         inline bool GetInto(const String& key, T& value) const;
00071 
00073         bool Remove(const String& key);
00074 
00076         bool Iterate(int &pos, String& key, Variant* &value) const;
00077         template <typename T>
00078         bool Iterate(int &pos, String& key, T& value) const;
00079 
00081         bool Reserve(int size);
00082 
00084         void Clear();
00085 
00087         bool IsEmpty() const;
00088 
00090         int Size() const;
00091 
00093         void Merge(const Dictionary& dict);
00094 
00095         // Copy
00096         void operator=(const Dictionary &dict);
00097 
00098 private:
00099         unsigned int num_full;  // Active + # Dummy
00100         unsigned int num_used;  // Active
00101 
00102         /* DICTIONARY_MINSIZE is the minimum size of a dictionary.  This many slots are
00103          * allocated directly in the dict object (in the small_table member).
00104          * It must be a power of 2, and at least 4.  8 allows dicts with no more
00105          * than 5 active entries to live in small_table (and so avoid an
00106          * additional malloc); instrumentation suggested this suffices for the
00107          * majority of dicts (consisting mostly of usually-small instance dicts and
00108          * usually-small dicts created to pass keyword arguments).
00109          */
00110         static const int DICTIONARY_MINSIZE = 8;
00111 
00112         // Individual entry in a dictionary
00113         struct DictionaryEntry
00114         {
00115                 DictionaryEntry() : hash(0) {}
00116                 Hash hash;              // Cached hash of key
00117                 String key;             // key in plain text
00118                 Variant value;  // Value for this entry
00119         };
00120   
00121   /* The table contains mask + 1 slots, and that's a power of 2.
00122          * We store the mask instead of the size because the mask is more
00123          * frequently needed.
00124          */
00125         unsigned int mask;
00126 
00127         // Small dictionaries just use this, saves mallocs for small tables
00128         DictionaryEntry small_table[DICTIONARY_MINSIZE];
00129 
00131         DictionaryEntry* table;
00132 
00134         void Insert(const String& key, Hash hash, const Variant& value);
00135 
00137         DictionaryEntry* Retrieve(const String& key, Hash hash) const;
00138 
00140         void ResetToMinimumSize();  
00141 
00142         // Copy another dict
00143         void Copy(const Dictionary &dict);
00144 };
00145 
00146 #include <Rocket/Core/Dictionary.inl>
00147 
00148 }
00149 }
00150 
00151 #endif