Changeset 17
- Timestamp:
- 01/17/09 19:04:12 (3 years ago)
- Files:
-
- trunk/alloc.d (modified) (24 diffs)
- trunk/cor.d (modified) (1 diff)
- trunk/distrib.d (modified) (2 diffs)
- trunk/random.d (modified) (1 diff)
- trunk/sort.d (modified) (12 diffs)
- trunk/stats.d (modified) (1 diff)
- trunk/summary.d (modified) (6 diffs)
- trunk/tests.d (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/alloc.d
r6 r17 2 2 * Author: David Simcha 3 3 * 4 * Copyright (c) 2009, David Simcha 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * * Neither the name of the <organization> nor the 15 * names of its contributors may be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 4 * Copyright (c) 2009, David Simcha 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * * Neither the name of the authors nor the 18 * names of its contributors may be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 28 31 29 32 module dstats.alloc; … … 148 151 } 149 152 153 // C functions, marked w/ nothrow. 154 extern(C) nothrow int printf(in char *,...); 155 extern(C) nothrow void exit(int); 156 extern(C) nothrow void* malloc(size_t); 157 extern(C) nothrow void* realloc(void*, size_t); 158 extern(C) nothrow void* free(void*); 159 alias malloc cstdmalloc; 160 alias realloc cstdrealloc; 161 alias free cfree; 162 150 163 /**TempAlloc struct. See TempAlloc project on Scrapple.*/ 151 164 struct TempAlloc { … … 154 167 size_t used; 155 168 block* prev; 169 170 void freeAll() { 171 if(prev !is null) 172 prev.freeAll(); 173 if(space !is null) 174 cfree(space); 175 cfree(&this); 176 } 156 177 } 157 178 … … 164 185 block* current; 165 186 block* freelist; 166 } 187 188 ~this() { 189 cfree(lastAlloc.ptr); 190 if(current !is null) // Should never be null, but better to be safe. 191 current.freeAll(); 192 if(freelist !is null) 193 freelist.freeAll(); 194 } 195 } 196 197 // core.thread.Thread.thread_needLock() is nothrow (read the code if you 198 // don't believe me) but not marked as such because nothrow is such a new 199 // feature in D. This is a workaround until that gets fixed. 200 static enum tnl = cast(bool function() nothrow) &thread_needLock; 167 201 168 202 enum blockSize = 4U * 1024U * 1024U; 203 enum nBookKeep = 1_048_576; 169 204 enum alignBytes = 16U; 170 205 static __thread State state; 171 206 static State mainThreadState; 172 207 208 static void die() nothrow { 209 printf("TempAlloc error: Out of memory.\0".ptr); 210 exit(1); 211 } 212 213 static void doubleSize(ref void*[] lastAlloc) nothrow { 214 size_t newSize = lastAlloc.length * 2; 215 void** ptr = cast(void**) crealloc(lastAlloc.ptr, newSize); 216 return ptr[0..newSize]; 217 } 218 219 static void* cmalloc(size_t size) nothrow { 220 void* ret = cstdmalloc(size); 221 if(ret is null) { 222 try { GC.collect(); } catch {} 223 ret = cstdmalloc(size); 224 225 if(ret is null) 226 die(); 227 } 228 return ret; 229 } 230 231 static void* crealloc(void* ptr, size_t size) nothrow { 232 void* ret = cstdrealloc(ptr, size); 233 if(ret is null) { 234 try { GC.collect(); } catch {} 235 ret = cstdrealloc(ptr, size); 236 237 if(ret is null) 238 die(); 239 } 240 return ret; 241 } 242 173 243 static size_t getAligned(size_t nbytes) pure nothrow { 174 244 size_t rem = nbytes % alignBytes; … … 176 246 } 177 247 178 static State stateInit() { 179 auto stateCopy = new State; 248 static State stateInit() nothrow { 249 State stateCopy; 250 try { 251 stateCopy = new State; 252 } catch { die; } 180 253 181 254 with(stateCopy) { 182 current = new block; 183 current.space = GC.malloc(blockSize, GC.BlkAttr.NO_SCAN); 184 lastAlloc = cast(void*[]) new size_t[262_144]; 255 current = cast(block*) cmalloc(block.sizeof); 256 *current = block.init; 257 current.space = cmalloc(blockSize); 258 lastAlloc = (cast(void**) cmalloc(nBookKeep))[0..nBookKeep / (void*).sizeof]; 185 259 nblocks++; 186 260 } 187 261 188 262 state = stateCopy; 189 if(!t hread_needLock)263 if(!tnl()) 190 264 mainThreadState = stateCopy; 191 265 return stateCopy; … … 196 270 * significant in some cases because it avoids a thread-local storage 197 271 * lookup. Also used internally.*/ 198 static State getState() {272 static State getState() nothrow { 199 273 // Believe it or not, even with builtin TLS, the thread_needLock() 200 274 // is worth it to avoid the TLS lookup. 201 State stateCopy = (t hread_needLock) ?275 State stateCopy = (tnl()) ? 202 276 state : 203 277 mainThreadState; … … 209 283 * freed when frameFree() is called. Returns a reference to the 210 284 * State class in case the caller wants to cache it for speed.*/ 211 static State frameInit() {285 static State frameInit() nothrow { 212 286 return frameInit(getState); 213 287 } … … 215 289 /**Same as frameInit() but uses stateCopy cached on stack by caller 216 290 * to avoid a thread-local storage lookup. Strictly a speed hack.*/ 217 static State frameInit(State stateCopy) {291 static State frameInit(State stateCopy) nothrow { 218 292 with(stateCopy) { 219 293 if(totalAllocs == lastAlloc.length) // Should happen very infrequently. 220 lastAlloc.length = lastAlloc.length * 2;294 doubleSize(lastAlloc); 221 295 lastAlloc[totalAllocs] = cast(void*) frameIndex; 222 296 frameIndex = totalAllocs; … … 228 302 /**Frees all memory allocated by TempAlloc since the last call to 229 303 * frameInit().*/ 230 static void frameFree() {304 static void frameFree() nothrow { 231 305 frameFree(getState); 232 306 } … … 234 308 /**Same as frameFree() but uses stateCopy cached on stack by caller 235 309 * to avoid a thread-local storage lookup. Strictly a speed hack.*/ 236 static void frameFree(State stateCopy) {310 static void frameFree(State stateCopy) nothrow { 237 311 with(stateCopy) { 238 312 while(totalAllocs > frameIndex + 1) { … … 244 318 245 319 /**Purely a convenience overload, forwards arguments to TempAlloc.malloc().*/ 246 static void* opCall(T...)(T args) {320 static void* opCall(T...)(T args) nothrow { 247 321 return TempAlloc.malloc(args); 248 322 } … … 257 331 * Do not store the only reference to a GC-allocated object in 258 332 * TempAlloc-allocated memory.*/ 259 static void* malloc(size_t nbytes) {333 static void* malloc(size_t nbytes) nothrow { 260 334 return malloc(nbytes, getState); 261 335 } … … 263 337 /**Same as malloc() but uses stateCopy cached on stack by caller 264 338 * to avoid a thread-local storage lookup. Strictly a speed hack.*/ 265 static void* malloc(size_t nbytes, State stateCopy) {339 static void* malloc(size_t nbytes, State stateCopy) nothrow { 266 340 nbytes = getAligned(nbytes); 267 341 with(stateCopy) { … … 271 345 current.used += nbytes; 272 346 } else if(nbytes > blockSize) { 273 ret = GC.malloc(nbytes, GC.BlkAttr.NO_SCAN);347 ret = cmalloc(nbytes); 274 348 } else if(nfree > 0) { 275 349 block* prev = freelist.prev; … … 282 356 ret = current.space; 283 357 } else { // Allocate more space. 284 block* newBlock = new block; 285 newBlock.space = GC.malloc(blockSize, GC.BlkAttr.NO_SCAN); 358 block* newBlock = cast(block*) cmalloc(block.sizeof); 359 *newBlock = block.init; 360 newBlock.space = cmalloc(blockSize); 286 361 nblocks++; 287 362 newBlock.prev = current; … … 291 366 } 292 367 if(totalAllocs == lastAlloc.length) // Should happen very infrequently. 293 lastAlloc.length = lastAlloc.length * 2;368 doubleSize(lastAlloc); 294 369 lastAlloc[totalAllocs++] = ret; 295 370 return ret; … … 301 376 * there's no need to pass a pointer in. All bookkeeping for figuring 302 377 * out what to free is done internally.*/ 303 static void free() {378 static void free() nothrow { 304 379 free(getState); 305 380 } … … 307 382 /**Same as free() but uses stateCopy cached on stack by caller 308 383 * to avoid a thread-local storage lookup. Strictly a speed hack.*/ 309 static void free(State stateCopy) {384 static void free(State stateCopy) nothrow { 310 385 with(stateCopy) { 311 386 void* lastPos = lastAlloc[--totalAllocs]; … … 314 389 if(lastPos > current.space + blockSize || 315 390 lastPos < current.space) { 316 GC.free(lastPos);391 cfree(lastPos); 317 392 return; 318 393 } … … 333 408 block* last = freelist; 334 409 freelist = freelist.prev; 335 GC.free(last.space);336 GC.free(last);410 cfree(last.space); 411 cfree(last); 337 412 nfree--; 338 413 } … … 354 429 * in an array allocated by newStack because this memory is not 355 430 * scanned by the GC.*/ 356 T[] newStack(T)(size_t size) {431 T[] newStack(T)(size_t size) nothrow { 357 432 size_t bytes = size * T.sizeof; 358 433 T* ptr = cast(T*) TempAlloc.malloc(bytes); … … 362 437 /**Same as newStack(size_t) but uses stateCopy cached on stack by caller 363 438 * to avoid a thread-local storage lookup. Strictly a speed hack.*/ 364 T[] newStack(T)(size_t size, TempAlloc.State state) {439 T[] newStack(T)(size_t size, TempAlloc.State state) nothrow { 365 440 size_t bytes = size * T.sizeof; 366 441 T* ptr = cast(T*) TempAlloc.malloc(bytes, state); … … 369 444 370 445 /**Creates a duplicate of an array on the TempAlloc struct.*/ 371 auto tempdup(T)(T[] data) {446 auto tempdup(T)(T[] data) nothrow { 372 447 alias Mutable!(T) U; 373 448 U[] ret = newStack!(U)(data.length); … … 378 453 /**Same as tempdup(T[]) but uses stateCopy cached on stack by caller 379 454 * to avoid a thread-local storage lookup. Strictly a speed hack.*/ 380 auto tempdup(T)(T[] data, TempAlloc.State state) {455 auto tempdup(T)(T[] data, TempAlloc.State state) nothrow { 381 456 alias Mutable!(T) U; 382 457 U[] ret = newStack!(U)(data.length, state); trunk/cor.d
r5 r17 3 3 * Author: David Simcha 4 4 * 5 * Copyright (c) 2009, David Simcha 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * * Neither the name of the <organization> nor the 16 * names of its contributors may be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 5 * Copyright (c) 2009, David Simcha 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * * Neither the name of the authors nor the 19 * names of its contributors may be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 23 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 29 32 30 33 module dstats.cor; trunk/distrib.d
r5 r17 39 39 * 40 40 * normalCDF <=> normalDistribution 41 * 41 42 * normalCDFR <=> normalDistributionCompl 43 * 42 44 * invNormalCDF <=> normalDistributionComplInv 45 * 43 46 * studentsTCDF <=> studentsTDistribution (Note reversal in argument order) 47 * 44 48 * invStudentsTCDF <=> studentsTDistributionInv (Again, arg order reversed) 49 * 45 50 * binomialCDF <=> binomialDistribution 51 * 46 52 * negBinomCDF <=> negativeBinomialDistribution 53 * 47 54 * poissonCDF <=> poissonDistribution 55 * 48 56 * chiSqrCDF <=> chiSqrDistribution 57 * 49 58 * chiSqrCDFR <=> chiSqrDistributionCompl 59 * 50 60 * invChiSqCDFR <=> chiSqrDistributionComplInv 61 * 51 62 * fisherCDF <=> fDistribution 63 * 52 64 * fisherCDFR <=> fDistributionCompl 65 * 53 66 * invFisherCDFR <=> fDistributionComplInv 67 * 54 68 * gammaCDF <=> gammaDistribution (Note arg reversal) 69 * 55 70 * gammaCDFR <=> gammaDistributionCompl (Note arg reversal) 56 71 * 57 * Note that poissonCDFR and binomialCDFR are NOT equivalent to their Tango 58 * counterparts because poissonCDFR and binomialCDFR represent P(X >= x), while 59 * poissonDistributionCompl and binomialDistributionCompl represent P(X > x). 60 * 61 * Copyright (c) 2008-2009, David Simcha and Don Clugston 62 * All rights reserved. 63 * 64 * Redistribution and use in source and binary forms, with or without 65 * modification, are permitted provided that the following conditions are met: 66 * * Redistributions of source code must retain the above copyright 67 * notice, this list of conditions and the following disclaimer. 68 * * Redistributions in binary form must reproduce the above copyright 69 * notice, this list of conditions and the following disclaimer in the 70 * documentation and/or other materials provided with the distribution. 71 * * Neither the name of the <organization> nor the 72 * names of its contributors may be used to endorse or promote products 73 * derived from this software without specific prior written permission. 74 * 75 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 76 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 77 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 78 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 79 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 80 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 81 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 82 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 83 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 84 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 72 * Note that CDFRs/Compls of continuous distributions are not equivalent, 73 * because in Tango/MathExtra they represent P(X > x) while in dstats they 74 * represent P(X >= x). 75 * 76 * Copyright (c) 2008-2009, David Simcha and Don Clugston 77 * All rights reserved. 78 * 79 * Redistribution and use in source and binary forms, with or without 80 * modification, are permitted provided that the following conditions are met: 81 * 82 * * Redistributions of source code must retain the above copyright 83 * notice, this list of conditions and the following disclaimer. 84 * 85 * * Redistributions in binary form must reproduce the above copyright 86 * notice, this list of conditions and the following disclaimer in the 87 * documentation and/or other materials provided with the distribution. 88 * 89 * * Neither the name of the authors nor the 90 * names of its contributors may be used to endorse or promote products 91 * derived from this software without specific prior written permission. 92 * 93 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 94 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 95 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 96 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 97 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 98 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 100 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 101 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 102 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 85 103 86 104 module dstats.distrib; … … 745 763 * Normal probability density function (integrated from 746 764 * minus infinity to x) is equal to p. 747 *748 * For small arguments 0 < p < exp(-2), the program computes749 * z = sqrt( -2 log(p) ); then the approximation is750 * x = z - log(z)/z - (1/z) P(1/z) / Q(1/z) .751 * For larger arguments, x/sqrt(2 pi) = w + w^3 R(w^2)/S(w^2)) ,752 * where w = p - 0.5 .753 765 */ 754 766 real invNormalCDF(real p, real mean = 0, real sd = 1) trunk/random.d
r5 r17 4 4 * Author: David Simcha 5 5 * 6 * Copyright (c) 2009, David Simcha 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions are met: 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * * Neither the name of the <organization> nor the 17 * names of its contributors may be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 6 * Copyright (c) 2009, David Simcha 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions are met: 11 * 12 * * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * * Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * * Neither the name of the authors nor the 20 * names of its contributors may be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 30 33 31 34 trunk/sort.d
r12 r17 16 16 * assert(foo == [1, 2, 3, 4, 5]); 17 17 * assert(bar == [6, 7, 8, 5, 3]); 18 * mergeSort!("a > b")(bar, foo); 18 * auto baz = [1.0, 0, -1, -2, -3].dup; 19 * mergeSort!("a > b")(bar, foo, baz); 19 20 * assert(bar == [8, 7, 6, 5, 3]); 20 21 * assert(foo == [3, 2, 1, 4, 5]); 22 * assert(baz == [-1.0, 0, 1, -2, -3]); 21 23 * --- 22 24 * 23 25 * Author: David Simcha 24 26 * 25 * Copyright (c) 2009, David Simcha 26 * All rights reserved. 27 * 28 * Redistribution and use in source and binary forms, with or without 29 * modification, are permitted provided that the following conditions are met: 30 * * Redistributions of source code must retain the above copyright 31 * notice, this list of conditions and the following disclaimer. 32 * * Redistributions in binary form must reproduce the above copyright 33 * notice, this list of conditions and the following disclaimer in the 34 * documentation and/or other materials provided with the distribution. 35 * * Neither the name of the <organization> nor the 36 * names of its contributors may be used to endorse or promote products 37 * derived from this software without specific prior written permission. 38 * 39 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 40 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 41 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 42 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 43 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 44 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 46 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 47 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 48 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 27 * Copyright (c) 2009, David Simcha 28 * All rights reserved. 29 * 30 * Redistribution and use in source and binary forms, with or without 31 * modification, are permitted provided that the following conditions are met: 32 * 33 * * Redistributions of source code must retain the above copyright 34 * notice, this list of conditions and the following disclaimer. 35 * 36 * * Redistributions in binary form must reproduce the above copyright 37 * notice, this list of conditions and the following disclaimer in the 38 * documentation and/or other materials provided with the distribution. 39 * 40 * * Neither the name of the authors nor the 41 * names of its contributors may be used to endorse or promote products 42 * derived from this software without specific prior written permission. 43 * 44 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 45 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 46 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 47 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 48 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 49 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 50 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 51 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 53 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 49 54 50 55 module dstats.sort; … … 63 68 } 64 69 65 privatevoid rotateLeft(T)(T[] input) {70 void rotateLeft(T)(T[] input) { 66 71 if(input.length < 2) return; 67 72 T temp = input[0]; … … 72 77 } 73 78 74 privatevoid rotateRight(T)(T[] input) {79 void rotateRight(T)(T[] input) { 75 80 if(input.length < 2) return; 76 81 T temp = input[$-1]; … … 116 121 } 117 122 118 /**Quick sort. Unstable, O(N log N) time average, O(N^2) timeworst123 /**Quick sort. Unstable, O(N log N) time average, worst 119 124 * case, O(log N) space, small constant term in time complexity. 120 125 * 121 * In this implementation, the following steps are taken to avoid the O(N^2)122 * worst case:126 * In this implementation, the following steps are taken to avoid the 127 * O(N<sup>2</sup>) worst case of naive quick sorts: 123 128 * 124 129 * 1. At each recursion, the median of the first, middle and last elements of … … 131 136 * 132 137 * 3. After a much larger than expected amount of recursion has occured, 133 * this function transitions to a heap sort.*/ 138 * this function transitions to a heap sort. This guarantees an O(N log N) 139 * worst case.*/ 134 140 T[0] qsort(alias compFun = lessThan, T...)(T data) 135 141 in { … … 366 372 * and U is a ulong* for calculating bubble sort distance, this can be called 367 373 * as mergeSortTemp(D, D, D, T, T, T, U) or mergeSortTemp(D, D, D, T, T, T) 368 * where each D has a T of corresponding type.*/ 374 * where each D has a T of corresponding type. 375 * 376 * Examples: 377 * --- 378 * int[] foo = [3, 1, 2, 4, 5].dup; 379 * int[] temp = new uint[5]; 380 * mergeSortTemp!("a < b")(foo, temp); 381 * assert(foo == [1, 2, 3, 4, 5]); // The contents of temp will be undefined. 382 * foo = [3, 1, 2, 4, 5].dup; 383 * real bar = [3.14L, 15.9, 26.5, 35.8, 97.9]; 384 * real temp2 = new real[5]; 385 * mergeSortTemp(foo, bar, temp, temp2); 386 * assert(foo == [1, 2, 3, 4, 5]); 387 * assert(bar == [15.9L, 26.5, 3.14, 35.8, 97.9]); 388 * // The contents of both temp and temp2 will be undefined. 389 * --- 390 */ 369 391 T[0] mergeSortTemp(alias compFun = lessThan, T...)(T data) 370 392 in { … … 487 509 } 488 510 489 /**In-place merge sort, based on C++ STL's stable_sort(). O(N * log(N)^2)511 /**In-place merge sort, based on C++ STL's stable_sort(). O(N log<sup>2</sup> N) 490 512 * time complexity, O(1) space complexity, stable. Much slower than plain 491 513 * old mergeSort(), so only use it if you really need the O(1) space.*/ … … 686 708 } 687 709 688 /**Insertion sort. O(N ^2) time worst, average case, O(1) space, VERY small689 * constant, which is why it's useful for sorting small subarrays in710 /**Insertion sort. O(N<sup>2</sup>) time worst, average case, O(1) space, VERY 711 * small constant, which is why it's useful for sorting small subarrays in 690 712 * divide and conquer algorithms. If last argument is a ulong*, increments 691 713 * the dereference of this argument by the bubble sort distance between the … … 794 816 } 795 817 796 /**Given a set of data points entered through the addElement function,797 * maintains the invariant that the top N according to compFun will be798 * contained in the data structure. Uses a heap internally, O(log N) insertion799 * time. Good for finding the top N elements of a very large dataset that800 * cannot be sorted quickly in its entirety. If less than N datapoints801 * have been entered, all are contained in the structure.802 *803 * Examples:804 * ---805 Random gen;806 gen.seed(unpredictableSeed);807 uint[] nums = seq(0U, 100U);808 auto less = TopN!(uint, "a < b")(10);809 auto more = TopN!(uint, "a > b")(10);810 randomShuffle(nums, gen);811 foreach(n; nums) {812 less.addElement(n);813 more.addElement(n);814 }815 assert(less.getSorted == [0U, 1,2,3,4,5,6,7,8,9]);816 assert(more.getSorted == [99U, 98, 97, 96, 95, 94, 93, 92, 91, 90]);817 ---*/818 struct TopN(T, alias compFun = greaterThan) {819 private:820 alias binaryFun!(compFun) comp;821 uint n;822 uint nAdded;823 824 T[] nodes;825 public:826 /**The variable ntop controls how many elements are retained.*/827 this(uint ntop) {828 n = ntop;829 nodes = new T[n];830 }831 832 /**Insert an element into the topN struct.*/833 void addElement(T elem) {834 if(nAdded < n) {835 nodes[nAdded] = elem;836 if(nAdded == n - 1) {837 makeMultiHeap!(comp)(nodes);838 }839 nAdded++;840 } else if(nAdded >= n) {841 if(comp(elem, nodes[0])) {842 nodes[0] = elem;843 multiSiftDown!(comp)(nodes, 0, nodes.length);844 }845 }846 }847 848 /**Get the elements currently in the struct. Returns a reference to849 * internal state, elements will be in an arbitrary order. Cheap.*/850 const(T)[] getElements() const {851 return nodes[0..min(n, nAdded)];852 }853 854 /**Returns the elements sorted by compFun. The array returned is a855 * duplicate of the input array. Not cheap.*/856 T[] getSorted() const {857 return qsort!(comp)(nodes[0..min(n, nAdded)].dup);858 }859 }860 861 unittest {862 alias TopN!(uint, "a < b") TopNLess;863 alias TopN!(uint, "a > b") TopNGreater;864 Random gen;865 gen.seed(unpredictableSeed);866 uint[] nums = new uint[100];867 foreach(i, ref n; nums) {868 n = i;869 }870 foreach(i; 0..100) {871 auto less = TopNLess(10);872 auto more = TopNGreater(10);873 randomShuffle(nums, gen);874 foreach(n; nums) {875 less.addElement(n);876 more.addElement(n);877 }878 assert(less.getSorted == [0U, 1,2,3,4,5,6,7,8,9]);879 assert(more.getSorted == [99U, 98, 97, 96, 95, 94, 93, 92, 91, 90]);880 }881 foreach(i; 0..100) {882 auto less = TopNLess(10);883 auto more = TopNGreater(10);884 randomShuffle(nums, gen);885 foreach(n; nums[0..5]) {886 less.addElement(n);887 more.addElement(n);888 }889 assert(less.getSorted == qsort!("a < b")(nums[0..5]));890 assert(more.getSorted == qsort!("a > b")(nums[0..5]));891 }892 writeln("Passed TopN test.");893 }894 895 818 /**Returns the kth largest/smallest element (depending on compFun, 0-indexed) 896 819 * in the input array in O(N) time. Allocates memory, does not modify input … … 920 843 * --- 921 844 * auto foo = [3, 1, 5, 4, 2].dup; 922 * auto second Elem= partitionK(foo, 1);923 * assert(second Elem== 2);845 * auto secondSmallest = partitionK(foo, 1); 846 * assert(secondSmallest == 2); 924 847 * foreach(elem; foo[0..1]) { 925 848 * assert(elem <= foo[1]); 926 849 * } 927 850 * foreach(elem; foo[2..$]) { 928 * assert(elem >= foo );851 * assert(elem >= foo[1]); 929 852 * } 930 * 931 * Returns: The kth element of the array.*/ 853 * --- 854 * 855 * Returns: The kth element of the array. 856 */ 932 857 ArrayElemType!(T[0]) partitionK(alias compFun = lessThan, T...)(T data, int k) 933 858 in { … … 1022 947 } 1023 948 949 /**Given a set of data points entered through the addElement function, 950 * maintains the invariant that the top N according to compFun will be 951 * contained in the data structure. Uses a heap internally, O(log N) insertion 952 * time. Good for finding the largest/smallest N elements of a very large 953 * dataset that cannot be sorted quickly in its entirety, and may not even fit 954 * in memory. If less than N datapoints have been entered, all are contained in 955 * the structure. 956 * 957 * Examples: 958 * --- 959 * Random gen; 960 * gen.seed(unpredictableSeed); 961 * uint[] nums = seq(0U, 100U); 962 * auto less = TopN!(uint, "a < b")(10); 963 * auto more = TopN!(uint, "a > b")(10); 964 * randomShuffle(nums, gen); 965 * foreach(n; nums) { 966 * less.addElement(n); 967 * more.addElement(n); 968 * } 969 * assert(less.getSorted == [0U, 1,2,3,4,5,6,7,8,9]); 970 * assert(more.getSorted == [99U, 98, 97, 96, 95, 94, 93, 92, 91, 90]); 971 * --- 972 */ 973 struct TopN(T, alias compFun = greaterThan) { 974 private: 975 alias binaryFun!(compFun) comp; 976 uint n; 977 uint nAdded; 978 979 T[] nodes; 980 public: 981 /** The variable ntop controls how many elements are retained.*/ 982 this(uint ntop) { 983 n = ntop; 984 nodes = new T[n]; 985 } 986 987 /** Insert an element into the topN struct.*/ 988 void addElement(T elem) { 989 if(nAdded < n) { 990 nodes[nAdded] = elem; 991 if(nAdded == n - 1) { 992 makeMultiHeap!(comp)(nodes); 993 } 994 nAdded++; 995 } else if(nAdded >= n) { 996 if(comp(elem, nodes[0])) { 997 nodes[0] = elem; 998 multiSiftDown!(comp)(nodes, 0, nodes.length); 999 } 1000 } 1001 } 1002 1003 /**Get the elements currently in the struct. Returns a reference to 1004 * internal state, elements will be in an arbitrary order. Cheap.*/ 1005 const(T)[] getElements() const { 1006 return nodes[0..min(n, nAdded)]; 1007 } 1008 1009 /**Returns the elements sorted by compFun. The array returned is a 1010 * duplicate of the input array. Not cheap.*/ 1011 T[] getSorted() const { 1012 return qsort!(comp)(nodes[0..min(n, nAdded)].dup); 1013 } 1014 } 1015 1016 unittest { 1017 alias TopN!(uint, "a < b") TopNLess; 1018 alias TopN!(uint, "a > b") TopNGreater; 1019 Random gen; 1020 gen.seed(unpredictableSeed); 1021 uint[] nums = new uint[100]; 1022 foreach(i, ref n; nums) { 1023 n = i; 1024 } 1025 foreach(i; 0..100) { 1026 auto less = TopNLess(10); 1027 auto more = TopNGreater(10); 1028 randomShuffle(nums, gen); 1029 foreach(n; nums) { 1030 less.addElement(n); 1031 more.addElement(n); 1032 } 1033 assert(less.getSorted == [0U, 1,2,3,4,5,6,7,8,9]); 1034 assert(more.getSorted == [99U, 98, 97, 96, 95, 94, 93, 92, 91, 90]); 1035 } 1036 foreach(i; 0..100) { 1037 auto less = TopNLess(10); 1038 auto more = TopNGreater(10); 1039 randomShuffle(nums, gen); 1040 foreach(n; nums[0..5]) { 1041 less.addElement(n); 1042 more.addElement(n); 1043 } 1044 assert(less.getSorted == qsort!("a < b")(nums[0..5])); 1045 assert(more.getSorted == qsort!("a > b")(nums[0..5])); 1046 } 1047 writeln("Passed TopN test."); 1048 } 1049 1024 1050 // Verify that there are no TempAlloc memory leaks anywhere in the code covered 1025 1051 // by the unittest. This should always be the last unittest of the module. … … 1029 1055 assert(TAState.nblocks < 2); 1030 1056 } 1057 trunk/stats.d
r5 r17 3 3 * Author: David Simcha 4 4 * 5 * Copyright (c) 2009, David Simcha 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * * Neither the name of the <organization> nor the 16 * names of its contributors may be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 5 * Copyright (c) 2009, David Simcha 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * * Neither the name of the authors nor the 19 * names of its contributors may be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 23 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 29 32 30 33 module dstats.stats; trunk/summary.d
r5 r17 7 7 * Author: David Simcha 8 8 * 9 * Copyright (c) 2009, David Simcha 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions are met: 14 * * Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * * Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * * Neither the name of the <organization> nor the 20 * names of its contributors may be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 9 * Copyright (c) 2009, David Simcha 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions are met: 14 * 15 * * Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 18 * * Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * * Neither the name of the authors nor the 23 * names of its contributors may be used to endorse or promote products 24 * derived from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 27 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 30 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 33 36 34 37 … … 169 172 } 170 173 174 /// 171 175 string toString() { 172 176 return to!(string)(mean); … … 268 272 } 269 273 274 /// 270 275 string toString() { 271 276 return format("N = ", cast(ulong) _k, "\nMean = ", mean, "\nVariance = ", … … 422 427 } 423 428 429 /// 424 430 string toString() { 425 431 return format("N = ", cast(ulong) _k, "\nMean = ", mean, "\nVariance = ", … … 496 502 real max; 497 503 504 /// 498 505 string toString() { 499 506 return format("N = ", N, "\nMean = ", mean, "\nVariance = ", … … 504 511 505 512 /**Calculates all summary dstats (mean, variance, standard dev., skewness 506 * and kurtosis on an array. Returns the results in a Summary struct.*/513 * and kurtosis) on an array. Returns the results in a Summary struct.*/ 507 514 Summary summary(T)(const T[] data) { 508 515 OnlineSummary summ; trunk/tests.d
r5 r17 3 3 * Author: David Simcha 4 4 * 5 * Copyright (c) 2009, David Simcha 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * * Neither the name of the <organization> nor the 16 * names of its contributors may be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 5 * Copyright (c) 2009, David Simcha 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * * Neither the name of the authors nor the 19 * names of its contributors may be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 23 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ 29 32 module dstats.tests; 30 33
