Mercurial > projects > dynamin
annotate dynamin/core/global.d @ 2:fab13ec910ba
Speed up toRomanNumerals() by removing heap allocations. Much faster for Tango's debug build.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Mon, 15 Jun 2009 22:17:26 -0500 |
parents | aa4efef0f0b1 |
children | d806edad4300 |
rev | line source |
---|---|
0 | 1 // Written in the D programming language |
2 // www.digitalmars.com/d/ | |
3 | |
4 /* | |
5 * The contents of this file are subject to the Mozilla Public License Version | |
6 * 1.1 (the "License"); you may not use this file except in compliance with | |
7 * the License. You may obtain a copy of the License at | |
8 * http://www.mozilla.org/MPL/ | |
9 * | |
10 * Software distributed under the License is distributed on an "AS IS" basis, | |
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 * for the specific language governing rights and limitations under the | |
13 * License. | |
14 * | |
15 * The Original Code is the Dynamin library. | |
16 * | |
17 * The Initial Developer of the Original Code is Jordan Miner. | |
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009 | |
19 * the Initial Developer. All Rights Reserved. | |
20 * | |
21 * Contributor(s): | |
22 * Jordan Miner <jminer7@gmail.com> | |
23 * | |
24 */ | |
25 | |
26 module dynamin.core.global; | |
27 | |
28 import dynamin.core.string; | |
29 import tango.math.Math; | |
30 import tango.io.model.IFile; | |
31 import tango.core.Exception; | |
32 | |
33 public import tango.util.Convert; | |
34 | |
35 static if((void*).sizeof == 4) { | |
36 /** | |
37 * Defined as an int on 32-bit platforms and as a long on 64-bit platforms. | |
38 */ | |
39 alias int word; | |
40 /** | |
41 * Defined as a uint on 32-bit platforms and | |
42 * as a ulong on 64-bit platforms. | |
43 */ | |
44 alias uint uword; | |
45 } else static if((void*).sizeof == 8) { | |
46 /** | |
47 * Defined as an int on 32-bit platforms and as a long on 64-bit platforms. | |
48 */ | |
49 alias long word; | |
50 /** | |
51 * Defined as a uint on 32-bit platforms and | |
52 * as a ulong on 64-bit platforms. | |
53 */ | |
54 alias ulong uword; | |
55 } | |
56 | |
57 /** | |
58 * The string used to separate lines. | |
59 * This is "\r\n" under Windows and "\n" under Linux. | |
60 */ | |
61 const string LineSeparator = FileConst.NewlineString; | |
62 /** | |
63 * The string used to separate directories in a path. | |
64 * This is "\\" under Windows and "/" under Linux. | |
65 */ | |
66 const string DirSeparator = FileConst.PathSeparatorString; | |
67 /// | |
68 const char DirSeparatorChar = FileConst.PathSeparatorChar; | |
69 /** | |
70 * The string used to separate paths. | |
71 * This is ";" under Windows and ":" under Linux | |
72 */ | |
73 const string PathSeparator = FileConst.SystemPathString; | |
74 /// | |
75 const char PathSeparatorChar = FileConst.SystemPathChar; | |
76 | |
77 /** | |
78 * Tests whether num1 and num2 are equal. They are considered equal | |
79 * if the difference between them is less than epsilon. | |
80 * Examples: | |
81 * ----- | |
82 * floatsEqual(3.14, 3.2, 0.1) == true | |
83 * floatsEqual(3.14, 3.3, 0.1) == false | |
84 * floatsEqual(3.14, 3.151, 0.01) == false | |
85 * ----- | |
86 */ | |
87 bool floatsEqual(real num1, real num2, real epsilon) { | |
88 return abs(num1 - num2) <= epsilon; | |
89 } | |
90 unittest { | |
91 assert(floatsEqual(3.14, 3.2, 0.1) == true); | |
92 assert(floatsEqual(3.14, 3.3, 0.1) == false); | |
93 assert(floatsEqual(3.14, 3.151, 0.01) == false); | |
94 } | |
95 | |
96 /** | |
97 * Copies length elements starting at srcStart in srcData to destStart | |
98 * in destData. Data is copied as if srcData and destData are two separate | |
99 * arrays, even if they are the same. | |
100 */ | |
101 void arrayCopy(T)(T[] srcData, uint srcStart, T[] destData, uint destStart, uint length) { | |
102 if((srcData is destData && srcStart == destStart) || length == 0) | |
103 return; | |
104 if(srcStart > destStart) { | |
105 //copy forward | |
106 for(int i = 0; i < length; ++i) | |
107 destData[destStart + i] = srcData[srcStart + i]; | |
108 } else { | |
109 //copy reverse | |
110 for(int i = length-1; i >= 0; --i) | |
111 destData[destStart + i] = srcData[srcStart + i]; | |
112 } | |
113 } | |
114 unittest { | |
115 char[] c = "Computer".dup; | |
116 arrayCopy!(char)(c, 3, c, 2, 4); | |
117 assert(c == "Coputeer"); | |
118 c = "Computer".dup; | |
119 arrayCopy!(char)(c, 2, c, 3, 4); | |
120 assert(c == "Commputr"); | |
121 c = "hi".dup; | |
122 arrayCopy!(char)(c, 1, c, 0, 1); | |
123 assert(c == "ii"); | |
124 } | |
125 | |
126 /** | |
127 * Sets every byte of the specified memory block to value. | |
128 */ | |
129 void memoryFill(void* mem, uword count, ubyte value) { | |
130 ubyte* memB = cast(ubyte*)mem; | |
131 while(count != 0) { | |
132 *memB++ = value; | |
133 --count; | |
134 } | |
135 } | |
136 unittest { | |
137 char[] buff = "jEdit".dup; | |
138 memoryFill(buff.ptr+1, 3, 0x23); | |
139 assert(buff == "j\x23\x23\x23t"); | |
140 } | |
141 | |
142 /** | |
143 * Sets every byte of the specified memory block to zero. | |
144 */ | |
145 void memoryZero(void* mem, uword count) { | |
146 memoryFill(mem, count, 0); | |
147 } | |
148 unittest { | |
149 char[] buff = "jEdit".dup; | |
150 memoryZero(buff.ptr+1, 3); | |
151 assert(buff == "j\0\0\0t"); | |
152 } | |
153 | |
154 /** | |
155 * Copies the specified number of bytes from srcMem to destMem. The source | |
156 * and destination should not overlap, or the results will be undefined. | |
157 * Note that the source and destination parameters are opposite in | |
158 * order from the C function memcpy(). If count is a multiple of the | |
159 * native pointer size, the copy will be done in blocks of that size. | |
160 */ | |
161 void memoryCopy(void* srcMem, void* destMem, uword count) { | |
162 // copy in blocks of the pointer size, if possible | |
163 if(count % word.sizeof == 0) { | |
164 count /= word.sizeof; | |
165 uword* src = cast(uword*)srcMem; | |
166 uword* dest = cast(uword*)destMem; | |
167 while(count != 0) { | |
168 *dest++ = *src++; | |
169 --count; | |
170 } | |
171 } else { | |
172 ubyte* src = cast(ubyte*)srcMem; | |
173 ubyte* dest = cast(ubyte*)destMem; | |
174 while(count != 0) { | |
175 *dest++ = *src++; | |
176 --count; | |
177 } | |
178 } | |
179 } | |
180 unittest { | |
181 char[] buff = "Hello".dup; | |
182 memoryCopy(buff.ptr, buff.ptr+3, 2); | |
183 assert(buff == "HelHe"); | |
184 buff = "Longer text here".dup; | |
185 memoryCopy(buff.ptr+7, buff.ptr+12, 4); | |
186 assert(buff == "Longer text text"); | |
187 } | |
188 | |
189 /** | |
190 * Converts a number into its roman numeral form. The number must | |
191 * be between 0 and 3,999, inclusive. | |
192 * Examples: | |
193 * ----- | |
194 * toRomanNumerals(2) == "II" | |
195 * toRomanNumerals(58) == "LVIII" | |
196 * toRomanNumerals(194) == "CXCIV" | |
197 * ----- | |
198 */ | |
199 string toRomanNumerals(int num) { | |
200 if(num > 3999 || num < 0) | |
201 throw new IllegalArgumentException("ToRomanNumerals():" ~ | |
202 "highest convertable roman numeral is 3999"); | |
2
fab13ec910ba
Speed up toRomanNumerals() by removing heap allocations. Much faster for Tango's debug build.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
203 static combos = [[0][0..0], [0], [0,0], [0,0,0], [0,1], |
0 | 204 [1], [1,0], [1,0,0], [1,0,0,0], [0,2]]; |
2
fab13ec910ba
Speed up toRomanNumerals() by removing heap allocations. Much faster for Tango's debug build.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
205 static letters = ['I', 'V', 'X', 'L', 'C', 'D', 'M']; |
0 | 206 string str = ""; |
207 int letterOffset = 0; | |
208 while(num > 0) { | |
209 foreach_reverse(int c; combos[num % 10]) | |
210 str = letters[c+letterOffset] ~ str; | |
211 num /= 10; | |
212 letterOffset += 2; | |
213 } | |
214 return str; | |
215 } | |
216 unittest { | |
217 assert(toRomanNumerals(2) == "II"); | |
218 assert(toRomanNumerals(58) == "LVIII"); | |
219 assert(toRomanNumerals(194) == "CXCIV"); | |
220 | |
221 assert(toRomanNumerals(0) == ""); | |
222 assert(toRomanNumerals(1) == "I"); | |
223 assert(toRomanNumerals(10) == "X"); | |
224 assert(toRomanNumerals(500) == "D"); | |
225 assert(toRomanNumerals(18) == "XVIII"); | |
226 assert(toRomanNumerals(3949) == "MMMCMXLIX"); | |
227 } | |
228 | |
229 int numeralToValue(char c) { | |
230 switch(c) { | |
231 case 'I': case 'i': return 1; | |
232 case 'V': case 'v': return 5; | |
233 case 'X': case 'x': return 10; | |
234 case 'L': case 'l': return 50; | |
235 case 'C': case 'c': return 100; | |
236 case 'D': case 'd': return 500; | |
237 case 'M': case 'm': return 1000; | |
238 default: return -1; | |
239 } | |
240 } | |
241 /** | |
242 * Parses the specified string of roman numerals and returns the value. | |
243 * The value must be less than or equal to 3,999. The string may be uppercase, | |
244 * lowercase, or a mixture of both cases. | |
245 * Examples: | |
246 * ----- | |
247 * parseRomanNumerals("II") == 2 | |
248 * parseRomanNumerals("LVIII") == 58 | |
249 * parseRomanNumerals("CXCIV") == 194 | |
250 * parseRomanNumerals("xxxxviiii") == 49 | |
251 * ----- | |
252 */ | |
253 int parseRomanNumerals(string str) { | |
254 int num = 0; | |
255 int largestSoFar = 1; | |
256 foreach_reverse(c; str) { | |
257 int value = numeralToValue(c); | |
258 if(value == -1) | |
259 throw new IllegalArgumentException("Invalid roman numeral: " ~ c); | |
260 if(value < largestSoFar) { | |
261 num -= value; | |
262 } else { | |
263 num += value; | |
264 largestSoFar = value; | |
265 } | |
266 } | |
267 if(num > 3999 || num < 0) | |
268 throw new IllegalArgumentException("ParseRomanNumerals():" ~ | |
269 "highest convertable roman numeral is 3999"); | |
270 return num; | |
271 } | |
272 unittest { | |
273 assert(parseRomanNumerals("II") == 2); | |
274 assert(parseRomanNumerals("LVIII") == 58); | |
275 assert(parseRomanNumerals("CXCIV") == 194); | |
276 assert(parseRomanNumerals("xxxxviiii") == 49); | |
277 | |
278 assert(parseRomanNumerals("") == 0); | |
279 assert(parseRomanNumerals("I") == 1); | |
280 assert(parseRomanNumerals("X") == 10); | |
281 assert(parseRomanNumerals("D") == 500); | |
282 assert(parseRomanNumerals("XVIII") == 18); | |
283 assert(parseRomanNumerals("MMMCMXLIX") == 3949); | |
284 } | |
285 unittest { | |
286 for(int i = 0; i < 4000; ++i) | |
287 assert(toRomanNumerals(i).parseRomanNumerals() == i); | |
288 } | |
289 | |
290 /** | |
291 * Converts a number of bytes into a human friendly string. The units | |
292 * supported are bytes, KB, MB, GB, TB, PB, EB, ZB, and YB. | |
293 * Examples: | |
294 * ----- | |
295 * byteCountToString(202) == "202 bytes" | |
296 * byteCountToString(1021) == "1021 bytes" | |
297 * byteCountToString(106_496) == "104 KB" | |
298 * byteCountToString(620_705_792) == "591 MB" | |
299 * ----- | |
300 */ | |
301 string byteCountToString(ulong num) { | |
302 const factor = 1024; | |
303 //kilo, mega, giga, tera, peta, exa, zetta, yotta | |
304 char[][] units = [ | |
305 " bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"]; | |
306 uint unitIndex = 0; | |
307 ulong div = factor; | |
308 uint rem; | |
309 while(num > factor-1 && unitIndex < units.length-1) { | |
310 rem = num % factor; | |
311 num /= factor; | |
312 ++unitIndex; | |
313 } | |
314 //rem/1024 equals the fraction of unit | |
315 string str = to!(string)(num); | |
316 if(str.length < 3) { | |
317 str ~= "." ~ to!(string)(rem*10/factor); | |
318 } | |
319 str ~= units[unitIndex]; | |
320 return str; | |
321 } | |
322 unittest { | |
323 assert(byteCountToString(202) == "202 bytes"); | |
324 assert(byteCountToString(1021) == "1021 bytes"); | |
325 assert(byteCountToString(106_496) == "104 KB"); | |
326 assert(byteCountToString(620_705_792) == "591 MB"); | |
327 } | |
328 |