#!/bin/sh
# This is a shell archive (produced by GNU sharutils 4.2).
# To extract the files from this archive, save it to some FILE, remove
# everything before the `!/bin/sh' line above, then type `sh FILE'.
#
# Made on 2000-02-16 10:51 EST by .
# Source directory was `/a/plank/blugreen/homes/plank/work/rb'.
#
# Existing files will *not* be overwritten unless `-c' is specified.
#
# This shar contains:
# length mode name
# ------ ---------- ------------------------------------------
# 28760 -rw-r--r-- LICENSE
# 5276 -rw-r--r-- README
# 14317 -rw-r--r-- ansi/rb.c
# 4040 -rw-r--r-- ansi/rb.h
# 2657 -rw-r--r-- ansi/main.c
# 1065 -rw-r--r-- ansi/makefile
# 2675 -rw-r--r-- non-ansi/main.c
# 1062 -rw-r--r-- non-ansi/makefile
# 14234 -rw-r--r-- non-ansi/rb.c
# 4083 -rw-r--r-- non-ansi/rb.h
#
save_IFS="${IFS}"
IFS="${IFS}:"
gettext_dir=FAILED
locale_dir=FAILED
first_param="$1"
for dir in $PATH
do
if test "$gettext_dir" = FAILED && test -f $dir/gettext \
&& ($dir/gettext --version >/dev/null 2>&1)
then
set `$dir/gettext --version 2>&1`
if test "$3" = GNU
then
gettext_dir=$dir
fi
fi
if test "$locale_dir" = FAILED && test -f $dir/shar \
&& ($dir/shar --print-text-domain-dir >/dev/null 2>&1)
then
locale_dir=`$dir/shar --print-text-domain-dir`
fi
done
IFS="$save_IFS"
if test "$locale_dir" = FAILED || test "$gettext_dir" = FAILED
then
echo=echo
else
TEXTDOMAINDIR=$locale_dir
export TEXTDOMAINDIR
TEXTDOMAIN=sharutils
export TEXTDOMAIN
echo="$gettext_dir/gettext -s"
fi
touch -am 1231235999 $$.touch >/dev/null 2>&1
if test ! -f 1231235999 && test -f $$.touch; then
shar_touch=touch
else
shar_touch=:
echo
$echo 'WARNING: not restoring timestamps. Consider getting and'
$echo "installing GNU \`touch', distributed in GNU File Utilities..."
echo
fi
rm -f 1231235999 $$.touch
#
if mkdir _sh18534; then
$echo 'x -' 'creating lock directory'
else
$echo 'failed to create lock directory'
exit 1
fi
# ============= LICENSE ==============
if test -f 'LICENSE' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'LICENSE' '(file already exists)'
else
$echo 'x -' extracting 'LICENSE' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'LICENSE' &&
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
X
[This is the first released version of the Lesser GPL. It also counts
X as the successor of the GNU Library Public License, version 2, hence
X the version number 2.1.]
X
[9]Preamble
X
X The licenses for most software are designed to take away your freedom
X to share and change it. By contrast, the GNU General Public Licenses
X are intended to guarantee your freedom to share and change free
X software--to make sure the software is free for all its users.
X
X This license, the Lesser General Public License, applies to some
X specially designated software packages--typically libraries--of the
X Free Software Foundation and other authors who decide to use it. You
X can use it too, but we suggest you first think carefully about whether
X this license or the ordinary General Public License is the better
X strategy to use in any particular case, based on the explanations
X below.
X
X When we speak of free software, we are referring to freedom of use,
X not price. Our General Public Licenses are designed to make sure that
X you have the freedom to distribute copies of free software (and charge
X for this service if you wish); that you receive source code or can get
X it if you want it; that you can change the software and use pieces of
X it in new free programs; and that you are informed that you can do
X these things.
X
X To protect your rights, we need to make restrictions that forbid
X distributors to deny you these rights or to ask you to surrender these
X rights. These restrictions translate to certain responsibilities for
X you if you distribute copies of the library or if you modify it.
X
X For example, if you distribute copies of the library, whether gratis
X or for a fee, you must give the recipients all the rights that we gave
X you. You must make sure that they, too, receive or can get the source
X code. If you link other code with the library, you must provide
X complete object files to the recipients, so that they can relink them
X with the library after making changes to the library and recompiling
X it. And you must show them these terms so they know their rights.
X
X We protect your rights with a two-step method: (1) we copyright the
X library, and (2) we offer you this license, which gives you legal
X permission to copy, distribute and/or modify the library.
X
X To protect each distributor, we want to make it very clear that there
X is no warranty for the free library. Also, if the library is modified
X by someone else and passed on, the recipients should know that what
X they have is not the original version, so that the original author's
X reputation will not be affected by problems that might be introduced
X by others.
X
X Finally, software patents pose a constant threat to the existence of
X any free program. We wish to make sure that a company cannot
X effectively restrict the users of a free program by obtaining a
X restrictive license from a patent holder. Therefore, we insist that
X any patent license obtained for a version of the library must be
X consistent with the full freedom of use specified in this license.
X
X Most GNU software, including some libraries, is covered by the
X ordinary GNU General Public License. This license, the GNU Lesser
X General Public License, applies to certain designated libraries, and
X is quite different from the ordinary General Public License. We use
X this license for certain libraries in order to permit linking those
X libraries into non-free programs.
X
X When a program is linked with a library, whether statically or using a
X shared library, the combination of the two is legally speaking a
X combined work, a derivative of the original library. The ordinary
X General Public License therefore permits such linking only if the
X entire combination fits its criteria of freedom. The Lesser General
X Public License permits more lax criteria for linking other code with
X the library.
X
X We call this license the "Lesser" General Public License because it
X does Less to protect the user's freedom than the ordinary General
X Public License. It also provides other free software developers Less
X of an advantage over competing non-free programs. These disadvantages
X are the reason we use the ordinary General Public License for many
X libraries. However, the Lesser license provides advantages in certain
X special circumstances.
X
X For example, on rare occasions, there may be a special need to
X encourage the widest possible use of a certain library, so that it
X becomes a de-facto standard. To achieve this, non-free programs must
X be allowed to use the library. A more frequent case is that a free
X library does the same job as widely used non-free libraries. In this
X case, there is little to gain by limiting the free library to free
X software only, so we use the Lesser General Public License.
X
X In other cases, permission to use a particular library in non-free
X programs enables a greater number of people to use a large body of
X free software. For example, permission to use the GNU C Library in
X non-free programs enables many more people to use the whole GNU
X operating system, as well as its variant, the GNU/Linux operating
X system.
X
X Although the Lesser General Public License is Less protective of the
X users' freedom, it does ensure that the user of a program that is
X linked with the Library has the freedom and the wherewithal to run
X that program using a modified version of the Library.
X
X The precise terms and conditions for copying, distribution and
X modification follow. Pay close attention to the difference between a
X "work based on the library" and a "work that uses the library". The
X former contains code derived from the library, whereas the latter must
X be combined with the library in order to run.
X
[10]TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
X
X 0. This License Agreement applies to any software library or other
X program which contains a notice placed by the copyright holder or
X other authorized party saying it may be distributed under the terms of
X this Lesser General Public License (also called "this License"). Each
X licensee is addressed as "you".
X
X A "library" means a collection of software functions and/or data
X prepared so as to be conveniently linked with application programs
X (which use some of those functions and data) to form executables.
X
X The "Library", below, refers to any such software library or work
X which has been distributed under these terms. A "work based on the
X Library" means either the Library or any derivative work under
X copyright law: that is to say, a work containing the Library or a
X portion of it, either verbatim or with modifications and/or translated
X straightforwardly into another language. (Hereinafter, translation is
X included without limitation in the term "modification".)
X
X "Source code" for a work means the preferred form of the work for
X making modifications to it. For a library, complete source code means
X all the source code for all modules it contains, plus any associated
X interface definition files, plus the scripts used to control
X compilation and installation of the library.
X
X Activities other than copying, distribution and modification are not
X covered by this License; they are outside its scope. The act of
X running a program using the Library is not restricted, and output from
X such a program is covered only if its contents constitute a work based
X on the Library (independent of the use of the Library in a tool for
X writing it). Whether that is true depends on what the Library does and
X what the program that uses the Library does.
X
X 1. You may copy and distribute verbatim copies of the Library's
X complete source code as you receive it, in any medium, provided that
X you conspicuously and appropriately publish on each copy an
X appropriate copyright notice and disclaimer of warranty; keep intact
X all the notices that refer to this License and to the absence of any
X warranty; and distribute a copy of this License along with the
X Library.
X
X You may charge a fee for the physical act of transferring a copy, and
X you may at your option offer warranty protection in exchange for a
X fee.
X
X 2. You may modify your copy or copies of the Library or any portion of
X it, thus forming a work based on the Library, and copy and distribute
X such modifications or work under the terms of Section 1 above,
X provided that you also meet all of these conditions:
X
X * a) The modified work must itself be a software library.
X * b) You must cause the files modified to carry prominent notices
X stating that you changed the files and the date of any change.
X * c) You must cause the whole of the work to be licensed at no
X charge to all third parties under the terms of this License.
X * d) If a facility in the modified Library refers to a function or a
X table of data to be supplied by an application program that uses
X the facility, other than as an argument passed when the facility
X is invoked, then you must make a good faith effort to ensure that,
X in the event an application does not supply such function or
X table, the facility still operates, and performs whatever part of
X its purpose remains meaningful.
X (For example, a function in a library to compute square roots has
X a purpose that is entirely well-defined independent of the
X application. Therefore, Subsection 2d requires that any
X application-supplied function or table used by this function must
X be optional: if the application does not supply it, the square
X root function must still compute square roots.)
X These requirements apply to the modified work as a whole. If
X identifiable sections of that work are not derived from the
X Library, and can be reasonably considered independent and separate
X works in themselves, then this License, and its terms, do not
X apply to those sections when you distribute them as separate
X works. But when you distribute the same sections as part of a
X whole which is a work based on the Library, the distribution of
X the whole must be on the terms of this License, whose permissions
X for other licensees extend to the entire whole, and thus to each
X and every part regardless of who wrote it.
X Thus, it is not the intent of this section to claim rights or
X contest your rights to work written entirely by you; rather, the
X intent is to exercise the right to control the distribution of
X derivative or collective works based on the Library.
X In addition, mere aggregation of another work not based on the
X Library with the Library (or with a work based on the Library) on
X a volume of a storage or distribution medium does not bring the
X other work under the scope of this License.
X
X 3. You may opt to apply the terms of the ordinary GNU General Public
X License instead of this License to a given copy of the Library. To do
X this, you must alter all the notices that refer to this License, so
X that they refer to the ordinary GNU General Public License, version 2,
X instead of to this License. (If a newer version than version 2 of the
X ordinary GNU General Public License has appeared, then you can specify
X that version instead if you wish.) Do not make any other change in
X these notices.
X
X Once this change is made in a given copy, it is irreversible for that
X copy, so the ordinary GNU General Public License applies to all
X subsequent copies and derivative works made from that copy.
X
X This option is useful when you wish to copy part of the code of the
X Library into a program that is not a library.
X
X 4. You may copy and distribute the Library (or a portion or derivative
X of it, under Section 2) in object code or executable form under the
X terms of Sections 1 and 2 above provided that you accompany it with
X the complete corresponding machine-readable source code, which must be
X distributed under the terms of Sections 1 and 2 above on a medium
X customarily used for software interchange.
X
X If distribution of object code is made by offering access to copy from
X a designated place, then offering equivalent access to copy the source
X code from the same place satisfies the requirement to distribute the
X source code, even though third parties are not compelled to copy the
X source along with the object code.
X
X 5. A program that contains no derivative of any portion of the
X Library, but is designed to work with the Library by being compiled or
X linked with it, is called a "work that uses the Library". Such a work,
X in isolation, is not a derivative work of the Library, and therefore
X falls outside the scope of this License.
X
X However, linking a "work that uses the Library" with the Library
X creates an executable that is a derivative of the Library (because it
X contains portions of the Library), rather than a "work that uses the
X library". The executable is therefore covered by this License. Section
X 6 states terms for distribution of such executables.
X
X When a "work that uses the Library" uses material from a header file
X that is part of the Library, the object code for the work may be a
X derivative work of the Library even though the source code is not.
X Whether this is true is especially significant if the work can be
X linked without the Library, or if the work is itself a library. The
X threshold for this to be true is not precisely defined by law.
X
X If such an object file uses only numerical parameters, data structure
X layouts and accessors, and small macros and small inline functions
X (ten lines or less in length), then the use of the object file is
X unrestricted, regardless of whether it is legally a derivative work.
X (Executables containing this object code plus portions of the Library
X will still fall under Section 6.)
X
X Otherwise, if the work is a derivative of the Library, you may
X distribute the object code for the work under the terms of Section 6.
X Any executables containing that work also fall under Section 6,
X whether or not they are linked directly with the Library itself.
X
X 6. As an exception to the Sections above, you may also combine or link
X a "work that uses the Library" with the Library to produce a work
X containing portions of the Library, and distribute that work under
X terms of your choice, provided that the terms permit modification of
X the work for the customer's own use and reverse engineering for
X debugging such modifications.
X
X You must give prominent notice with each copy of the work that the
X Library is used in it and that the Library and its use are covered by
X this License. You must supply a copy of this License. If the work
X during execution displays copyright notices, you must include the
X copyright notice for the Library among them, as well as a reference
X directing the user to the copy of this License. Also, you must do one
X of these things:
X
X * a) Accompany the work with the complete corresponding
X machine-readable source code for the Library including whatever
X changes were used in the work (which must be distributed under
X Sections 1 and 2 above); and, if the work is an executable linked
X with the Library, with the complete machine-readable "work that
X uses the Library", as object code and/or source code, so that the
X user can modify the Library and then relink to produce a modified
X executable containing the modified Library. (It is understood that
X the user who changes the contents of definitions files in the
X Library will not necessarily be able to recompile the application
X to use the modified definitions.)
X * b) Use a suitable shared library mechanism for linking with the
X Library. A suitable mechanism is one that (1) uses at run time a
X copy of the library already present on the user's computer system,
X rather than copying library functions into the executable, and (2)
X will operate properly with a modified version of the library, if
X the user installs one, as long as the modified version is
X interface-compatible with the version that the work was made with.
X * c) Accompany the work with a written offer, valid for at least
X three years, to give the same user the materials specified in
X Subsection 6a, above, for a charge no more than the cost of
X performing this distribution.
X * d) If distribution of the work is made by offering access to copy
X from a designated place, offer equivalent access to copy the above
X specified materials from the same place.
X * e) Verify that the user has already received a copy of these
X materials or that you have already sent this user a copy.
X
X For an executable, the required form of the "work that uses the
X Library" must include any data and utility programs needed for
X reproducing the executable from it. However, as a special exception,
X the materials to be distributed need not include anything that is
X normally distributed (in either source or binary form) with the major
X components (compiler, kernel, and so on) of the operating system on
X which the executable runs, unless that component itself accompanies
X the executable.
X
X It may happen that this requirement contradicts the license
X restrictions of other proprietary libraries that do not normally
X accompany the operating system. Such a contradiction means you cannot
X use both them and the Library together in an executable that you
X distribute.
X
X 7. You may place library facilities that are a work based on the
X Library side-by-side in a single library together with other library
X facilities not covered by this License, and distribute such a combined
X library, provided that the separate distribution of the work based on
X the Library and of the other library facilities is otherwise
X permitted, and provided that you do these two things:
X
X * a) Accompany the combined library with a copy of the same work
X based on the Library, uncombined with any other library
X facilities. This must be distributed under the terms of the
X Sections above.
X * b) Give prominent notice with the combined library of the fact
X that part of it is a work based on the Library, and explaining
X where to find the accompanying uncombined form of the same work.
X
X 8. You may not copy, modify, sublicense, link with, or distribute the
X Library except as expressly provided under this License. Any attempt
X otherwise to copy, modify, sublicense, link with, or distribute the
X Library is void, and will automatically terminate your rights under
X this License. However, parties who have received copies, or rights,
X from you under this License will not have their licenses terminated so
X long as such parties remain in full compliance.
X
X 9. You are not required to accept this License, since you have not
X signed it. However, nothing else grants you permission to modify or
X distribute the Library or its derivative works. These actions are
X prohibited by law if you do not accept this License. Therefore, by
X modifying or distributing the Library (or any work based on the
X Library), you indicate your acceptance of this License to do so, and
X all its terms and conditions for copying, distributing or modifying
X the Library or works based on it.
X
X 10. Each time you redistribute the Library (or any work based on the
X Library), the recipient automatically receives a license from the
X original licensor to copy, distribute, link with or modify the Library
X subject to these terms and conditions. You may not impose any further
X restrictions on the recipients' exercise of the rights granted herein.
X You are not responsible for enforcing compliance by third parties with
X this License.
X
X 11. If, as a consequence of a court judgment or allegation of patent
X infringement or for any other reason (not limited to patent issues),
X conditions are imposed on you (whether by court order, agreement or
X otherwise) that contradict the conditions of this License, they do not
X excuse you from the conditions of this License. If you cannot
X distribute so as to satisfy simultaneously your obligations under this
X License and any other pertinent obligations, then as a consequence you
X may not distribute the Library at all. For example, if a patent
X license would not permit royalty-free redistribution of the Library by
X all those who receive copies directly or indirectly through you, then
X the only way you could satisfy both it and this License would be to
X refrain entirely from distribution of the Library.
X
X If any portion of this section is held invalid or unenforceable under
X any particular circumstance, the balance of the section is intended to
X apply, and the section as a whole is intended to apply in other
X circumstances.
X
X It is not the purpose of this section to induce you to infringe any
X patents or other property right claims or to contest validity of any
X such claims; this section has the sole purpose of protecting the
X integrity of the free software distribution system which is
X implemented by public license practices. Many people have made
X generous contributions to the wide range of software distributed
X through that system in reliance on consistent application of that
X system; it is up to the author/donor to decide if he or she is willing
X to distribute software through any other system and a licensee cannot
X impose that choice.
X
X This section is intended to make thoroughly clear what is believed to
X be a consequence of the rest of this License.
X
X 12. If the distribution and/or use of the Library is restricted in
X certain countries either by patents or by copyrighted interfaces, the
X original copyright holder who places the Library under this License
X may add an explicit geographical distribution limitation excluding
X those countries, so that distribution is permitted only in or among
X countries not thus excluded. In such case, this License incorporates
X the limitation as if written in the body of this License.
X
X 13. The Free Software Foundation may publish revised and/or new
X versions of the Lesser General Public License from time to time. Such
X new versions will be similar in spirit to the present version, but may
X differ in detail to address new problems or concerns.
X
X Each version is given a distinguishing version number. If the Library
X specifies a version number of this License which applies to it and
X "any later version", you have the option of following the terms and
X conditions either of that version or of any later version published by
X the Free Software Foundation. If the Library does not specify a
X license version number, you may choose any version ever published by
X the Free Software Foundation.
X
X 14. If you wish to incorporate parts of the Library into other free
X programs whose distribution conditions are incompatible with these,
X write to the author to ask for permission. For software which is
X copyrighted by the Free Software Foundation, write to the Free
X Software Foundation; we sometimes make exceptions for this. Our
X decision will be guided by the two goals of preserving the free status
X of all derivatives of our free software and of promoting the sharing
X and reuse of software generally.
X
X NO WARRANTY
X
X 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
X WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
X EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
X OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
X KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
X IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
X PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
X LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
X THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
X
X 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
X WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
X AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
X FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
X CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
X LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
X RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
X FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
X SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
X DAMAGES.
X
END OF TERMS AND CONDITIONS
X
[11]How to Apply These Terms to Your New Libraries
X
X If you develop a new library, and you want it to be of the greatest
X possible use to the public, we recommend making it free software that
X everyone can redistribute and change. You can do so by permitting
X redistribution under these terms (or, alternatively, under the terms
X of the ordinary General Public License).
X
X To apply these terms, attach the following notices to the library. It
X is safest to attach them to the start of each source file to most
X effectively convey the exclusion of warranty; and each file should
X have at least the "copyright" line and a pointer to where the full
X notice is found.
X
one line to give the library's name and an idea of what it does.
Copyright (C) year name of author
X
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
X
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
X
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
X
X Also add information on how to contact you by electronic and paper
X mail.
X
X You should also get your employer (if you work as a programmer) or
X your school, if any, to sign a "copyright disclaimer" for the library,
X if necessary. Here is a sample; alter the names:
X
Yoyodyne, Inc., hereby disclaims all copyright interest in
the library `Frob' (a library for tweaking knobs) written
by James Random Hacker.
X
signature of Ty Coon, 1 April 1990
Ty Coon, President of Vice
X
X That's all there is to it!
X _________________________________________________________________
X
X Return to [12]GNU's home page.
X
X FSF & GNU inquiries & questions to [13]gnu@gnu.org. Other [14]ways to
X contact the FSF.
X
X Comments on these web pages to [15]webmasters@www.gnu.org, send other
X questions to [16]gnu@gnu.org.
X
X Copyright notice above.
X Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
X MA 02111, USA
X
X Updated: 7 Jan 2000 rms
X _________________________________________________________________
X
References
X
X 1. http://www.gnu.org/graphics/philosophicalgnu.html
X 2. http://www.gnu.org/copyleft/copyleft.html#translationsLGPL
X 3. http://www.gnu.org/philosophy/why-not-lgpl.html
X 4. http://www.gnu.org/copyleft/lesser.html#SEC1
X 5. http://www.gnu.org/copyleft/lesser.html#SEC2
X 6. http://www.gnu.org/copyleft/lesser.html#SEC3
X 7. http://www.gnu.org/copyleft/lesser.html#SEC4
X 8. http://www.gnu.org/copyleft/lesser.html#TOC1
X 9. http://www.gnu.org/copyleft/lesser.html#TOC2
X 10. http://www.gnu.org/copyleft/lesser.html#TOC3
X 11. http://www.gnu.org/copyleft/lesser.html#TOC4
X 12. http://www.gnu.org/home.html
X 13. mailto:gnu@gnu.org
X 14. http://www.gnu.org/home.html#ContactInfo
X 15. mailto:webmasters@www.gnu.org
X 16. mailto:gnu@gnu.org
SHAR_EOF
$shar_touch -am 02161051100 'LICENSE' &&
chmod 0644 'LICENSE' ||
$echo 'restore of' 'LICENSE' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'LICENSE:' 'MD5 check failed'
c99b5156d2f2badddd962c51fd6440a6 LICENSE
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'LICENSE'`"
test 28760 -eq "$shar_count" ||
$echo 'LICENSE:' 'original size' '28760,' 'current size' "$shar_count!"
fi
fi
# ============= README ==============
if test -f 'README' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'README' '(file already exists)'
else
$echo 'x -' extracting 'README' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'README' &&
Generic C code for red-black trees.
Copyright (C) 2000 James S. Plank
X
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
X
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
X
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
X
---------------------------------------------------------------------------
Jim Plank
plank@cs.utk.edu
http://web.eecs.utk.edu/~jplank
X
Department of Computer Science
University of Tennessee
107 Ayres Hall
Knoxville, TN 37996
X
615-974-4397
$Revision: 1.2 $
---------------------------------------------------------------------------
X
Rb.c and rb.h are files for doing general red-black trees.
X
The directory ansi contains ansi-standard c code for rb-trees (there
are some gross pointer casts there but don't worry about them. They
work). The directory non-ansi contains straight c code for rb-trees.
The ansi version can also be used by c++ (it has been tested).
X
Rb.h contains the typedef for red-black tree structures. Basically,
red-black trees are balanced trees whose external nodes are sorted
by a key, and connected in a linked list. The following is how
you use rb.c and rb.h:
X
Include rb.h in your source.
X
Make_rb() returns the head of a red-black tree. It serves two functions:
Its p.root pointer points to the root of the red-black tree. Its
c.list.flink and c.list.blink pointers point to the first and last
external nodes of the tree. When the tree is empty, all these pointers
point to itself.
X
The external nodes can be traversed in sorted order with their
c.list.flink and c.list.blink pointers. The macros rb_first, rb_last,
rb_next, rb_prev, and rb_traverse can be used to traverse external node lists.
X
External nodes hold two pieces of information: the key and the value
(in k.key and v.val, respectively). The key can be a character
string, an integer, or a general pointer. Val is typed as a character
pointer, but can be any pointer. If the key is a character string,
then one can insert it, and a value into a tree with rb_insert(). If
it is an integer, then rb_inserti() should be used. If it is a general
pointer, then rb_insertg() must be used, with a comparison function
passed as the fourth argument. This function takes two keys as arguments,
and returns a negative value, positive value, or 0, depending on whether
the first key is less than, greater than or equal to the second. Thus,
one could use rb_insertg(t, s, v, strcmp) to insert the value v with
a character string s into the tree t.
X
Rb_find_key(t, k) returns the external node in t whose value is equal
k or whose value is the smallest value greater than k. (Here, k is
a string). If there is no value greater than or equal to k, then
t is returned. Rb_find_ikey(t,k) works when k is an integer, and
Rb_find_gkey(t,k,cmp) works for general pointers.
X
Rb_find_key_n(t, k, n) is the same as rb_find_key, except that it
returns whether or not k was found in n (n is an int *). Rb_find_ikey_n
and Rb_find_gkey_n are the analogous routines for integer and general
keys.
X
Rb_insert_b(e, k, v) makes a new external node with key k and val v, and
inserts it before the external node e. If e is the head of a tree,
then the new node is inserted at the end of the external node list.
If this insertion violates sorted order, no error is flagged. It is
assumed that the user knows what he/she is doing. Rb_insert_a(e,k,v)
inserts the new node after e (if e = t, it inserts the new node at the
beginning of the list).
X
Rb_insert() is therefore really a combination of Rb_find_key() and
Rb_insert_b().
X
Rb_delete_node(e) deletes the external node e from a tree (and thus from
the linked list of external nodes). The node is free'd as well, so
don't retain pointers to it.
X
Red-black trees are spiffy because find_key, insert, and delete are all
done in log(n) time. Thus, they can be freely used instead of hash-tables,
with the benifit of having the elements in sorted order at all times, and
with the guarantee of operations being in log(n) time.
X
Other routines:
X
Rb_print_tree() will grossly print out a red-black tree with string keys.
Rb_iprint_tree() will do the same with trees with integer keys.
Rb_nblack(e) will report the number of black nodes on the path from external
X node e to the root. The path length is less than twice this value.
Rb_plength(e) reports the length of the path from e to the root.
X
X
You can find a general description of red-black trees in any basic algorithms
text. E.g. ``Introduction to Algorithms'', by Cormen, Leiserson and Rivest
(McGraw Hill). An excellent and complete description of red-black trees
can also be found in Chapter 1 of Heather Booth's PhD disseratation:
``Some Fast Algorithms on Graphs and Trees'', Princeton University, 1990.
SHAR_EOF
$shar_touch -am 02161044100 'README' &&
chmod 0644 'README' ||
$echo 'restore of' 'README' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'README:' 'MD5 check failed'
6cbacf3b8913ec2d267174cecc7f5d37 README
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'README'`"
test 5276 -eq "$shar_count" ||
$echo 'README:' 'original size' '5276,' 'current size' "$shar_count!"
fi
fi
# ============= ansi/rb.c ==============
if test ! -d 'ansi'; then
$echo 'x -' 'creating directory' 'ansi'
mkdir 'ansi'
fi
if test -f 'ansi/rb.c' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'ansi/rb.c' '(file already exists)'
else
$echo 'x -' extracting 'ansi/rb.c' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'ansi/rb.c' &&
/*
Generic C code for red-black trees.
Copyright (C) 2000 James S. Plank
X
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
X
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
X
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
X */
/* Revision 1.2. Jim Plank */
X
/* Original code by Jim Plank (plank@cs.utk.edu) */
/* modified for THINK C 6.0 for Macintosh by Chris Bartley */
X
#include
#include
#include
#include
#include "rb.h"
X
static void mk_new_int(Rb_node l, Rb_node r, Rb_node p, int il);
static Rb_node lprev(Rb_node n);
static Rb_node rprev(Rb_node n);
static void recolor(Rb_node n);
static void single_rotate(Rb_node y, int l);
static void rb_print_tree(Rb_node t, int level);
static void rb_iprint_tree(Rb_node t, int level);
X
X
#define isred(n) (n->s.red)
#define isblack(n) (!isred(n))
#define isleft(n) (n->s.left)
#define isright(n) (!isleft(n))
#define isint(n) (n->s.internal)
#define isext(n) (!isint(n))
#define ishead(n) (n->s.head)
#define isroot(n) (n->s.root)
#define setred(n) n->s.red = 1
#define setblack(n) n->s.red = 0
#define setleft(n) n->s.left = 1
#define setright(n) n->s.left = 0
#define sethead(n) n->s.head = 1
#define setroot(n) n->s.root = 1
#define setint(n) n->s.internal = 1
#define setext(n) n->s.internal = 0
#define setnormal(n) { n->s.root = 0; n ->s.head = 0; }
#define sibling(n) ((isleft(n)) ? n->p.parent->c.child.right \
X : n->p.parent->c.child.left)
X
static void insert(Rb_node item, Rb_node list) /* Inserts to the end of a list */
{
X Rb_node last_node;
X
X last_node = list->c.list.blink;
X
X list->c.list.blink = item;
X last_node->c.list.flink = item;
X item->c.list.blink = last_node;
X item->c.list.flink = list;
}
X
static void delete_item(Rb_node item) /* Deletes an arbitrary iterm */
{
X item->c.list.flink->c.list.blink = item->c.list.blink;
X item->c.list.blink->c.list.flink = item->c.list.flink;
}
X
#define mk_new_ext(new, kkkey, vvval) {\
X new = (Rb_node) malloc(sizeof(struct rb_node));\
X new->v.val = vvval;\
X new->k.key = kkkey;\
X setext(new);\
X setblack(new);\
X setnormal(new);\
}
X
static void mk_new_int(Rb_node l, Rb_node r, Rb_node p, int il)
{
X Rb_node newnode;
X
X newnode = (Rb_node) malloc(sizeof(struct rb_node));
X setint(newnode);
X setred(newnode);
X setnormal(newnode);
X newnode->c.child.left = l;
X newnode->c.child.right = r;
X newnode->p.parent = p;
X newnode->k.lext = l;
X newnode->v.rext = r;
X l->p.parent = newnode;
X r->p.parent = newnode;
X setleft(l);
X setright(r);
X if (ishead(p)) {
X p->p.root = newnode;
X setroot(newnode);
X } else if (il) {
X setleft(newnode);
X p->c.child.left = newnode;
X } else {
X setright(newnode);
X p->c.child.right = newnode;
X }
X recolor(newnode);
}
X
X
Rb_node lprev(Rb_node n)
{
X if (ishead(n)) return n;
X while (!isroot(n)) {
X if (isright(n)) return n->p.parent;
X n = n->p.parent;
X }
X return n->p.parent;
}
X
Rb_node rprev(Rb_node n)
{
X if (ishead(n)) return n;
X while (!isroot(n)) {
X if (isleft(n)) return n->p.parent;
X n = n->p.parent;
X }
X return n->p.parent;
}
X
Rb_node make_rb()
{
X Rb_node head;
X
X head = (Rb_node) malloc (sizeof(struct rb_node));
X head->c.list.flink = head;
X head->c.list.blink = head;
X head->p.root = head;
X head->k.key = "";
X sethead(head);
X return head;
}
X
Rb_node rb_find_key_n(Rb_node n, char *key, int *fnd)
{
X int cmp;
X
X *fnd = 0;
X if (!ishead(n)) {
X fprintf(stderr, "rb_find_key_n called on non-head 0x%x\n", n);
X exit(1);
X }
X if (n->p.root == n) return n;
X cmp = strcmp(key, n->c.list.blink->k.key);
X if (cmp == 0) {
X *fnd = 1;
X return n->c.list.blink;
X }
X if (cmp > 0) return n;
X else n = n->p.root;
X while (1) {
X if (isext(n)) return n;
X cmp = strcmp(key, n->k.lext->k.key);
X if (cmp == 0) {
X *fnd = 1;
X return n->k.lext;
X }
X if (cmp < 0) n = n->c.child.left ; else n = n->c.child.right;
X }
}
X
Rb_node rb_find_key(Rb_node n, char *key)
{
X int fnd;
X return rb_find_key_n(n, key, &fnd);
}
X
Rb_node rb_find_ikey_n(Rb_node n, int ikey, int *fnd)
{
X *fnd = 0;
X if (!ishead(n)) {
X fprintf(stderr, "rb_find_ikey_n called on non-head 0x%x\n", n);
X exit(1);
X }
X if (n->p.root == n) return n;
X if (ikey == n->c.list.blink->k.ikey) {
X *fnd = 1;
X return n->c.list.blink;
X }
X if (ikey > n->c.list.blink->k.ikey) return n;
X else n = n->p.root;
X while (1) {
X if (isext(n)) return n;
X if (ikey == n->k.lext->k.ikey) {
X *fnd = 1;
X return n->k.lext;
X }
X n = (ikey < n->k.lext->k.ikey) ? n->c.child.left : n->c.child.right;
X }
}
X
Rb_node rb_find_ikey(Rb_node n, int ikey)
{
X int fnd;
X return rb_find_ikey_n(n, ikey, &fnd);
}
X
Rb_node rb_find_gkey_n(Rb_node n, char *key,int (*fxn)(char *, char *), int *fnd)
{
X int cmp;
X
X *fnd = 0;
X if (!ishead(n)) {
X fprintf(stderr, "rb_find_key_n called on non-head 0x%x\n", n);
X exit(1);
X }
X if (n->p.root == n) return n;
X cmp = (*fxn)(key, n->c.list.blink->k.key);
X if (cmp == 0) {
X *fnd = 1;
X return n->c.list.blink;
X }
X if (cmp > 0) return n;
X else n = n->p.root;
X while (1) {
X if (isext(n)) return n;
X cmp = (*fxn)(key, n->k.lext->k.key);
X if (cmp == 0) {
X *fnd = 1;
X return n->k.lext;
X }
X if (cmp < 0) n = n->c.child.left ; else n = n->c.child.right;
X }
}
X
Rb_node rb_find_gkey(Rb_node n, char *key, int (*fxn)(char *, char *))
{
X int fnd;
X return rb_find_gkey_n(n, key, fxn, &fnd);
}
X
Rb_node rb_insert_b(Rb_node n, char *key, char *val)
{
X Rb_node newleft, newright, newnode, list, p;
X
X if (ishead(n)) {
X if (n->p.root == n) { /* Tree is empty */
X mk_new_ext(newnode, key, val);
X insert(newnode, n);
X n->p.root = newnode;
X newnode->p.parent = n;
X setroot(newnode);
X return newnode;
X } else {
X mk_new_ext(newright, key, val);
X insert(newright, n);
X newleft = newright->c.list.blink;
X setnormal(newleft);
X mk_new_int(newleft, newright, newleft->p.parent, isleft(newleft));
X p = rprev(newright);
X if (!ishead(p)) p->k.lext = newright;
X return newright;
X }
X } else {
X mk_new_ext(newleft, key, val);
X insert(newleft, n);
X setnormal(n);
X mk_new_int(newleft, n, n->p.parent, isleft(n));
X p = lprev(newleft);
X if (!ishead(p)) p->v.rext = newleft;
X return newleft;
X }
}
X
static void recolor(Rb_node n)
{
X Rb_node p, gp, s;
X int done = 0;
X
X while(!done) {
X if (isroot(n)) {
X setblack(n);
X return;
X }
X
X p = n->p.parent;
X
X if (isblack(p)) return;
X
X if (isroot(p)) {
X setblack(p);
X return;
X }
X
X gp = p->p.parent;
X s = sibling(p);
X if (isred(s)) {
X setblack(p);
X setred(gp);
X setblack(s);
X n = gp;
X } else {
X done = 1;
X }
X }
X /* p's sibling is black, p is red, gp is black */
X
X if ((isleft(n) == 0) == (isleft(p) == 0)) {
X single_rotate(gp, isleft(n));
X setblack(p);
X setred(gp);
X } else {
X single_rotate(p, isleft(n));
X single_rotate(gp, isleft(n));
X setblack(n);
X setred(gp);
X }
}
X
static void single_rotate(Rb_node y, int l)
{
X int rl, ir;
X Rb_node x, yp;
X char *tmp;
X
X ir = isroot(y);
X yp = y->p.parent;
X if (!ir) {
X rl = isleft(y);
X }
X
X if (l) {
X x = y->c.child.left;
X y->c.child.left = x->c.child.right;
X setleft(y->c.child.left);
X y->c.child.left->p.parent = y;
X x->c.child.right = y;
X setright(y);
X } else {
X x = y->c.child.right;
X y->c.child.right = x->c.child.left;
X setright(y->c.child.right);
X y->c.child.right->p.parent = y;
X x->c.child.left = y;
X setleft(y);
X }
X
X x->p.parent = yp;
X y->p.parent = x;
X if (ir) {
X yp->p.root = x;
X setnormal(y);
X setroot(x);
X } else {
X if (rl) {
X yp->c.child.left = x;
X setleft(x);
X } else {
X yp->c.child.right = x;
X setright(x);
X }
X }
}
X
void rb_delete_node(Rb_node n)
{
X Rb_node s, p, gp;
X char ir;
X
X if (isint(n)) {
X fprintf(stderr, "Cannot delete an internal node: 0x%x\n", n);
X exit(1);
X }
X if (ishead(n)) {
X fprintf(stderr, "Cannot delete the head of an rb_tree: 0x%x\n", n);
X exit(1);
X }
X delete_item(n); /* Delete it from the list */
X p = n->p.parent; /* The only node */
X if (isroot(n)) {
X p->p.root = p;
X free(n);
X return;
X }
X s = sibling(n); /* The only node after deletion */
X if (isroot(p)) {
X s->p.parent = p->p.parent;
X s->p.parent->p.root = s;
X setroot(s);
X free(p);
X free(n);
X return;
X }
X gp = p->p.parent; /* Set parent to sibling */
X s->p.parent = gp;
X if (isleft(p)) {
X gp->c.child.left = s;
X setleft(s);
X } else {
X gp->c.child.right = s;
X setright(s);
X }
X ir = isred(p);
X free(p);
X free(n);
X
X if (isext(s)) { /* Update proper rext and lext values */
X p = lprev(s);
X if (!ishead(p)) p->v.rext = s;
X p = rprev(s);
X if (!ishead(p)) p->k.lext = s;
X } else if (isblack(s)) {
X fprintf(stderr, "DELETION PROB -- sib is black, internal\n");
X exit(1);
X } else {
X p = lprev(s);
X if (!ishead(p)) p->v.rext = s->c.child.left;
X p = rprev(s);
X if (!ishead(p)) p->k.lext = s->c.child.right;
X setblack(s);
X return;
X }
X
X if (ir) return;
X
X /* Recolor */
X
X n = s;
X p = n->p.parent;
X s = sibling(n);
X while(isblack(p) && isblack(s) && isint(s) &&
X isblack(s->c.child.left) && isblack(s->c.child.right)) {
X setred(s);
X n = p;
X if (isroot(n)) return;
X p = n->p.parent;
X s = sibling(n);
X }
X
X if (isblack(p) && isred(s)) { /* Rotation 2.3b */
X single_rotate(p, isright(n));
X setred(p);
X setblack(s);
X s = sibling(n);
X }
X
X { Rb_node x, z; char il;
X
X if (isext(s)) {
X fprintf(stderr, "DELETION ERROR: sibling not internal\n");
X exit(1);
X }
X
X il = isleft(n);
X x = il ? s->c.child.left : s->c.child.right ;
X z = sibling(x);
X
X if (isred(z)) { /* Rotation 2.3f */
X single_rotate(p, !il);
X setblack(z);
X if (isred(p)) setred(s); else setblack(s);
X setblack(p);
X } else if (isblack(x)) { /* Recoloring only (2.3c) */
X if (isred(s) || isblack(p)) {
X fprintf(stderr, "DELETION ERROR: 2.3c not quite right\n");
X exit(1);
X }
X setblack(p);
X setred(s);
X return;
X } else if (isred(p)) { /* 2.3d */
X single_rotate(s, il);
X single_rotate(p, !il);
X setblack(x);
X setred(s);
X return;
X } else { /* 2.3e */
X single_rotate(s, il);
X single_rotate(p, !il);
X setblack(x);
X return;
X }
X }
}
X
X
void rb_print_tree(Rb_node t, int level)
{
X int i;
X if (ishead(t) && t->p.parent == t) {
X printf("tree 0x%x is empty\n", t);
X } else if (ishead(t)) {
X printf("Head: 0x%x. Root = 0x%x\n", t, t->p.root);
X rb_print_tree(t->p.root, 0);
X } else {
X if (isext(t)) {
X for (i = 0; i < level; i++) putchar(' ');
X printf("Ext node 0x%x: %c,%c: p=0x%x, k=%s\n",
X t, isred(t)?'R':'B', isleft(t)?'l':'r', t->p.parent, t->k.key);
X } else {
X rb_print_tree(t->c.child.left, level+2);
X rb_print_tree(t->c.child.right, level+2);
X for (i = 0; i < level; i++) putchar(' ');
X printf("Int node 0x%x: %c,%c: l=0x%x, r=0x%x, p=0x%x, lr=(%s,%s)\n",
X t, isred(t)?'R':'B', isleft(t)?'l':'r', t->c.child.left,
X t->c.child.right,
X t->p.parent, t->k.lext->k.key, t->v.rext->k.key);
X }
X }
}
X
void rb_iprint_tree(Rb_node t, int level)
{
X int i;
X if (ishead(t) && t->p.parent == t) {
X printf("tree 0x%x is empty\n", t);
X } else if (ishead(t)) {
X printf("Head: 0x%x. Root = 0x%x, < = 0x%x, > = 0x%x\n",
X t, t->p.root, t->c.list.blink, t->c.list.flink);
X rb_iprint_tree(t->p.root, 0);
X } else {
X if (isext(t)) {
X for (i = 0; i < level; i++) putchar(' ');
X printf("Ext node 0x%x: %c,%c: p=0x%x, <=0x%x, >=0x%x k=%d\n",
X t, isred(t)?'R':'B', isleft(t)?'l':'r', t->p.parent,
X t->c.list.blink, t->c.list.flink, t->k.ikey);
X } else {
X rb_iprint_tree(t->c.child.left, level+2);
X rb_iprint_tree(t->c.child.right, level+2);
X for (i = 0; i < level; i++) putchar(' ');
X printf("Int node 0x%x: %c,%c: l=0x%x, r=0x%x, p=0x%x, lr=(%d,%d)\n",
X t, isred(t)?'R':'B', isleft(t)?'l':'r', t->c.child.left,
X t->c.child.right,
X t->p.parent, t->k.lext->k.ikey, t->v.rext->k.ikey);
X }
X }
}
X
int rb_nblack(Rb_node n)
{
X int nb;
X if (ishead(n) || isint(n)) {
X fprintf(stderr, "ERROR: rb_nblack called on a non-external node 0x%x\n",
X n);
X exit(1);
X }
X nb = 0;
X while(!ishead(n)) {
X if (isblack(n)) nb++;
X n = n->p.parent;
X }
X return nb;
}
X
int rb_plength(Rb_node n)
{
X int pl;
X if (ishead(n) || isint(n)) {
X fprintf(stderr, "ERROR: rb_plength called on a non-external node 0x%x\n",
X n);
X exit(1);
X }
X pl = 0;
X while(!ishead(n)) {
X pl++;
X n = n->p.parent;
X }
X return pl;
}
X
void rb_free_tree(Rb_node n)
{
X if (!ishead(n)) {
X fprintf(stderr, "ERROR: Rb_free_tree called on a non-head node\n");
X exit(1);
X }
X
X while(rb_first(n) != rb_nil(n)) {
X rb_delete_node(rb_first(n));
X }
X free(n);
}
X
char *rb_val(Rb_node n)
{
X return n->v.val;
}
X
Rb_node rb_insert_a(Rb_node nd, char *key, char *val)
{
X rb_insert_b(nd->c.list.flink, key, val);
}
X
Rb_node rb_insert(Rb_node tree, char *key, char *val)
{
X rb_insert_b(rb_find_key(tree, key), key, val);
}
X
Rb_node rb_inserti(Rb_node tree, int ikey, char *val)
{
X rb_insert_b(rb_find_ikey(tree, ikey), (char *) ikey, val);
}
X
Rb_node rb_insertg(Rb_node tree, char *key, char *val,
X int (*func)(char *, char *))
{
X rb_insert_b(rb_find_gkey(tree, key, func), key, val);
}
X
X
SHAR_EOF
$shar_touch -am 02161045100 'ansi/rb.c' &&
chmod 0644 'ansi/rb.c' ||
$echo 'restore of' 'ansi/rb.c' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'ansi/rb.c:' 'MD5 check failed'
451b718b60cf80b9ce8158e0697f4942 ansi/rb.c
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'ansi/rb.c'`"
test 14317 -eq "$shar_count" ||
$echo 'ansi/rb.c:' 'original size' '14317,' 'current size' "$shar_count!"
fi
fi
# ============= ansi/rb.h ==============
if test -f 'ansi/rb.h' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'ansi/rb.h' '(file already exists)'
else
$echo 'x -' extracting 'ansi/rb.h' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'ansi/rb.h' &&
/*
Generic C code for red-black trees.
Copyright (C) 2000 James S. Plank
X
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
X
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
X
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
X */
X
/* Revision 1.2. Jim Plank */
X
/* Original code by Jim Plank (plank@cs.utk.edu) */
/* modified for THINK C 6.0 for Macintosh by Chris Bartley */
X
typedef struct {
X unsigned red : 1 ;
X unsigned internal : 1 ;
X unsigned left : 1 ;
X unsigned root : 1 ;
X unsigned head : 1 ;
} status;
X
/* Main rb_node. You only ever use the fields
X
X c.list.flink
X c.list.blink
X
X k.key or k.ikey
X v.val
*/
X
X
typedef struct rb_node {
X union {
X struct {
X struct rb_node *flink;
X struct rb_node *blink;
X } list;
X struct {
X struct rb_node *left;
X struct rb_node *right;
X } child;
X } c;
X union {
X struct rb_node *parent;
X struct rb_node *root;
X } p;
X status s;
X union {
X int ikey;
X char *key;
X struct rb_node *lext;
X } k;
X union {
X char *val;
X struct rb_node *rext;
X } v;
} *Rb_node;
X
X
extern Rb_node make_rb(); /* Creates a new rb-tree */
X
X
X
/* Creates a node with key key and val val and inserts it into the tree.
X rb_insert uses strcmp() as comparison funcion. rb_inserti uses <>=,
X rb_insertg uses func() */
X
extern Rb_node rb_insert(Rb_node tree, char *key, char *val);
extern Rb_node rb_inserti(Rb_node tree, int ikey, char *val);
extern Rb_node rb_insertg(Rb_node tree, char *key, char *val,
X int (*func)(char *, char *));
X
X
/* returns an external node in t whose value is equal
X k or whose value is the smallest value greater than k. */
X
extern Rb_node rb_find_key(Rb_node root, char *key);
extern Rb_node rb_find_ikey(Rb_node root, int ikey);
extern Rb_node rb_find_gkey(Rb_node root, char *key,
X int (*func)(char *, char *));
X
X
/* Works just like the find_key versions only it returns whether or not
X it found the key in the integer variable found */
X
extern Rb_node rb_find_key_n(Rb_node root, char *key, int *found);
extern Rb_node rb_find_ikey_n(Rb_node root, int ikey, int *found);
extern Rb_node rb_find_gkey_n(Rb_node root, char *key,
X int (*func)(char *, char *), int *found);
X
X
/* Creates a node with key key and val val and inserts it into the
X tree before/after node nd. Does not check to ensure that you are
X keeping the correct order */
X
extern Rb_node rb_insert_b(Rb_node nd, char *key, char *val);
extern Rb_node rb_insert_a(Rb_node nd, char *key, char *val);
X
X
extern void rb_delete_node(Rb_node node); /* Deletes and frees a node (but
X not the key or val) */
extern void rb_free_tree(Rb_node root); /* Deletes and frees an entire tree */
X
extern char *rb_val(Rb_node node); /* Returns node->v.val -- this is to shut
X lint up */
X
extern int rb_nblack(Rb_node n); /* returns # of black nodes in path from
X n to the root */
int rb_plength(Rb_node n); /* returns the # of nodes in path from
X n to the root */
X
#define rb_first(n) (n->c.list.flink)
#define rb_last(n) (n->c.list.blink)
#define rb_next(n) (n->c.list.flink)
#define rb_prev(n) (n->c.list.blink)
#define rb_empty(t) (t->c.list.flink == t)
#ifndef rb_nil
#define rb_nil(t) (t)
#endif
X
#define rb_traverse(ptr, lst) \
X for(ptr = rb_first(lst); ptr != rb_nil(lst); ptr = rb_next(ptr))
X
SHAR_EOF
$shar_touch -am 02161045100 'ansi/rb.h' &&
chmod 0644 'ansi/rb.h' ||
$echo 'restore of' 'ansi/rb.h' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'ansi/rb.h:' 'MD5 check failed'
1f874ae8f4709c77e0f4a9bab487e476 ansi/rb.h
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'ansi/rb.h'`"
test 4040 -eq "$shar_count" ||
$echo 'ansi/rb.h:' 'original size' '4040,' 'current size' "$shar_count!"
fi
fi
# ============= ansi/main.c ==============
if test -f 'ansi/main.c' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'ansi/main.c' '(file already exists)'
else
$echo 'x -' extracting 'ansi/main.c' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'ansi/main.c' &&
/*
Generic C code for red-black trees.
Copyright (C) 2000 James S. Plank
X
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
X
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
X
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
X
/* Revision 1.2. Jim Plank */
X
#include "rb.h"
#include
X
/* an example -- this allocates a red-black tree for integers. For a
X * user-specified number of iterations, it does the following:
X
X * delete a random element in the tree.
X * make two new random elements, and insert them into the tree
X
X * At the end, it prints the sorted list of elements, and then prints
X * stats of the number of black nodes in any path in the tree, and
X * the minimum and maximum path lengths.
X
X * Rb_find_ikey and rb_inserti could have been used, but instead
X * rb_find_gkey and rb_insertg were used to show how they work.
X
X */
X
int icomp(char *i, char *j)
{
X int I, J;
X
X I = (int) i;
X J = (int) j;
X if (I == J) return 0;
X if (I > J) return -1; else return 1;
}
X
main(int argc, char **argv)
{
X int i, j, tb, nb, mxp, mnp, p;
X int iterations;
X Rb_node argt, t;
X int *a;
X
X if (argc != 2) {
X fprintf(stderr, "usage: main #iterations\n");
X exit(1);
X }
X argt = make_rb();
X srandom(time(0));
X iterations = atoi(argv[1]);
X a = (int *) malloc (iterations*sizeof(int));
X
X for (i = 0; i < atoi(argv[1]); i++) {
X if (i > 0) {
X j = random()%i;
X
X rb_delete_node(rb_find_gkey(argt, (char *) (a[j]), icomp));
X a[j] = random() % 1000;
X rb_insertg(argt, (char *) (a[j]), NULL, icomp);
X }
X a[i] = random() % 1000;
X rb_insertg(argt, (char *) (a[i]), NULL, icomp);
X }
X tb = 0;
X mxp = 0;
X mnp = 0;
X rb_traverse(t, argt) {
X printf("%d ", t->k.ikey);
X nb = rb_nblack(t);
X p = rb_plength(t);
X if (tb == 0) {
X tb = nb;
X } else if (tb != nb) {
X printf("Conflict: tb=%d, nb=%d\n", tb, nb);
X exit(1);
X }
X mxp = (mxp == 0 || mxp < p) ? p : mxp;
X mnp = (mnp == 0 || mxp > p) ? p : mnp;
X }
X printf("\n");
X
X printf("Nblack = %d\n", tb);
X printf("Max = %d\n", mxp);
X printf("Min = %d\n", mnp);
}
SHAR_EOF
$shar_touch -am 02161045100 'ansi/main.c' &&
chmod 0644 'ansi/main.c' ||
$echo 'restore of' 'ansi/main.c' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'ansi/main.c:' 'MD5 check failed'
b15f31478faf4cd32edc211cc6a39ba0 ansi/main.c
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'ansi/main.c'`"
test 2657 -eq "$shar_count" ||
$echo 'ansi/main.c:' 'original size' '2657,' 'current size' "$shar_count!"
fi
fi
# ============= ansi/makefile ==============
if test -f 'ansi/makefile' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'ansi/makefile' '(file already exists)'
else
$echo 'x -' extracting 'ansi/makefile' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'ansi/makefile' &&
# Generic C code for red-black trees.
# Copyright (C) 2000 James S. Plank
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Revision 1.2. Jim Plank
X
CC = gcc
X
EXECUTABLES = main
X
all: $(EXECUTABLES)
X
main: rb.o
X
rb.o: rb.h
main.o: rb.h
X
clean:
X rm -f core *.o $(EXECUTABLES) a.out
X
X.SUFFIXES: .c .o
X.c.o:
X $(CC) $(CFLAGS) -c $*.c
X
main: main.o
X $(CC) $(CFLAGS) -o main main.o rb.o
SHAR_EOF
$shar_touch -am 02161045100 'ansi/makefile' &&
chmod 0644 'ansi/makefile' ||
$echo 'restore of' 'ansi/makefile' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'ansi/makefile:' 'MD5 check failed'
19d5ed18c284952201209d368b50824a ansi/makefile
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'ansi/makefile'`"
test 1065 -eq "$shar_count" ||
$echo 'ansi/makefile:' 'original size' '1065,' 'current size' "$shar_count!"
fi
fi
# ============= non-ansi/main.c ==============
if test ! -d 'non-ansi'; then
$echo 'x -' 'creating directory' 'non-ansi'
mkdir 'non-ansi'
fi
if test -f 'non-ansi/main.c' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'non-ansi/main.c' '(file already exists)'
else
$echo 'x -' extracting 'non-ansi/main.c' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'non-ansi/main.c' &&
/*
Generic C code for red-black trees.
Copyright (C) 2000 James S. Plank
X
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
X
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
X
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
X */
/* Revision 1.2. Jim Plank */
X
#include "rb.h"
#include
X
/* an example -- this allocates a red-black tree for integers. For a
X * user-specified number of iterations, it does the following:
X
X * delete a random element in the tree.
X * make two new random elements, and insert them into the tree
X
X * At the end, it prints the sorted list of elements, and then prints
X * stats of the number of black nodes in any path in the tree, and
X * the minimum and maximum path lengths.
X
X * Rb_find_ikey and rb_inserti could have been used, but instead
X * rb_find_gkey and rb_insertg were used to show how they work.
X
X */
X
int icomp(i, j)
char *i;
char *j;
{
X int I, J;
X
X I = (int) i;
X J = (int) j;
X if (I == J) return 0;
X if (I > J) return -1; else return 1;
}
X
main(argc, argv)
int argc;
char **argv;
{
X int i, j, tb, nb, mxp, mnp, p;
X int iterations;
X Rb_node argt, t;
X int *a;
X
X if (argc != 2) {
X fprintf(stderr, "usage: main #iterations\n");
X exit(1);
X }
X argt = make_rb();
X srandom(time(0));
X iterations = atoi(argv[1]);
X a = (int *) malloc (iterations*sizeof(int));
X
X for (i = 0; i < atoi(argv[1]); i++) {
X if (i > 0) {
X j = random()%i;
X
X rb_delete_node(rb_find_gkey(argt, (char *) (a[j]), icomp));
X a[j] = random() % 1000;
X rb_insertg(argt, (char *) (a[j]), NULL, icomp);
X }
X a[i] = random() % 1000;
X rb_insertg(argt, (char *) (a[i]), NULL, icomp);
X }
X tb = 0;
X mxp = 0;
X mnp = 0;
X rb_traverse(t, argt) {
X printf("%d ", t->k.ikey);
X nb = rb_nblack(t);
X p = rb_plength(t);
X if (tb == 0) {
X tb = nb;
X } else if (tb != nb) {
X printf("Conflict: tb=%d, nb=%d\n", tb, nb);
X exit(1);
X }
X mxp = (mxp == 0 || mxp < p) ? p : mxp;
X mnp = (mnp == 0 || mxp > p) ? p : mnp;
X }
X printf("\n");
X
X printf("Nblack = %d\n", tb);
X printf("Max = %d\n", mxp);
X printf("Min = %d\n", mnp);
}
SHAR_EOF
$shar_touch -am 02161045100 'non-ansi/main.c' &&
chmod 0644 'non-ansi/main.c' ||
$echo 'restore of' 'non-ansi/main.c' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'non-ansi/main.c:' 'MD5 check failed'
170b5b20f79a21ed94941e6fd676d999 non-ansi/main.c
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'non-ansi/main.c'`"
test 2675 -eq "$shar_count" ||
$echo 'non-ansi/main.c:' 'original size' '2675,' 'current size' "$shar_count!"
fi
fi
# ============= non-ansi/makefile ==============
if test -f 'non-ansi/makefile' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'non-ansi/makefile' '(file already exists)'
else
$echo 'x -' extracting 'non-ansi/makefile' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'non-ansi/makefile' &&
# Generic C code for red-black trees.
# Copyright (C) 2000 James S. Plank
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
X
# Revision 1.2. Jim Plank
X
CC = cc
X
EXECUTABLES = main
X
all: $(EXECUTABLES)
X
main: rb.o
X
rb.o: rb.h
main.o: rb.h
X
clean:
X rm -f core *.o $(EXECUTABLES) a.out
X
X.SUFFIXES: .c .o
X.c.o:
X $(CC) $(CFLAGS) -c $*.c
X
main: main.o
X $(CC) $(CFLAGS) -o main main.o rb.o
SHAR_EOF
$shar_touch -am 02161046100 'non-ansi/makefile' &&
chmod 0644 'non-ansi/makefile' ||
$echo 'restore of' 'non-ansi/makefile' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'non-ansi/makefile:' 'MD5 check failed'
aab04c23a4c18db009d0c2d75adb860d non-ansi/makefile
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'non-ansi/makefile'`"
test 1062 -eq "$shar_count" ||
$echo 'non-ansi/makefile:' 'original size' '1062,' 'current size' "$shar_count!"
fi
fi
# ============= non-ansi/rb.c ==============
if test -f 'non-ansi/rb.c' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'non-ansi/rb.c' '(file already exists)'
else
$echo 'x -' extracting 'non-ansi/rb.c' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'non-ansi/rb.c' &&
/*
Generic C code for red-black trees.
Copyright (C) 2000 James S. Plank
X
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
X
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
X
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
X */
X
/* Revision 1.2. Jim Plank */
X
/* Original code by Jim Plank (plank@cs.utk.edu) */
/* modified for THINK C 6.0 for Macintosh by Chris Bartley */
X
X
#include
#include
#include
#include
#include "rb.h"
X
static void recolor();
static void single_rotate();
X
#define isred(n) (n->s.red)
#define isblack(n) (!isred(n))
#define isleft(n) (n->s.left)
#define isright(n) (!isleft(n))
#define isint(n) (n->s.internal)
#define isext(n) (!isint(n))
#define ishead(n) (n->s.head)
#define isroot(n) (n->s.root)
#define setred(n) n->s.red = 1
#define setblack(n) n->s.red = 0
#define setleft(n) n->s.left = 1
#define setright(n) n->s.left = 0
#define sethead(n) n->s.head = 1
#define setroot(n) n->s.root = 1
#define setint(n) n->s.internal = 1
#define setext(n) n->s.internal = 0
#define setnormal(n) { n->s.root = 0; n ->s.head = 0; }
#define sibling(n) ((isleft(n)) ? n->p.parent->c.child.right \
X : n->p.parent->c.child.left)
X
static void insert(item, list) /* Inserts to the end of a list */
Rb_node item;
Rb_node list;
{
X Rb_node last_node;
X
X last_node = list->c.list.blink;
X
X list->c.list.blink = item;
X last_node->c.list.flink = item;
X item->c.list.blink = last_node;
X item->c.list.flink = list;
}
X
static void delete_item(item) /* Deletes an arbitrary iterm */
Rb_node item;
{
X item->c.list.flink->c.list.blink = item->c.list.blink;
X item->c.list.blink->c.list.flink = item->c.list.flink;
}
X
#define mk_new_ext(new, kkkey, vvval) {\
X new = (Rb_node) malloc(sizeof(struct rb_node));\
X new->v.val = vvval;\
X new->k.key = kkkey;\
X setext(new);\
X setblack(new);\
X setnormal(new);\
}
X
static void mk_new_int(l, r, p, il)
Rb_node l;
Rb_node r;
Rb_node p;
int il;
{
X Rb_node newnode;
X
X newnode = (Rb_node) malloc(sizeof(struct rb_node));
X setint(newnode);
X setred(newnode);
X setnormal(newnode);
X newnode->c.child.left = l;
X newnode->c.child.right = r;
X newnode->p.parent = p;
X newnode->k.lext = l;
X newnode->v.rext = r;
X l->p.parent = newnode;
X r->p.parent = newnode;
X setleft(l);
X setright(r);
X if (ishead(p)) {
X p->p.root = newnode;
X setroot(newnode);
X } else if (il) {
X setleft(newnode);
X p->c.child.left = newnode;
X } else {
X setright(newnode);
X p->c.child.right = newnode;
X }
X recolor(newnode);
}
X
X
Rb_node lprev(n)
Rb_node n;
{
X if (ishead(n)) return n;
X while (!isroot(n)) {
X if (isright(n)) return n->p.parent;
X n = n->p.parent;
X }
X return n->p.parent;
}
X
Rb_node rprev(n)
Rb_node n;
{
X if (ishead(n)) return n;
X while (!isroot(n)) {
X if (isleft(n)) return n->p.parent;
X n = n->p.parent;
X }
X return n->p.parent;
}
X
Rb_node make_rb()
{
X Rb_node head;
X
X head = (Rb_node) malloc (sizeof(struct rb_node));
X head->c.list.flink = head;
X head->c.list.blink = head;
X head->p.root = head;
X head->k.key = "";
X sethead(head);
X return head;
}
X
Rb_node rb_find_key_n(n, key, fnd)
Rb_node n;
char *key;
int *fnd;
{
X int cmp;
X
X *fnd = 0;
X if (!ishead(n)) {
X fprintf(stderr, "rb_find_key_n called on non-head 0x%x\n", n);
X exit(1);
X }
X if (n->p.root == n) return n;
X cmp = strcmp(key, n->c.list.blink->k.key);
X if (cmp == 0) {
X *fnd = 1;
X return n->c.list.blink;
X }
X if (cmp > 0) return n;
X else n = n->p.root;
X while (1) {
X if (isext(n)) return n;
X cmp = strcmp(key, n->k.lext->k.key);
X if (cmp == 0) {
X *fnd = 1;
X return n->k.lext;
X }
X if (cmp < 0) n = n->c.child.left ; else n = n->c.child.right;
X }
}
X
Rb_node rb_find_key(n, key)
Rb_node n;
char *key;
{
X int fnd;
X return rb_find_key_n(n, key, &fnd);
}
X
Rb_node rb_find_ikey_n(n, ikey, fnd)
Rb_node n;
int ikey;
int *fnd;
{
X *fnd = 0;
X if (!ishead(n)) {
X fprintf(stderr, "rb_find_ikey_n called on non-head 0x%x\n", n);
X exit(1);
X }
X if (n->p.root == n) return n;
X if (ikey == n->c.list.blink->k.ikey) {
X *fnd = 1;
X return n->c.list.blink;
X }
X if (ikey > n->c.list.blink->k.ikey) return n;
X else n = n->p.root;
X while (1) {
X if (isext(n)) return n;
X if (ikey == n->k.lext->k.ikey) {
X *fnd = 1;
X return n->k.lext;
X }
X n = (ikey < n->k.lext->k.ikey) ? n->c.child.left : n->c.child.right;
X }
}
X
Rb_node rb_find_ikey(n, ikey)
Rb_node n;
int ikey;
{
X int fnd;
X return rb_find_ikey_n(n, ikey, &fnd);
}
X
Rb_node rb_find_gkey_n(n, key, fxn, fnd)
Rb_node n;
char *key;
int (*fxn)();
int *fnd;
{
X int cmp;
X
X *fnd = 0;
X if (!ishead(n)) {
X fprintf(stderr, "rb_find_key_n called on non-head 0x%x\n", n);
X exit(1);
X }
X if (n->p.root == n) return n;
X cmp = (*fxn)(key, n->c.list.blink->k.key);
X if (cmp == 0) {
X *fnd = 1;
X return n->c.list.blink;
X }
X if (cmp > 0) return n;
X else n = n->p.root;
X while (1) {
X if (isext(n)) return n;
X cmp = (*fxn)(key, n->k.lext->k.key);
X if (cmp == 0) {
X *fnd = 1;
X return n->k.lext;
X }
X if (cmp < 0) n = n->c.child.left ; else n = n->c.child.right;
X }
}
X
Rb_node rb_find_gkey(n, key, fxn)
Rb_node n;
char *key;
int (*fxn)();
{
X int fnd;
X return rb_find_gkey_n(n, key, fxn, &fnd);
}
X
Rb_node rb_insert_b(n, key, val)
Rb_node n;
char *key;
char *val;
{
X Rb_node newleft, newright, newnode, list, p;
X
X if (ishead(n)) {
X if (n->p.root == n) { /* Tree is empty */
X mk_new_ext(newnode, key, val);
X insert(newnode, n);
X n->p.root = newnode;
X newnode->p.parent = n;
X setroot(newnode);
X return newnode;
X } else {
X mk_new_ext(newright, key, val);
X insert(newright, n);
X newleft = newright->c.list.blink;
X setnormal(newleft);
X mk_new_int(newleft, newright, newleft->p.parent, isleft(newleft));
X p = rprev(newright);
X if (!ishead(p)) p->k.lext = newright;
X return newright;
X }
X } else {
X mk_new_ext(newleft, key, val);
X insert(newleft, n);
X setnormal(n);
X mk_new_int(newleft, n, n->p.parent, isleft(n));
X p = lprev(newleft);
X if (!ishead(p)) p->v.rext = newleft;
X return newleft;
X }
}
X
static void recolor(n)
Rb_node n;
{
X Rb_node p, gp, s;
X int done = 0;
X
X while(!done) {
X if (isroot(n)) {
X setblack(n);
X return;
X }
X
X p = n->p.parent;
X
X if (isblack(p)) return;
X
X if (isroot(p)) {
X setblack(p);
X return;
X }
X
X gp = p->p.parent;
X s = sibling(p);
X if (isred(s)) {
X setblack(p);
X setred(gp);
X setblack(s);
X n = gp;
X } else {
X done = 1;
X }
X }
X /* p's sibling is black, p is red, gp is black */
X
X if ((isleft(n) == 0) == (isleft(p) == 0)) {
X single_rotate(gp, isleft(n));
X setblack(p);
X setred(gp);
X } else {
X single_rotate(p, isleft(n));
X single_rotate(gp, isleft(n));
X setblack(n);
X setred(gp);
X }
}
X
static void single_rotate(y, l)
Rb_node y;
int l;
{
X int rl, ir;
X Rb_node x, yp;
X char *tmp;
X
X ir = isroot(y);
X yp = y->p.parent;
X if (!ir) {
X rl = isleft(y);
X }
X
X if (l) {
X x = y->c.child.left;
X y->c.child.left = x->c.child.right;
X setleft(y->c.child.left);
X y->c.child.left->p.parent = y;
X x->c.child.right = y;
X setright(y);
X } else {
X x = y->c.child.right;
X y->c.child.right = x->c.child.left;
X setright(y->c.child.right);
X y->c.child.right->p.parent = y;
X x->c.child.left = y;
X setleft(y);
X }
X
X x->p.parent = yp;
X y->p.parent = x;
X if (ir) {
X yp->p.root = x;
X setnormal(y);
X setroot(x);
X } else {
X if (rl) {
X yp->c.child.left = x;
X setleft(x);
X } else {
X yp->c.child.right = x;
X setright(x);
X }
X }
}
X
void rb_delete_node(n)
Rb_node n;
{
X Rb_node s, p, gp;
X char ir;
X
X if (isint(n)) {
X fprintf(stderr, "Cannot delete an internal node: 0x%x\n", n);
X exit(1);
X }
X if (ishead(n)) {
X fprintf(stderr, "Cannot delete the head of an rb_tree: 0x%x\n", n);
X exit(1);
X }
X delete_item(n); /* Delete it from the list */
X p = n->p.parent; /* The only node */
X if (isroot(n)) {
X p->p.root = p;
X free(n);
X return;
X }
X s = sibling(n); /* The only node after deletion */
X if (isroot(p)) {
X s->p.parent = p->p.parent;
X s->p.parent->p.root = s;
X setroot(s);
X free(p);
X free(n);
X return;
X }
X gp = p->p.parent; /* Set parent to sibling */
X s->p.parent = gp;
X if (isleft(p)) {
X gp->c.child.left = s;
X setleft(s);
X } else {
X gp->c.child.right = s;
X setright(s);
X }
X ir = isred(p);
X free(p);
X free(n);
X
X if (isext(s)) { /* Update proper rext and lext values */
X p = lprev(s);
X if (!ishead(p)) p->v.rext = s;
X p = rprev(s);
X if (!ishead(p)) p->k.lext = s;
X } else if (isblack(s)) {
X fprintf(stderr, "DELETION PROB -- sib is black, internal\n");
X exit(1);
X } else {
X p = lprev(s);
X if (!ishead(p)) p->v.rext = s->c.child.left;
X p = rprev(s);
X if (!ishead(p)) p->k.lext = s->c.child.right;
X setblack(s);
X return;
X }
X
X if (ir) return;
X
X /* Recolor */
X
X n = s;
X p = n->p.parent;
X s = sibling(n);
X while(isblack(p) && isblack(s) && isint(s) &&
X isblack(s->c.child.left) && isblack(s->c.child.right)) {
X setred(s);
X n = p;
X if (isroot(n)) return;
X p = n->p.parent;
X s = sibling(n);
X }
X
X if (isblack(p) && isred(s)) { /* Rotation 2.3b */
X single_rotate(p, isright(n));
X setred(p);
X setblack(s);
X s = sibling(n);
X }
X
X { Rb_node x, z; char il;
X
X if (isext(s)) {
X fprintf(stderr, "DELETION ERROR: sibling not internal\n");
X exit(1);
X }
X
X il = isleft(n);
X x = il ? s->c.child.left : s->c.child.right ;
X z = sibling(x);
X
X if (isred(z)) { /* Rotation 2.3f */
X single_rotate(p, !il);
X setblack(z);
X if (isred(p)) setred(s); else setblack(s);
X setblack(p);
X } else if (isblack(x)) { /* Recoloring only (2.3c) */
X if (isred(s) || isblack(p)) {
X fprintf(stderr, "DELETION ERROR: 2.3c not quite right\n");
X exit(1);
X }
X setblack(p);
X setred(s);
X return;
X } else if (isred(p)) { /* 2.3d */
X single_rotate(s, il);
X single_rotate(p, !il);
X setblack(x);
X setred(s);
X return;
X } else { /* 2.3e */
X single_rotate(s, il);
X single_rotate(p, !il);
X setblack(x);
X return;
X }
X }
}
X
X
void rb_print_tree(t, level)
Rb_node t;
int level;
{
X int i;
X if (ishead(t) && t->p.parent == t) {
X printf("tree 0x%x is empty\n", t);
X } else if (ishead(t)) {
X printf("Head: 0x%x. Root = 0x%x\n", t, t->p.root);
X rb_print_tree(t->p.root, 0);
X } else {
X if (isext(t)) {
X for (i = 0; i < level; i++) putchar(' ');
X printf("Ext node 0x%x: %c,%c: p=0x%x, k=%s\n",
X t, isred(t)?'R':'B', isleft(t)?'l':'r', t->p.parent, t->k.key);
X } else {
X rb_print_tree(t->c.child.left, level+2);
X rb_print_tree(t->c.child.right, level+2);
X for (i = 0; i < level; i++) putchar(' ');
X printf("Int node 0x%x: %c,%c: l=0x%x, r=0x%x, p=0x%x, lr=(%s,%s)\n",
X t, isred(t)?'R':'B', isleft(t)?'l':'r', t->c.child.left,
X t->c.child.right,
X t->p.parent, t->k.lext->k.key, t->v.rext->k.key);
X }
X }
}
X
void rb_iprint_tree(t, level)
Rb_node t;
int level;
{
X int i;
X if (ishead(t) && t->p.parent == t) {
X printf("tree 0x%x is empty\n", t);
X } else if (ishead(t)) {
X printf("Head: 0x%x. Root = 0x%x, < = 0x%x, > = 0x%x\n",
X t, t->p.root, t->c.list.blink, t->c.list.flink);
X rb_iprint_tree(t->p.root, 0);
X } else {
X if (isext(t)) {
X for (i = 0; i < level; i++) putchar(' ');
X printf("Ext node 0x%x: %c,%c: p=0x%x, <=0x%x, >=0x%x k=%d\n",
X t, isred(t)?'R':'B', isleft(t)?'l':'r', t->p.parent,
X t->c.list.blink, t->c.list.flink, t->k.ikey);
X } else {
X rb_iprint_tree(t->c.child.left, level+2);
X rb_iprint_tree(t->c.child.right, level+2);
X for (i = 0; i < level; i++) putchar(' ');
X printf("Int node 0x%x: %c,%c: l=0x%x, r=0x%x, p=0x%x, lr=(%d,%d)\n",
X t, isred(t)?'R':'B', isleft(t)?'l':'r', t->c.child.left,
X t->c.child.right,
X t->p.parent, t->k.lext->k.ikey, t->v.rext->k.ikey);
X }
X }
}
X
int rb_nblack(n)
Rb_node n;
{
X int nb;
X if (ishead(n) || isint(n)) {
X fprintf(stderr, "ERROR: rb_nblack called on a non-external node 0x%x\n",
X n);
X exit(1);
X }
X nb = 0;
X while(!ishead(n)) {
X if (isblack(n)) nb++;
X n = n->p.parent;
X }
X return nb;
}
X
int rb_plength(n)
Rb_node n;
{
X int pl;
X if (ishead(n) || isint(n)) {
X fprintf(stderr, "ERROR: rb_plength called on a non-external node 0x%x\n",
X n);
X exit(1);
X }
X pl = 0;
X while(!ishead(n)) {
X pl++;
X n = n->p.parent;
X }
X return pl;
}
X
void rb_free_tree(n)
Rb_node n;
{
X if (!ishead(n)) {
X fprintf(stderr, "ERROR: Rb_free_tree called on a non-head node\n");
X exit(1);
X }
X
X while(rb_first(n) != rb_nil(n)) {
X rb_delete_node(rb_first(n));
X }
X free(n);
}
X
char *rb_val(n)
Rb_node n;
{
X return n->v.val;
}
X
Rb_node rb_insert_a(nd, key, val)
Rb_node nd;
char *key;
char *val;
{
X rb_insert_b(nd->c.list.flink, key, val);
}
X
Rb_node rb_insert(tree, key, val)
Rb_node tree;
char *key;
char *val;
{
X rb_insert_b(rb_find_key(tree, key), key, val);
}
X
Rb_node rb_inserti(tree, ikey, val)
Rb_node tree;
int ikey;
char *val;
{
X rb_insert_b(rb_find_ikey(tree, ikey), (char *) ikey, val);
}
X
Rb_node rb_insertg(tree, key, val, func)
Rb_node tree;
char *key;
char *val;
int (*func)();
{
X rb_insert_b(rb_find_gkey(tree, key, func), key, val);
}
X
X
SHAR_EOF
$shar_touch -am 02161046100 'non-ansi/rb.c' &&
chmod 0644 'non-ansi/rb.c' ||
$echo 'restore of' 'non-ansi/rb.c' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'non-ansi/rb.c:' 'MD5 check failed'
c9db3e01102c7cad5d149d3e8a60ec2f non-ansi/rb.c
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'non-ansi/rb.c'`"
test 14234 -eq "$shar_count" ||
$echo 'non-ansi/rb.c:' 'original size' '14234,' 'current size' "$shar_count!"
fi
fi
# ============= non-ansi/rb.h ==============
if test -f 'non-ansi/rb.h' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'non-ansi/rb.h' '(file already exists)'
else
$echo 'x -' extracting 'non-ansi/rb.h' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'non-ansi/rb.h' &&
/*
Generic C code for red-black trees.
Copyright (C) 2000 James S. Plank
X
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
X
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
X
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
X */
X
/* Revision 1.2. Jim Plank */
X
/* Original code by Jim Plank (plank@cs.utk.edu) */
/* modified for THINK C 6.0 for Macintosh by Chris Bartley */
X
typedef struct {
X unsigned red : 1 ;
X unsigned internal : 1 ;
X unsigned left : 1 ;
X unsigned root : 1 ;
X unsigned head : 1 ;
} status;
X
/* Main rb_node. You only ever use the fields
X
X c.list.flink
X c.list.blink
X
X k.key or k.ikey
X v.val
*/
X
X
typedef struct rb_node {
X union {
X struct {
X struct rb_node *flink;
X struct rb_node *blink;
X } list;
X struct {
X struct rb_node *left;
X struct rb_node *right;
X } child;
X } c;
X union {
X struct rb_node *parent;
X struct rb_node *root;
X } p;
X status s;
X union {
X int ikey;
X char *key;
X struct rb_node *lext;
X } k;
X union {
X char *val;
X struct rb_node *rext;
X } v;
} *Rb_node;
X
X
extern Rb_node make_rb(); /* Creates a new rb-tree */
X
X
X
/* Creates a node with key key and val val and inserts it into the tree.
X rb_insert uses strcmp() as comparison funcion. rb_inserti uses <>=,
X rb_insertg uses func() */
X
extern Rb_node rb_insert(/* Rb_node tree, char *key, char *val */);
extern Rb_node rb_inserti(/* Rb_node tree, int ikey, char *val */);
extern Rb_node rb_insertg(/* Rb_node tree, char *key, char *val,
X int (/* *func)(char *, char *) */);
X
X
/* returns an external node in t whose value is equal
X k or whose value is the smallest value greater than k. */
X
extern Rb_node rb_find_key(/* Rb_node root, char *key */);
extern Rb_node rb_find_ikey(/* Rb_node root, int ikey */);
extern Rb_node rb_find_gkey(/* Rb_node root, char *key, int (*func)() */);
X
X
/* Works just like the find_key versions only it returns whether or not
X it found the key in the integer variable found */
X
extern Rb_node rb_find_key_n(/* Rb_node root, char *key, int *found */);
extern Rb_node rb_find_ikey_n(/* Rb_node root, int ikey, int *found */);
extern Rb_node rb_find_gkey_n(/* Rb_node root, char *key, int (*func)(),
X int *found */);
X
X
/* Creates a node with key key and val val and inserts it into the
X tree before/after node nd. Does not check to ensure that you are
X keeping the correct order */
X
extern Rb_node rb_insert_b(/* Rb_node nd, char *key, char *val */);
extern Rb_node rb_insert_a(/* Rb_node nd, char *key, char *val */);
X
X
extern void rb_delete_node(/* Rb_node node */); /* Deletes and frees a node (but
X not the key or val) */
extern void rb_free_tree(/* Rb_node root */); /* Deletes and frees an entire tree */
X
extern char *rb_val(/* Rb_node node */); /* Returns node->v.val -- this is to shut
X lint up */
X
extern int rb_nblack(/* Rb_node n */); /* returns # of black nodes in path from
X n to the root */
int rb_plength(/* Rb_node n */); /* returns the # of nodes in path from
X n to the root */
X
#define rb_first(n) (n->c.list.flink)
#define rb_last(n) (n->c.list.blink)
#define rb_next(n) (n->c.list.flink)
#define rb_prev(n) (n->c.list.blink)
#define rb_empty(t) (t->c.list.flink == t)
#ifndef rb_nil
#define rb_nil(t) (t)
#endif
X
#define rb_traverse(ptr, lst) \
X for(ptr = rb_first(lst); ptr != rb_nil(lst); ptr = rb_next(ptr))
X
SHAR_EOF
$shar_touch -am 02161046100 'non-ansi/rb.h' &&
chmod 0644 'non-ansi/rb.h' ||
$echo 'restore of' 'non-ansi/rb.h' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'non-ansi/rb.h:' 'MD5 check failed'
75f7700df94b4ba46dd413612e5bf031 non-ansi/rb.h
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'non-ansi/rb.h'`"
test 4083 -eq "$shar_count" ||
$echo 'non-ansi/rb.h:' 'original size' '4083,' 'current size' "$shar_count!"
fi
fi
rm -fr _sh18534
exit 0