Changeset 17

Show
Ignore:
Timestamp:
01/17/09 19:04:12 (3 years ago)
Author:
dsimcha
Message:

Optimize TempAlloc?. Fix a few minor bugs.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/alloc.d

    r6 r17  
    22 * Author:  David Simcha 
    33 * 
    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.*/ 
    2831 
    2932module dstats.alloc; 
     
    148151} 
    149152 
     153// C functions, marked w/ nothrow. 
     154extern(C) nothrow int printf(in char *,...); 
     155extern(C) nothrow void exit(int); 
     156extern(C) nothrow void* malloc(size_t); 
     157extern(C) nothrow void* realloc(void*, size_t); 
     158extern(C) nothrow void* free(void*); 
     159alias malloc cstdmalloc; 
     160alias realloc cstdrealloc; 
     161alias free cfree; 
     162 
    150163/**TempAlloc struct.  See TempAlloc project on Scrapple.*/ 
    151164struct TempAlloc { 
     
    154167        size_t used; 
    155168        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        } 
    156177    } 
    157178 
     
    164185         block* current; 
    165186         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; 
    167201 
    168202    enum blockSize = 4U * 1024U * 1024U; 
     203    enum nBookKeep = 1_048_576; 
    169204    enum alignBytes = 16U; 
    170205    static __thread State state; 
    171206    static State mainThreadState; 
    172207 
     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 
    173243    static size_t getAligned(size_t nbytes) pure nothrow { 
    174244        size_t rem = nbytes % alignBytes; 
     
    176246    } 
    177247 
    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; } 
    180253 
    181254        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]; 
    185259            nblocks++; 
    186260        } 
    187261 
    188262        state = stateCopy; 
    189         if(!thread_needLock
     263        if(!tnl()
    190264            mainThreadState = stateCopy; 
    191265        return stateCopy; 
     
    196270     * significant in some cases because it avoids a thread-local storage 
    197271     * lookup.  Also used internally.*/ 
    198     static State getState()
     272    static State getState() nothrow
    199273        // Believe it or not, even with builtin TLS, the thread_needLock() 
    200274        // is worth it to avoid the TLS lookup. 
    201         State stateCopy = (thread_needLock) ? 
     275        State stateCopy = (tnl()) ? 
    202276                          state : 
    203277                          mainThreadState; 
     
    209283     * freed when frameFree() is called.  Returns a reference to the 
    210284     * State class in case the caller wants to cache it for speed.*/ 
    211     static State frameInit()
     285    static State frameInit() nothrow
    212286        return frameInit(getState); 
    213287    } 
     
    215289    /**Same as frameInit() but uses stateCopy cached on stack by caller 
    216290     * to avoid a thread-local storage lookup.  Strictly a speed hack.*/ 
    217     static State frameInit(State stateCopy)
     291    static State frameInit(State stateCopy) nothrow
    218292        with(stateCopy) { 
    219293        if(totalAllocs == lastAlloc.length) // Should happen very infrequently. 
    220             lastAlloc.length = lastAlloc.length * 2
     294            doubleSize(lastAlloc)
    221295        lastAlloc[totalAllocs] = cast(void*) frameIndex; 
    222296        frameIndex = totalAllocs; 
     
    228302    /**Frees all memory allocated by TempAlloc since the last call to 
    229303     * frameInit().*/ 
    230     static void frameFree()
     304    static void frameFree() nothrow
    231305        frameFree(getState); 
    232306    } 
     
    234308    /**Same as frameFree() but uses stateCopy cached on stack by caller 
    235309    * to avoid a thread-local storage lookup.  Strictly a speed hack.*/ 
    236     static void frameFree(State stateCopy)
     310    static void frameFree(State stateCopy) nothrow
    237311        with(stateCopy) { 
    238312        while(totalAllocs > frameIndex + 1) { 
     
    244318 
    245319    /**Purely a convenience overload, forwards arguments to TempAlloc.malloc().*/ 
    246     static void* opCall(T...)(T args)
     320    static void* opCall(T...)(T args) nothrow
    247321        return TempAlloc.malloc(args); 
    248322    } 
     
    257331     * Do not store the only reference to a GC-allocated object in 
    258332     * TempAlloc-allocated memory.*/ 
    259     static void* malloc(size_t nbytes)
     333    static void* malloc(size_t nbytes) nothrow
    260334        return malloc(nbytes, getState); 
    261335    } 
     
    263337    /**Same as malloc() but uses stateCopy cached on stack by caller 
    264338    * 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
    266340        nbytes = getAligned(nbytes); 
    267341        with(stateCopy) { 
     
    271345                current.used += nbytes; 
    272346            } else if(nbytes > blockSize) { 
    273                 ret = GC.malloc(nbytes, GC.BlkAttr.NO_SCAN); 
     347                ret = cmalloc(nbytes); 
    274348            } else if(nfree > 0) { 
    275349                block* prev = freelist.prev; 
     
    282356                ret = current.space; 
    283357            } 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); 
    286361                nblocks++; 
    287362                newBlock.prev = current; 
     
    291366            } 
    292367            if(totalAllocs == lastAlloc.length) // Should happen very infrequently. 
    293                 lastAlloc.length = lastAlloc.length * 2
     368                doubleSize(lastAlloc)
    294369            lastAlloc[totalAllocs++] = ret; 
    295370            return ret; 
     
    301376     * there's no need to pass a pointer in.  All bookkeeping for figuring 
    302377     * out what to free is done internally.*/ 
    303     static void free()
     378    static void free() nothrow
    304379        free(getState); 
    305380    } 
     
    307382    /**Same as free() but uses stateCopy cached on stack by caller 
    308383    * to avoid a thread-local storage lookup.  Strictly a speed hack.*/ 
    309     static void free(State stateCopy)
     384    static void free(State stateCopy) nothrow
    310385        with(stateCopy) { 
    311386            void* lastPos = lastAlloc[--totalAllocs]; 
     
    314389            if(lastPos > current.space + blockSize || 
    315390               lastPos < current.space) { 
    316                GC.free(lastPos); 
     391               cfree(lastPos); 
    317392               return; 
    318393            } 
     
    333408                        block* last = freelist; 
    334409                        freelist = freelist.prev; 
    335                         GC.free(last.space); 
    336                         GC.free(last); 
     410                        cfree(last.space); 
     411                        cfree(last); 
    337412                        nfree--; 
    338413                    } 
     
    354429 * in an array allocated by newStack because this memory is not 
    355430 * scanned by the GC.*/ 
    356 T[] newStack(T)(size_t size)
     431T[] newStack(T)(size_t size) nothrow
    357432    size_t bytes = size * T.sizeof; 
    358433    T* ptr = cast(T*) TempAlloc.malloc(bytes); 
     
    362437/**Same as newStack(size_t) but uses stateCopy cached on stack by caller 
    363438* to avoid a thread-local storage lookup.  Strictly a speed hack.*/ 
    364 T[] newStack(T)(size_t size, TempAlloc.State state)
     439T[] newStack(T)(size_t size, TempAlloc.State state) nothrow
    365440    size_t bytes = size * T.sizeof; 
    366441    T* ptr = cast(T*) TempAlloc.malloc(bytes, state); 
     
    369444 
    370445/**Creates a duplicate of an array on the TempAlloc struct.*/ 
    371 auto tempdup(T)(T[] data)
     446auto tempdup(T)(T[] data) nothrow
    372447    alias Mutable!(T) U; 
    373448    U[] ret = newStack!(U)(data.length); 
     
    378453/**Same as tempdup(T[]) but uses stateCopy cached on stack by caller 
    379454 * to avoid a thread-local storage lookup.  Strictly a speed hack.*/ 
    380 auto tempdup(T)(T[] data, TempAlloc.State state)
     455auto tempdup(T)(T[] data, TempAlloc.State state) nothrow
    381456    alias Mutable!(T) U; 
    382457    U[] ret = newStack!(U)(data.length, state); 
  • trunk/cor.d

    r5 r17  
    33 * Author:  David Simcha 
    44 * 
    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.*/ 
    2932 
    3033module dstats.cor; 
  • trunk/distrib.d

    r5 r17  
    3939 * 
    4040 * normalCDF <=> normalDistribution 
     41 * 
    4142 * normalCDFR <=> normalDistributionCompl 
     43 * 
    4244 * invNormalCDF <=> normalDistributionComplInv 
     45 * 
    4346 * studentsTCDF <=> studentsTDistribution  (Note reversal in argument order) 
     47 * 
    4448 * invStudentsTCDF <=> studentsTDistributionInv (Again, arg order reversed) 
     49 * 
    4550 * binomialCDF <=> binomialDistribution 
     51 * 
    4652 * negBinomCDF <=> negativeBinomialDistribution 
     53 * 
    4754 * poissonCDF <=> poissonDistribution 
     55 * 
    4856 * chiSqrCDF <=> chiSqrDistribution 
     57 * 
    4958 * chiSqrCDFR <=> chiSqrDistributionCompl 
     59 * 
    5060 * invChiSqCDFR <=> chiSqrDistributionComplInv 
     61 * 
    5162 * fisherCDF <=> fDistribution 
     63 * 
    5264 * fisherCDFR <=> fDistributionCompl 
     65 * 
    5366 * invFisherCDFR <=> fDistributionComplInv 
     67 * 
    5468 * gammaCDF <=> gammaDistribution  (Note arg reversal) 
     69 * 
    5570 * gammaCDFR <=> gammaDistributionCompl  (Note arg reversal) 
    5671 * 
    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.*/ 
    85103 
    86104module dstats.distrib; 
     
    745763 * Normal probability density function (integrated from 
    746764 * minus infinity to x) is equal to p. 
    747  * 
    748  * For small arguments 0 < p < exp(-2), the program computes 
    749  * z = sqrt( -2 log(p) );  then the approximation is 
    750  * 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 . 
    753765 */ 
    754766real invNormalCDF(real p, real mean = 0, real sd = 1) 
  • trunk/random.d

    r5 r17  
    44 * Author:  David Simcha 
    55 * 
    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.*/ 
    3033 
    3134 
  • trunk/sort.d

    r12 r17  
    1616 * assert(foo == [1, 2, 3, 4, 5]); 
    1717 * 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); 
    1920 * assert(bar == [8, 7, 6, 5, 3]); 
    2021 * assert(foo == [3, 2, 1, 4, 5]); 
     22 * assert(baz == [-1.0, 0, 1, -2, -3]); 
    2123 * --- 
    2224 * 
    2325 * Author:  David Simcha 
    2426 * 
    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.*/ 
    4954 
    5055module dstats.sort; 
     
    6368} 
    6469 
    65 private void rotateLeft(T)(T[] input) { 
     70void rotateLeft(T)(T[] input) { 
    6671    if(input.length < 2) return; 
    6772    T temp = input[0]; 
     
    7277} 
    7378 
    74 private void rotateRight(T)(T[] input) { 
     79void rotateRight(T)(T[] input) { 
    7580    if(input.length < 2) return; 
    7681    T temp = input[$-1]; 
     
    116121} 
    117122 
    118 /**Quick sort.  Unstable, O(N log N) time average, O(N^2) time worst 
     123/**Quick sort.  Unstable, O(N log N) time average, worst 
    119124 * case, O(log N) space, small constant term in time complexity. 
    120125 * 
    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
    123128 * 
    124129 * 1.  At each recursion, the median of the first, middle and last elements of 
     
    131136 * 
    132137 * 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.*/ 
    134140T[0] qsort(alias compFun = lessThan, T...)(T data) 
    135141in { 
     
    366372 * and U is a ulong* for calculating bubble sort distance, this can be called 
    367373 * 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 */ 
    369391T[0] mergeSortTemp(alias compFun = lessThan, T...)(T data) 
    370392in { 
     
    487509} 
    488510 
    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
    490512 * time complexity, O(1) space complexity, stable.  Much slower than plain 
    491513 * old mergeSort(), so only use it if you really need the O(1) space.*/ 
     
    686708} 
    687709 
    688 /**Insertion sort.  O(N^2) time worst, average case, O(1) space, VERY small 
    689  * constant, which is why it's useful for sorting small subarrays in 
     710/**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 
    690712 * divide and conquer algorithms.  If last argument is a ulong*, increments 
    691713 * the dereference of this argument by the bubble sort distance between the 
     
    794816} 
    795817 
    796 /**Given a set of data points entered through the addElement function, 
    797  * maintains the invariant that the top N according to compFun will be 
    798  * contained in the data structure.  Uses a heap internally, O(log N) insertion 
    799  * time.  Good for finding the top N elements of a very large dataset that 
    800  * cannot be sorted quickly in its entirety.  If less than N datapoints 
    801  * 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 to 
    849      * 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 a 
    855      * 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  
    895818/**Returns the kth largest/smallest element (depending on compFun, 0-indexed) 
    896819 * in the input array in O(N) time.  Allocates memory, does not modify input 
     
    920843 * --- 
    921844 * auto foo = [3, 1, 5, 4, 2].dup; 
    922  * auto secondElem = partitionK(foo, 1); 
    923  * assert(secondElem == 2); 
     845 * auto secondSmallest = partitionK(foo, 1); 
     846 * assert(secondSmallest == 2); 
    924847 * foreach(elem; foo[0..1]) { 
    925848 *     assert(elem <= foo[1]); 
    926849 * } 
    927850 * foreach(elem; foo[2..$]) { 
    928  *     assert(elem >= foo); 
     851 *     assert(elem >= foo[1]); 
    929852 * } 
    930  * 
    931  * Returns:  The kth element of the array.*/ 
     853 * --- 
     854 * 
     855 * Returns:  The kth element of the array. 
     856 */ 
    932857ArrayElemType!(T[0]) partitionK(alias compFun = lessThan, T...)(T data, int k) 
    933858in { 
     
    1022947} 
    1023948 
     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 */ 
     973struct TopN(T, alias compFun = greaterThan) { 
     974private: 
     975    alias binaryFun!(compFun) comp; 
     976    uint n; 
     977    uint nAdded; 
     978 
     979    T[] nodes; 
     980public: 
     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 
     1016unittest { 
     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 
    10241050// Verify that there are no TempAlloc memory leaks anywhere in the code covered 
    10251051// by the unittest.  This should always be the last unittest of the module. 
     
    10291055    assert(TAState.nblocks < 2); 
    10301056} 
     1057 
  • trunk/stats.d

    r5 r17  
    33 * Author:  David Simcha 
    44 * 
    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.*/ 
    2932 
    3033module dstats.stats; 
  • trunk/summary.d

    r5 r17  
    77 * Author:  David Simcha 
    88 * 
    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.*/ 
    3336 
    3437 
     
    169172    } 
    170173 
     174    /// 
    171175    string toString() { 
    172176        return to!(string)(mean); 
     
    268272    } 
    269273 
     274    /// 
    270275    string toString() { 
    271276        return format("N = ", cast(ulong) _k, "\nMean = ", mean, "\nVariance = ", 
     
    422427    } 
    423428 
     429    /// 
    424430    string toString() { 
    425431        return format("N = ", cast(ulong) _k, "\nMean = ", mean, "\nVariance = ", 
     
    496502    real max; 
    497503 
     504    /// 
    498505    string toString() { 
    499506        return format("N = ", N, "\nMean = ", mean, "\nVariance = ", 
     
    504511 
    505512/**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.*/ 
    507514Summary summary(T)(const T[] data) { 
    508515    OnlineSummary summ; 
  • trunk/tests.d

    r5 r17  
    33 * Author:  David Simcha 
    44 * 
    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.*/ 
    2932module dstats.tests; 
    3033