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

  1. From decwrl!athertn!sander.cupertino.ca.us!paul@cs.purdue.edu Wed Jan  6 13:53:19 EST 1993
  2. Submit chipset-2 04/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 4 (of 10)."
  10. # Contents:  src/btree/README src/btree/bt_next.c src/btree/bt_prev.c
  11. #   src/btree/btdestroy.c src/btree/btpriv.h src/btree/btrank.c
  12. #   src/btree/bttraverse.c src/list/dlist.h src/list/dljoin.c
  13. # Wrapped by paul@sander on Sun Nov 22 15:41:51 1992
  14. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  15. if test -f src/btree/README -a "${1}" != "-c" ; then 
  16.   echo shar: Will not over-write existing file \"src/btree/README\"
  17. else
  18. echo shar: Extracting \"src/btree/README\" \(3120 characters\)
  19. sed "s/^X//" >src/btree/README <<'END_OF_src/btree/README'
  20. XThis directory contains source code to an in-memory B-tree implementation.
  21. XThe tree can hold arbitrary keys, but duplicate keys cannot be inserted.
  22. X
  23. XThis implementation has been placed in the public domain by its author,
  24. XPaul Sander, to be used as you wish.  However, if you add neat new features,
  25. XI'd appreciate having a copy sent to me at paul@sander.cupertino.ca.us .
  26. X
  27. XYou are strongly urged to review the Makefile.  It contains a lot of stuff
  28. Xthat is specific to my system, but configuring it to your needs should be
  29. Xstraightforward.  The most important places to look are at the macros near
  30. Xthe beginning of the file, and in the "installDocs" and "installLib" recipes.
  31. X
  32. XAlso review the btdump.c file.  It contains additional debugging code that
  33. Xdisplay the contents of a B-tree to the standard output device.  This file
  34. Xcontains platform-specific code to format pointers.
  35. X
  36. XTo build the library on a Unix system, invoke "make".  VMS users will need
  37. Xto translate the Makefile into a DESCRIP.MMS file.  Others will need to do
  38. Xsomething different, of course.  The code should compile under either an
  39. XANSI or a K&R compiler.  If your implementation predates the "void" type,
  40. Xyou should add a typedef or macro to the .h files to alias the "char" type
  41. Xas "void".
  42. X
  43. XOnline documentation has been provided.  It consists of source code using
  44. Xthe Unix "man" macros, and also ASCII plaintext and PostScript versions
  45. Xproduced by nroff and troff, respectively.  The plaintext and PostScript
  46. Xdocuments can be rebuilt by invoking the "make docs" command.
  47. X
  48. XInstallation should be done by invoking other tools provided with the
  49. XSoftware ChipSet that package the components up and install them in one
  50. Xpass.  For details about using these tools, please see the ../../README
  51. Xfile.
  52. X
  53. XThere is a test program that performs a very rigorous test of the B-tree
  54. Ximplementation.  The program is built by invoking "make test".  It is
  55. Xalso built as the result of "make all" or simply "make".  To run the test,
  56. Xinvoke the command "./test | tail -1".  This should write "TEST PASSED" to
  57. Xthe controlling tty.
  58. X
  59. XAn attempt to do coverage analysis was done by hand.  To view the coverage,
  60. Xcompile the library while #define'ing the COVERAGE macro and run the test
  61. Xprogram.  The test program's output will then contain data reflecting each
  62. Xdecision point in the library.  Portions of the code not stimulated by the
  63. Xtest program are noted in comments in the source code, or fall along paths
  64. Xwhere malloc fails (which is difficult to induce in a controlled way).  Each
  65. Xof these paths has been reviewed and appears to be correct.
  66. X
  67. XIf you make changes to the library and discover that the heap is becoming
  68. Xcorrupt, there is some heap debugging scaffolding provided in the file
  69. Xbtmalloc.c .  It matches mallocs and frees, and detects when a block is
  70. Xfreed twice.  This debugging tool can be added to the library by compiling
  71. Xit with the DEBUG macro #define'd.  A word of warning:  This package was
  72. Xsufficient to debug the library, but is very primitive.  Other debugging
  73. Xmalloc packages are available that are much more effective at diagnosing
  74. Xheap problems.
  75. X
  76. END_OF_src/btree/README
  77. if test 3120 -ne `wc -c <src/btree/README`; then
  78.     echo shar: \"src/btree/README\" unpacked with wrong size!
  79. fi
  80. # end of overwriting check
  81. fi
  82. if test -f src/btree/bt_next.c -a "${1}" != "-c" ; then 
  83.   echo shar: Will not over-write existing file \"src/btree/bt_next.c\"
  84. else
  85. echo shar: Extracting \"src/btree/bt_next.c\" \(3095 characters\)
  86. sed "s/^X//" >src/btree/bt_next.c <<'END_OF_src/btree/bt_next.c'
  87. X/********************************************************************
  88. X *
  89. X * bt_next.c -- This file contains functions needed to scan an in-memory
  90. X *              B-tree in ascending order.
  91. X *
  92. X * This file is part of a suite of programs called Software Chipset.
  93. X * The source code for Software Chipset has been released into the
  94. X * public domain by its author, Paul Sander.
  95. X *
  96. X ********************************************************************/
  97. X
  98. X#include <stdio.h>
  99. X#include "btpriv.h"
  100. X
  101. X/********************************************************************
  102. X *
  103. X * bt_next -- This function returns the key that appears next in
  104. X *            the lexical order of the items stored in the tree,
  105. X *            after the last bt_search, bt_next, bt_prev, bt_last,
  106. X *            or bt_first.  NULL is returned if the tree is empty, an
  107. X *            insertion or deletion invalidated the state of the
  108. X *            tree, or the last item in the tree was already visited.
  109. X *
  110. X *******************************************************************/
  111. X
  112. X#ifdef __STDC__
  113. Xvoid *bt_next(void *ptree, void **data)
  114. X#else
  115. Xvoid *bt_next(ptree,data)
  116. Xvoid    *ptree;
  117. Xvoid    **data;
  118. X#endif
  119. X{
  120. X    BTNODE    *node;
  121. X    int    key;
  122. X    BTREE    tree;
  123. X
  124. X    COVER("bt_next.c",1);
  125. X
  126. X    /* Return if error, or insertion/deletion invalidated state */
  127. X    tree = (BTREE) ptree;
  128. X    if (tree == NULL)
  129. X    {
  130. X        COVER("bt_next.c",2);
  131. X        return NULL;
  132. X    }
  133. X    if (tree->currNode == NULL)
  134. X    {
  135. X        COVER("bt_next.c",3);
  136. X        return NULL;
  137. X    }
  138. X
  139. X    /* Set up temporaries */
  140. X    node = tree->currNode;
  141. X    key = node->currKey;
  142. X
  143. X    /* Return if we've overrun the tree */
  144. X    if (key >= node->nkeys)
  145. X    {
  146. X        COVER("bt_next.c",4);
  147. X        return NULL;
  148. X    }
  149. X
  150. X    /*
  151. X     * Bump current key in current node, compensating for unsuccessful
  152. X     * search
  153. X     */
  154. X    if (tree->nextOk)
  155. X    {
  156. X        COVER("bt_next.c",5);
  157. X        key++;
  158. X    }
  159. X    tree->nextOk = 1;
  160. X
  161. X    /* If node has children, return leftmost key of next child */
  162. X    if (node->children != NULL)
  163. X    {
  164. X        COVER("bt_next.c",6);
  165. X        node->currKey = key;
  166. X        node = node->children[key];
  167. X        while (node->children != NULL)
  168. X        {
  169. X            COVER("bt_next.c",7);
  170. X            node->currKey = 0;
  171. X            node = node->children[0];
  172. X        }
  173. X        node->currKey = 0;
  174. X        tree->currNode = node;
  175. X        if (data != NULL)
  176. X        {
  177. X            COVER("bt_next.c",8);
  178. X            *data = node->data[0];
  179. X        }
  180. X        return node->keys[0];
  181. X    }
  182. X
  183. X    /* Leaf node, return next key */
  184. X    if (key < node->nkeys)
  185. X    {
  186. X        COVER("bt_next.c",9);
  187. X        node->currKey = key;
  188. X        if (data != NULL)
  189. X        {
  190. X            COVER("bt_next.c",10);
  191. X            *data = node->data[key];
  192. X        }
  193. X        return node->keys[key];
  194. X    }
  195. X
  196. X    /* Already visited rightmost key of leaf, go back to parent */
  197. X    COVER("bt_next.c",11);
  198. X    while ((node->parent != NULL) && (key >= node->nkeys))
  199. X    {
  200. X        COVER("bt_next.c",12);
  201. X        node = node->parent;
  202. X        key = node->currKey;
  203. X    }
  204. X
  205. X    COVER("bt_next.c",13);
  206. X    /* Return NULL after last key in tree */
  207. X    if (key >= node->nkeys)
  208. X    {
  209. X        COVER("bt_next.c",14);
  210. X        tree->currNode = node;
  211. X        node->currKey = key;
  212. X        return NULL;
  213. X    }
  214. X
  215. X    /* Return next key in parent */
  216. X    COVER("bt_next.c",15);
  217. X    tree->currNode = node;
  218. X    if (data != NULL)
  219. X    {
  220. X        COVER("bt_next.c",16);
  221. X        *data = node->data[key];
  222. X    }
  223. X    return node->keys[key];
  224. X}
  225. X
  226. X/*********** End of file ***********/
  227. X
  228. END_OF_src/btree/bt_next.c
  229. if test 3095 -ne `wc -c <src/btree/bt_next.c`; then
  230.     echo shar: \"src/btree/bt_next.c\" unpacked with wrong size!
  231. fi
  232. # end of overwriting check
  233. fi
  234. if test -f src/btree/bt_prev.c -a "${1}" != "-c" ; then 
  235.   echo shar: Will not over-write existing file \"src/btree/bt_prev.c\"
  236. else
  237. echo shar: Extracting \"src/btree/bt_prev.c\" \(3012 characters\)
  238. sed "s/^X//" >src/btree/bt_prev.c <<'END_OF_src/btree/bt_prev.c'
  239. X/********************************************************************
  240. X *
  241. X * btprev.c -- This file contains functions needed to scan an in-memory
  242. X *             B-tree in descending order.
  243. X *
  244. X * This file is part of a suite of programs called Software Chipset.
  245. X * The source code for Software Chipset has been released into the
  246. X * public domain by its author, Paul Sander.
  247. X *
  248. X ********************************************************************/
  249. X
  250. X#include <stdio.h>
  251. X#include "btpriv.h"
  252. X
  253. X/********************************************************************
  254. X *
  255. X * bt_prev -- This function returns the next key that appears
  256. X *            earlier in the lexical order of the items stored in
  257. X *            the tree, after the last bt_search, bt_next, bt_prev, or
  258. X *            bt_last.  NULL is returned if the tree is empty, an
  259. X *            insertion or deletion invalidated the state of the
  260. X *            tree, or the next item in the tree was already visited.
  261. X *
  262. X *******************************************************************/
  263. X
  264. X#ifdef __STDC__
  265. Xvoid *bt_prev(void *ptree, void **data)
  266. X#else
  267. Xvoid *bt_prev(ptree,data)
  268. Xvoid    *ptree;
  269. Xvoid    **data;
  270. X#endif
  271. X{
  272. X    BTNODE    *node;
  273. X    int    key;
  274. X    BTREE    tree;
  275. X
  276. X    /* Return if error, or insertion/deletion invalidated state */
  277. X    tree = (BTREE) ptree;
  278. X    if (tree == NULL)
  279. X    {
  280. X        COVER("bt_prev.c",1);
  281. X        return NULL;
  282. X    }
  283. X    if (tree->currNode == NULL)
  284. X    {
  285. X        COVER("bt_prev.c",2);
  286. X        return NULL;
  287. X    }
  288. X    COVER("bt_prev.c",3);
  289. X    tree->nextOk = 1;
  290. X
  291. X    /* Set up temporaries */
  292. X    node = tree->currNode;
  293. X    key = node->currKey;
  294. X
  295. X    /* Return if we've overrun the tree */
  296. X    if (key < 0)
  297. X    {
  298. X        COVER("bt_prev.c",4);
  299. X        return NULL;
  300. X    }
  301. X
  302. X    /*
  303. X     * If node has children, return rightmost key of previous
  304. X     * child
  305. X     */
  306. X    if (node->children != NULL)
  307. X    {
  308. X        COVER("bt_prev.c",5);
  309. X        node = node->children[key];
  310. X        while (node->children != NULL)
  311. X        {
  312. X            COVER("bt_prev.c",6);
  313. X            node->currKey = node->nkeys;
  314. X            node = node->children[node->nkeys];
  315. X        }
  316. X        node->currKey = node->nkeys - 1;
  317. X        tree->currNode = node;
  318. X        if (data != NULL)
  319. X        {
  320. X            COVER("bt_prev.c",7);
  321. X            *data = node->data[node->currKey];
  322. X        }
  323. X        return node->keys[node->currKey];
  324. X    }
  325. X
  326. X    /* Leaf node, return previous key */
  327. X    COVER("bt_prev.c",8);
  328. X    key--;
  329. X    if (key >= 0)
  330. X    {
  331. X        COVER("bt_prev.c",9);
  332. X        node->currKey = key;
  333. X        if (data != NULL)
  334. X        {
  335. X            COVER("bt_prev.c",10);
  336. X            *data = node->data[key];
  337. X        }
  338. X        COVER("bt_prev.c",11);
  339. X        return node->keys[key];
  340. X    }
  341. X
  342. X    /* Already visited leftmost key of node, go back to parent */
  343. X    COVER("bt_prev.c",12);
  344. X    while ((node->parent != NULL) && (key < 0))
  345. X    {
  346. X        COVER("bt_prev.c",13);
  347. X        node = node->parent;
  348. X        key = node->currKey - 1;
  349. X    }
  350. X
  351. X    /* Return NULL after last key in tree */
  352. X    if (key < 0)
  353. X    {
  354. X        COVER("bt_prev.c",14);
  355. X        node->currKey = -1;
  356. X        tree->currNode = node;
  357. X        return NULL;
  358. X    }
  359. X
  360. X    /* Return next key in parent */
  361. X    COVER("bt_prev.c",15);
  362. X    node->currKey = key;
  363. X    tree->currNode = node;
  364. X    if (data != NULL)
  365. X    {
  366. X        COVER("bt_prev.c",16);
  367. X        *data = node->data[key];
  368. X    }
  369. X    return node->keys[key];
  370. X}
  371. X
  372. X/*********** End of file ***********/
  373. X
  374. END_OF_src/btree/bt_prev.c
  375. if test 3012 -ne `wc -c <src/btree/bt_prev.c`; then
  376.     echo shar: \"src/btree/bt_prev.c\" unpacked with wrong size!
  377. fi
  378. # end of overwriting check
  379. fi
  380. if test -f src/btree/btdestroy.c -a "${1}" != "-c" ; then 
  381.   echo shar: Will not over-write existing file \"src/btree/btdestroy.c\"
  382. else
  383. echo shar: Extracting \"src/btree/btdestroy.c\" \(3219 characters\)
  384. sed "s/^X//" >src/btree/btdestroy.c <<'END_OF_src/btree/btdestroy.c'
  385. X/********************************************************************
  386. X *
  387. X * btdestroy.c -- This function contains functions needed to deallocate
  388. X *                an in-memory B-tree in its entirety.
  389. X *
  390. X * This file is part of a suite of programs called Software Chipset.
  391. X * The source code for Software Chipset has been released into the
  392. X * public domain by its author, Paul Sander.
  393. X *
  394. X ********************************************************************/
  395. X
  396. X#include <stdio.h>
  397. X#include "btpriv.h"
  398. X
  399. X/********************************************************************
  400. X *
  401. X * bt_freeNode -- This function frees a node in a b-tree.  It is
  402. X *                provided with functions that free each key and its
  403. X *                related data in the node also.  The free_key and
  404. X *                free_data functions have the following interface:
  405. X *
  406. X *            void free_stuff(keyOrData,info)
  407. X *            void *keyOrData;
  408. X *            void *info;
  409. X *
  410. X *              If NULL is passed as the free_stuff function, the node
  411. X *              is freed, but no action is taken for the keys or data.
  412. X *
  413. X *              The info parameter is used for passing information from
  414. X *              the client to the free_stuff function.
  415. X *
  416. X *              The free_data function is always called before the
  417. X *              free_key function.
  418. X *
  419. X ********************************************************************/
  420. X
  421. X#ifdef __STDC__
  422. Xstatic void bt_freeNode(BTNODE *node, void (*free_key)(void*,void*),
  423. X                        void (*free_data)(void*,void*), void *info)
  424. X#else
  425. Xstatic void bt_freeNode(node,free_key,free_data,info)
  426. XBTNODE    *node;
  427. Xvoid    (*free_key)();
  428. Xvoid    (*free_data)();
  429. Xvoid    *info;
  430. X#endif
  431. X{
  432. X    int    i;
  433. X
  434. X    if (node->children != NULL)
  435. X    {
  436. X        COVER("btdestroy.c",1);
  437. X        for (i = 0; i <= node->nkeys; i++)
  438. X        {
  439. X            COVER("btdestroy.c",2);
  440. X            bt_freeNode(node->children[i],free_key,free_data,info);
  441. X        }
  442. X        bt_free(node->children);
  443. X    }
  444. X    for (i = 0; i < node->nkeys; i++)
  445. X    {
  446. X        COVER("btdestroy.c",3);
  447. X        if (free_data != NULL)
  448. X        {
  449. X            COVER("btdestroy.c",4);
  450. X            (*free_data)(node->data[i],info);
  451. X        }
  452. X        if (free_key != NULL)
  453. X        {
  454. X            COVER("btdestroy.c",5);
  455. X            (*free_key)(node->keys[i],info);
  456. X        }
  457. X    }
  458. X    COVER("btdestroy.c",6);
  459. X    bt_free(node->keys);
  460. X    bt_free(node->data);
  461. X    bt_free(node);
  462. X    return;
  463. X}
  464. X
  465. X/********************************************************************
  466. X *
  467. X * bt_destroy -- This function frees a b-tree structure.  It is also
  468. X *               passed functions that free the keys and data contained
  469. X *               by the tree.  The free_key and free_data functions have the
  470. X *               same calling protocol their counterparts passed to bt_freeNode
  471. X *               above.
  472. X *
  473. X ********************************************************************/
  474. X
  475. X#ifdef __STDC__
  476. Xvoid bt_destroy(void *ptree, void (*free_key)(void*,void*),
  477. X                void (*free_data)(void*,void*), void *info)
  478. X#else
  479. Xvoid bt_destroy(ptree,free_key,free_data,info)
  480. Xvoid    *ptree;
  481. Xvoid    (*free_key)();
  482. Xvoid    (*free_data)();
  483. Xvoid    *info;
  484. X#endif
  485. X{
  486. X    BTREE    tree;
  487. X
  488. X    COVER("btdestroy.c",7);
  489. X    if (ptree != NULL)
  490. X    {
  491. X        COVER("btdestroy.c",8);
  492. X        tree = (BTREE) ptree;
  493. X        bt_freeNode(tree->root,free_key,free_data,info);
  494. X        bt_free(tree);
  495. X    }
  496. X    return;
  497. X}
  498. X
  499. X/************ End of file ***********/
  500. X
  501. END_OF_src/btree/btdestroy.c
  502. if test 3219 -ne `wc -c <src/btree/btdestroy.c`; then
  503.     echo shar: \"src/btree/btdestroy.c\" unpacked with wrong size!
  504. fi
  505. # end of overwriting check
  506. fi
  507. if test -f src/btree/btpriv.h -a "${1}" != "-c" ; then 
  508.   echo shar: Will not over-write existing file \"src/btree/btpriv.h\"
  509. else
  510. echo shar: Extracting \"src/btree/btpriv.h\" \(2718 characters\)
  511. sed "s/^X//" >src/btree/btpriv.h <<'END_OF_src/btree/btpriv.h'
  512. X/**********************************************************************
  513. X *
  514. X * btpriv.h -- This file contains private declarations and definitions
  515. X *             required by the in-memory B-tree implementation.
  516. X *
  517. X * This file is part of a suite of programs called Software Chipset.
  518. X * The source code for Software Chipset has been released into the
  519. X * public domain by its author, Paul Sander.
  520. X *
  521. X **********************************************************************/
  522. X
  523. X/* The following structure describes a B-tree node. */
  524. X
  525. X#ifdef __STDC__
  526. Xtypedef int(*COMPFN)(void*,void*);
  527. X#else
  528. Xtypedef int(*COMPFN)();
  529. X#endif
  530. X
  531. Xstruct btnode {
  532. X    int        nkeys;        /* Number of keys stored here */
  533. X    int        tsize;        /* Keys in subtree rooted here */
  534. X    int        currKey;    /* Last key found */
  535. X    struct btnode    *parent;    /* Parent of this node */
  536. X    void        **keys;        /* array[order-1] of keys */
  537. X    void        **data;        /* array[order-1] of other data */
  538. X    struct btnode    **children;    /* array[order] of children */
  539. X};
  540. X
  541. X/* The following structure describes a B-tree. */
  542. X
  543. Xstruct btree {
  544. X    int        order;        /* Max children permitted */
  545. X    COMPFN        comp;        /* Comparison function for keys */
  546. X    struct btnode    *root;        /* Root of tree */
  547. X    int        capacity;    /* Max keys that will fit */
  548. X    struct btnode    *currNode;    /* Node accessed */
  549. X    int        nextOk;        /* Indicates last search successful */
  550. X    void        *data;        /* Other data */
  551. X};
  552. X
  553. Xtypedef struct btree *BTREE;
  554. Xtypedef struct btnode BTNODE;
  555. X
  556. X/* The following structure describes a B-tree setup structure. */
  557. X
  558. Xstruct bt_setup {
  559. X    int        order;        /* Max children permitted per node */
  560. X    int        (*comp)();    /* Comparision function for keys */
  561. X    void        *data;        /* Other data */
  562. X};
  563. X
  564. Xtypedef struct bt_setup BT_SETUP;
  565. X
  566. X#ifndef DEBUG
  567. X#define bt_malloc malloc
  568. X#define bt_free (void) free
  569. X#else
  570. X#ifdef __STDC__
  571. Xextern void *bt_malloc(unsigned);
  572. Xextern void bt_free(void *);
  573. X#else
  574. Xextern void *bt_malloc();
  575. Xextern void bt_free();
  576. X#endif
  577. X#endif
  578. X
  579. X#ifdef __STDC__
  580. Xextern int bt_malloc_verify(void);
  581. X#else
  582. Xextern int bt_malloc_verify();
  583. X#endif
  584. X
  585. X#ifndef __BTUTIL_C__
  586. X#ifdef __STDC__
  587. Xextern int bt_searchNode(BTNODE*, void*, int(*)(), int*);
  588. Xextern int bt_rotateRight(BTNODE*, int, int);
  589. Xextern int bt_rotateLeft(BTNODE*, int, int);
  590. X#else
  591. Xextern int bt_searchNode();
  592. Xextern int bt_rotateRight();
  593. Xextern int bt_rotateLeft();
  594. X#endif
  595. X#endif
  596. X
  597. X#ifndef __BTDELUTL_C__
  598. X#ifdef __STDC__
  599. Xextern void bt_delKey(BTNODE*, int);
  600. Xextern void bt_weld(BTREE, BTNODE*, int);
  601. Xextern void *bt_delPred(BTREE, BTNODE*, void**);
  602. X#else
  603. Xextern void bt_delKey();
  604. Xextern void bt_weld();
  605. Xextern void *bt_delPred();
  606. X#endif
  607. X#endif
  608. X
  609. X#ifdef COVERAGE
  610. X#define    COVER(fn,loc)    printf("Coverage:  file %s, location %03d\n",fn,loc)
  611. X#else
  612. X#define COVER(file,loc)
  613. X#endif
  614. X
  615. X/*********** End of File *************/
  616. X
  617. END_OF_src/btree/btpriv.h
  618. if test 2718 -ne `wc -c <src/btree/btpriv.h`; then
  619.     echo shar: \"src/btree/btpriv.h\" unpacked with wrong size!
  620. fi
  621. # end of overwriting check
  622. fi
  623. if test -f src/btree/btrank.c -a "${1}" != "-c" ; then 
  624.   echo shar: Will not over-write existing file \"src/btree/btrank.c\"
  625. else
  626. echo shar: Extracting \"src/btree/btrank.c\" \(3343 characters\)
  627. sed "s/^X//" >src/btree/btrank.c <<'END_OF_src/btree/btrank.c'
  628. X/********************************************************************
  629. X *
  630. X * btrank.c -- This file contains functions needed to locate a
  631. X *             key given its rank location.
  632. X *
  633. X * This file is part of a suite of programs called Software Chipset.
  634. X * The source code for Software Chipset has been released into the
  635. X * public domain by its author, Paul Sander.
  636. X *
  637. X ********************************************************************/
  638. X
  639. X#include <stdio.h>
  640. X#include "btpriv.h"
  641. X
  642. X/********************************************************************
  643. X *
  644. X * bt_locRank -- This function performs a recursive descent of the
  645. X *               tree until the key at the desired rank location is
  646. X *               found.
  647. X *
  648. X *               Note that "node" is an in-out parameter.  As input,
  649. X *               it is the current node to be searched.  As output,
  650. X *               it is the node where the key was actually found.
  651. X *               It is used to mark the updated current node stored
  652. X *               in the root so that bt_next and bt_prev will work
  653. X *               later.
  654. X *
  655. X ********************************************************************/
  656. X
  657. X#ifdef __STDC__
  658. Xstatic void *bt_locRank(BTNODE **node, int rank, void **data)
  659. X#else
  660. Xstatic void *bt_locRank(node,rank,data)
  661. XBTNODE    **node;
  662. Xint    rank;
  663. Xvoid    **data;
  664. X#endif
  665. X{
  666. X    void    *retval;
  667. X    int    i;
  668. X    int    acc;
  669. X    int    last;
  670. X
  671. X    COVER("btrank.c",1);
  672. X    if ((*node)->children == NULL)
  673. X    {
  674. X        COVER("btrank.c",2);
  675. X        /* rank < (*node)->nkeys */
  676. X        (*node)->currKey = rank;
  677. X        retval = (*node)->keys[rank];
  678. X        if (data != NULL)
  679. X        {
  680. X            COVER("btrank.c",3);
  681. X            *data = (*node)->data[rank];
  682. X        }
  683. X        COVER("btrank.c",4);
  684. X    }
  685. X    else
  686. X    {
  687. X        /*
  688. X         * Scan to find subtree containing node containing key of
  689. X         * given rank
  690. X         */
  691. X        COVER("btrank.c",5);
  692. X        acc = 0;
  693. X        last = 0;
  694. X        for (i = 0; acc += (*node)->children[i]->tsize, acc < rank; i++)
  695. X        {
  696. X            COVER("btrank.c",6);
  697. X            acc++;        /* Count key in this node */
  698. X            last = acc;
  699. X        }
  700. X        COVER("btrank.c",7);
  701. X
  702. X        (*node)->currKey = i;
  703. X        if (acc == rank)
  704. X        {
  705. X            COVER("btrank.c",8);
  706. X            /* Key is contained in this node */
  707. X            if (data != NULL) *data = (*node)->data[i];
  708. X            retval = (*node)->keys[i];
  709. X        }
  710. X        else
  711. X        {
  712. X            COVER("btrank.c",9);
  713. X            /* Key is in the subtree rooted at the i'th child */
  714. X            *node = (*node)->children[i];
  715. X            retval = bt_locRank(node, rank - last,data);
  716. X        }
  717. X        COVER("btrank.c",10);
  718. X    }
  719. X    COVER("btrank.c",11);
  720. X    return retval;
  721. X}
  722. X
  723. X/********************************************************************
  724. X *
  725. X * bt_rank -- This function locates a key within a tree that has
  726. X *            the given rank number.  NULL is returned if the
  727. X *            rank is out of range; rank is zero-based.
  728. X *
  729. X *******************************************************************/
  730. X
  731. X#ifdef __STDC__
  732. Xvoid *bt_rank(void *ptree, int rank, void *data)
  733. X#else
  734. Xvoid *bt_rank(ptree,rank,data)
  735. Xvoid    *ptree;
  736. Xint    rank;
  737. Xvoid    *data;
  738. X#endif
  739. X{
  740. X    void    *retval;
  741. X    BTREE    tree;
  742. X
  743. X    tree = (BTREE) ptree;
  744. X    if (tree == NULL)
  745. X    {
  746. X        COVER("btrank.c",12);
  747. X        retval = NULL;
  748. X    }
  749. X    else if (rank < 0)
  750. X    {
  751. X        COVER("btrank.c",13);
  752. X        retval = NULL;
  753. X    }
  754. X    else if (rank >= tree->root->tsize)
  755. X    {
  756. X        COVER("btrank.c",14);
  757. X        retval = NULL;
  758. X    }
  759. X    else
  760. X    {
  761. X        COVER("btrank.c",15);
  762. X        tree->currNode = tree->root;
  763. X        retval = bt_locRank(&tree->currNode,rank,data);
  764. X        tree->nextOk = 1;
  765. X    }
  766. X    return retval;
  767. X}
  768. X
  769. X/********* End of file ************/
  770. X
  771. END_OF_src/btree/btrank.c
  772. if test 3343 -ne `wc -c <src/btree/btrank.c`; then
  773.     echo shar: \"src/btree/btrank.c\" unpacked with wrong size!
  774. fi
  775. # end of overwriting check
  776. fi
  777. if test -f src/btree/bttraverse.c -a "${1}" != "-c" ; then 
  778.   echo shar: Will not over-write existing file \"src/btree/bttraverse.c\"
  779. else
  780. echo shar: Extracting \"src/btree/bttraverse.c\" \(2911 characters\)
  781. sed "s/^X//" >src/btree/bttraverse.c <<'END_OF_src/btree/bttraverse.c'
  782. X/****************************************************************
  783. X *
  784. X * bttraverse.c -- This file contains functions needed to
  785. X *                 traverse an in-memory B-tree, passing each
  786. X *                 item stored there to a callback function.
  787. X *
  788. X * This file is part of a suite of programs called Software Chipset.
  789. X * The source code for Software Chipset has been released into the
  790. X * public domain by its author, Paul Sander.
  791. X *
  792. X ****************************************************************/
  793. X
  794. X#include <stdio.h>
  795. X#include "btpriv.h"
  796. X
  797. X/****************************************************************
  798. X *
  799. X * bt_visit -- This function is used while traversing a b-tree.
  800. X *             It is called once for each node.  It calls some
  801. X *             specified function once for each key in its
  802. X *             key array, and calls itself once for each child
  803. X *             in its child array.
  804. X *
  805. X ****************************************************************/
  806. X
  807. X#ifdef __STDC__
  808. Xstatic void bt_visit(BTNODE *node, void (*fn)(void*, void*, void*), void *parms)
  809. X#else
  810. Xstatic void bt_visit(node,fn,parms)
  811. XBTNODE    *node;
  812. Xvoid    (*fn)();
  813. Xvoid    *parms;
  814. X#endif
  815. X{
  816. X    int    i;
  817. X
  818. X    if (node == NULL)
  819. X    {
  820. X        /* Should never happen */
  821. X        COVER("bttraverse.c",1);
  822. X        return;
  823. X    }
  824. X    COVER("bttraverse.c",2);
  825. X    for (i = 0; i < node->nkeys; i++)
  826. X    {
  827. X        if (node->children != NULL)
  828. X        {
  829. X            COVER("bttraverse.c",3);
  830. X            bt_visit(node->children[i],fn,parms);
  831. X        }
  832. X        COVER("bttraverse.c",4);
  833. X        (*fn)(node->keys[i],parms,node->data[i]);
  834. X    }
  835. X    COVER("bttraverse.c",5);
  836. X    if (node->children != NULL)
  837. X    {
  838. X        COVER("bttraverse.c",6);
  839. X        bt_visit(node->children[i],fn,parms);
  840. X    }
  841. X    COVER("bttraverse.c",7);
  842. X    return;
  843. X}
  844. X
  845. X/****************************************************************
  846. X *
  847. X * bt_traverse -- This function traverses a b-tree, calling some
  848. X *                specified function once for each key stored in
  849. X *                it.  The specified function has the following
  850. X *                protocol:
  851. X *
  852. X *            void fn(key,parms)
  853. X *            void *key;
  854. X *            void *parms;
  855. X *                      void *data;
  856. X *
  857. X *                where key is a pointer to a key stored in the
  858. X *                btree, parms is the parameter structure passed by
  859. X *                the caller, and data is the secondary data stored
  860. X *                with the key.
  861. X *
  862. X *                The nodes are visited in descending order, if
  863. X *                the comp function passed to bt_new is
  864. X *                strcmp-like.
  865. X *
  866. X ****************************************************************/
  867. X
  868. X#ifdef __STDC__
  869. Xvoid bt_traverse(void *ptree, void (*fn)(void*, void*, void*), void *parms)
  870. X#else
  871. Xvoid bt_traverse(ptree,fn,parms)
  872. Xvoid    *ptree;
  873. Xvoid    (*fn)();
  874. Xvoid    *parms;
  875. X#endif
  876. X{
  877. X    BTREE    tree;
  878. X
  879. X    COVER("bttraverse.c",8);
  880. X    tree = (BTREE) ptree;
  881. X    if ((tree != NULL) && (fn != NULL))
  882. X    {
  883. X        COVER("bttraverse.c",9);
  884. X        bt_visit(tree->root,fn,parms);
  885. X    }
  886. X    return;
  887. X}
  888. X
  889. X/********* End of file *********/
  890. X
  891. END_OF_src/btree/bttraverse.c
  892. if test 2911 -ne `wc -c <src/btree/bttraverse.c`; then
  893.     echo shar: \"src/btree/bttraverse.c\" unpacked with wrong size!
  894. fi
  895. # end of overwriting check
  896. fi
  897. if test -f src/list/dlist.h -a "${1}" != "-c" ; then 
  898.   echo shar: Will not over-write existing file \"src/list/dlist.h\"
  899. else
  900. echo shar: Extracting \"src/list/dlist.h\" \(2690 characters\)
  901. sed "s/^X//" >src/list/dlist.h <<'END_OF_src/list/dlist.h'
  902. X/******************************************************************
  903. X *
  904. X * dlist.h -- This file contains public declarations and definitions
  905. X *            used by the in-memory doubly-linked list implementation.
  906. X *
  907. X * This file is part of a suite of programs called Software Chipset.
  908. X * The source code for Software Chipset has been released into the
  909. X * public domain by its author, Paul Sander.
  910. X *
  911. X ******************************************************************/
  912. X
  913. X#ifndef DLIST_H
  914. X#define DLIST_H
  915. X
  916. X/* Hidden data types */
  917. X
  918. Xtypedef void *DL_LIST;
  919. Xtypedef void *DLL_SETUP;
  920. X
  921. X/* Peek/push/pop locations */
  922. X
  923. X#define    DLL_FRONT    0
  924. X#define    DLL_BACK    1
  925. X
  926. X/* Function declarations */
  927. X
  928. X#ifdef __STDC__
  929. Xextern    DLL_SETUP    dll_setup(int(*)(void*,void*),void*);
  930. Xextern    void        dll_freeSetup(DLL_SETUP);
  931. Xextern    DL_LIST        dll_new(DLL_SETUP);
  932. Xextern    void        dll_destroy(DL_LIST,void(*)(void*,void*),
  933. X                        void(*)(void*,void*),void*);
  934. Xextern    int        dll_insert(DL_LIST, void*, void*);
  935. Xextern    void        *dll_delete(DL_LIST, void*, void**);
  936. Xextern    void        *dll_search(DL_LIST,void*,void**);
  937. Xextern    void        dll_traverse(DL_LIST,void(*)(void*,void*,void*),void*);
  938. Xextern    void        dll_dump(DL_LIST, void(*)(void*,void*,void*),void*);
  939. Xextern    void        *dll_first(DL_LIST, void**);
  940. Xextern    void        *dll_next(DL_LIST,void**);
  941. Xextern    void        *dll_last(DL_LIST,void**);
  942. Xextern    void        *dll_prev(DL_LIST,void**);
  943. Xextern    void        *dll_rank(DL_LIST,int,void**);
  944. Xextern    void        *dll_delRank(DL_LIST, int, void**);
  945. Xextern    void        dll_setData(DL_LIST, void*);
  946. Xextern    void        *dll_data(DL_LIST);
  947. Xextern    void        *dll_pushf(DL_LIST,void*,void*);
  948. Xextern    void        *dll_pushr(DL_LIST,void*,void*);
  949. Xextern    void        *dll_push(DL_LIST,int,void*,void*);
  950. Xextern    void        *dll_popf(DL_LIST,void**);
  951. Xextern    void        *dll_popr(DL_LIST,void**);
  952. Xextern    void        *dll_pop(DL_LIST,int,void**);
  953. Xextern    void        *dll_peekf(DL_LIST,void**);
  954. Xextern    void        *dll_peekr(DL_LIST,void**);
  955. Xextern    void        *dll_peek(DL_LIST,int,void**);
  956. X
  957. X#else
  958. X
  959. Xextern    DLL_SETUP    dll_setup();
  960. Xextern    void        dll_freeSetup();
  961. Xextern    DL_LIST        dll_new();
  962. Xextern    void        dll_destroy();
  963. Xextern    int        dll_insert();
  964. Xextern    void        *dll_delete();
  965. Xextern    void        *dll_search();
  966. Xextern    void        dll_traverse();
  967. Xextern    void        dll_dump();
  968. Xextern    void        *dll_first();
  969. Xextern    void        *dll_next();
  970. Xextern    void        *dll_last();
  971. Xextern    void        *dll_prev();
  972. Xextern    void        *dll_rank();
  973. Xextern    void        *dll_delRank();
  974. Xextern    void        dll_setData();
  975. Xextern    void        *dll_data();
  976. Xextern    void        *dll_pushf();
  977. Xextern    void        *dll_pushr();
  978. Xextern    void        *dll_push();
  979. Xextern    void        *dll_popf();
  980. Xextern    void        *dll_popr();
  981. Xextern    void        *dll_pop();
  982. Xextern    void        *dll_peekf();
  983. Xextern    void        *dll_peekr();
  984. Xextern    void        *dll_peek();
  985. X
  986. X#endif
  987. X
  988. X#endif
  989. X
  990. X/************* End of file *************/
  991. X
  992. END_OF_src/list/dlist.h
  993. if test 2690 -ne `wc -c <src/list/dlist.h`; then
  994.     echo shar: \"src/list/dlist.h\" unpacked with wrong size!
  995. fi
  996. # end of overwriting check
  997. fi
  998. if test -f src/list/dljoin.c -a "${1}" != "-c" ; then 
  999.   echo shar: Will not over-write existing file \"src/list/dljoin.c\"
  1000. else
  1001. echo shar: Extracting \"src/list/dljoin.c\" \(3184 characters\)
  1002. sed "s/^X//" >src/list/dljoin.c <<'END_OF_src/list/dljoin.c'
  1003. X/***********************************************************************
  1004. X *
  1005. X * dljoin.c -- This file contains functions used for concatenating
  1006. X *             two lists.
  1007. X *
  1008. X * This file is part of a suite of programs called Software Chipset.
  1009. X * The source code for Software Chipset has been released into the
  1010. X * public domain by its author, Paul Sander.
  1011. X *
  1012. X ***********************************************************************/
  1013. X
  1014. X#include <stdio.h>
  1015. X#include "dlpriv.h"
  1016. X
  1017. X/***********************************************************************
  1018. X *
  1019. X * dll_join -- This function 
  1020. X *
  1021. X ***********************************************************************/
  1022. X
  1023. X#ifdef __STDC__
  1024. XDL_LIST dll_join(DL_LIST plist1, DL_LIST plist2)
  1025. X#else
  1026. XDL_LIST dll_data(plist1,plist2)
  1027. XDL_LIST    plist1;
  1028. XDL_LIST    plist2;
  1029. X#endif
  1030. X{
  1031. X    LIST    *list1;
  1032. X    LIST    *list2;
  1033. X    LIST    *retval;
  1034. X
  1035. X    list1 = (LIST*) plist1;
  1036. X    list2 = (LIST*) plist2;
  1037. X    COVER("dll_join.c",1);
  1038. X
  1039. X    if ( list1 == NULL )
  1040. X    {
  1041. X        /*
  1042. X         * Return second list if first is NULL.  Note that the
  1043. X         * second may be NULL, which indicates an error.
  1044. X         */
  1045. X        COVER("dll_join.c",2);
  1046. X        retval = list2;
  1047. X    }
  1048. X    else if ( list2 == NULL )
  1049. X    {
  1050. X        /* Return first list if second is NULL */
  1051. X        COVER("dll_join.c",3);
  1052. X        retval = list1;
  1053. X    }
  1054. X    else if ( list1->comp != list2->comp )
  1055. X    {
  1056. X        /*
  1057. X         * Error if the comparison functions differ.  To
  1058. X         * do otherwise would mix data types in the same
  1059. X         * structure.
  1060. X         */
  1061. X        COVER("dll_join.c",4);
  1062. X        retval = NULL;
  1063. X    }
  1064. X    else if ( (list1->data != NULL) && (list2->data != NULL) &&
  1065. X              (list1->data != list2->data) )
  1066. X    {
  1067. X        /*
  1068. X         * Error if both lists have the global data item set and
  1069. X         * they differ.  To do otherwise loses one of the global
  1070. X         * data items.
  1071. X         */
  1072. X        COVER("dll_join.c",5);
  1073. X        retval = NULL;
  1074. X    }
  1075. X    else if (list1->last == NULL)
  1076. X    {
  1077. X        /*
  1078. X         * list1 is empty, return second list.  Note that the
  1079. X         * global data items must be checked, and if one of the
  1080. X         * lists has one, it must be returned.  list1 is freed.
  1081. X         */
  1082. X        COVER("dll_join.c",6);
  1083. X        retval = list2;
  1084. X        if (list1->data != NULL)
  1085. X        {
  1086. X            COVER("dll_join.c",7);
  1087. X            list2->data = list1->data;
  1088. X        }
  1089. X        free(list1);
  1090. X    }
  1091. X    else if (list2->last == NULL)
  1092. X    {
  1093. X        /*
  1094. X         * list2 is empty, return first list.  Note that the
  1095. X         * global data items must be checked, and if one of the
  1096. X         * lists has one, it must be returned.  list2 is freed.
  1097. X         */
  1098. X        COVER("dll_join.c",8);
  1099. X        retval = list1;
  1100. X        if (list2->data != NULL)
  1101. X        {
  1102. X            COVER("dll_join.c",9);
  1103. X            list1->data = list2->data;
  1104. X        }
  1105. X        free(list2);
  1106. X    }
  1107. X    else if ((list1->comp != NULL) &&
  1108. X             ((list1->comp)(list1->last->key,list2->last->next->key) > 0))
  1109. X    {
  1110. X        /*
  1111. X         * The lists are ordered, but the last item in the first list
  1112. X         * compares greater than the first item in the second list.
  1113. X         */
  1114. X        COVER("dll_join.c",10);
  1115. X        retval = NULL;
  1116. X    }
  1117. X    else
  1118. X    {
  1119. X        /* Concatenate the lists. */
  1120. X        COVER("dll_join.c",11);
  1121. X        retval = list1;
  1122. X        list2->last->next = list1->last->next;
  1123. X        list1->last = list2->last;
  1124. X        list1->size = list1->size + list2->size;
  1125. X        dll_touch(list1);
  1126. X        if (list1->data == NULL)
  1127. X        {
  1128. X            /* Don't lose the global data */
  1129. X            COVER("dll_join.c",12);
  1130. X            list1->data = list2->data;
  1131. X        }
  1132. X        free(list2);
  1133. X    }
  1134. X    return retval;
  1135. X}
  1136. X
  1137. X/****** End of File ******/
  1138. X
  1139. END_OF_src/list/dljoin.c
  1140. if test 3184 -ne `wc -c <src/list/dljoin.c`; then
  1141.     echo shar: \"src/list/dljoin.c\" unpacked with wrong size!
  1142. fi
  1143. # end of overwriting check
  1144. fi
  1145. echo shar: End of archive 4 \(of 10\).
  1146. cp /dev/null ark4isdone
  1147. MISSING=""
  1148. for I in 1 2 3 4 5 6 7 8 9 10 ; do
  1149.     if test ! -f ark${I}isdone ; then
  1150.     MISSING="${MISSING} ${I}"
  1151.     fi
  1152. done
  1153. if test "${MISSING}" = "" ; then
  1154.     echo You have unpacked all 10 archives.
  1155.     echo "Now edit common.mk and do a 'make all'"
  1156.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1157. else
  1158.     echo You still need to unpack the following archives:
  1159.     echo "        " ${MISSING}
  1160. fi
  1161. ##  End of shell archive.
  1162. exit 0
  1163.  
  1164.