home *** CD-ROM | disk | FTP | other *** search
- /*
- * high speed load of ordered key values
- *
- * This program is the CONFIDENTIAL and PROPRIETARY property
- * of FairCom(R) Corporation. Any unauthorized use, reproduction or
- * transfer of this program is strictly prohibited.
- *
- * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989 FairCom Corporation
- * (Subject to limited distribution and
- * restricted disclosure only.)
- * *** ALL RIGHTS RESERVED ***
- *
- * 4006 West Broadway
- * Columbia, MO 65203
- *
- *
- * c-tree(R) Version 4.3
- * Release C
- * February 7, 1989 17:30
- *
- */
-
- #include "ctstdr.h" /* standard i/o header */
- #include "ctoptn.h" /* c-tree configuration options */
- #include "cterrc.h" /* c-tree error codes */
- #include "ctstrc.h" /* c-tree data structures */
- #include "ctgvar.h" /* c-tree global variables */
-
- COUNT uerr(),putnod(),compar(),delexp(),tstupd();
- TREEBUFF *newnod(),*getnod();
- POINTER LSTKEY();
- VOID prpdup(),insexp();
- TEXT *valpnt(),*hghpnt();
-
- LOCAL TEXT lastkey[MAXLEN];
- LOCAL LONG begnod,curnod,prvnod,numnod;
- LOCAL LONG ct_nnods[MAXLEV];
- LOCAL COUNT elem;
- LOCAL COUNT started = -1;
- LOCAL TREEBUFF *savbp;
-
- TREEBUFF *lodins(buffer,idxval,recbyt,knum)
- PFAST TREEBUFF *buffer;
- TEXT *idxval;
- LONG recbyt;
- PFAST CTFILE *knum;
- {
- FAST TREEBUFF *lbp;
- COUNT leaflg;
-
- VOID newleaf(),nonleaf();
-
- ct_tky = 0;
- if (elem)
- valpnt(buffer,elem);
- insexp(buffer,idxval,recbyt);
- buffer->nkv++;
-
- if (buffer->nkb > buffer->maxb) {
- leaflg = buffer->leaf == LEAF ? YES : NO;
- valpnt(buffer,buffer->nkv);
- delexp(buffer);
- if (leaflg)
- cpybuf(hghpnt(buffer),lastkey,knum->length);
- putnod(buffer,buffer->nkv);
- prvnod = curnod;
- if ((savbp = buffer = newnod(knum,&curnod,NO)) == NULL)
- return(NULL);
- if (leaflg)
- newleaf(buffer,knum);
- else
- nonleaf(buffer,knum);
- ct_tkp = elem = 0;
- numnod++;
- insexp(buffer,idxval,recbyt);
- buffer->nkv++;
- if ((lbp = getnod(prvnod,knum)) == NULL)
- return(NULL);
- lbp->sucesr = curnod;
- putnod(lbp,lbp->nkv);
- if ((buffer = getnod(curnod,knum)) == NULL)
- return(NULL);
- }
-
- putnod(buffer,buffer->nkv);
- elem++;
- cpybuf(lastkey,idxval,knum->length);
- return(buffer);
- }
-
- TREEBUFF *skim(knum)
- PFAST CTFILE *knum;
- {
- FAST TREEBUFF *buffer;
- LONG lnods,nnods;
-
- LONG nodpnt(),gtroot();
-
- btlev = 0;
- begnod = curnod = gtroot(knum);
- while (curnod) {
- prvnod = DRNZERO;
- nnods = 0L;
- do {
- if ((buffer = getnod(curnod,knum)) == NULL)
- return(NULL);
- nnods++;
- if (nnods == 1)
- lnods = curnod;
- prvnod = curnod;
- curnod = buffer->sucesr;
- } while (curnod);
- if (buffer->leaf == LEAF)
- break;
- if (++btlev >= MAXLEV)
- terr(247);
- ct_npath[btlev] = prvnod;
- ct_nnods[btlev] = nnods;
- if ((buffer = getnod(lnods,knum)) == NULL)
- return(NULL);
- curnod = nodpnt(buffer,1);
- }
-
- if ((begnod = curnod = prvnod) == DRNZERO)
- terr(248);
-
- numnod = nnods;
- return(getnod(curnod,knum));
- }
-
- COUNT LOADKEY(keyno,target,recbyt,typadd)
- COUNT keyno;
- TEXT *target;
- POINTER recbyt;
- COUNT typadd;
- {
- FAST CTFILE *knum;
- FAST TREEBUFF *buffer;
- TREEBUFF *lbp;
- TEXT idxval[MAXLEN];
- TEXT *tp;
- LONG lnod;
-
- VOID newleaf(),nonleaf();
- CTFILE *tstfnm();
-
- uerr_cod = NO_ERROR;
- if ((knum = tstfnm(keyno)) == NULL)
- return(uerr_cod);
- else if (knum->clstyp != IDX_CLOSE)
- return(uerr(FMOD_ERR));
-
- if (typadd != BLDADD) {
- cpybuf(idxval,target,knum->length);
- if (knum->autodup == DUPKEY)
- prpdup(idxval,knum,&recbyt);
- }
-
- if (typadd == NXTADD) {
- /* additional key value */
- if (started != keyno)
- return(uerr(KLOD_ERR));
- buffer = savbp;
- if ((ct_tkp = compar(idxval,valpnt(buffer,elem),knum)) <= 0)
- return(uerr(KLOR_ERR));
- } else if (typadd == FRSADD) {
- /* first key value */
- if (started != -1)
- return(uerr(KLOD_ERR));
- else if (LSTKEY(keyno,lastkey) == DRNZERO) {
- /* start with empty tree */
- if (uerr_cod)
- return(uerr_cod);
- elem = ct_tkp = 0;
- prvnod = DRNZERO;
- if ((savbp = buffer = newnod(knum,&begnod,NO)) == NULL)
- return(uerr_cod);
- curnod = begnod;
- numnod = 1L;
- newleaf(buffer,knum);
- started = keyno;
- } else if (compar(idxval,lastkey,knum) > 0) {
- /* adding to end of tree */
- if ((savbp = buffer = skim(knum)) == NULL)
- return(uerr_cod);
- elem = buffer->nkv;
- if (elem)
- ct_tkp = compar(idxval,lastkey,knum);
- else
- ct_tkp = 0;
- started = keyno;
- if (tstupd(knum))
- return(uerr_cod);
- } else
- return(uerr(KLOR_ERR));
- } else if (typadd == BLDADD) {
- /* no more key values */
- if (started != keyno)
- return(uerr(KLOD_ERR));
- if ((buffer = getnod(curnod,knum)) == NULL)
- return(uerr_cod);
- for (tp = hghpnt(buffer), elem = 0; elem++ < knum->length; )
- *tp++ = 0xff;
- putnod(buffer,buffer->nkv);
- for ( ;; ) {
- if (numnod == 1) {
- knum->root = begnod;
- if (wrthdr(knum) || tstupd(knum))
- return(uerr_cod);
- else {
- started = -1;
- return(NO_ERROR);
- }
- }
- lnod = begnod;
- if (btlev > 0) {
- numnod = ct_nnods[btlev];
- begnod = ct_npath[btlev--];
- if ((buffer = getnod(begnod,knum)) == NULL)
- return(uerr_cod);
- if (buffer->sucesr != DRNZERO) terr(249);
- valpnt(buffer,buffer->nkv);
- delexp(buffer);
- putnod(buffer,buffer->nkv);
- cpybuf(lastkey,valpnt(buffer,elem = buffer->nkv),
- buffer->klen);
- } else {
- elem = ct_tkp = 0;
- numnod = 1;
- if ((buffer = newnod(knum,&begnod,NO)) == NULL)
- return(uerr_cod);
- nonleaf(buffer,knum);
- }
- curnod = begnod;
- do {
- if ((lbp = getnod(lnod,knum)) == NULL)
- return(uerr_cod);
- if (lbp->leaf == LEAF)
- cpybuf(idxval,hghpnt(lbp),knum->length);
- else
- cpybuf(idxval,valpnt(lbp,lbp->nkv),
- knum->length);
- if (elem != 0)
- ct_tkp = compar(idxval,lastkey,knum);
- if ((buffer = getnod(curnod,knum)) == NULL)
- return(uerr_cod);
- if ((buffer = lodins(buffer,idxval,lnod,knum)) == NULL)
- return(uerr_cod);
- lnod = lbp->sucesr;
- } while (lnod);
- }
- }
-
- if (lodins(buffer,idxval,recbyt,knum) == NULL || tstupd(knum))
- return(uerr_cod);
- knum->nument++;
- return(NO_ERROR);
- }
-
- VOID newleaf(bp,kp)
- PFAST TREEBUFF *bp;
- PFAST CTFILE *kp;
- {
- bp->leaf = LEAF;
- bp->maxkv = kp->maxkvl;
- bp->maxb = kp->maxkbl;
- bp->sucesr = DRNZERO;
- bp->predsr = prvnod;
- if (kp->autodup)
- bp->confg = DUPLEAF;
- else
- bp->confg = REGULAR;
- }
-
- VOID nonleaf(bp,kp)
- PFAST TREEBUFF *bp;
- PFAST CTFILE *kp;
- {
- bp->leaf = NOLEAF;
- bp->maxkv = kp->maxkvn;
- bp->maxb = kp->maxkbn;
- bp->sucesr = DRNZERO;
- bp->predsr = DRNZERO;
- if (kp->autodup)
- bp->confg = DUPNONL;
- else
- bp->confg = REGULAR;
- }
-
- /* end of ctload.c */
-