home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / reviewed / volume03 / chipset2 / 05 < prev    next >
Encoding:
Text File  |  1993-03-13  |  31.6 KB  |  1,199 lines

  1. From decwrl!athertn!sander.cupertino.ca.us!paul@cs.purdue.edu Wed Jan  6 13:53:24 EST 1993
  2. Submit chipset-2 05/10 
  3. #! /bin/sh
  4. # This is a shell archive.  Remove anything before this line, then unpack
  5. # it by saving it into a file and typing "sh file".  To overwrite existing
  6. # files, type "sh file -c".  You can also feed this as standard input via
  7. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  8. # will see the following message at the end:
  9. #        "End of archive 5 (of 10)."
  10. # Contents:  common.mk src/btree/btdelete.c src/btree/btdelrank.c
  11. #   src/btree/btdelutl.c src/btree/btdump.c src/btree/btnew.c
  12. # Wrapped by paul@sander on Sun Nov 22 15:41:51 1992
  13. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  14. if test -f common.mk -a "${1}" != "-c" ; then 
  15.   echo shar: Will not over-write existing file \"common.mk\"
  16. else
  17. echo shar: Extracting \"common.mk\" \(6426 characters\)
  18. sed "s/^X//" >common.mk <<'END_OF_common.mk'
  19. X# common.mk -- contains common make macros that are used the Makefiles
  20. X#              included with the Software ChipSet.  Please edit this file
  21. X#              as needed for your system.
  22. X
  23. X# This file is part of a suite of programs called Software Chipset.
  24. X# The source code for Software Chipset has been released into the
  25. X# public domain by its author, Paul Sander.
  26. X
  27. XSHELL=/bin/sh
  28. X
  29. X################
  30. X
  31. X# This is the default list of components that are built and installed,
  32. X# or installed into an archive when a new distribution is built.
  33. X
  34. XCOMPONENTS=`/bin/ls ./src`
  35. X
  36. X################
  37. X
  38. X# The following macros describe the staging that is used when the top-level
  39. X# makefile builds the entire Software ChipSet distribution.  This staging
  40. X# area provides a means to test the all of the components while keeping
  41. X# them isolated from a live system.  All of these macros must specify
  42. X# fullpaths.  It would be a bad idea to put the staging area in the
  43. X# directory tree rooted at "`pwd`/src".
  44. X
  45. X# STAGE is the root of a test staging area.  The full component library is
  46. X# built in the directory hierarchy rooted here, then installed into a
  47. X# live system from here.
  48. X
  49. XSTAGE=/tmp/ChipSet
  50. X
  51. X# The following macros specify where Software ChipSet public header files,
  52. X# libraries, and documentation go, and also specify working areas that are
  53. X# used when building the libraries and distribution media.  Each of these
  54. X# macros are used directly by the build procedures for the Software ChipSet.
  55. X# Note that these need not mirror the organization of the filesystem into
  56. X# which the component libraries are ultimately installed.
  57. X
  58. X# Public header files go here.
  59. X
  60. XSTAGEINC=$(STAGE)/include
  61. X
  62. X# Library archive files go here.
  63. X
  64. XSTAGELIB=$(STAGE)/lib
  65. X
  66. X# Documentation goes here.
  67. X
  68. XSTAGEMAN=$(STAGE)/man
  69. X
  70. X# Temporary work area.  This must be on the same physical device as
  71. X# the directory containing this file if new distributions of the Software
  72. X# ChipSet are made from your site.
  73. X
  74. XSTAGETMP=$(STAGE)/tmp
  75. X
  76. X# Distribution media placed here.
  77. X
  78. XSTAGEINST=$(STAGE)/install
  79. X
  80. X
  81. X################
  82. X
  83. X# The following macros describe the filesystem into which the Software
  84. X# ChipSet is ultimately installed.  This is the live system in general
  85. X# use.  All of these macros must specify fullpaths.
  86. X
  87. X# This is where the components are installed for production use.
  88. X
  89. XDEST=/usr/local
  90. X
  91. X# This is where public header files go.
  92. X
  93. XDESTINC=$(DEST)/include
  94. X
  95. X# This is where the libchipset.a file goes.
  96. X
  97. XDESTLIB=$(DEST)/lib
  98. X
  99. X# This is where the man pages are.  Software ChipSet man pages are
  100. X# installed in a man3 directory contained by this directory.
  101. X
  102. XDESTMAN=$(DEST)/man
  103. X
  104. X
  105. X################
  106. X
  107. X# The following macros are used to describe the development area in
  108. X# which components are built and tested in isolation from a live system.
  109. X# These macros are used by the individual makefiles within the component
  110. X# source directories themselves, and not by the top-level makefile that
  111. X# installs ALL of the components into a single staging area.  Though these
  112. X# macros need not have the same values as those for the staging area, they
  113. X# typically are out of convenience; this is because it allows a programmer
  114. X# to invoke "make" in each of the component source directories and at the
  115. X# top-level and get similar results.  Again, these must all be fullpaths.
  116. X
  117. X# The root of the developer's installation area.  This is not used directly,
  118. X# but may be used to compute the locations of needed directories.
  119. X
  120. XROOT=$(STAGE)
  121. X
  122. X# The location in which public header files are installed.
  123. X
  124. XINCDIR=$(STAGEINC)
  125. X
  126. X# The location in which component libraries are installed.
  127. X
  128. XLIBDIR=$(STAGELIB)
  129. X
  130. X# The location in which man pages are installed.
  131. X
  132. XMANDIR=$(STAGEMAN)
  133. X
  134. X################
  135. X
  136. X# These are directives to the compiler and linker to specify search
  137. X# paths for needed files when components are built.
  138. X
  139. XINCLUDES=-I$(INCDIR)
  140. XLIBSEARCH=-L$(LIBDIR)
  141. X
  142. X################
  143. X
  144. X# The following macros are used when the native C compiler is used.  The
  145. X# COVERAGE macro causes the components to write message to stdout that
  146. X# indicate decisions made.  This feature is useful for debugging the
  147. X# components, and should be turned off when the components are installed.
  148. X
  149. X#CC = cc $(INCLUDES)
  150. X#CFLAGS = -O
  151. X#CFLAGS = -g -DCOVERAGE
  152. X#LD = cc $(LIBSEARCH)
  153. X#LFLAGS =
  154. X
  155. X# The following macros are used when the Gnu C compiler is used.
  156. X
  157. XCC = gcc $(INCLUDES)
  158. XCFLAGS = -O -ansi -pedantic
  159. X#CFLAGS = -g -DCOVERAGE -ansi -pedantic
  160. X#CFLAGS = -g -ansi -pedantic
  161. XLD = gcc $(LIBSEARCH)
  162. XLFLAGS =
  163. X
  164. X################
  165. X
  166. X# This macro lists all of the special libraries with which the test programs
  167. X# must be linked.  Presently, none are really needed, but libmalloc often
  168. X# provides a better memory allocator than libc.a .
  169. X
  170. XTESTLIBS=
  171. X#TESTLIBS=-lmalloc
  172. X
  173. X################
  174. X
  175. X# Manpage suffix after installation
  176. X
  177. XMANSUFF=3l
  178. X
  179. X################
  180. X
  181. X# Set this to your ranlib program; must specify fullpath.  If you do not
  182. X# have ranlib, leave this as-is.
  183. X
  184. XRANLIB=/bin/ranlib
  185. X
  186. X################
  187. X
  188. X# Set this appropriately to produce PostScript output.  Must be a filter.
  189. X
  190. XPSROFF=psroff -t -man
  191. X#PSROFF=troff -Tpsc -man | psdit
  192. X
  193. X################
  194. X
  195. X# Set this appropriately to produce ASCII plaintext.  Must be a filter.
  196. X
  197. XNROFF=nroff -man | col -b -x
  198. X#NROFF=nroff -man
  199. X
  200. X################
  201. X
  202. X# This compresses ASCII plaintext files for some systems.  COMPRESS must
  203. X# be a filter.
  204. X
  205. XCOMPRESS=compress
  206. XZSUFF=.Z
  207. X#COMPRESS=/bin/cat
  208. X#ZSUFF=
  209. X
  210. X################
  211. X
  212. X# These macros describe how man pages are processed prior to installation.
  213. X# Some systems prefer Troff source to be installed in /usr/man/whatever,
  214. X# while others prefer plaintext to be installed in /usr/catman/whatever.
  215. X# Still others want compressed plaintext.  And they figure out what type
  216. X# of file they have by looking at the file's extension.  So, INSTALLMAN
  217. X# specifies a filter that processes Troff source to produce the proper
  218. X# file for the "man" command, and INSTALLSUFF specifies the proper
  219. X# extension for the file after processing.  INSTALLSUFF is appended to
  220. X# MANSUFF when the man pages are processed.
  221. X
  222. XINSTALLMAN=$(NROFF) | $(COMPRESS)
  223. XINSTALLSUFF=$(ZSUFF)
  224. X
  225. X#INSTALLMAN=/bin/cat
  226. X#INSTALLSUFF=
  227. X
  228. X################
  229. X
  230. X# Default rules
  231. X
  232. X.SUFFIXES: .$(MANSUFF) .man .doc .ps
  233. X
  234. X# Format man pages
  235. X
  236. X.man.$(MANSUFF):
  237. X    < $*.man $(NROFF) > $*.$(MANSUFF)
  238. X
  239. X.man.doc:
  240. X    < $*.man $(NROFF) > $*.doc
  241. X
  242. X.man.ps:
  243. X    < $*.man $(PSROFF) > $*.ps
  244. X
  245. X################
  246. X
  247. X# Default target, just in case
  248. X
  249. Xall:
  250. X
  251. X### End of File ###
  252. X
  253. END_OF_common.mk
  254. if test 6426 -ne `wc -c <common.mk`; then
  255.     echo shar: \"common.mk\" unpacked with wrong size!
  256. fi
  257. # end of overwriting check
  258. fi
  259. if test -f src/btree/btdelete.c -a "${1}" != "-c" ; then 
  260.   echo shar: Will not over-write existing file \"src/btree/btdelete.c\"
  261. else
  262. echo shar: Extracting \"src/btree/btdelete.c\" \(4533 characters\)
  263. sed "s/^X//" >src/btree/btdelete.c <<'END_OF_src/btree/btdelete.c'
  264. X/********************************************************************
  265. X *
  266. X * btdelete.c -- This file contains functions needed to delete a key
  267. X *               from an in-memory B-tree.
  268. X *
  269. X * This file is part of a suite of programs called Software Chipset.
  270. X * The source code for Software Chipset has been released into the
  271. X * public domain by its author, Paul Sander.
  272. X *
  273. X ********************************************************************/
  274. X
  275. X#include <stdio.h>
  276. X#include "btpriv.h"
  277. X
  278. X/********************************************************************
  279. X *
  280. X * bt_delNode -- This function searches a sub-tree for a key, and
  281. X *               deletes the key.  If the key is found in a leaf node,
  282. X *               it is simply removed and returned to the caller;
  283. X *               otherwise, it is swapped with its predecessor, and
  284. X *               the returned to the caller.  If any node loses its
  285. X *               b-tree'ness, the tree is reorganized.  If the key is
  286. X *               not found, NULL is returned.
  287. X *
  288. X *               Note that there is another static function sharing
  289. X *               this name in the btdelrank.c file.  There is no
  290. X *               interaction between the two.
  291. X *
  292. X ********************************************************************/
  293. X
  294. X#ifdef __STDC__
  295. Xstatic void *bt_delNode(BTREE tree, BTNODE *node, void *key, void **data)
  296. X#else
  297. Xstatic void *bt_delNode(tree,node,key,data)
  298. XBTREE    tree;
  299. XBTNODE    *node;
  300. Xvoid    *key;
  301. Xvoid    **data;
  302. X#endif
  303. X{
  304. X    int    i;
  305. X    int    res;
  306. X    void    *retval;
  307. X
  308. X    /* Key not found */
  309. X    if (node == NULL)
  310. X    {
  311. X        /* This branch is never taken. */
  312. X        COVER("btdelete.c",1);
  313. X        return NULL;
  314. X    }
  315. X
  316. X    /* Search for key, descend tree until found or find leaf */
  317. X    i = bt_searchNode(node,key,tree->comp,&res);
  318. X    if (res == 0)
  319. X    {
  320. X        COVER("btdelete.c",2);
  321. X        if (node->children == NULL)
  322. X        {
  323. X            /* Key was not found */
  324. X            COVER("btdelete.c",3);
  325. X            return NULL;
  326. X        }
  327. X        COVER("btdelete.c",4);
  328. X        retval = bt_delNode(tree,node->children[i],key,data);
  329. X        if (retval)
  330. X        {
  331. X            COVER("btdelete.c",5);
  332. X            node->tsize--;
  333. X        }
  334. X    }
  335. X    else
  336. X    {
  337. X        COVER("btdelete.c",6);
  338. X        /* Return the deleted node to caller */
  339. X        if (data != NULL)
  340. X        {
  341. X            COVER("btdelete.c",7);
  342. X            *data = node->data[i];
  343. X        }
  344. X        retval = node->keys[i];
  345. X
  346. X        if (node->children == NULL)
  347. X        {
  348. X            /* Delete from leaf */
  349. X            COVER("btdelete.c",8);
  350. X            bt_delKey(node,i);
  351. X        }
  352. X        else
  353. X        {
  354. X            /* Swap with predecessor */
  355. X            COVER("btdelete.c",9);
  356. X            node->keys[i] = bt_delPred(tree,node->children[i],
  357. X                                       &node->data[i]);
  358. X            node->tsize--;
  359. X        }
  360. X    }
  361. X
  362. X    /* If a child node was reorganized, rebalancing may be necessary */
  363. X    if ((node->children != NULL) &&
  364. X        (node->children[i]->nkeys < (tree->order - 1) / 2))
  365. X    {
  366. X        COVER("btdelete.c",10);
  367. X        res = bt_rotateRight(node,i - 1,tree->order);
  368. X        if (res == 0)
  369. X        {
  370. X            COVER("btdelete.c",11);
  371. X            res = bt_rotateLeft(node,i,tree->order);
  372. X        }
  373. X        if (res == 0)
  374. X        {
  375. X            COVER("btdelete.c",12);
  376. X            if (i >= node->nkeys)
  377. X            {
  378. X                COVER("btdelete.c",13);
  379. X                i--;
  380. X            }
  381. X            bt_weld(tree,node,i);
  382. X        }
  383. X    }
  384. X    return retval;
  385. X}
  386. X
  387. X/********************************************************************
  388. X *
  389. X * bt_delete -- This function deletes the specified key from a
  390. X *              b-tree and reorganizes it as necessary.  The deleted
  391. X *              key is returned to the caller if it is found, NULL
  392. X *              is returned otherwise.
  393. X *
  394. X ********************************************************************/
  395. X
  396. X#ifdef __STDC__
  397. Xvoid *bt_delete(void *ptree, void *key, void **data)
  398. X#else
  399. Xvoid    *bt_delete(ptree,key,data)
  400. Xvoid    *ptree;
  401. Xvoid    *key;
  402. Xvoid    **data;
  403. X#endif
  404. X{
  405. X    BTNODE    *root;
  406. X    void    *retval;
  407. X    BTREE    tree;
  408. X
  409. X    tree = (BTREE) ptree;
  410. X
  411. X    /* Check parameters */
  412. X    if (ptree == NULL)
  413. X    {
  414. X        COVER("btdelete.c",14);
  415. X        return NULL;
  416. X    }
  417. X
  418. X    if (key == NULL)
  419. X    {
  420. X        COVER("btdelete.c",15);
  421. X        return NULL;
  422. X    }
  423. X
  424. X    /* Do no deletion if tree is empty */
  425. X    root = tree->root;
  426. X    if (root->nkeys == 0)
  427. X    {
  428. X        COVER("btdelete.c",16);
  429. X        return NULL;
  430. X    }
  431. X
  432. X    /* Delete the key */
  433. X    retval = bt_delNode(tree,root,key,data);
  434. X
  435. X    /*
  436. X     * Delete the root if it is empty, yet its children are not.
  437. X     * This happens after a weld on the last key in the root.
  438. X     */
  439. X    COVER("btdelete.c",16);
  440. X    if ((root->nkeys == 0) && (root->children != NULL))
  441. X    {
  442. X        COVER("btdelete.c",17);
  443. X        tree->root = root->children[0];
  444. X        bt_free(root->keys);
  445. X        bt_free(root->children);
  446. X        bt_free(root);
  447. X        tree->capacity -= tree->order - 1;
  448. X        tree->root->parent = NULL;
  449. X    }
  450. X    if (retval != NULL)
  451. X    {
  452. X        COVER("btdelete.c",18);
  453. X        tree->currNode = NULL;
  454. X    }
  455. X    return retval;
  456. X}
  457. X
  458. X/************ End of file ***************/
  459. X
  460. END_OF_src/btree/btdelete.c
  461. if test 4533 -ne `wc -c <src/btree/btdelete.c`; then
  462.     echo shar: \"src/btree/btdelete.c\" unpacked with wrong size!
  463. fi
  464. # end of overwriting check
  465. fi
  466. if test -f src/btree/btdelrank.c -a "${1}" != "-c" ; then 
  467.   echo shar: Will not over-write existing file \"src/btree/btdelrank.c\"
  468. else
  469. echo shar: Extracting \"src/btree/btdelrank.c\" \(4452 characters\)
  470. sed "s/^X//" >src/btree/btdelrank.c <<'END_OF_src/btree/btdelrank.c'
  471. X/********************************************************************
  472. X *
  473. X * btdelrank.c -- This file contains functions necessary to perform
  474. X *                deletion by rank from a B-tree.
  475. X *
  476. X * This file is part of a suite of programs called Software Chipset.
  477. X * The source code for Software Chipset has been released into the
  478. X * public domain by its author, Paul Sander.
  479. X *
  480. X ********************************************************************/
  481. X
  482. X#include <stdio.h>
  483. X#include "btpriv.h"
  484. X
  485. X/********************************************************************
  486. X *
  487. X * bt_delNode -- This function performs a recursive descent of the
  488. X *               tree until the key at the desired rank location is
  489. X *               found.  If that key is found in a leaf node, it is
  490. X *               removed; if not, its predecessor replaces it.  In
  491. X *               any case, the tree is reorganized as necessary.
  492. X *
  493. X *               Note that there is another static function sharing
  494. X *               this name in the btdelete.c file.  There is no
  495. X *               interaction between the two.
  496. X *
  497. X ********************************************************************/
  498. X
  499. X#ifdef __STDC__
  500. Xstatic void *bt_delNode(BTREE tree, BTNODE *node, int rank, void **data)
  501. X#else
  502. Xstatic void *bt_delNode(tree,node,rank,data)
  503. XBTREE    tree;
  504. XBTNODE    *node;
  505. Xint    rank;
  506. Xvoid    **data;
  507. X#endif
  508. X{
  509. X    void    *retval;
  510. X    int    i;
  511. X    int    acc;
  512. X    int    last;
  513. X    int    res;
  514. X
  515. X    if (node->children == NULL)
  516. X    {
  517. X        /* rank < node->nkeys */
  518. X        COVER("btdelrank.c",1);
  519. X        if (data != NULL)
  520. X        {
  521. X            COVER("btdelrank.c",2);
  522. X            *data = node->data[rank];
  523. X        }
  524. X        retval = node->keys[rank];
  525. X        bt_delKey(node,rank);
  526. X    }
  527. X    else
  528. X    {
  529. X        /*
  530. X         * Scan to find subtree containing node containing key of
  531. X         * given rank
  532. X         */
  533. X        COVER("btdelrank.c",3);
  534. X        acc = 0;
  535. X        last = 0;
  536. X        for (i = 0; acc += node->children[i]->tsize, acc < rank; i++)
  537. X        {
  538. X            COVER("btdelrank.c",4);
  539. X            acc++;        /* Count key in this node */
  540. X            last = acc;
  541. X        }
  542. X
  543. X        if (acc == rank)
  544. X        {
  545. X            COVER("btdelrank.c",5);
  546. X            /* Key is contained in this node */
  547. X            if (data != NULL)
  548. X            {
  549. X                COVER("btdelrank.c",6);
  550. X                *data = node->data[i];
  551. X            }
  552. X            retval = node->keys[i];
  553. X            node->keys[i] = bt_delPred(tree,node->children[i],
  554. X                                       &node->data[i]);
  555. X        }
  556. X        else
  557. X        {
  558. X            /* Key is in the subtree rooted at the i'th child */
  559. X            COVER("btdelrank.c",7);
  560. X            retval = bt_delNode(tree,node->children[i],rank-last,
  561. X                                data);
  562. X        }
  563. X        node->tsize--;
  564. X
  565. X        /*
  566. X         * If a child node was reorganized, rebalancing may be
  567. X         * necessary
  568. X         */
  569. X        if (node->children[i]->nkeys < (tree->order - 1) / 2)
  570. X        {
  571. X            COVER("btdelrank.c",8);
  572. X            res = bt_rotateRight(node,i - 1,tree->order);
  573. X            if (res == 0)
  574. X            {
  575. X                COVER("btdelrank.c",9);
  576. X                res = bt_rotateLeft(node,i,tree->order);
  577. X            }
  578. X            if (res == 0)
  579. X            {
  580. X                COVER("btdelrank.c",10);
  581. X                if (i >= node->nkeys)
  582. X                {
  583. X                    COVER("btdelrank.c",11);
  584. X                    i--;
  585. X                }
  586. X                bt_weld(tree,node,i);
  587. X            }
  588. X        }
  589. X    }
  590. X    return retval;
  591. X}
  592. X
  593. X/********************************************************************
  594. X *
  595. X * bt_delRank -- This function deletes the key having the specified
  596. X *               rank from a b-tree and reorganizes it as necessary.
  597. X *               The deleted key is returned to the caller if it is
  598. X *               found, NULL is returned otherwise.
  599. X *
  600. X ********************************************************************/
  601. X
  602. X#ifdef __STDC__
  603. Xvoid *bt_delRank(void *ptree, int rank, void **data)
  604. X#else
  605. Xvoid    *bt_delRank(ptree,rank,data)
  606. Xvoid    *ptree;
  607. Xint    rank;
  608. Xvoid    **data;
  609. X#endif
  610. X{
  611. X    BTNODE    *root;
  612. X    void    *retval;
  613. X    BTREE    tree;
  614. X
  615. X    tree = (BTREE) ptree;
  616. X
  617. X    /* Do no deletion if tree is empty */
  618. X    if (tree == NULL)
  619. X    {
  620. X        COVER("btdelrank.c",12);
  621. X        return NULL;
  622. X    }
  623. X    if (rank < 0)
  624. X    {
  625. X        COVER("btdelrank.c",13);
  626. X        return NULL;
  627. X    }
  628. X    if (rank >= tree->root->tsize)
  629. X    {
  630. X        COVER("btdelrank.c",14);
  631. X        return NULL;
  632. X    }
  633. X    COVER("btdelrank.c",15);
  634. X    root = tree->root;
  635. X
  636. X    /* Delete the key */
  637. X    retval = bt_delNode(tree,root,rank,data);
  638. X
  639. X    /*
  640. X     * Delete the root if it is empty, yet its children are not.
  641. X     * This happens after a weld on the last key in the root.
  642. X     */
  643. X    if ((root->nkeys == 0) && (root->children != NULL))
  644. X    {
  645. X        COVER("btdelrank.c",16);
  646. X        tree->root = root->children[0];
  647. X        bt_free(root->keys);
  648. X        bt_free(root->children);
  649. X        bt_free(root);
  650. X        tree->capacity -= tree->order - 1;
  651. X        tree->root->parent = NULL;
  652. X    }
  653. X    if (retval != NULL)
  654. X    {
  655. X        COVER("btdelrank.c",17);
  656. X        tree->currNode = NULL;
  657. X    }
  658. X    return retval;
  659. X}
  660. X
  661. X/******** End of file ********/
  662. X
  663. END_OF_src/btree/btdelrank.c
  664. if test 4452 -ne `wc -c <src/btree/btdelrank.c`; then
  665.     echo shar: \"src/btree/btdelrank.c\" unpacked with wrong size!
  666. fi
  667. # end of overwriting check
  668. fi
  669. if test -f src/btree/btdelutl.c -a "${1}" != "-c" ; then 
  670.   echo shar: Will not over-write existing file \"src/btree/btdelutl.c\"
  671. else
  672. echo shar: Extracting \"src/btree/btdelutl.c\" \(4382 characters\)
  673. sed "s/^X//" >src/btree/btdelutl.c <<'END_OF_src/btree/btdelutl.c'
  674. X/********************************************************************
  675. X *
  676. X * btdelutl.c -- This file contains functions needed to delete a key
  677. X *               from an in-memory B-tree.
  678. X *
  679. X * This file is part of a suite of programs called Software Chipset.
  680. X * The source code for Software Chipset has been released into the
  681. X * public domain by its author, Paul Sander.
  682. X *
  683. X ********************************************************************/
  684. X
  685. X#include <stdio.h>
  686. X#include "btpriv.h"
  687. X
  688. X/********************************************************************
  689. X *
  690. X * bt_delKey -- This function deletes the specified key from the
  691. X *              specified node.
  692. X *
  693. X ********************************************************************/
  694. X
  695. X#ifdef __STDC__
  696. Xvoid bt_delKey(BTNODE *node, int n)
  697. X#else
  698. Xvoid bt_delKey(node,n)
  699. XBTNODE    *node;
  700. Xint    n;
  701. X#endif
  702. X{
  703. X    int    i;
  704. X
  705. X    COVER("btdelutl.c",1);
  706. X    for (i = n; i < node->nkeys - 1; i++)
  707. X    {
  708. X        COVER("btdelutl.c",2);
  709. X        node->keys[i] = node->keys[i+1];
  710. X        node->data[i] = node->data[i+1];
  711. X    }
  712. X    if (node->children != NULL)
  713. X    {
  714. X        /*
  715. X         * This branch should never be taken, since the only places
  716. X         * where keys are actually deleted are from leaf nodes.
  717. X         */
  718. X        COVER("btdelutl.c",3);
  719. X        for (i = n; i < node->nkeys; i++)
  720. X        {
  721. X            COVER("btdelutl.c",4);
  722. X            node->children[i] = node->children[i+1];
  723. X        }
  724. X    }
  725. X    node->nkeys--;
  726. X    node->tsize--;
  727. X    return;
  728. X}
  729. X
  730. X/********************************************************************
  731. X *
  732. X * bt_weld -- This function is the opposite of burst, above.  It joins
  733. X *            two neighboring children of the given node, and lowers a
  734. X *            key into the result.
  735. X *
  736. X ********************************************************************/
  737. X
  738. X#ifdef __STDC__
  739. Xvoid bt_weld(BTREE tree, BTNODE *node, int n)
  740. X#else
  741. Xvoid bt_weld(tree,node,n)
  742. XBTREE    tree;
  743. XBTNODE    *node;
  744. Xint    n;
  745. X#endif
  746. X{
  747. X    int    i;
  748. X    int    j;
  749. X    BTNODE    *left;
  750. X    BTNODE    *right;
  751. X
  752. X    if (node->children == NULL)
  753. X    {
  754. X        /*
  755. X         * This branch is never taken, because all functions that
  756. X         * call bt_burst explicitly test for child nodes.
  757. X         */
  758. X        COVER("btdelutl.c",5);
  759. X        return;
  760. X    }
  761. X    COVER("btdelutl.c",6);
  762. X    left = node->children[n];
  763. X    right = node->children[n+1];
  764. X    i = left->nkeys;
  765. X    left->keys[i] = node->keys[n];
  766. X    left->data[i] = node->data[n];
  767. X    for (i++, j = 0; j < right->nkeys; i++, j++)
  768. X    {
  769. X        COVER("btdelutl.c",7);
  770. X        left->keys[i] = right->keys[j];
  771. X        left->data[i] = right->data[j];
  772. X    }
  773. X    for (i = n + 1; i < node->nkeys; i++)
  774. X    {
  775. X        COVER("btdelutl.c",8);
  776. X        node->keys[i-1] = node->keys[i];
  777. X        node->data[i-1] = node->data[i];
  778. X        node->children[i] = node->children[i+1];
  779. X    }
  780. X    node->nkeys--;
  781. X    if (left->children != NULL)
  782. X    {
  783. X        COVER("btdelutl.c",9);
  784. X        for (i = left->nkeys + 1, j = 0; j <= right->nkeys; i++, j++)
  785. X        {
  786. X            COVER("btdelutl.c",10);
  787. X            left->children[i] = right->children[j];
  788. X            left->children[i]->parent = left;
  789. X        }
  790. X        bt_free(right->children);
  791. X    }
  792. X    left->nkeys += right->nkeys + 1;
  793. X    left->tsize += right->tsize + 1;
  794. X    bt_free(right->keys);
  795. X    bt_free(right);
  796. X    tree->capacity -= tree->order - 1;
  797. X    return;
  798. X}
  799. X
  800. X/********************************************************************
  801. X *
  802. X * bt_delPred -- This function searches a sub-tree, and deletes the
  803. X *               highest key it finds, and returns it to its caller.
  804. X *               If any node along the way  loses its b-tree'ness, the
  805. X *               tree is reorganized after the deletion.
  806. X *
  807. X ********************************************************************/
  808. X
  809. X#ifdef __STDC__
  810. Xvoid *bt_delPred(BTREE tree, BTNODE *node, void **data)
  811. X#else
  812. Xvoid *bt_delPred(tree,node,data)
  813. XBTREE    tree;
  814. XBTNODE    *node;
  815. Xvoid    **data;
  816. X#endif
  817. X{
  818. X    BTNODE    *pnode;
  819. X    int    res;
  820. X    void    *retval;
  821. X
  822. X    /*
  823. X     * If at a leaf node, delete the highest key, otherwise
  824. X     * call self recursively
  825. X     */
  826. X    if (node->children == NULL)
  827. X    {
  828. X        COVER("btdelutl.c",11);
  829. X        if (data != NULL)
  830. X        {
  831. X            COVER("btdelutl.c",12);
  832. X            *data = node->data[node->nkeys - 1];
  833. X        }
  834. X        retval = node->keys[node->nkeys - 1];
  835. X        node->nkeys--;
  836. X    }
  837. X    else
  838. X    {
  839. X        COVER("btdelutl.c",13);
  840. X        pnode = node->children[node->nkeys];
  841. X        retval = bt_delPred(tree,pnode,data);
  842. X
  843. X        /* Reorganize tree if node gets too empty */
  844. X        if (pnode->nkeys < (tree->order - 1) / 2)
  845. X        {
  846. X            COVER("btdelutl.c",14);
  847. X            res = bt_rotateRight(node,node->nkeys - 1,tree->order);
  848. X            if (res == 0)
  849. X            {
  850. X                COVER("btdelutl.c",15);
  851. X                bt_weld(tree,node,node->nkeys - 1);
  852. X            }
  853. X        }
  854. X    }
  855. X    node->tsize--;
  856. X    return retval;
  857. X}
  858. X
  859. X/******** End of File ********/
  860. X
  861. END_OF_src/btree/btdelutl.c
  862. if test 4382 -ne `wc -c <src/btree/btdelutl.c`; then
  863.     echo shar: \"src/btree/btdelutl.c\" unpacked with wrong size!
  864. fi
  865. # end of overwriting check
  866. fi
  867. if test -f src/btree/btdump.c -a "${1}" != "-c" ; then 
  868.   echo shar: Will not over-write existing file \"src/btree/btdump.c\"
  869. else
  870. echo shar: Extracting \"src/btree/btdump.c\" \(3884 characters\)
  871. sed "s/^X//" >src/btree/btdump.c <<'END_OF_src/btree/btdump.c'
  872. X/*********************************************************************
  873. X *
  874. X * btdump.c -- This function contains functions needed to display
  875. X *             the contents of an in-memory B-tree, passing each key
  876. X *             to a callback function.  This file contains non-portable
  877. X *             code that is intended to help debug the B-tree
  878. X *             implementation.  Also, all pointers are displayed
  879. X *             surrounded by "X", so that an automated test program
  880. X *             can filter out machine-dependent pointer values.
  881. X *
  882. X * This file is part of a suite of programs called Software Chipset.
  883. X * The source code for Software Chipset has been released into the
  884. X * public domain by its author, Paul Sander.
  885. X *
  886. X *********************************************************************/
  887. X
  888. X#include <stdio.h>
  889. X#include "btpriv.h"
  890. X
  891. X/*********************************************************************
  892. X *
  893. X * bt_dumpNode -- This function displays the contents of a node and
  894. X *                then recursively displays its children.  Note
  895. X *                that this function assumes that a pointer to a node
  896. X *                is the same size as an integer.
  897. X *
  898. X *********************************************************************/
  899. X
  900. X#ifdef __STDC__
  901. Xstatic void bt_dumpNode(BTNODE *node, void (*key_dump)(void*,void*,void*),
  902. X                        void *info)
  903. X#else
  904. Xstatic void bt_dumpNode(node,key_dump,info)
  905. XBTNODE    *node;
  906. Xvoid    (*key_dump)();
  907. Xvoid    *info;
  908. X#endif
  909. X{
  910. X    int    i;
  911. X
  912. X    if (node == NULL)
  913. X    {
  914. X        printf("ERROR:  null node pointer\n");
  915. X        return;
  916. X    }
  917. X    printf("%08x: %d keys (keys %08x, children %08x, data %08x),\n",
  918. X           (int) node,node->nkeys,node->keys,node->children,node->data);
  919. X    printf("currKey = %d, parent = %08x, tsize = %d\n",
  920. X           node->currKey,(int) node->parent,node->tsize);
  921. X    printf("--------\n");
  922. X    for (i = 0; i < node->nkeys; i++)
  923. X    {
  924. X        if (node->children != NULL)
  925. X        {
  926. X            printf("    %08x\n",(int)(node->children[i]));
  927. X        }
  928. X        else
  929. X        {
  930. X            printf("    %08x\n",0);
  931. X        }
  932. X        if (key_dump != NULL)
  933. X        {
  934. X            printf("        ");
  935. X            (*key_dump)(node->keys[i],node->data[i],info);
  936. X        }
  937. X    }
  938. X    if (node->children != NULL)
  939. X    {
  940. X        printf("    %08x\n",(int)(node->children[i]));
  941. X        for (i = 0; i <= node->nkeys; i++)
  942. X        {
  943. X            bt_dumpNode(node->children[i],key_dump,info);
  944. X        }
  945. X    }
  946. X    else
  947. X    {
  948. X        printf("    %08x\n",0);
  949. X    }
  950. X
  951. X    fflush(stdout);
  952. X    return;
  953. X}
  954. X
  955. X/*********************************************************************
  956. X *
  957. X * bt_dump -- This function displays the contents of a B-tree.  The
  958. X *            caller passes in a function that displays the contents
  959. X *            of one of the keys stored in the tree.  The calling
  960. X *            protocol for this function is:
  961. X *
  962. X *                       key_dump(key,data,info)
  963. X *                       void *key;
  964. X *                       void *data;
  965. X *                       void *info;
  966. X *
  967. X *            If key_dump is NULL, no action is taken.  This is useful
  968. X *            if the structure of the tree is desired without including
  969. X *            its contents.
  970. X *
  971. X *********************************************************************/
  972. X
  973. X#ifdef __STDC__
  974. Xvoid bt_dump(void *ptree, void (*key_dump)(void*,void*,void*), void *info)
  975. X#else
  976. Xvoid bt_dump(ptree,key_dump,info)
  977. Xvoid    *ptree;
  978. Xvoid    (*key_dump)();
  979. Xvoid    *info;
  980. X#endif
  981. X{
  982. X    BTREE    tree;
  983. X
  984. X    tree = (BTREE) ptree;
  985. X    printf("B-tree dump  %x:\n\n",(int)ptree);
  986. X    printf("order = %d\n",tree->order);
  987. X    printf("capacity = %d\n",tree->capacity);
  988. X    printf("contains %d keys\n",tree->root->tsize);
  989. X    printf("efficiency is %d per cent\n",
  990. X           (tree->root->tsize*100)/tree->capacity);
  991. X    printf("handle at %08x\n",(int)tree);
  992. X    printf("root  = %08x\n",(int) tree->root);
  993. X    printf("currNode  = %08x\n",(int) tree->currNode);
  994. X    printf("nextOk = %d\n",tree->nextOk);
  995. X    printf("data = %x\n",(int)(tree->data));
  996. X    printf("\n");
  997. X    bt_dumpNode(tree->root,key_dump,info);
  998. X    return;
  999. X}
  1000. X
  1001. X/******** End of file *********/
  1002. X
  1003. END_OF_src/btree/btdump.c
  1004. if test 3884 -ne `wc -c <src/btree/btdump.c`; then
  1005.     echo shar: \"src/btree/btdump.c\" unpacked with wrong size!
  1006. fi
  1007. # end of overwriting check
  1008. fi
  1009. if test -f src/btree/btnew.c -a "${1}" != "-c" ; then 
  1010.   echo shar: Will not over-write existing file \"src/btree/btnew.c\"
  1011. else
  1012. echo shar: Extracting \"src/btree/btnew.c\" \(3819 characters\)
  1013. sed "s/^X//" >src/btree/btnew.c <<'END_OF_src/btree/btnew.c'
  1014. X/***********************************************************************
  1015. X *
  1016. X * btnew.c -- This file contains functions to create a new in-memory
  1017. X *            B-tree.
  1018. X *
  1019. X * This file is part of a suite of programs called Software Chipset.
  1020. X * The source code for Software Chipset has been released into the
  1021. X * public domain by its author, Paul Sander.
  1022. X *
  1023. X ***********************************************************************/
  1024. X
  1025. X#include <stdio.h>
  1026. X#include "btpriv.h"
  1027. X
  1028. X/***********************************************************************
  1029. X *
  1030. X * bt_new -- Given a BT_SETUP structure, this function creates an empty
  1031. X *           B-tree structure.
  1032. X *
  1033. X ***********************************************************************/
  1034. X
  1035. X#ifdef __STDC__
  1036. Xvoid    *bt_new(void *psetup)
  1037. X#else
  1038. Xvoid    *bt_new(psetup)
  1039. Xvoid    *psetup;
  1040. X#endif
  1041. X{
  1042. X    BTREE        tree;
  1043. X    BT_SETUP    *setup;
  1044. X    int        i;
  1045. X    int        order;
  1046. X
  1047. X    /* Validate parameters */
  1048. X    setup = (BT_SETUP*) psetup;
  1049. X    if (setup == NULL)
  1050. X    {
  1051. X        COVER("btnew.c",1);
  1052. X        return (void *) NULL;
  1053. X    }
  1054. X    COVER("btnew.c",2);
  1055. X    order = setup->order;
  1056. X
  1057. X    /* Allocate the handle */
  1058. X    tree = (BTREE) bt_malloc(sizeof(struct btree));
  1059. X    if (tree != NULL)
  1060. X    {
  1061. X        /* Allocate the root */
  1062. X        tree->root = (BTNODE*) bt_malloc(sizeof(struct btnode));
  1063. X        if (tree->root != NULL)
  1064. X        {
  1065. X            /* Initialize root */
  1066. X            tree->order = order;
  1067. X            tree->comp = setup->comp;
  1068. X            tree->data = setup->data;
  1069. X            tree->root->nkeys = 0;
  1070. X            tree->root->children = NULL;
  1071. X
  1072. X            /* Allocate key array */
  1073. X            tree->root->keys =
  1074. X                (void**)bt_malloc((order - 1) * sizeof(void*));
  1075. X            if (tree->root->keys != NULL)
  1076. X            {
  1077. X                tree->root->data = (void**)bt_malloc(
  1078. X                                  (order - 1) * sizeof(void*));
  1079. X                if (tree->root->data != NULL)
  1080. X                {
  1081. X                    /* Initialize keys */
  1082. X                    for (i = 0; i < order - 1; i++)
  1083. X                    {
  1084. X                        tree->root->keys[i] = NULL;
  1085. X                        tree->root->data[i] = NULL;
  1086. X                    }
  1087. X                    tree->root->nkeys = 0;
  1088. X                    tree->root->parent = NULL;
  1089. X                    tree->root->tsize = 0;
  1090. X                    tree->capacity = order - 1;
  1091. X                    tree->currNode = NULL;
  1092. X                    return (void *) tree;
  1093. X                }
  1094. X                /* Failed to allocate data, free keys */
  1095. X                bt_free(tree->root->keys);
  1096. X            }
  1097. X
  1098. X            /* Failed to allocate keys, free tree */
  1099. X            bt_free(tree->root);
  1100. X        }
  1101. X        /* Failed to allocate root, free handle */
  1102. X        bt_free(tree);
  1103. X    }
  1104. X    /* Failed to allocate handle, return null (error) */
  1105. X    return (void *) NULL;
  1106. X}
  1107. X
  1108. X/***********************************************************************
  1109. X *
  1110. X * bt_setup -- This function allocates a B-tree setup structure, and
  1111. X *             returns it to the caller.  The parameters are the order
  1112. X *             (max number of children per node) of the tree, and a
  1113. X *             pointer to a comparison function for keys.  The setup
  1114. X *             structure is then passed to bt_new() when a new tree
  1115. X *             is created.
  1116. X *
  1117. X ***********************************************************************/
  1118. X
  1119. X#ifdef __STDC__
  1120. Xvoid *bt_setup(int order, int (*comp)(void*,void*), void *data)
  1121. X#else
  1122. Xvoid *bt_setup(order,comp,data)
  1123. Xint    order;
  1124. Xint    (*comp)();
  1125. Xvoid    *data;
  1126. X#endif
  1127. X{
  1128. X    BT_SETUP    *retval;
  1129. X
  1130. X    if (comp == NULL)
  1131. X    {
  1132. X        COVER("btnew.c",3);
  1133. X        return (void *) NULL;
  1134. X    }
  1135. X    if (order < 3)
  1136. X    {
  1137. X        COVER("btnew.c",4);
  1138. X        return (void *) NULL;
  1139. X    }
  1140. X    COVER("btnew.c",5);
  1141. X    retval = (BT_SETUP*) bt_malloc(sizeof(BT_SETUP));
  1142. X    if (retval != NULL)
  1143. X    {
  1144. X        retval->order = order;
  1145. X        retval->comp = (COMPFN) comp;
  1146. X        retval->data = data;
  1147. X    }
  1148. X    return (void*) retval;
  1149. X}
  1150. X
  1151. X/***********************************************************************
  1152. X *
  1153. X * bt_freeSetup -- This function frees a B-tree setup structure.
  1154. X *
  1155. X ***********************************************************************/
  1156. X
  1157. X#ifdef __STDC__
  1158. Xvoid bt_freeSetup(void *setup)
  1159. X#else
  1160. Xvoid bt_freeSetup(setup)
  1161. Xvoid    *setup;
  1162. X#endif
  1163. X{
  1164. X    COVER("btnew.c",6);
  1165. X    if (setup != NULL)
  1166. X    {
  1167. X        bt_free(setup);
  1168. X    }
  1169. X    return;
  1170. X}
  1171. X
  1172. X/********** End of file **********/
  1173. X
  1174. END_OF_src/btree/btnew.c
  1175. if test 3819 -ne `wc -c <src/btree/btnew.c`; then
  1176.     echo shar: \"src/btree/btnew.c\" unpacked with wrong size!
  1177. fi
  1178. # end of overwriting check
  1179. fi
  1180. echo shar: End of archive 5 \(of 10\).
  1181. cp /dev/null ark5isdone
  1182. MISSING=""
  1183. for I in 1 2 3 4 5 6 7 8 9 10 ; do
  1184.     if test ! -f ark${I}isdone ; then
  1185.     MISSING="${MISSING} ${I}"
  1186.     fi
  1187. done
  1188. if test "${MISSING}" = "" ; then
  1189.     echo You have unpacked all 10 archives.
  1190.     echo "Now edit common.mk and do a 'make all'"
  1191.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1192. else
  1193.     echo You still need to unpack the following archives:
  1194.     echo "        " ${MISSING}
  1195. fi
  1196. ##  End of shell archive.
  1197. exit 0
  1198.  
  1199.