00001 /******************************************************************************* 00002 00003 @file Dictionary.d 00004 00005 Copyright (c) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, April 2004 00034 @author Kris 00035 00036 00037 *******************************************************************************/ 00038 00039 module mango.http.utils.Dictionary; 00040 00041 /****************************************************************************** 00042 00043 Houses the content of a Dictionary entry 00044 00045 ******************************************************************************/ 00046 00047 struct DElement 00048 { 00049 char[] name, 00050 value; 00051 } 00052 00053 /****************************************************************************** 00054 00055 Implements a dictionary for mapping names to values. This is 00056 really just a hashmap with support for certain domain-specific 00057 idioms. Note that there is no notion of thread-safety whatsoever; 00058 you are expected to ensure multiple threads do not contend over 00059 the content therein. In particular, iterating over the content 00060 is an unknown quantity in terms of time (from the perspective 00061 of this module) so you ought to consider that in a multi-threaded 00062 environment. 00063 00064 ******************************************************************************/ 00065 00066 class Dictionary 00067 { 00068 private char[][char[]] dictionary; 00069 00070 /********************************************************************** 00071 00072 Return the dictionary entry with the given name, or null 00073 if there is no such name. 00074 00075 **********************************************************************/ 00076 00077 char[] get (char[] name) 00078 { 00079 if (name in dictionary) 00080 return dictionary[name]; 00081 return null; 00082 } 00083 00084 /********************************************************************** 00085 00086 Perform some post population optimization. 00087 00088 **********************************************************************/ 00089 00090 void optimize () 00091 { 00092 dictionary.rehash; 00093 } 00094 00095 /********************************************************************** 00096 00097 Iterate over the entire dictionary 00098 00099 **********************************************************************/ 00100 00101 int opApply (int delegate(inout DElement element) dg) 00102 { 00103 DElement element; 00104 int result = 0; 00105 char[][] keys = dictionary.keys; 00106 00107 for (int i=0; i < keys.length; ++i) 00108 { 00109 element.name = keys[i]; 00110 element.value = dictionary[element.name]; 00111 00112 result = dg (element); 00113 if (result) 00114 break; 00115 } 00116 return result; 00117 } 00118 } 00119 00120 00121 /****************************************************************************** 00122 00123 Implements a dictionary for mapping names to values. This is 00124 really just a hashmap with support for certain domain-specific 00125 idioms. Note that there is no notion of thread-safety whatsoever; 00126 you are expected to ensure multiple threads do not contend over 00127 the content therein. In particular, iterating over the content 00128 is an unknown quantity in terms of time (from the perspective 00129 of this module) so you ought to consider that in a multi-threaded 00130 environment. 00131 00132 ******************************************************************************/ 00133 00134 class MutableDictionary : Dictionary 00135 { 00136 /********************************************************************** 00137 00138 Place a name/value pair into the dictionary. If the name 00139 already exists, the prior value is replaced. 00140 00141 **********************************************************************/ 00142 00143 void put (char[] name, char[] value) 00144 { 00145 dictionary[name] = value; 00146 } 00147 00148 /********************************************************************** 00149 00150 Delete the named entry from the dictionary 00151 00152 **********************************************************************/ 00153 00154 void remove (char[] name) 00155 { 00156 dictionary[name] = null; 00157 dictionary.remove(name); 00158 } 00159 00160 /********************************************************************** 00161 00162 Clear all dictionary entries 00163 00164 **********************************************************************/ 00165 00166 void reset () 00167 { 00168 void*[] tmp; 00169 00170 // allocate array of void* 00171 tmp.length = 0; 00172 00173 // Ben Hinkle's wizardry (reprise) 00174 dictionary = cast(char[][char[]]) tmp; 00175 } 00176 00177 /********************************************************************** 00178 00179 loader for use with Properties.load () 00180 00181 **********************************************************************/ 00182 00183 void loader (char[] name, char[] value) 00184 { 00185 put (name, value); 00186 } 00187 } 00188 00189