rs-rasp.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann/***********************************************************************
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann* This software is part of the ast package *
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann* Copyright (c) 1996-2011 AT&T Intellectual Property *
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann* and is licensed under the *
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann* Eclipse Public License, Version 1.0 *
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann* by AT&T Intellectual Property *
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann* A copy of the License is available at *
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann* http://www.eclipse.org/org/documents/epl-v10.html *
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann* Information and Software Systems Research *
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann* AT&T Research *
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann* Florham Park NJ *
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann* Phong Vo <kpv@research.att.com> *
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann* Glenn Fowler <gsf@research.att.com> *
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann***********************************************************************/
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann/* Radix + splay tree
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann** 1. Records are partitioned by first bytes.
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann** 2. Short records are sorted by a radix sort.
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann** 3. Long records are sorted in splay trees.
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann** 4. A final merge phase put things back together.
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann** Written by Kiem-Phong Vo (07/08/96).
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann { if((mp = (cr = one->keylen) - two->keylen) > 0) cr -= mp; \
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann else if((cr = (int)*o++ - (int)*t++)) break; \
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmannstatic int raspinsert(Rs_t* rs, reg Rsobj_t* obj)
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann reg Rsrasp_t* rasp = (Rsrasp_t*)rs->methdata;
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann obj->order = (((ulong)k[1]) << (CHAR_BIT*7)) |
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann#else /* sizeof(long) == 4 */
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann obj->order = (k[1] << (CHAR_BIT*3)) | (k[2] << (CHAR_BIT*2)) |
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann (k[3] << (CHAR_BIT*1)) | (k[4] << (CHAR_BIT*0)) ;
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann for(l = r = &link;; )
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann else if(cr == 0)
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann else /*if(cr > 0)*/
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann else if(cr == 0)
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann reg Rsobj_t **bin, *t, *endl, *next, **lo, **maxpart;
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann for(lo = part, maxpart = part + UCHAR_MAX+1; lo < maxpart; ++lo)
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann { next = work->left->right; work->left->right = NIL(Rsobj_t*);
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann { if((r = *bin) )
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann work = *lo; t = work->left; *lo = NIL(Rsobj_t*);
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann { if((r = *bin) )
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann for(work = work->right; work; work = work->right)
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann#define CHARCMP(k1,k2,v,i) (v = (int)k1[i] - (int)k2[i])
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann { for(i = 2; i <= n; ++i) \
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmannstatic Rsobj_t* listmerge(reg Rsobj_t* one, reg Rsobj_t* two, reg int n)
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann endone = one->left; one->left->right = NIL(Rsobj_t*);
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann endtwo = two->left; two->left->right = NIL(Rsobj_t*);
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann /* find smallest element and make it head of list */
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann while((t = r->left) )
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann /* flatten tree */
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann for(list = p = r, r = r->right;; p = r, r = r->right)
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann else if((t = r->left) )
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann while((t = r->left) );
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann reg Rsrasp_t* rasp = (Rsrasp_t*)rs->methdata;
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann list = endl = rasp->empty; rasp->empty = NIL(Rsobj_t*);
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann for(n = 0, t = NIL(Rsobj_t*); n <= UCHAR_MAX; ++n)
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann t = t ? listmerge(r,t,e) : r;
2450a4210dee64b064499a3a1154129bdfc74981Daniel Hausmann { if((r->right = t) )
return list;
{ raspinsert,
sizeof(Rsrasp_t),
#ifdef NoF