⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dkmrsort.c

📁 roots--一个轻量级的内存数据库系统。基于Hash Map的table设计。快速轻巧。
💻 C
字号:
/*
Copyright (c) 2003, Dan Kranz and Arnold Rom
All rights reserved.

Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:

    * Redistributions of source code must retain the above
      copyright notice, this list of conditions and the following
      disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials
      provided with the distribution.

    * The names of its contributors may not be used to endorse or
      promote products derived from this software without specific
      prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <roots.h>

// Compare function used for database sorting

int armrCompare(const void *e1, const void *e2, size_t len, short type)
{
   double rnum1, rnum2;
   long num1, num2;
   short datatype;
   int rc;
   static long v[2]={1,0};
   static long n1=1;

   datatype=type;
   if (datatype < 0)
      datatype = -type;

   switch (datatype) {
   case B_TYPE:
   case D_TYPE:
   case Z_TYPE:
      v[1]=len;
      bunpac((void*)e1,v,&num1,&n1);
      bunpac((void*)e2,v,&num2,&n1);
      if (num1 < num2)
         rc = -1;
      else if (num1==num2)
         rc = 0;
      else
         rc = 1;
      break;
   case R_TYPE:
      v[1]=len;
      runpac((void*)e1,v,&rnum1,&n1);
      runpac((void*)e2,v,&rnum2,&n1);
      if (rnum1 < rnum2)
         rc = -1;
      else if (rnum1==rnum2)
         rc = 0;
      else
         rc = 1;
      break;
   default:
      rc = memcmp(e1,e2,len);
   }

   if (type < 0)
      return -rc;
   return rc;
}


// Compare function used for sorting tables

int tableCompare(const void *s1, const void *s2, size_t len, short type)
{
   int rc;
#if defined (WIN32)
   rc = CompareString(LOCALE_USER_DEFAULT,0,(char*)s1,(int)len,(char*)s2,(int)len);
   rc -= 2;
#else
   // This code requires testing.
   // I tried to code it so it behaves the same as WIN32.
   rc = strnicoll(s1,s2,len);
   if (rc==0) {
      rc = memcmp(s1,s2,len);
      rc = -rc;
   }
#endif
   return rc;
}


// Compare function used by chsort

int memoryCompare(const void *s1, const void *s2, size_t len, short dummy)
{
   return memcmp(s1,s2,len);
}


// Arnold Rom's MicroArmr sort algorithm, natural merge sort using lists
// Output array sorti contains the sort sequence.

BOOL mrsort(void *arg_block, long *cpl, long *nline,
            long *sorti, long *fields, short *types, long *nf,
            int (*compare)(const void *el, const void *e2, size_t count, short type))
{
   long a, b, last;
   long ngroup, pair, npairs;
   unsigned short *fptr, *lptr;
   ShortArray lnextl={NULL,0}, group={NULL,0};
   BYTE *block=arg_block, *apt, *bpt;
   long *fld;
   int j, rc;

   if (
      *cpl < 1        ||
      *nline < 0      ||
      *nline > MAXLIN ||
      *nf < 1 )
   {
      RootsSOS("mrsort() sos!");
      return FALSE;
   }

   if (*nline==0)
      return TRUE;

   // Only one line?
   if (*nline==1) {
      *sorti=1;
      return TRUE;
   }

   ExpandShort(lnextl,*nline);
   ExpandShort(group,*nline);

   // Adjust arrays for relative zero addressing
   block -= *cpl;
   --block;
   --lnextl.array;

// --------------------------------------
//    Derive initial natural groupings
// --------------------------------------

   // Define very first group
   ngroup=1;
   *(group.array)=1;
   *(lnextl.array+1)=0;

   fptr=group.array;
   lptr=lnextl.array+1;

   for (a=1, b=2; b <= *nline; ++a, ++b) {

      // Compare line b with line a
      apt = block + (*cpl * a);
      bpt = block + (*cpl * b);
      for (j=0, fld=fields; j<*nf; j++, fld+=2) {
         if ((rc = (*compare)(bpt+*(fld),apt+*(fld),*(fld+1),types[j])) != 0)
            break;
      }

      // New group
      if (rc < 0) {
         *lptr++ = 0;
         ++ngroup;
         *++fptr = (unsigned short) b;
      }

      // Same group
      else
         *lptr++ = (unsigned short) b;
   }
   *lptr = 0;

// -----------------
//    Merge Phase
// -----------------

   for (npairs=ngroup/2; npairs > 0; npairs=ngroup/2) {

      fptr = group.array;
      --fptr;

      // Merge group A with group B
      for (pair=0; pair < npairs; ++pair) {

         a = *++fptr;
         b = *++fptr;

         // Initial compare
         apt = block + (*cpl * a);
         bpt = block + (*cpl * b);
         for (j=0, fld=fields; j<*nf; j++, fld+=2) {
            if ((rc = (*compare)(bpt+*(fld),apt+*(fld),*(fld+1),types[j])) != 0)
               break;
         }

         // B is the 1st member of a new list
         if (rc < 0) {
            *(group.array+pair) = (unsigned short) b;
            last = b;
            b = *(lnextl.array+b);

            // No more B?  Append the rest of A
            if (b==0) {
               *(lnextl.array+last) = (unsigned short) a;
               continue;
            }
         }

         // A is the 1st member of a new group
         else {
            *(group.array+pair) = (unsigned short) a;
            last = a;
            a = *(lnextl.array+a);

            // No more A?  Append the rest of B
            if (a==0) {
               *(lnextl.array+last) = (unsigned short) b;
               continue;
            }
         }

         // More compares
         for (;;) {

            apt = block + (*cpl * a);
            bpt = block + (*cpl * b);
            for (j=0, fld=fields; j<*nf; j++, fld+=2) {
               if ((rc = (*compare)(bpt+*(fld),apt+*(fld),*(fld+1),types[j])) != 0)
                  break;
            }

            // Append b to an established group
            if (rc < 0) {

               *(lnextl.array+last) = (unsigned short) b;
               last = b;
               b = *(lnextl.array+b);

               // No more B?
               if (b==0) {
                  *(lnextl.array+last) = (unsigned short) a;
                  break;
               }
            }

            // Append a to an established group
            else {
               *(lnextl.array+last) = (unsigned short) a;
               last = a;
               a = *(lnextl.array+a);

               // No more A?
               if (a == 0) {
                  *(lnextl.array+last) = (unsigned short) b;
                  break;
               }
            }
         }

      } // Next group pair

      // Reset ngroup for next pass

      // There was an unused group.  Add the odd group to new groups
      if (ngroup%2) {
         *(group.array+npairs) = *(group.array+ngroup-1);
         ngroup = npairs+1;
      }
      
      // Groups paired evenly
      else
         ngroup = npairs;

   } // Next merge pass

   // Derive sorti
   for (a = *(group.array); a!=0; a = *(lnextl.array+a))
      *sorti++ = a;

   FreeArray(group);
   ++lnextl.array;
   FreeArray(lnextl);

   return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -