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 00027 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00028 00029 00030 @version Initial version, April 2004 00031 @author Kris 00032 00033 00034 *******************************************************************************/ 00035 00036 module mango.http.utils.Dictionary; 00037 00038 /****************************************************************************** 00039 00040 Houses the content of a Dictionary entry 00041 00042 ******************************************************************************/ 00043 00044 struct DElement 00045 { 00046 char[] name, 00047 value; 00048 } 00049 00050 /****************************************************************************** 00051 00052 Implements a dictionary for mapping names to values. This is 00053 really just a hashmap with support for certain domain-specific 00054 idioms. Note that there is no notion of thread-safety whatsoever; 00055 you are expected to ensure multiple threads do not contend over 00056 the content therein. In particular, iterating over the content 00057 is an unknown quantity in terms of time (from the perspective 00058 of this module) so you ought to consider that in a multi-threaded 00059 environment. 00060 00061 ******************************************************************************/ 00062 00063 class Dictionary 00064 { 00065 private char[][char[]] dictionary; 00066 00067 /********************************************************************** 00068 00069 Return the dictionary entry with the given name, or null 00070 if there is no such name. 00071 00072 **********************************************************************/ 00073 00074 char[] get (char[] name) 00075 { 00076 if (name in dictionary) 00077 return dictionary[name]; 00078 return null; 00079 } 00080 00081 /********************************************************************** 00082 00083 Perform some post population optimization. 00084 00085 **********************************************************************/ 00086 00087 void optimize () 00088 { 00089 dictionary.rehash; 00090 } 00091 00092 /********************************************************************** 00093 00094 Iterate over the entire dictionary 00095 00096 **********************************************************************/ 00097 00098 int opApply (int delegate(inout DElement element) dg) 00099 { 00100 DElement element; 00101 int result = 0; 00102 char[][] keys = dictionary.keys; 00103 00104 for (int i=0; i < keys.length; ++i) 00105 { 00106 element.name = keys[i]; 00107 element.value = dictionary[element.name]; 00108 00109 result = dg (element); 00110 if (result) 00111 break; 00112 } 00113 return result; 00114 } 00115 } 00116 00117 00118 /****************************************************************************** 00119 00120 Implements a dictionary for mapping names to values. This is 00121 really just a hashmap with support for certain domain-specific 00122 idioms. Note that there is no notion of thread-safety whatsoever; 00123 you are expected to ensure multiple threads do not contend over 00124 the content therein. In particular, iterating over the content 00125 is an unknown quantity in terms of time (from the perspective 00126 of this module) so you ought to consider that in a multi-threaded 00127 environment. 00128 00129 ******************************************************************************/ 00130 00131 class MutableDictionary : Dictionary 00132 { 00133 /********************************************************************** 00134 00135 Place a name/value pair into the dictionary. If the name 00136 already exists, the prior value is replaced. 00137 00138 **********************************************************************/ 00139 00140 void put (char[] name, char[] value) 00141 { 00142 dictionary[name] = value; 00143 } 00144 00145 /********************************************************************** 00146 00147 Delete the named entry from the dictionary 00148 00149 **********************************************************************/ 00150 00151 void remove (char[] name) 00152 { 00153 dictionary[name] = null; 00154 delete dictionary[name]; 00155 } 00156 00157 /********************************************************************** 00158 00159 Clear all dictionary entries 00160 00161 **********************************************************************/ 00162 00163 void reset () 00164 { 00165 void*[] tmp; 00166 00167 // allocate array of void* 00168 tmp.length = 0; 00169 00170 // Ben Hinkle's wizardry (reprise) 00171 dictionary = cast(char[][char[]]) tmp; 00172 } 00173 00174 /********************************************************************** 00175 00176 loader for use with Properties.load () 00177 00178 **********************************************************************/ 00179 00180 void loader (char[] name, char[] value) 00181 { 00182 put (name, value); 00183 } 00184 } 00185 00186