Move flushq() macro into tty.h.
This commit is contained in:
parent
2077a43922
commit
c6f70a8e22
|
@ -1,6 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \e3.d
|
||||
E
|
||||
w \e3.o
|
||||
EOT
|
|
@ -1 +0,0 @@
|
|||
E !echo hello world-
|
|
@ -1,11 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \g1.d
|
||||
g/./m0
|
||||
g/./s/$/\
|
||||
hello world
|
||||
g/hello /s/lo/p!/\
|
||||
a\
|
||||
order
|
||||
w \g1.o
|
||||
EOT
|
|
@ -1,15 +0,0 @@
|
|||
line5
|
||||
help! world
|
||||
order
|
||||
line 4
|
||||
help! world
|
||||
order
|
||||
line 3
|
||||
help! world
|
||||
order
|
||||
line 2
|
||||
help! world
|
||||
order
|
||||
line 1
|
||||
help! world
|
||||
order
|
|
@ -1,7 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \g2.d
|
||||
g/[2-4]/-1,+1c\
|
||||
hello world
|
||||
w \g2.o
|
||||
EOT
|
|
@ -1 +0,0 @@
|
|||
hello world
|
|
@ -1,14 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \i.d
|
||||
1i
|
||||
hello world
|
||||
.
|
||||
2i
|
||||
hello world!
|
||||
.
|
||||
$i
|
||||
hello world!!
|
||||
.
|
||||
w \i.o
|
||||
EOT
|
|
@ -1,8 +0,0 @@
|
|||
hello world
|
||||
hello world!
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
hello world!!
|
||||
line5
|
|
@ -1,7 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \j.d
|
||||
1,1j
|
||||
2,3j
|
||||
w \j.o
|
||||
EOT
|
|
@ -1,4 +0,0 @@
|
|||
line 1
|
||||
line 2line 3
|
||||
line 4
|
||||
line5
|
|
@ -1,5 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \l.d
|
||||
w \l.o
|
||||
EOT
|
|
@ -1,12 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \m.d
|
||||
1,2m$
|
||||
1,2m$
|
||||
1,2m$
|
||||
$m0
|
||||
$m0
|
||||
2,3m1
|
||||
2,3m3
|
||||
w \m.o
|
||||
EOT
|
|
@ -1,5 +0,0 @@
|
|||
line5
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
|
@ -1,5 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \n.d
|
||||
w \n.o
|
||||
EOT
|
|
@ -1,5 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \p.d
|
||||
w \p.o
|
||||
EOT
|
|
@ -1,8 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \r1.d
|
||||
1;r !echo hello world
|
||||
1
|
||||
r !echo hello world
|
||||
w \r1.o
|
||||
EOT
|
|
@ -1,7 +0,0 @@
|
|||
line 1
|
||||
hello world
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
line5
|
||||
hello world
|
|
@ -1,6 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \r2.d
|
||||
r
|
||||
w \r2.o
|
||||
EOT
|
|
@ -1,10 +0,0 @@
|
|||
line 1
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
line5
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
line5
|
|
@ -1,6 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \r3.d
|
||||
r r3.t
|
||||
w \r3.o
|
||||
EOT
|
|
@ -1,2 +0,0 @@
|
|||
r r3.t
|
||||
r r3.t
|
|
@ -1,11 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \s1.d
|
||||
s/\([^ ][^ ]*\)/(\1)/g
|
||||
2s
|
||||
/3/s
|
||||
/\(4\)/sr
|
||||
/\(.\)/srg
|
||||
%s/i/&e/
|
||||
w \s1.o
|
||||
EOT
|
|
@ -1,5 +0,0 @@
|
|||
liene 1
|
||||
(liene) (2)
|
||||
(liene) (3)
|
||||
liene (4)
|
||||
(()liene5)
|
|
@ -1,9 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \s2.d
|
||||
,s/./(&)/3
|
||||
s/$/00
|
||||
2s//%/g
|
||||
s/^l
|
||||
w \s2.o
|
||||
EOT
|
|
@ -1,5 +0,0 @@
|
|||
li(n)e 1
|
||||
i(n)e 200
|
||||
li(n)e 3
|
||||
li(n)e 4
|
||||
li(n)e500
|
|
@ -1,8 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \t.d
|
||||
1t1
|
||||
2,3t2
|
||||
,t$
|
||||
w \t.o
|
||||
EOT
|
|
@ -1,16 +0,0 @@
|
|||
line 1
|
||||
line 1
|
||||
line 1
|
||||
line 2
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
line5
|
||||
line 1
|
||||
line 1
|
||||
line 1
|
||||
line 2
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
line5
|
|
@ -1,3 +0,0 @@
|
|||
1t1
|
||||
2,3t2
|
||||
,t$
|
|
@ -1,36 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \u.d
|
||||
1;r u.t
|
||||
u
|
||||
a
|
||||
hello
|
||||
world
|
||||
.
|
||||
g/./s//x/\
|
||||
a\
|
||||
hello\
|
||||
world
|
||||
u
|
||||
u
|
||||
u
|
||||
a
|
||||
hello world!
|
||||
.
|
||||
u
|
||||
1,$d
|
||||
u
|
||||
2,3d
|
||||
u
|
||||
c
|
||||
hello world!!
|
||||
.
|
||||
u
|
||||
u
|
||||
-1;.,+1j
|
||||
u
|
||||
u
|
||||
u
|
||||
.,+1t$
|
||||
w \u.o
|
||||
EOT
|
|
@ -1,9 +0,0 @@
|
|||
line 1
|
||||
hello
|
||||
hello world!!
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
line5
|
||||
hello
|
||||
hello world!!
|
|
@ -1,11 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \v.d
|
||||
v/[ ]/m0
|
||||
v/[ ]/s/$/\
|
||||
hello world
|
||||
v/hello /s/lo/p!/\
|
||||
a\
|
||||
order
|
||||
w \v.o
|
||||
EOT
|
|
@ -1,11 +0,0 @@
|
|||
line5
|
||||
order
|
||||
hello world
|
||||
line 1
|
||||
order
|
||||
line 2
|
||||
order
|
||||
line 3
|
||||
order
|
||||
line 4
|
||||
order
|
|
@ -1,7 +0,0 @@
|
|||
#!/bin/sh -
|
||||
../ed - <<\EOT
|
||||
r \w.d
|
||||
w !cat >\!.z
|
||||
r \!.z
|
||||
w \w.o
|
||||
EOT
|
|
@ -1,10 +0,0 @@
|
|||
line 1
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
line5
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
line5
|
|
@ -1,2 +0,0 @@
|
|||
parenthesis are broken
|
||||
not well tested, may have other obscure flaws
|
499
bin/ps/ps.c
499
bin/ps/ps.c
|
@ -1,499 +0,0 @@
|
|||
/*-
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef lint
|
||||
char copyright[] =
|
||||
"@(#) Copyright (c) 1990 The Regents of the University of California.\n\
|
||||
All rights reserved.\n";
|
||||
#endif /* not lint */
|
||||
|
||||
#ifndef lint
|
||||
static char sccsid[] = "@(#)ps.c 5.43 (Berkeley) 7/1/91";
|
||||
static char rcsid[] = "$Header: /cvsroot/src/bin/ps/ps.c,v 1.6 1993/07/11 17:34:29 cgd Exp $";
|
||||
#endif /* not lint */
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/user.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/proc.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/kinfo.h>
|
||||
#include <nlist.h>
|
||||
#include <kvm.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <paths.h>
|
||||
#include "ps.h"
|
||||
|
||||
#ifdef SPPWAIT
|
||||
#define NEWVM
|
||||
#endif
|
||||
|
||||
KINFO *kinfo;
|
||||
struct varent *vhead, *vtail;
|
||||
|
||||
int eval; /* exit value */
|
||||
int rawcpu; /* -C */
|
||||
int sumrusage; /* -S */
|
||||
int termwidth; /* width of screen (0 == infinity) */
|
||||
int totwidth; /* calculated width of requested variables */
|
||||
|
||||
static int needuser, needcomm, needenv;
|
||||
|
||||
enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
|
||||
|
||||
uid_t getuid();
|
||||
char *ttyname();
|
||||
double getpcpu(); /* 14 Sep 92*/
|
||||
|
||||
char dfmt[] = "pid tt state time command";
|
||||
char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
|
||||
char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command";
|
||||
char o1[] = "pid";
|
||||
char o2[] = "tt state time command";
|
||||
char ufmt[] = "user pid %cpu %mem vsz rss tt state start time command";
|
||||
char vfmt[] =
|
||||
"pid state time sl re pagein vsz rss lim tsiz trs %cpu %mem command";
|
||||
|
||||
main(argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
{
|
||||
extern char *optarg;
|
||||
extern int optind;
|
||||
register struct proc *p;
|
||||
register size_t nentries;
|
||||
register struct varent *vent;
|
||||
register int i;
|
||||
struct winsize ws;
|
||||
dev_t ttydev;
|
||||
int all, ch, flag, fmt, lineno, pid, prtheader, uid, wflag, what, xflg;
|
||||
int pscomp();
|
||||
char *nlistf, *memf, *swapf;
|
||||
char *kludge_oldps_options();
|
||||
|
||||
if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
|
||||
ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
|
||||
ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) ||
|
||||
ws.ws_col == 0)
|
||||
termwidth = 79;
|
||||
else
|
||||
termwidth = ws.ws_col - 1;
|
||||
|
||||
if (argc > 1)
|
||||
argv[1] = kludge_oldps_options(argv[1]);
|
||||
|
||||
fmt = 0;
|
||||
all = wflag = xflg = 0;
|
||||
pid = uid = -1;
|
||||
ttydev = NODEV;
|
||||
memf = nlistf = swapf = NULL;
|
||||
while ((ch = getopt(argc, argv,
|
||||
"aCeghjLlM:mN:O:o:p:rSTt:uvW:wx")) != EOF)
|
||||
switch((char)ch) {
|
||||
case 'a':
|
||||
all = 1;
|
||||
break;
|
||||
case 'C':
|
||||
rawcpu = 1;
|
||||
break;
|
||||
case 'e':
|
||||
needenv = 1;
|
||||
break;
|
||||
case 'g':
|
||||
break; /* no-op */
|
||||
case 'h':
|
||||
prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
|
||||
break;
|
||||
case 'j':
|
||||
parsefmt(jfmt);
|
||||
fmt = 1;
|
||||
jfmt[0] = '\0';
|
||||
break;
|
||||
case 'L':
|
||||
showkey();
|
||||
exit(0);
|
||||
case 'l':
|
||||
parsefmt(lfmt);
|
||||
fmt = 1;
|
||||
lfmt[0] = '\0';
|
||||
break;
|
||||
case 'M':
|
||||
memf = optarg;
|
||||
break;
|
||||
case 'm':
|
||||
sortby = SORTMEM;
|
||||
break;
|
||||
case 'N':
|
||||
nlistf = optarg;
|
||||
break;
|
||||
case 'O':
|
||||
parsefmt(o1);
|
||||
parsefmt(optarg);
|
||||
parsefmt(o2);
|
||||
o1[0] = o2[0] = '\0';
|
||||
fmt = 1;
|
||||
break;
|
||||
case 'o':
|
||||
parsefmt(optarg);
|
||||
fmt = 1;
|
||||
break;
|
||||
case 'p':
|
||||
pid = atoi(optarg);
|
||||
xflg = 1;
|
||||
break;
|
||||
case 'r':
|
||||
sortby = SORTCPU;
|
||||
break;
|
||||
case 'S':
|
||||
sumrusage = 1;
|
||||
break;
|
||||
case 'T':
|
||||
if ((optarg = ttyname(STDIN_FILENO)) == NULL)
|
||||
err("stdin: not a terminal");
|
||||
/* FALLTHROUGH */
|
||||
case 't': {
|
||||
char *ttypath;
|
||||
struct stat stbuf;
|
||||
char pathbuf[MAXPATHLEN];
|
||||
|
||||
if (strcmp(optarg, "co") == 0)
|
||||
ttypath = _PATH_CONSOLE;
|
||||
else if (*optarg != '/')
|
||||
(void) sprintf(ttypath = pathbuf, "%s%s",
|
||||
_PATH_TTY, optarg);
|
||||
else
|
||||
ttypath = optarg;
|
||||
if (stat(ttypath, &stbuf) == -1)
|
||||
err("%s: %s", ttypath, strerror(errno));
|
||||
if (!S_ISCHR(stbuf.st_mode))
|
||||
err("%s: not a terminal", ttypath);
|
||||
ttydev = stbuf.st_rdev;
|
||||
break;
|
||||
}
|
||||
case 'u':
|
||||
parsefmt(ufmt);
|
||||
sortby = SORTCPU;
|
||||
fmt = 1;
|
||||
ufmt[0] = '\0';
|
||||
break;
|
||||
case 'v':
|
||||
parsefmt(vfmt);
|
||||
sortby = SORTMEM;
|
||||
fmt = 1;
|
||||
vfmt[0] = '\0';
|
||||
break;
|
||||
case 'W':
|
||||
swapf = optarg;
|
||||
break;
|
||||
case 'w':
|
||||
if (wflag)
|
||||
termwidth = UNLIMITED;
|
||||
else if (termwidth < 131)
|
||||
termwidth = 131;
|
||||
wflag++;
|
||||
break;
|
||||
case 'x':
|
||||
xflg = 1;
|
||||
break;
|
||||
case '?':
|
||||
default:
|
||||
usage();
|
||||
}
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
|
||||
#define BACKWARD_COMPATIBILITY
|
||||
#ifdef BACKWARD_COMPATIBILITY
|
||||
if (*argv) {
|
||||
|
||||
nlistf = *argv;
|
||||
if (*++argv) {
|
||||
memf = *argv;
|
||||
if (*++argv)
|
||||
swapf = *argv;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (kvm_openfiles(nlistf, memf, swapf) == -1)
|
||||
err("kvm_openfiles: %s", kvm_geterr());
|
||||
|
||||
if (!fmt)
|
||||
parsefmt(dfmt);
|
||||
|
||||
if (!all && ttydev == NODEV && pid == -1) /* XXX - should be cleaner */
|
||||
uid = getuid();
|
||||
|
||||
/*
|
||||
* scan requested variables, noting what structures are needed,
|
||||
* and adjusting header widths as appropiate.
|
||||
*/
|
||||
scanvars();
|
||||
/*
|
||||
* get proc list
|
||||
*/
|
||||
if (uid != -1) {
|
||||
what = KINFO_PROC_UID;
|
||||
flag = uid;
|
||||
} else if (ttydev != NODEV) {
|
||||
what = KINFO_PROC_TTY;
|
||||
flag = ttydev;
|
||||
} else if (pid != -1) {
|
||||
what = KINFO_PROC_PID;
|
||||
flag = pid;
|
||||
} else
|
||||
what = KINFO_PROC_ALL;
|
||||
/*
|
||||
* select procs
|
||||
*/
|
||||
if ((nentries = kvm_getprocs(what, flag)) == -1)
|
||||
err("%s", kvm_geterr());
|
||||
kinfo = malloc(nentries * sizeof(KINFO));
|
||||
if (kinfo == NULL)
|
||||
err("%s", strerror(errno));
|
||||
for (nentries = 0; p = kvm_nextproc(); ++nentries) {
|
||||
kinfo[nentries].ki_p = p;
|
||||
kinfo[nentries].ki_e = kvm_geteproc(p);
|
||||
if (needuser)
|
||||
saveuser(&kinfo[nentries]);
|
||||
}
|
||||
/*
|
||||
* print header
|
||||
*/
|
||||
printheader();
|
||||
if (nentries == 0)
|
||||
exit(0);
|
||||
/*
|
||||
* sort proc list
|
||||
*/
|
||||
qsort((void *)kinfo, nentries, sizeof(KINFO), pscomp);
|
||||
/*
|
||||
* for each proc, call each variable output function.
|
||||
*/
|
||||
for (i = lineno = 0; i < nentries; i++) {
|
||||
if (xflg == 0 && (kinfo[i].ki_e->e_tdev == NODEV ||
|
||||
(kinfo[i].ki_p->p_flag & SCTTY ) == 0))
|
||||
continue;
|
||||
for (vent = vhead; vent; vent = vent->next) {
|
||||
(*vent->var->oproc)(&kinfo[i], vent->var, vent->next);
|
||||
if (vent->next != NULL)
|
||||
(void) putchar(' ');
|
||||
}
|
||||
(void) putchar('\n');
|
||||
if (prtheader && lineno++ == prtheader-4) {
|
||||
(void) putchar('\n');
|
||||
printheader();
|
||||
lineno = 0;
|
||||
}
|
||||
}
|
||||
exit(eval);
|
||||
}
|
||||
|
||||
scanvars()
|
||||
{
|
||||
register struct varent *vent;
|
||||
register VAR *v;
|
||||
register int i;
|
||||
|
||||
for (vent = vhead; vent; vent = vent->next) {
|
||||
v = vent->var;
|
||||
i = strlen(v->header);
|
||||
if (v->width < i)
|
||||
v->width = i;
|
||||
totwidth += v->width + 1; /* +1 for space */
|
||||
if (v->flag & USER)
|
||||
needuser = 1;
|
||||
if (v->flag & COMM)
|
||||
needcomm = 1;
|
||||
}
|
||||
totwidth--;
|
||||
}
|
||||
|
||||
|
||||
/* XXX - redo */
|
||||
saveuser(ki)
|
||||
KINFO *ki;
|
||||
{
|
||||
register struct usave *usp;
|
||||
register struct user *up;
|
||||
|
||||
if ((usp = calloc(1, sizeof(struct usave))) == NULL)
|
||||
err("%s", strerror(errno));
|
||||
up = kvm_getu(ki->ki_p);
|
||||
/*
|
||||
* save arguments and environment if needed
|
||||
*/
|
||||
ki->ki_args = needcomm ? strdup(kvm_getargs(ki->ki_p, up)) : NULL;
|
||||
ki->ki_env = needenv ? strdup(kvm_getenv(ki->ki_p, up)) : NULL;
|
||||
|
||||
if (up != NULL) {
|
||||
ki->ki_u = usp;
|
||||
/*
|
||||
* save important fields
|
||||
*/
|
||||
#ifdef NEWVM
|
||||
usp->u_start = up->u_stats.p_start;
|
||||
usp->u_ru = up->u_stats.p_ru;
|
||||
usp->u_cru = up->u_stats.p_cru;
|
||||
#else
|
||||
usp->u_procp = up->u_procp;
|
||||
usp->u_start = up->u_start;
|
||||
usp->u_ru = up->u_ru;
|
||||
usp->u_cru = up->u_cru;
|
||||
usp->u_acflag = up->u_acflag;
|
||||
#endif
|
||||
} else
|
||||
free(usp);
|
||||
}
|
||||
|
||||
pscomp(k1, k2)
|
||||
KINFO *k1, *k2;
|
||||
{
|
||||
int i;
|
||||
#ifdef NEWVM
|
||||
#define VSIZE(k) ((k)->ki_e->e_vm.vm_dsize + (k)->ki_e->e_vm.vm_ssize + \
|
||||
(k)->ki_e->e_vm.vm_tsize)
|
||||
#else
|
||||
#define VSIZE(k) ((k)->ki_p->p_dsize + (k)->ki_p->p_ssize + (k)->ki_e->e_xsize)
|
||||
#endif
|
||||
|
||||
if (sortby == SORTCPU)
|
||||
return (getpcpu(k2) - getpcpu(k1));
|
||||
if (sortby == SORTMEM)
|
||||
return (VSIZE(k2) - VSIZE(k1));
|
||||
i = k1->ki_e->e_tdev - k2->ki_e->e_tdev;
|
||||
if (i == 0)
|
||||
i = k1->ki_p->p_pid - k2->ki_p->p_pid;
|
||||
return (i);
|
||||
}
|
||||
|
||||
/*
|
||||
* ICK (all for getopt), would rather hide the ugliness
|
||||
* here than taint the main code.
|
||||
*
|
||||
* ps foo -> ps -foo
|
||||
* ps 34 -> ps -p34
|
||||
*
|
||||
* The old convention that 't' with no trailing tty arg means the users
|
||||
* tty, is only supported if argv[1] doesn't begin with a '-'. This same
|
||||
* feature is available with the option 'T', which takes no argument.
|
||||
*/
|
||||
char *
|
||||
kludge_oldps_options(s)
|
||||
char *s;
|
||||
{
|
||||
size_t len;
|
||||
char *newopts, *ns, *cp;
|
||||
|
||||
len = strlen(s);
|
||||
if ((newopts = ns = malloc(len + 2)) == NULL)
|
||||
err("%s", strerror(errno));
|
||||
/*
|
||||
* options begin with '-'
|
||||
*/
|
||||
if (*s != '-')
|
||||
*ns++ = '-'; /* add option flag */
|
||||
/*
|
||||
* gaze to end of argv[1]
|
||||
*/
|
||||
cp = s + len - 1;
|
||||
/*
|
||||
* if last letter is a 't' flag with no argument (in the context
|
||||
* of the oldps options -- option string NOT starting with a '-' --
|
||||
* then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
|
||||
*/
|
||||
if (*cp == 't' && *s != '-')
|
||||
*cp = 'T';
|
||||
else {
|
||||
/*
|
||||
* otherwise check for trailing number, which *may* be a
|
||||
* pid.
|
||||
*/
|
||||
while (cp >= s && isdigit(*cp))
|
||||
--cp;
|
||||
}
|
||||
cp++;
|
||||
bcopy(s, ns, (size_t)(cp - s)); /* copy up to trailing number */
|
||||
ns += cp - s;
|
||||
/*
|
||||
* if there's a trailing number, and not a preceding 'p' (pid) or
|
||||
* 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
|
||||
*/
|
||||
if (isdigit(*cp) && (cp == s || cp[-1] != 't' && cp[-1] != 'p' &&
|
||||
(cp - 1 == s || cp[-2] != 't')))
|
||||
*ns++ = 'p';
|
||||
(void) strcpy(ns, cp); /* and append the number */
|
||||
|
||||
return (newopts);
|
||||
}
|
||||
|
||||
#if __STDC__
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
|
||||
void
|
||||
#if __STDC__
|
||||
err(const char *fmt, ...)
|
||||
#else
|
||||
err(fmt, va_alist)
|
||||
char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
va_list ap;
|
||||
#if __STDC__
|
||||
va_start(ap, fmt);
|
||||
#else
|
||||
va_start(ap);
|
||||
#endif
|
||||
(void)fprintf(stderr, "ps: ");
|
||||
(void)vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
(void)fprintf(stderr, "\n");
|
||||
exit(1);
|
||||
/* NOTREACHED */
|
||||
}
|
||||
|
||||
usage()
|
||||
{
|
||||
(void) fprintf(stderr,
|
||||
"usage: ps [-aChjlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty]\n\t [-M core] [-N system] [-W swap]\n ps [-L]\n");
|
||||
exit(1);
|
||||
}
|
|
@ -1,86 +0,0 @@
|
|||
#!/bin/sh -
|
||||
#
|
||||
# Copyright (c) 1991 The Regents of the University of California.
|
||||
# All rights reserved.
|
||||
#
|
||||
# This code is derived from software contributed to Berkeley by
|
||||
# Kenneth Almquist.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions
|
||||
# are met:
|
||||
# 1. Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
# 3. All advertising materials mentioning features or use of this software
|
||||
# must display the following acknowledgement:
|
||||
# This product includes software developed by the University of
|
||||
# California, Berkeley and its contributors.
|
||||
# 4. Neither the name of the University nor the names of its contributors
|
||||
# may be used to endorse or promote products derived from this software
|
||||
# without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
# SUCH DAMAGE.
|
||||
#
|
||||
# @(#)builtins 5.1 (Berkeley) 3/7/91
|
||||
#
|
||||
# $Header: /cvsroot/src/bin/sh/builtins.def,v 1.4 1993/07/07 01:11:56 jtc Exp $
|
||||
|
||||
#
|
||||
# This file lists all the builtin commands. The first column is the name
|
||||
# of a C routine. The -j flag, if present, specifies that this command
|
||||
# is to be excluded from systems without job control. The rest of the line
|
||||
# specifies the command name or names used to run the command. The entry
|
||||
# for nullcmd, which is run when the user does not specify a command, must
|
||||
# come first.
|
||||
#
|
||||
# Copyright (C) 1989 by Kenneth Almquist. All rights reserved.
|
||||
# This file is part of ash, which is distributed under the terms specified
|
||||
# by the Ash General Public License. See the file named LICENSE.
|
||||
|
||||
bltincmd command
|
||||
#alloccmd alloc
|
||||
bgcmd -j bg
|
||||
breakcmd break continue
|
||||
#catfcmd catf
|
||||
cdcmd cd chdir
|
||||
dotcmd .
|
||||
echocmd echo
|
||||
evalcmd eval
|
||||
execcmd exec
|
||||
exitcmd exit
|
||||
exportcmd export readonly
|
||||
#exprcmd expr test [
|
||||
falsecmd false
|
||||
fgcmd -j fg
|
||||
getoptscmd getopts
|
||||
hashcmd hash
|
||||
jobidcmd jobid
|
||||
jobscmd jobs
|
||||
lccmd lc
|
||||
#linecmd line
|
||||
localcmd local
|
||||
#nlechocmd nlecho
|
||||
pwdcmd pwd
|
||||
readcmd read
|
||||
returncmd return
|
||||
setcmd set
|
||||
setvarcmd setvar
|
||||
shiftcmd shift
|
||||
trapcmd trap
|
||||
truecmd : true
|
||||
umaskcmd umask
|
||||
unsetcmd unset
|
||||
waitcmd wait
|
|
@ -1,397 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
|
||||
/*
|
||||
Copyright (C) 1989 Free Software Foundation
|
||||
written by Eric Newton (newton@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifndef _CursesWindow_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _CursesWindow_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
|
||||
#include <curses.h>
|
||||
|
||||
/*
|
||||
*
|
||||
* C++ class for windows.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
class CursesWindow
|
||||
{
|
||||
protected:
|
||||
static int count; // count of all active windows:
|
||||
// We rely on the c++ promise that
|
||||
// all otherwise uninitialized
|
||||
// static class vars are set to 0
|
||||
|
||||
WINDOW * w; // the curses WINDOW
|
||||
|
||||
int alloced; // true if we own the WINDOW
|
||||
|
||||
CursesWindow* par; // parent, if subwindow
|
||||
CursesWindow* subwins; // head of subwindows list
|
||||
CursesWindow* sib; // next subwindow of parent
|
||||
|
||||
void kill_subwindows(); // disable all subwindows
|
||||
|
||||
public:
|
||||
CursesWindow(WINDOW* &window); // useful only for stdscr
|
||||
|
||||
CursesWindow(int lines, // number of lines
|
||||
int cols, // number of columns
|
||||
int begin_y, // line origin
|
||||
int begin_x); // col origin
|
||||
|
||||
CursesWindow(CursesWindow& par, // parent window
|
||||
int lines, // number of lines
|
||||
int cols, // number of columns
|
||||
int by, // absolute or relative
|
||||
int bx, // origins:
|
||||
char absrel = 'a'); // if `a', by & bx are
|
||||
// absolute screen pos,
|
||||
// else if `r', they are
|
||||
// relative to par origin
|
||||
~CursesWindow();
|
||||
|
||||
// terminal status
|
||||
int lines(); // number of lines on terminal, *not* window
|
||||
int cols(); // number of cols on terminal, *not* window
|
||||
|
||||
// window status
|
||||
int height(); // number of lines in this window
|
||||
int width(); // number of cols in this window
|
||||
int begx(); // smallest x coord in window
|
||||
int begy(); // smallest y coord in window
|
||||
int maxx(); // largest x coord in window
|
||||
int maxy(); // largest x coord in window
|
||||
|
||||
// window positioning
|
||||
int move(int y, int x);
|
||||
|
||||
// coordinate positioning
|
||||
void getyx(int& y, int& x);
|
||||
int mvcur(int sy, int ey, int sx, int ex);
|
||||
|
||||
// input
|
||||
int getch();
|
||||
int getstr(char * str);
|
||||
int scanw(const char *, ...);
|
||||
|
||||
// input + positioning
|
||||
int mvgetch(int y, int x);
|
||||
int mvgetstr(int y, int x, char * str);
|
||||
int mvscanw(int, int, const char*, ...);
|
||||
|
||||
// output
|
||||
int addch(const char ch);
|
||||
int addstr(const char * str);
|
||||
int printw(const char * fmt, ...);
|
||||
int inch();
|
||||
int insch(char c);
|
||||
int insertln();
|
||||
|
||||
// output + positioning
|
||||
int mvaddch(int y, int x, char ch);
|
||||
int mvaddstr(int y, int x, char * str);
|
||||
int mvprintw(int y, int x, const char * fmt, ...);
|
||||
int mvinch(int y, int x);
|
||||
int mvinsch(int y, int x, char ch);
|
||||
|
||||
// borders
|
||||
int box(char vert, char hor);
|
||||
|
||||
// erasure
|
||||
int erase();
|
||||
int clear();
|
||||
int clearok(cbool bf);
|
||||
int clrtobot();
|
||||
int clrtoeol();
|
||||
int delch();
|
||||
int mvdelch(int y, int x);
|
||||
int deleteln();
|
||||
|
||||
// screen control
|
||||
int scroll();
|
||||
int scrollok(cbool bf);
|
||||
int touchwin();
|
||||
int refresh();
|
||||
int leaveok(cbool bf);
|
||||
int flushok(cbool bf);
|
||||
int standout();
|
||||
int standend();
|
||||
|
||||
// multiple window control
|
||||
int overlay(CursesWindow &win);
|
||||
int overwrite(CursesWindow &win);
|
||||
|
||||
|
||||
// traversal support
|
||||
CursesWindow* child();
|
||||
CursesWindow* sibling();
|
||||
CursesWindow* parent();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
|
||||
inline int CursesWindow::begx()
|
||||
{
|
||||
return w->_begx;
|
||||
}
|
||||
|
||||
inline int CursesWindow::begy()
|
||||
{
|
||||
return w->_begy;
|
||||
}
|
||||
|
||||
inline int CursesWindow::maxx()
|
||||
{
|
||||
return w->_maxx;
|
||||
}
|
||||
|
||||
inline int CursesWindow::maxy()
|
||||
{
|
||||
return w->_maxy;
|
||||
}
|
||||
|
||||
inline int CursesWindow::height()
|
||||
{
|
||||
return maxy() - begy() + 1;
|
||||
}
|
||||
|
||||
inline int CursesWindow::width()
|
||||
{
|
||||
return maxx() - begx() + 1;
|
||||
}
|
||||
|
||||
inline int CursesWindow::box(char vert, char hor)
|
||||
{
|
||||
return ::box(w, vert, hor);
|
||||
}
|
||||
|
||||
inline int CursesWindow::overlay(CursesWindow &win)
|
||||
{
|
||||
return ::overlay(w, win.w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::overwrite(CursesWindow &win)
|
||||
{
|
||||
return ::overwrite(w, win.w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::scroll()
|
||||
{
|
||||
return ::scroll(w);
|
||||
}
|
||||
|
||||
|
||||
inline int CursesWindow::touchwin()
|
||||
{
|
||||
return ::touchwin(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::addch(const char ch)
|
||||
{
|
||||
return ::waddch(w, ch);
|
||||
}
|
||||
|
||||
inline int CursesWindow::addstr(const char * str)
|
||||
{
|
||||
return ::waddstr(w, str);
|
||||
}
|
||||
|
||||
inline int CursesWindow::clear()
|
||||
{
|
||||
return ::wclear(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::clrtobot()
|
||||
{
|
||||
return ::wclrtobot(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::clrtoeol()
|
||||
{
|
||||
return ::wclrtoeol(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::delch()
|
||||
{
|
||||
return ::wdelch(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::deleteln()
|
||||
{
|
||||
return ::wdeleteln(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::erase()
|
||||
{
|
||||
return ::werase(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::getch()
|
||||
{
|
||||
return ::wgetch(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::getstr(char * str)
|
||||
{
|
||||
return ::wgetstr(w, str);
|
||||
}
|
||||
|
||||
inline int CursesWindow::inch()
|
||||
{
|
||||
return winch(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::insch(char c)
|
||||
{
|
||||
return ::winsch(w, c);
|
||||
}
|
||||
|
||||
inline int CursesWindow::insertln()
|
||||
{
|
||||
return ::winsertln(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::move(int y, int x)
|
||||
{
|
||||
return ::wmove(w, y, x);
|
||||
}
|
||||
|
||||
|
||||
inline int CursesWindow::mvcur(int sy, int ey, int sx, int ex)
|
||||
{
|
||||
return ::mvcur(sy, ey, sx,ex);
|
||||
}
|
||||
|
||||
inline int CursesWindow::mvaddch(int y, int x, char ch)
|
||||
{
|
||||
return (::wmove(w, y, x)==0) ? 0 : ::waddch(w, ch);
|
||||
}
|
||||
|
||||
inline int CursesWindow::mvgetch(int y, int x)
|
||||
{
|
||||
return (::wmove(w, y, x)==0) ? 0 : ::wgetch(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::mvaddstr(int y, int x, char * str)
|
||||
{
|
||||
return (::wmove(w, y, x)==0) ? 0 : ::waddstr(w, str);
|
||||
}
|
||||
|
||||
inline int CursesWindow::mvgetstr(int y, int x, char * str)
|
||||
{
|
||||
return (::wmove(w, y, x)==0) ? 0 : ::wgetstr(w, str);
|
||||
}
|
||||
|
||||
inline int CursesWindow::mvinch(int y, int x)
|
||||
{
|
||||
return (::wmove(w, y, x)==0) ? 0 : ::winch(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::mvdelch(int y, int x)
|
||||
{
|
||||
return (::wmove(w, y, x)==0) ? 0 : ::wdelch(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::mvinsch(int y, int x, char ch)
|
||||
{
|
||||
return (::wmove(w, y, x)==0) ? 0 : ::winsch(w, ch);
|
||||
}
|
||||
|
||||
inline int CursesWindow::refresh()
|
||||
{
|
||||
return ::wrefresh(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::clearok(cbool bf)
|
||||
{
|
||||
return ::clearok(w,bf);
|
||||
}
|
||||
|
||||
inline int CursesWindow::leaveok(cbool bf)
|
||||
{
|
||||
return ::leaveok(w,bf);
|
||||
}
|
||||
|
||||
inline int CursesWindow::scrollok(cbool bf)
|
||||
{
|
||||
return ::scrollok(w,bf);
|
||||
}
|
||||
|
||||
inline int CursesWindow::flushok(cbool bf)
|
||||
{
|
||||
return ::flushok(w, bf);
|
||||
}
|
||||
|
||||
inline void CursesWindow::getyx(int& y, int& x)
|
||||
{
|
||||
::getyx(w, y, x);
|
||||
}
|
||||
|
||||
inline int CursesWindow::standout()
|
||||
{
|
||||
return ::wstandout(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::standend()
|
||||
{
|
||||
return ::wstandend(w);
|
||||
}
|
||||
|
||||
inline int CursesWindow::lines()
|
||||
{
|
||||
return LINES;
|
||||
}
|
||||
|
||||
inline int CursesWindow::cols()
|
||||
{
|
||||
return COLS;
|
||||
}
|
||||
|
||||
inline CursesWindow* CursesWindow::child()
|
||||
{
|
||||
return subwins;
|
||||
}
|
||||
|
||||
inline CursesWindow* CursesWindow::parent()
|
||||
{
|
||||
return par;
|
||||
}
|
||||
|
||||
inline CursesWindow* CursesWindow::sibling()
|
||||
{
|
||||
return sib;
|
||||
}
|
||||
|
||||
|
||||
# endif
|
||||
#endif
|
|
@ -1,79 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef _DiscreteUniform_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _DiscreteUniform_h 1
|
||||
|
||||
#include <Random.h>
|
||||
|
||||
//
|
||||
// The interval [lo..hi)
|
||||
//
|
||||
|
||||
class DiscreteUniform: public Random {
|
||||
long pLow;
|
||||
long pHigh;
|
||||
double delta;
|
||||
public:
|
||||
DiscreteUniform(long low, long high, RNG *gen);
|
||||
|
||||
long low();
|
||||
long low(long x);
|
||||
long high();
|
||||
long high(long x);
|
||||
|
||||
virtual double operator()();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline DiscreteUniform::DiscreteUniform(long low, long high, RNG *gen) : (gen)
|
||||
{
|
||||
pLow = (low < high) ? low : high;
|
||||
pHigh = (low < high) ? high : low;
|
||||
delta = (pHigh - pLow) + 1;
|
||||
}
|
||||
|
||||
inline long DiscreteUniform::low() { return pLow; }
|
||||
|
||||
inline long DiscreteUniform::low(long x) {
|
||||
long tmp = pLow;
|
||||
pLow = x;
|
||||
delta = (pHigh - pLow) + 1;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
inline long DiscreteUniform::high() { return pHigh; }
|
||||
|
||||
inline long DiscreteUniform::high(long x) {
|
||||
long tmp = pHigh;
|
||||
pHigh = x;
|
||||
delta = (pHigh - pLow) + 1;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,77 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef _Erlang_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Erlang_h 1
|
||||
|
||||
#include <Random.h>
|
||||
|
||||
class Erlang: public Random {
|
||||
protected:
|
||||
double pMean;
|
||||
double pVariance;
|
||||
int k;
|
||||
double a;
|
||||
void setState();
|
||||
public:
|
||||
Erlang(double mean, double variance, RNG *gen);
|
||||
|
||||
double mean();
|
||||
double mean(double x);
|
||||
double variance();
|
||||
double variance(double x);
|
||||
|
||||
virtual double operator()();
|
||||
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline void Erlang::setState() {
|
||||
k = int( (pMean * pMean ) / pVariance + 0.5 );
|
||||
k = (k > 0) ? k : 1;
|
||||
a = k / pMean;
|
||||
}
|
||||
|
||||
inline Erlang::Erlang(double mean, double variance, RNG *gen) : (gen)
|
||||
{
|
||||
pMean = mean; pVariance = variance;
|
||||
setState();
|
||||
}
|
||||
|
||||
inline double Erlang::mean() { return pMean; }
|
||||
inline double Erlang::mean(double x) {
|
||||
double tmp = pMean; pMean = x; setState(); return tmp;
|
||||
};
|
||||
|
||||
inline double Erlang::variance() { return pVariance; }
|
||||
inline double Erlang::variance(double x) {
|
||||
double tmp = pVariance; pVariance = x; setState(); return tmp;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,302 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifndef _File_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _File_h 1
|
||||
|
||||
#include <builtin.h>
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include <Fmodes.h>
|
||||
|
||||
class Filebuf;
|
||||
|
||||
class File
|
||||
{
|
||||
friend class Filebuf;
|
||||
protected:
|
||||
FILE* fp; // _iobuf file pointer
|
||||
char* nm; // file name (dynamically allocated)
|
||||
char rw; // 1 = read; 2 = write; 3 = readwrite
|
||||
// bit 2 (4) means read/write into string
|
||||
state_value state; // _good/_eof/_fail/_bad
|
||||
long stat; // last read/write/... return value
|
||||
|
||||
void initialize();
|
||||
void reinitialize(const char*);
|
||||
char *readline (int chunk_number, char terminator);
|
||||
|
||||
public:
|
||||
File();
|
||||
File(const char* filename, io_mode m, access_mode a);
|
||||
File(const char* filename, const char* m);
|
||||
File(int filedesc, io_mode m);
|
||||
File(FILE* fileptr);
|
||||
File(int sz, char* buf, io_mode m);
|
||||
|
||||
~File();
|
||||
|
||||
// binding, rebinding, unbinding to physical files
|
||||
|
||||
File& open(const char* filename, io_mode m, access_mode a);
|
||||
File& open(const char* filename, const char* m);
|
||||
File& open(int filedesc, io_mode m);
|
||||
File& open(FILE* fileptr);
|
||||
|
||||
File& close();
|
||||
File& remove();
|
||||
|
||||
// class variable access
|
||||
|
||||
int filedesc();
|
||||
const char* name();
|
||||
void setname(const char* newname);
|
||||
int iocount();
|
||||
|
||||
int rdstate();
|
||||
int eof();
|
||||
int fail();
|
||||
int bad();
|
||||
int good();
|
||||
|
||||
// other status queries
|
||||
|
||||
int readable();
|
||||
int writable();
|
||||
int is_open();
|
||||
|
||||
operator void*();
|
||||
|
||||
// error handling
|
||||
|
||||
void error();
|
||||
void clear(state_value f = _good); // poorly named
|
||||
void set(state_value f); // set corresponding but
|
||||
void unset(state_value f); // clear corresponding bit
|
||||
File& failif(int cond);
|
||||
void check_state();
|
||||
|
||||
// character IO
|
||||
|
||||
File& get(char& c);
|
||||
File& put(char c);
|
||||
File& unget(char c);
|
||||
File& putback(char c); // a synonym for unget
|
||||
|
||||
// char* IO
|
||||
|
||||
File& put(const char* s);
|
||||
File& get (char* s, int n, char terminator = '\n');
|
||||
File& getline(char* s, int n, char terminator = '\n');
|
||||
File& gets (char **s, char terminator = '\n');
|
||||
|
||||
// binary IO
|
||||
|
||||
File& read(void* x, int sz, int n);
|
||||
File& write(void* x, int sz, int n);
|
||||
|
||||
// formatted IO
|
||||
|
||||
File& form(const char* ...);
|
||||
File& scan(const char* ...);
|
||||
|
||||
// buffer IO
|
||||
|
||||
File& flush();
|
||||
|
||||
// position control
|
||||
|
||||
File& seek(long pos, int seek_mode=0); // default seek mode=absolute
|
||||
long tell();
|
||||
|
||||
// buffer control
|
||||
|
||||
File& setbuf(int buffer_kind); // legal vals: _IONBF, _IOFBF, _IOLBF
|
||||
File& setbuf(int size, char* buf);
|
||||
File& raw();
|
||||
};
|
||||
|
||||
|
||||
// error handlers
|
||||
|
||||
extern void verbose_File_error_handler(const char*);
|
||||
extern void quiet_File_error_handler(const char*);
|
||||
extern void fatal_File_error_handler(const char*);
|
||||
extern one_arg_error_handler_t File_error_handler;
|
||||
extern one_arg_error_handler_t set_File_error_handler(one_arg_error_handler_t);
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
|
||||
|
||||
inline int File::filedesc()
|
||||
{
|
||||
return fileno(fp);
|
||||
}
|
||||
|
||||
inline const char* File::name()
|
||||
{
|
||||
return nm;
|
||||
}
|
||||
|
||||
inline int File::iocount()
|
||||
{
|
||||
return stat;
|
||||
}
|
||||
|
||||
inline void File::clear(state_value flag)
|
||||
{
|
||||
state = flag;
|
||||
}
|
||||
|
||||
inline void File::set(state_value flag)
|
||||
{
|
||||
state = state_value(int(state) | int(flag));
|
||||
}
|
||||
|
||||
inline void File::unset(state_value flag)
|
||||
{
|
||||
state = state_value(int(state) & ~int(flag));
|
||||
}
|
||||
|
||||
inline int File::readable()
|
||||
{
|
||||
if (fp != 0) { if (feof(fp)) set(_eof); if (ferror(fp)) set(_bad);}
|
||||
return (state == _good && (rw & 01));
|
||||
}
|
||||
|
||||
inline int File::writable()
|
||||
{
|
||||
if (fp != 0 && ferror(fp)) set(_bad);
|
||||
return ((int(state) & (int(_fail)|int(_bad))) == 0 && (rw & 02));
|
||||
}
|
||||
|
||||
inline int File::is_open()
|
||||
{
|
||||
return (fp != 0);
|
||||
}
|
||||
|
||||
|
||||
inline File& File::raw()
|
||||
{
|
||||
return this->File::setbuf(_IONBF);
|
||||
}
|
||||
|
||||
|
||||
inline File& File::failif(int cond)
|
||||
{
|
||||
if (cond) set(_fail); return *this;
|
||||
}
|
||||
|
||||
inline File& File::get(char& c)
|
||||
{
|
||||
if (readable())
|
||||
{
|
||||
int ch = getc(fp);
|
||||
c = ch;
|
||||
failif (ch == EOF);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline File& File::put(char c)
|
||||
{
|
||||
return failif (!writable() || putc(c, fp) == EOF);
|
||||
}
|
||||
|
||||
inline File& File::unget(char c)
|
||||
{
|
||||
return failif(!is_open() || !(rw & 01) || ungetc(c, fp) == EOF);
|
||||
}
|
||||
|
||||
inline File& File::putback(char c)
|
||||
{
|
||||
return failif (!is_open() || !(rw & 01) || ungetc(c, fp) == EOF);
|
||||
}
|
||||
|
||||
inline File& File::read(void* x, int sz, int n)
|
||||
{
|
||||
return failif (!readable() || (stat = fread(x, sz, n, fp)) != n);
|
||||
}
|
||||
|
||||
inline File& File::write(void* x, int sz, int n)
|
||||
{
|
||||
return failif (!writable() || (stat = fwrite(x, sz, n, fp)) != n);
|
||||
}
|
||||
|
||||
inline File& File::flush()
|
||||
{
|
||||
return failif(!is_open() || fflush(fp) == EOF);
|
||||
}
|
||||
|
||||
|
||||
inline File& File::seek(long pos, int seek_mode)
|
||||
{
|
||||
return failif (!is_open() || fseek(fp, pos, seek_mode) < 0);
|
||||
}
|
||||
|
||||
inline long File::tell()
|
||||
{
|
||||
failif (!is_open() || ((stat = ftell(fp)) < 0));
|
||||
return stat;
|
||||
}
|
||||
|
||||
inline int File::rdstate()
|
||||
{
|
||||
check_state(); return state; // check_state is necessary in rare but
|
||||
} // possible circumstances
|
||||
|
||||
inline File::operator void*()
|
||||
{
|
||||
check_state(); return (int(state) & (int(_bad)|int(_fail)))? 0 : this ;
|
||||
}
|
||||
|
||||
inline int File::eof()
|
||||
{
|
||||
check_state(); return state & _eof;
|
||||
}
|
||||
|
||||
inline int File::fail()
|
||||
{
|
||||
check_state(); return state & _fail;
|
||||
}
|
||||
|
||||
inline int File::bad()
|
||||
{
|
||||
check_state(); return state & _bad;
|
||||
}
|
||||
|
||||
inline int File::good()
|
||||
{
|
||||
check_state(); return rdstate() == _good;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,32 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifndef _Filebuf_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Filebuf_h 1
|
||||
|
||||
|
||||
#endif
|
|
@ -1,468 +0,0 @@
|
|||
//
|
||||
// Fix.h : variable length fixed point data type
|
||||
//
|
||||
|
||||
#ifndef _Fix_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Fix_h 1
|
||||
|
||||
#include <stream.h>
|
||||
#include <std.h>
|
||||
#include <stddef.h>
|
||||
#include <Integer.h>
|
||||
|
||||
typedef unsigned short uint16;
|
||||
typedef short int16;
|
||||
typedef unsigned long uint32;
|
||||
typedef long int32;
|
||||
|
||||
#define _Fix_min_length 1
|
||||
#define _Fix_max_length 65535
|
||||
|
||||
#define _Fix_min_value -1.0
|
||||
#define _Fix_max_value 1.0
|
||||
|
||||
extern uint16 Fix_default_length;
|
||||
extern int Fix_default_print_width;
|
||||
|
||||
struct _Frep // internal Fix representation
|
||||
{
|
||||
uint16 len; // length in bits
|
||||
uint16 siz; // allocated storage
|
||||
int16 ref; // reference count
|
||||
uint16 s[1]; // start of ushort array represention
|
||||
};
|
||||
|
||||
typedef struct _Frep* _Fix;
|
||||
|
||||
extern _Frep _Frep_0;
|
||||
extern _Frep _Frep_m1;
|
||||
extern _Frep _Frep_quotient_bump;
|
||||
|
||||
class Fix
|
||||
{
|
||||
_Fix rep;
|
||||
|
||||
Fix(_Fix);
|
||||
|
||||
void unique();
|
||||
|
||||
public:
|
||||
Fix();
|
||||
Fix(Fix&);
|
||||
Fix(double&);
|
||||
Fix(int);
|
||||
Fix(int, Fix&);
|
||||
Fix(int, double&);
|
||||
Fix(int, _Frep*);
|
||||
|
||||
~Fix();
|
||||
|
||||
Fix operator = (Fix&);
|
||||
Fix operator = (double&);
|
||||
|
||||
friend int operator == (Fix&, Fix& );
|
||||
friend int operator != (Fix&, Fix&);
|
||||
|
||||
friend int operator < (Fix&, Fix&);
|
||||
friend int operator <= (Fix&, Fix&);
|
||||
friend int operator > (Fix&, Fix&);
|
||||
friend int operator >= (Fix&, Fix&);
|
||||
|
||||
Fix& operator + ();
|
||||
Fix operator - ();
|
||||
|
||||
friend Fix operator + (Fix&, Fix&);
|
||||
friend Fix operator - (Fix&, Fix&);
|
||||
friend Fix operator * (Fix&, Fix&);
|
||||
friend Fix operator / (Fix&, Fix&);
|
||||
|
||||
friend Fix operator * (Fix&, int);
|
||||
friend Fix operator * (int, Fix&);
|
||||
friend Fix operator % (Fix&, int);
|
||||
friend Fix operator << (Fix&, int);
|
||||
friend Fix operator >> (Fix&, int);
|
||||
|
||||
#ifdef __GNUG__
|
||||
friend Fix operator <? (Fix&, Fix&); // min
|
||||
friend Fix operator >? (Fix&, Fix&); // max
|
||||
#endif
|
||||
|
||||
Fix operator += (Fix&);
|
||||
Fix operator -= (Fix&);
|
||||
Fix operator *= (Fix&);
|
||||
Fix operator /= (Fix&);
|
||||
|
||||
Fix operator *= (int);
|
||||
Fix operator %= (int);
|
||||
Fix operator <<=(int);
|
||||
Fix operator >>=(int);
|
||||
|
||||
friend char* Ftoa(Fix&, int width = Fix_default_print_width);
|
||||
friend Fix atoF(const char*, int len = Fix_default_length);
|
||||
|
||||
friend istream& operator >> (istream&, Fix&);
|
||||
friend ostream& operator << (ostream&, Fix&);
|
||||
|
||||
// built-in functions
|
||||
friend Fix abs(Fix); // absolute value
|
||||
friend int sgn(Fix&); // -1, 0, +1
|
||||
friend Integer mantissa(Fix&); // integer representation
|
||||
friend double value(Fix&); // double value
|
||||
friend int length(Fix&); // field length
|
||||
friend void show(Fix&); // show contents
|
||||
|
||||
// error handlers
|
||||
void error(const char* msg); // error handler
|
||||
void range_error(const char* msg); // range error handler
|
||||
|
||||
// internal class functions
|
||||
friend void mask(_Fix);
|
||||
friend int compare(_Fix, _Fix = &_Frep_0);
|
||||
|
||||
friend _Fix new_Fix(uint16);
|
||||
friend _Fix new_Fix(uint16, _Fix);
|
||||
friend _Fix new_Fix(uint16, double);
|
||||
|
||||
friend _Fix copy(_Fix, _Fix);
|
||||
friend _Fix negate(_Fix, _Fix = NULL);
|
||||
friend _Fix add(_Fix, _Fix, _Fix = NULL);
|
||||
friend _Fix subtract(_Fix, _Fix, _Fix = NULL);
|
||||
friend _Fix multiply(_Fix, _Fix, _Fix = NULL);
|
||||
friend _Fix multiply(_Fix, int, _Fix = NULL);
|
||||
friend _Fix divide(_Fix, _Fix, _Fix = NULL, _Fix = NULL);
|
||||
friend _Fix shift(_Fix, int, _Fix = NULL);
|
||||
|
||||
// non-operator versions for user
|
||||
friend void negate(Fix& x, Fix& r);
|
||||
friend void add(Fix& x, Fix& y, Fix& r);
|
||||
friend void subtract(Fix& x, Fix& y, Fix& r);
|
||||
friend void multiply(Fix& x, Fix& y, Fix& r);
|
||||
friend void divide(Fix& x, Fix& y, Fix& q, Fix& r);
|
||||
friend void shift(Fix& x, int y, Fix& r);
|
||||
};
|
||||
|
||||
// error handlers
|
||||
|
||||
extern void
|
||||
default_Fix_error_handler(const char*),
|
||||
default_Fix_range_error_handler(const char*);
|
||||
|
||||
extern one_arg_error_handler_t
|
||||
Fix_error_handler,
|
||||
Fix_range_error_handler;
|
||||
|
||||
extern one_arg_error_handler_t
|
||||
set_Fix_error_handler(one_arg_error_handler_t f),
|
||||
set_Fix_range_error_handler(one_arg_error_handler_t f);
|
||||
|
||||
typedef void (*Fix_peh)(_Fix&);
|
||||
extern Fix_peh Fix_overflow_handler;
|
||||
|
||||
extern void
|
||||
Fix_overflow_saturate(_Fix&),
|
||||
Fix_overflow_wrap(_Fix&),
|
||||
Fix_overflow_warning_saturate(_Fix&),
|
||||
Fix_overflow_warning(_Fix&),
|
||||
Fix_overflow_error(_Fix&);
|
||||
|
||||
extern Fix_peh set_overflow_handler(Fix_peh);
|
||||
|
||||
extern int Fix_set_default_length(int);
|
||||
|
||||
// function definitions
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline void Fix::unique()
|
||||
{
|
||||
if ( rep->ref > 1 )
|
||||
{
|
||||
rep->ref--;
|
||||
rep = new_Fix(rep->len,rep);
|
||||
}
|
||||
}
|
||||
|
||||
inline void mask (_Fix x)
|
||||
{
|
||||
int n = x->len & 0x0f;
|
||||
if ( n )
|
||||
x->s[x->siz - 1] &= 0xffff0000 >> n;
|
||||
}
|
||||
|
||||
inline _Fix copy(_Fix from, _Fix to)
|
||||
{
|
||||
uint16 *ts = to->s, *fs = from->s;
|
||||
int ilim = to->siz < from->siz ? to->siz : from->siz;
|
||||
for ( int i=0; i < ilim; i++ )
|
||||
*ts++ = *fs++;
|
||||
for ( ; i < to->siz; i++ )
|
||||
*ts++ = 0;
|
||||
mask(to);
|
||||
return to;
|
||||
}
|
||||
|
||||
inline Fix::Fix(_Fix f)
|
||||
{
|
||||
rep = f;
|
||||
}
|
||||
|
||||
inline Fix::Fix()
|
||||
{
|
||||
rep = new_Fix(Fix_default_length);
|
||||
}
|
||||
|
||||
inline Fix::Fix(int len)
|
||||
{
|
||||
if ( len < _Fix_min_length || len > _Fix_max_length )
|
||||
error("illegal length in declaration");
|
||||
rep = new_Fix((uint16 )len);
|
||||
}
|
||||
|
||||
inline Fix::Fix(double& d)
|
||||
{
|
||||
rep = new_Fix(Fix_default_length,d);
|
||||
}
|
||||
|
||||
inline Fix::Fix(Fix& y)
|
||||
{
|
||||
rep = y.rep; rep->ref++;
|
||||
}
|
||||
|
||||
inline Fix::Fix(int len, Fix& y)
|
||||
{
|
||||
if ( len < _Fix_min_length || len > _Fix_max_length )
|
||||
error("illegal length in declaration");
|
||||
rep = new_Fix((uint16 )len,y.rep);
|
||||
}
|
||||
|
||||
inline Fix::Fix(int len, _Frep* fr)
|
||||
{
|
||||
if ( len < 1 || len > 65535 )
|
||||
error("illegal length in declaration");
|
||||
rep = new_Fix((uint16 )len,fr);
|
||||
}
|
||||
|
||||
inline Fix::Fix(int len, double& d)
|
||||
{
|
||||
if ( len < _Fix_min_length || len > _Fix_max_length )
|
||||
error("illegal length in declaration");
|
||||
rep = new_Fix((uint16 )len,d);
|
||||
}
|
||||
|
||||
inline Fix::~Fix()
|
||||
{
|
||||
if ( --rep->ref <= 0 ) delete rep;
|
||||
}
|
||||
|
||||
inline Fix Fix::operator = (Fix& y)
|
||||
{
|
||||
if ( rep->len == y.rep->len ) {
|
||||
++y.rep->ref;
|
||||
if ( --rep->ref <= 0 ) delete rep;
|
||||
rep = y.rep;
|
||||
}
|
||||
else {
|
||||
unique();
|
||||
copy(y.rep,rep);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Fix Fix::operator = (double& d)
|
||||
{
|
||||
int oldlen = rep->len;
|
||||
if ( --rep->ref <= 0 ) delete rep;
|
||||
rep = new_Fix(oldlen,d);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline int operator == (Fix& x, Fix& y)
|
||||
{
|
||||
return compare(x.rep, y.rep) == 0;
|
||||
}
|
||||
|
||||
inline int operator != (Fix& x, Fix& y)
|
||||
{
|
||||
return compare(x.rep, y.rep) != 0;
|
||||
}
|
||||
|
||||
inline int operator < (Fix& x, Fix& y)
|
||||
{
|
||||
return compare(x.rep, y.rep) < 0;
|
||||
}
|
||||
|
||||
inline int operator <= (Fix& x, Fix& y)
|
||||
{
|
||||
return compare(x.rep, y.rep) <= 0;
|
||||
}
|
||||
|
||||
inline int operator > (Fix& x, Fix& y)
|
||||
{
|
||||
return compare(x.rep, y.rep) > 0;
|
||||
}
|
||||
|
||||
inline int operator >= (Fix& x, Fix& y)
|
||||
{
|
||||
return compare(x.rep, y.rep) >= 0;
|
||||
}
|
||||
|
||||
inline Fix& Fix::operator + ()
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Fix Fix::operator - ()
|
||||
{
|
||||
_Fix r = negate(rep); return r;
|
||||
}
|
||||
|
||||
inline Fix operator + (Fix& x, Fix& y)
|
||||
{
|
||||
_Fix r = add(x.rep, y.rep); return r;
|
||||
}
|
||||
|
||||
inline Fix operator - (Fix& x, Fix& y)
|
||||
{
|
||||
_Fix r = subtract(x.rep, y.rep); return r;
|
||||
}
|
||||
|
||||
inline Fix operator * (Fix& x, Fix& y)
|
||||
{
|
||||
_Fix r = multiply(x.rep, y.rep); return r;
|
||||
}
|
||||
|
||||
inline Fix operator * (Fix& x, int y)
|
||||
{
|
||||
_Fix r = multiply(x.rep, y); return r;
|
||||
}
|
||||
|
||||
inline Fix operator * (int y, Fix& x)
|
||||
{
|
||||
_Fix r = multiply(x.rep, y); return r;
|
||||
}
|
||||
|
||||
inline Fix operator / (Fix& x, Fix& y)
|
||||
{
|
||||
_Fix r = divide(x.rep, y.rep); return r;
|
||||
}
|
||||
|
||||
inline Fix Fix::operator += (Fix& y)
|
||||
{
|
||||
unique(); add(rep, y.rep, rep); return *this;
|
||||
}
|
||||
|
||||
inline Fix Fix::operator -= (Fix& y)
|
||||
{
|
||||
unique(); subtract(rep, y.rep, rep); return *this;
|
||||
}
|
||||
|
||||
inline Fix Fix::operator *= (Fix& y)
|
||||
{
|
||||
unique(); multiply(rep, y.rep, rep); return *this;
|
||||
}
|
||||
|
||||
inline Fix Fix::operator *= (int y)
|
||||
{
|
||||
unique(); multiply(rep, y, rep); return *this;
|
||||
}
|
||||
|
||||
inline Fix Fix::operator /= (Fix& y)
|
||||
{
|
||||
unique(); divide(rep, y.rep, rep); return *this;
|
||||
}
|
||||
|
||||
inline Fix operator % (Fix& x, int y)
|
||||
{
|
||||
Fix r((int )x.rep->len + y, x); return r;
|
||||
}
|
||||
|
||||
inline Fix operator << (Fix& x, int y)
|
||||
{
|
||||
_Fix rep = shift(x.rep, y); return rep;
|
||||
}
|
||||
|
||||
inline Fix operator >> (Fix& x, int y)
|
||||
{
|
||||
_Fix rep = shift(x.rep, -y); return rep;
|
||||
}
|
||||
|
||||
inline Fix Fix::operator <<= (int y)
|
||||
{
|
||||
unique(); shift(rep, y, rep); return *this;
|
||||
}
|
||||
|
||||
inline Fix Fix::operator >>= (int y)
|
||||
{
|
||||
unique(); shift(rep, -y, rep); return *this;
|
||||
}
|
||||
|
||||
#ifdef __GNUG__
|
||||
inline Fix operator <? (Fix& x, Fix& y)
|
||||
{
|
||||
if ( compare(x.rep, y.rep) <= 0 ) return x; else return y;
|
||||
}
|
||||
|
||||
inline Fix operator >? (Fix& x, Fix& y)
|
||||
{
|
||||
if ( compare(x.rep, y.rep) >= 0 ) return x; else return y;
|
||||
}
|
||||
#endif
|
||||
|
||||
inline Fix abs(Fix x)
|
||||
{
|
||||
_Fix r = (compare(x.rep) >= 0 ? new_Fix(x.rep->len,x.rep) : negate(x.rep));
|
||||
return r;
|
||||
}
|
||||
|
||||
inline int sgn(Fix& x)
|
||||
{
|
||||
int a = compare(x.rep);
|
||||
return a == 0 ? 0 : (a > 0 ? 1 : -1);
|
||||
}
|
||||
|
||||
inline int length(Fix& x)
|
||||
{
|
||||
return x.rep->len;
|
||||
}
|
||||
|
||||
inline ostream& operator << (ostream& s, Fix& y)
|
||||
{
|
||||
return s << Ftoa(y);
|
||||
}
|
||||
|
||||
inline void negate (Fix& x, Fix& r)
|
||||
{
|
||||
negate(x.rep, r.rep);
|
||||
}
|
||||
|
||||
inline void add (Fix& x, Fix& y, Fix& r)
|
||||
{
|
||||
add(x.rep, y.rep, r.rep);
|
||||
}
|
||||
|
||||
inline void subtract (Fix& x, Fix& y, Fix& r)
|
||||
{
|
||||
subtract(x.rep, y.rep, r.rep);
|
||||
}
|
||||
|
||||
inline void multiply (Fix& x, Fix& y, Fix& r)
|
||||
{
|
||||
multiply(x.rep, y.rep, r.rep);
|
||||
}
|
||||
|
||||
inline void divide (Fix& x, Fix& y, Fix& q, Fix& r)
|
||||
{
|
||||
divide(x.rep, y.rep, q.rep, r.rep);
|
||||
}
|
||||
|
||||
inline void shift (Fix& x, int y, Fix& r)
|
||||
{
|
||||
shift(x.rep, y, r.rep);
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,647 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Kurt Baudendistel (gt-eedsp!baud@gatech.edu)
|
||||
adapted for libg++ by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifndef _Fix16_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Fix16_h 1
|
||||
|
||||
#include <stream.h>
|
||||
#include <std.h>
|
||||
|
||||
// constant definitions
|
||||
|
||||
#define Fix16_fs ((double)((unsigned)(1 << 15)))
|
||||
|
||||
#define Fix16_msb (1 << 15)
|
||||
#define Fix16_m_max ((1 << 15) - 1)
|
||||
#define Fix16_m_min ((short)(1 << 15))
|
||||
|
||||
#define Fix16_mult Fix16_fs
|
||||
#define Fix16_div (1./Fix16_fs)
|
||||
#define Fix16_max (1. - .5/Fix16_fs)
|
||||
#define Fix16_min (-1.)
|
||||
|
||||
|
||||
#define Fix32_fs ((double)((unsigned long)(1 << 31)))
|
||||
|
||||
#define Fix32_msb ((unsigned long)(1 << 31))
|
||||
#define Fix32_m_max ((1 << 31) - 1)
|
||||
#define Fix32_m_min ((long)(1 << 31))
|
||||
|
||||
#define Fix32_mult Fix32_fs
|
||||
#define Fix32_div (1./Fix32_fs)
|
||||
#define Fix32_max (1. - .5/Fix32_fs)
|
||||
#define Fix32_min (-1.)
|
||||
|
||||
|
||||
//
|
||||
// Fix16 class: 16-bit Fixed point data type
|
||||
//
|
||||
// consists of a 16-bit mantissa (sign bit & 15 data bits).
|
||||
//
|
||||
|
||||
class Fix16
|
||||
{
|
||||
friend class Fix32;
|
||||
|
||||
short m;
|
||||
|
||||
short round(double d);
|
||||
short assign(double d);
|
||||
Fix16(short i);
|
||||
Fix16(int i);
|
||||
|
||||
operator double();
|
||||
|
||||
|
||||
public:
|
||||
Fix16();
|
||||
Fix16(Fix16& f);
|
||||
Fix16(double d);
|
||||
Fix16(Fix32& f);
|
||||
|
||||
~Fix16();
|
||||
|
||||
Fix16& operator=(Fix16& f);
|
||||
Fix16& operator=(double d);
|
||||
Fix16& operator=(Fix32& f);
|
||||
|
||||
friend short& mantissa(Fix16& f);
|
||||
friend double value(Fix16& f);
|
||||
|
||||
Fix16 operator + ();
|
||||
Fix16 operator - ();
|
||||
|
||||
friend Fix16 operator + (Fix16& f, Fix16& g);
|
||||
friend Fix16 operator - (Fix16& f, Fix16& g);
|
||||
friend Fix32 operator * (Fix16& f, Fix16& g);
|
||||
friend Fix16 operator / (Fix16& f, Fix16& g);
|
||||
friend Fix16 operator << (Fix16& f, int b);
|
||||
friend Fix16 operator >> (Fix16& f, int b);
|
||||
|
||||
Fix16& operator += (Fix16& f);
|
||||
Fix16& operator -= (Fix16& f);
|
||||
Fix16& operator *= (Fix16& );
|
||||
Fix16& operator /= (Fix16& f);
|
||||
|
||||
Fix16& operator <<=(int b);
|
||||
Fix16& operator >>=(int b);
|
||||
|
||||
friend int operator == (Fix16& f, Fix16& g);
|
||||
friend int operator != (Fix16& f, Fix16& g);
|
||||
friend int operator >= (Fix16& f, Fix16& g);
|
||||
friend int operator <= (Fix16& f, Fix16& g);
|
||||
friend int operator > (Fix16& f, Fix16& g);
|
||||
friend int operator < (Fix16& f, Fix16& g);
|
||||
|
||||
friend istream& operator >> (istream& s, Fix16& f);
|
||||
friend ostream& operator << (ostream& s, Fix16& f);
|
||||
|
||||
void overflow(short&);
|
||||
void range_error(short&);
|
||||
|
||||
friend Fix16 operator * (Fix16& f, int g);
|
||||
friend Fix16 operator * (int g, Fix16& f);
|
||||
Fix16& operator *= (int g);
|
||||
};
|
||||
|
||||
|
||||
//
|
||||
// Fix32 class: 32-bit Fixed point data type
|
||||
//
|
||||
// consists of a 32-bit mantissa (sign bit & 31 data bits).
|
||||
//
|
||||
|
||||
class Fix32
|
||||
{
|
||||
friend class Fix16;
|
||||
|
||||
long m;
|
||||
|
||||
long round(double d);
|
||||
long assign(double d);
|
||||
|
||||
Fix32(long i);
|
||||
operator double();
|
||||
|
||||
|
||||
public:
|
||||
Fix32();
|
||||
Fix32(Fix32& f);
|
||||
Fix32(Fix16& f);
|
||||
Fix32(double d);
|
||||
~Fix32();
|
||||
|
||||
Fix32& operator = (Fix32& f);
|
||||
Fix32& operator = (Fix16& f);
|
||||
Fix32& operator = (double d);
|
||||
|
||||
friend long& mantissa(Fix32& f);
|
||||
friend double value(Fix32& f);
|
||||
|
||||
Fix32 operator + ();
|
||||
Fix32 operator - ();
|
||||
|
||||
friend Fix32 operator + (Fix32& f, Fix32& g);
|
||||
friend Fix32 operator - (Fix32& f, Fix32& g);
|
||||
friend Fix32 operator * (Fix32& f, Fix32& g);
|
||||
friend Fix32 operator / (Fix32& f, Fix32& g);
|
||||
friend Fix32 operator << (Fix32& f, int b);
|
||||
friend Fix32 operator >> (Fix32& f, int b);
|
||||
|
||||
friend Fix32 operator * (Fix16& f, Fix16& g);
|
||||
|
||||
Fix32& operator += (Fix32& f);
|
||||
Fix32& operator -= (Fix32& f);
|
||||
Fix32& operator *= (Fix32& f);
|
||||
Fix32& operator /= (Fix32& f);
|
||||
Fix32& operator <<=(int b);
|
||||
Fix32& operator >>=(int b);
|
||||
|
||||
friend int operator == (Fix32& f, Fix32& g);
|
||||
friend int operator != (Fix32& f, Fix32& g);
|
||||
friend int operator >= (Fix32& f, Fix32& g);
|
||||
friend int operator <= (Fix32& f, Fix32& g);
|
||||
friend int operator > (Fix32& f, Fix32& g);
|
||||
friend int operator < (Fix32& f, Fix32& g);
|
||||
|
||||
friend istream& operator >> (istream& s, Fix32& f);
|
||||
friend ostream& operator << (ostream& s, Fix32& f);
|
||||
|
||||
void overflow(long& i);
|
||||
void range_error(long& i);
|
||||
|
||||
friend Fix32 operator * (Fix32& f, int g);
|
||||
friend Fix32 operator * (int g, Fix32& f);
|
||||
Fix32& operator *= (int g);
|
||||
};
|
||||
|
||||
// active error handler declarations
|
||||
|
||||
typedef void (*Fix16_peh)(short&);
|
||||
typedef void (*Fix32_peh)(long&);
|
||||
|
||||
extern Fix16_peh Fix16_overflow_handler;
|
||||
extern Fix32_peh Fix32_overflow_handler;
|
||||
|
||||
extern Fix16_peh Fix16_range_error_handler;
|
||||
extern Fix32_peh Fix32_range_error_handler;
|
||||
|
||||
#if defined(SHORT_NAMES) || defined(VMS)
|
||||
#define set_overflow_handler sohndl
|
||||
#define set_range_error_handler srnghdl
|
||||
#endif
|
||||
|
||||
|
||||
// error handler declarations
|
||||
|
||||
extern Fix16_peh set_Fix16_overflow_handler(Fix16_peh);
|
||||
extern Fix32_peh set_Fix32_overflow_handler(Fix32_peh);
|
||||
extern void set_overflow_handler(Fix16_peh, Fix32_peh);
|
||||
|
||||
extern Fix16_peh set_Fix16_range_error_handler(Fix16_peh);
|
||||
extern Fix32_peh set_Fix32_range_error_handler(Fix32_peh);
|
||||
extern void set_range_error_handler(Fix16_peh, Fix32_peh);
|
||||
|
||||
extern void
|
||||
Fix16_ignore(short&),
|
||||
Fix16_overflow_saturate(short&),
|
||||
Fix16_overflow_warning_saturate(short&),
|
||||
Fix16_warning(short&),
|
||||
Fix16_abort(short&);
|
||||
|
||||
extern void
|
||||
Fix32_ignore(long&),
|
||||
Fix32_overflow_saturate(long&),
|
||||
Fix32_overflow_warning_saturate(long&),
|
||||
Fix32_warning(long&),
|
||||
Fix32_abort(long&);
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline Fix16::~Fix16() {}
|
||||
|
||||
inline short Fix16::round(double d)
|
||||
{
|
||||
return short( (d >= 0)? d + 0.5 : d - 0.5);
|
||||
}
|
||||
|
||||
inline Fix16::Fix16(short i)
|
||||
{
|
||||
m = i;
|
||||
}
|
||||
|
||||
inline Fix16::Fix16(int i)
|
||||
{
|
||||
m = i;
|
||||
}
|
||||
|
||||
inline Fix16::operator double()
|
||||
{
|
||||
return Fix16_div * m;
|
||||
}
|
||||
|
||||
inline Fix16::Fix16()
|
||||
{
|
||||
m = 0;
|
||||
}
|
||||
|
||||
inline Fix16::Fix16(Fix16& f)
|
||||
{
|
||||
m = f.m;
|
||||
}
|
||||
|
||||
inline Fix16::Fix16(double d)
|
||||
{
|
||||
m = assign(d);
|
||||
}
|
||||
|
||||
|
||||
inline Fix16& Fix16::operator=(Fix16& f)
|
||||
{
|
||||
m = f.m;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Fix16& Fix16::operator=(double d)
|
||||
{
|
||||
m = assign(d);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline Fix32::Fix32()
|
||||
{
|
||||
m = 0;
|
||||
}
|
||||
|
||||
inline Fix32::Fix32(long i)
|
||||
{
|
||||
m = i;
|
||||
}
|
||||
|
||||
inline Fix32:: operator double()
|
||||
{
|
||||
return Fix32_div * m;
|
||||
}
|
||||
|
||||
|
||||
inline Fix32::Fix32(Fix32& f)
|
||||
{
|
||||
m = f.m;
|
||||
}
|
||||
|
||||
inline Fix32::Fix32(Fix16& f)
|
||||
{
|
||||
m = long(f.m) << 16;
|
||||
}
|
||||
|
||||
inline Fix32::Fix32(double d)
|
||||
{
|
||||
m = assign(d);
|
||||
}
|
||||
|
||||
inline Fix16::Fix16(Fix32& f)
|
||||
{
|
||||
m = f.m >> 16;
|
||||
}
|
||||
|
||||
|
||||
inline Fix16& Fix16::operator=(Fix32& f)
|
||||
{
|
||||
m = f.m >> 16;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Fix32& Fix32::operator=(Fix32& f)
|
||||
{
|
||||
m = f.m;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Fix32& Fix32::operator=(Fix16& f)
|
||||
{
|
||||
m = long(f.m) << 16;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Fix32& Fix32::operator=(double d)
|
||||
{
|
||||
m = assign(d);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline short& mantissa(Fix16& f)
|
||||
{
|
||||
return f.m;
|
||||
}
|
||||
|
||||
inline double value(Fix16& f)
|
||||
{
|
||||
return double(f);
|
||||
}
|
||||
|
||||
inline Fix16 Fix16::operator+()
|
||||
{
|
||||
return m;
|
||||
}
|
||||
|
||||
inline Fix16 Fix16::operator-()
|
||||
{
|
||||
return -m;
|
||||
}
|
||||
|
||||
inline Fix16 operator+(Fix16& f, Fix16& g)
|
||||
{
|
||||
short sum = f.m + g.m;
|
||||
if ( (f.m ^ sum) & (g.m ^ sum) & Fix16_msb )
|
||||
f.overflow(sum);
|
||||
return sum;
|
||||
}
|
||||
|
||||
inline Fix16 operator-(Fix16& f, Fix16& g)
|
||||
{
|
||||
short sum = f.m - g.m;
|
||||
if ( (f.m ^ sum) & (-g.m ^ sum) & Fix16_msb )
|
||||
f.overflow(sum);
|
||||
return sum;
|
||||
}
|
||||
|
||||
inline Fix32 operator*(Fix16& f, Fix16& g)
|
||||
{
|
||||
return Fix32( long( long(f.m) * long(g.m) << 1));
|
||||
}
|
||||
|
||||
inline Fix16 operator<<(Fix16& a, int b)
|
||||
{
|
||||
return a.m << b;
|
||||
}
|
||||
|
||||
inline Fix16 operator>>(Fix16& a, int b)
|
||||
{
|
||||
return a.m >> b;
|
||||
}
|
||||
|
||||
inline Fix16& Fix16:: operator+=(Fix16& f)
|
||||
{
|
||||
return *this = *this + f;
|
||||
}
|
||||
|
||||
inline Fix16& Fix16:: operator-=(Fix16& f)
|
||||
{
|
||||
return *this = *this - f;
|
||||
}
|
||||
|
||||
inline Fix16& Fix16::operator*=(Fix16& f)
|
||||
{
|
||||
return *this = *this * f;
|
||||
}
|
||||
|
||||
inline Fix16& Fix16:: operator/=(Fix16& f)
|
||||
{
|
||||
return *this = *this / f;
|
||||
}
|
||||
|
||||
inline Fix16& Fix16:: operator<<=(int b)
|
||||
{
|
||||
return *this = *this << b;
|
||||
}
|
||||
|
||||
inline Fix16& Fix16:: operator>>=(int b)
|
||||
{
|
||||
return *this = *this >> b;
|
||||
}
|
||||
|
||||
inline int operator==(Fix16& f, Fix16& g)
|
||||
{
|
||||
return f.m == g.m;
|
||||
}
|
||||
|
||||
inline int operator!=(Fix16& f, Fix16& g)
|
||||
{
|
||||
return f.m != g.m;
|
||||
}
|
||||
|
||||
inline int operator>=(Fix16& f, Fix16& g)
|
||||
{
|
||||
return f.m >= g.m;
|
||||
}
|
||||
|
||||
inline int operator<=(Fix16& f, Fix16& g)
|
||||
{
|
||||
return f.m <= g.m;
|
||||
}
|
||||
|
||||
inline int operator>(Fix16& f, Fix16& g)
|
||||
{
|
||||
return f.m > g.m;
|
||||
}
|
||||
|
||||
inline int operator<(Fix16& f, Fix16& g)
|
||||
{
|
||||
return f.m < g.m;
|
||||
}
|
||||
|
||||
inline istream& operator>>(istream& s, Fix16& f)
|
||||
{
|
||||
double d;
|
||||
s >> d;
|
||||
f = d;
|
||||
return s;
|
||||
}
|
||||
|
||||
inline ostream& operator<<(ostream& s, Fix16& f)
|
||||
{
|
||||
return s << double(f);
|
||||
}
|
||||
|
||||
|
||||
inline Fix16 operator*(Fix16& f, int g)
|
||||
{
|
||||
return Fix16(short(f.m * g));
|
||||
}
|
||||
|
||||
inline Fix16 operator*(int g, Fix16& f)
|
||||
{
|
||||
return f * g;
|
||||
}
|
||||
|
||||
|
||||
inline Fix16& Fix16::operator*=(int g)
|
||||
{
|
||||
return *this = *this * g;
|
||||
}
|
||||
|
||||
inline Fix32::~Fix32() {}
|
||||
|
||||
inline long Fix32::round(double d)
|
||||
{
|
||||
return long( (d >= 0)? d + 0.5 : d - 0.5);
|
||||
}
|
||||
|
||||
|
||||
inline long& mantissa(Fix32& f)
|
||||
{
|
||||
return f.m;
|
||||
}
|
||||
|
||||
inline double value(Fix32& f)
|
||||
{
|
||||
return double(f);
|
||||
}
|
||||
|
||||
inline Fix32 Fix32::operator+()
|
||||
{
|
||||
return m;
|
||||
}
|
||||
|
||||
inline Fix32 Fix32::operator-()
|
||||
{
|
||||
return -m;
|
||||
}
|
||||
|
||||
inline Fix32 operator+(Fix32& f, Fix32& g)
|
||||
{
|
||||
long sum = f.m + g.m;
|
||||
if ( (f.m ^ sum) & (g.m ^ sum) & Fix32_msb )
|
||||
f.overflow(sum);
|
||||
return sum;
|
||||
}
|
||||
|
||||
inline Fix32 operator-(Fix32& f, Fix32& g)
|
||||
{
|
||||
long sum = f.m - g.m;
|
||||
if ( (f.m ^ sum) & (-g.m ^ sum) & Fix32_msb )
|
||||
f.overflow(sum);
|
||||
return sum;
|
||||
}
|
||||
|
||||
inline Fix32 operator<<(Fix32& a, int b)
|
||||
{
|
||||
return a.m << b;
|
||||
}
|
||||
|
||||
inline Fix32 operator>>(Fix32& a, int b)
|
||||
{
|
||||
return a.m >> b;
|
||||
}
|
||||
|
||||
inline Fix32& Fix32::operator+=(Fix32& f)
|
||||
{
|
||||
return *this = *this + f;
|
||||
}
|
||||
|
||||
inline Fix32& Fix32::operator-=(Fix32& f)
|
||||
{
|
||||
return *this = *this - f;
|
||||
}
|
||||
|
||||
inline Fix32& Fix32::operator*=(Fix32& f)
|
||||
{
|
||||
return *this = *this * f;
|
||||
}
|
||||
|
||||
inline Fix32& Fix32::operator/=(Fix32& f)
|
||||
{
|
||||
return *this = *this / f;
|
||||
}
|
||||
|
||||
|
||||
inline Fix32& Fix32::operator<<=(int b)
|
||||
{
|
||||
return *this = *this << b;
|
||||
}
|
||||
|
||||
inline Fix32& Fix32::operator>>=(int b)
|
||||
{
|
||||
return *this = *this >> b;
|
||||
}
|
||||
|
||||
inline int operator==(Fix32& f, Fix32& g)
|
||||
{
|
||||
return f.m == g.m;
|
||||
}
|
||||
|
||||
inline int operator!=(Fix32& f, Fix32& g)
|
||||
{
|
||||
return f.m != g.m;
|
||||
}
|
||||
|
||||
inline int operator>=(Fix32& f, Fix32& g)
|
||||
{
|
||||
return f.m >= g.m;
|
||||
}
|
||||
|
||||
inline int operator<=(Fix32& f, Fix32& g)
|
||||
{
|
||||
return f.m <= g.m;
|
||||
}
|
||||
|
||||
inline int operator>(Fix32& f, Fix32& g)
|
||||
{
|
||||
return f.m > g.m;
|
||||
}
|
||||
|
||||
inline int operator<(Fix32& f, Fix32& g)
|
||||
{
|
||||
return f.m < g.m;
|
||||
}
|
||||
|
||||
inline istream& operator>>(istream& s, Fix32& f)
|
||||
{
|
||||
double d;
|
||||
s >> d;
|
||||
f = d;
|
||||
return s;
|
||||
}
|
||||
|
||||
inline ostream& operator<<(ostream& s, Fix32& f)
|
||||
{
|
||||
return s << double(f);
|
||||
}
|
||||
|
||||
inline Fix32 operator*(Fix32& f, int g)
|
||||
{
|
||||
return Fix32(long(f.m * g));
|
||||
}
|
||||
|
||||
inline Fix32 operator*(int g, Fix32& f)
|
||||
{
|
||||
return f * g;
|
||||
}
|
||||
|
||||
|
||||
|
||||
inline Fix32& Fix32::operator*=(int g)
|
||||
{
|
||||
return *this = *this * g;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
@ -1,595 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Kurt Baudendistel (gt-eedsp!baud@gatech.edu)
|
||||
adapted for libg++ by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifndef _Fix24_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Fix24_h 1
|
||||
|
||||
#include <stream.h>
|
||||
#include <std.h>
|
||||
|
||||
// extra type definitions
|
||||
|
||||
typedef struct {
|
||||
long u;
|
||||
unsigned long l;
|
||||
} twolongs;
|
||||
|
||||
// constant definitions
|
||||
|
||||
static const int
|
||||
Fix24_shift = 31;
|
||||
|
||||
static const double
|
||||
Fix24_fs = 2147483648., // 2^Fix24_shift
|
||||
Fix24_mult = Fix24_fs,
|
||||
Fix24_div = 1./Fix24_fs,
|
||||
Fix24_max = 1. - .5/Fix24_fs,
|
||||
Fix24_min = -1.;
|
||||
|
||||
static const unsigned long
|
||||
Fix24_msb = 0x80000000L,
|
||||
Fix24_lsb = 0x00000100L,
|
||||
Fix24_m_max = 0x7fffff00L,
|
||||
Fix24_m_min = 0x80000000L;
|
||||
|
||||
static const double
|
||||
Fix48_fs = 36028797018963968., // 2^(24+Fix24_shift)
|
||||
Fix48_max = 1. - .5/Fix48_fs,
|
||||
Fix48_min = -1.,
|
||||
Fix48_div_u = 1./Fix24_fs,
|
||||
Fix48_div_l = 1./Fix48_fs;
|
||||
|
||||
static const twolongs
|
||||
Fix48_msb = { 0x80000000L, 0L },
|
||||
Fix48_lsb = { 0L, 0x00000100L },
|
||||
Fix48_m_max = { 0x7fffff00L, 0xffffff00L },
|
||||
Fix48_m_min = { 0x80000000L, 0L };
|
||||
|
||||
//
|
||||
// Fix24 class: 24-bit Fixed point data type
|
||||
//
|
||||
// consists of a 24-bit mantissa (sign bit & 23 data bits).
|
||||
//
|
||||
|
||||
class Fix24
|
||||
{
|
||||
friend class Fix48;
|
||||
|
||||
long m;
|
||||
|
||||
long assign(double d);
|
||||
operator double();
|
||||
Fix24(long i);
|
||||
Fix24(int i);
|
||||
|
||||
|
||||
public:
|
||||
Fix24();
|
||||
Fix24(Fix24& f);
|
||||
Fix24(double d);
|
||||
Fix24(Fix48& f);
|
||||
|
||||
~Fix24();
|
||||
|
||||
Fix24& operator=(Fix24& f);
|
||||
Fix24& operator=(double d);
|
||||
Fix24& operator=(Fix48& f);
|
||||
|
||||
friend long& mantissa(Fix24& f);
|
||||
friend double value(Fix24& f);
|
||||
|
||||
Fix24 operator + ();
|
||||
Fix24 operator - ();
|
||||
|
||||
friend Fix24 operator + (Fix24& f, Fix24& g);
|
||||
friend Fix24 operator - (Fix24& f, Fix24& g);
|
||||
friend Fix48 operator * (Fix24& f, Fix24& g);
|
||||
friend Fix24 operator * (Fix24& f, int g);
|
||||
friend Fix24 operator * (int g, Fix24& f);
|
||||
friend Fix24 operator / (Fix24& f, Fix24& g);
|
||||
friend Fix24 operator << (Fix24& f, int b);
|
||||
friend Fix24 operator >> (Fix24& f, int b);
|
||||
|
||||
Fix24& operator += (Fix24& f);
|
||||
Fix24& operator -= (Fix24& f);
|
||||
Fix24& operator *= (Fix24& f);
|
||||
Fix24& operator *= (int b);
|
||||
Fix24& operator /= (Fix24& f);
|
||||
|
||||
Fix24& operator <<=(int b);
|
||||
Fix24& operator >>=(int b);
|
||||
|
||||
friend int operator == (Fix24& f, Fix24& g);
|
||||
friend int operator != (Fix24& f, Fix24& g);
|
||||
friend int operator >= (Fix24& f, Fix24& g);
|
||||
friend int operator <= (Fix24& f, Fix24& g);
|
||||
friend int operator > (Fix24& f, Fix24& g);
|
||||
friend int operator < (Fix24& f, Fix24& g);
|
||||
|
||||
friend istream& operator >> (istream& s, Fix24& f);
|
||||
friend ostream& operator << (ostream& s, Fix24& f);
|
||||
|
||||
void overflow(long&);
|
||||
void range_error(long&);
|
||||
};
|
||||
|
||||
|
||||
//
|
||||
// Fix48 class: 48-bit Fixed point data type
|
||||
//
|
||||
// consists of a 48-bit mantissa (sign bit & 47 data bits).
|
||||
//
|
||||
|
||||
class Fix48
|
||||
{
|
||||
friend class Fix24;
|
||||
|
||||
twolongs m;
|
||||
|
||||
twolongs assign(double d);
|
||||
operator double();
|
||||
Fix48(twolongs i);
|
||||
|
||||
public:
|
||||
Fix48();
|
||||
Fix48(Fix48& f);
|
||||
Fix48(Fix24& f);
|
||||
Fix48(double d);
|
||||
~Fix48();
|
||||
|
||||
Fix48& operator = (Fix48& f);
|
||||
Fix48& operator = (Fix24& f);
|
||||
Fix48& operator = (double d);
|
||||
|
||||
friend twolongs& mantissa(Fix48& f);
|
||||
friend double value(Fix48& f);
|
||||
|
||||
Fix48 operator + ();
|
||||
Fix48 operator - ();
|
||||
|
||||
friend Fix48 operator + (Fix48& f, Fix48& g);
|
||||
friend Fix48 operator - (Fix48& f, Fix48& g);
|
||||
friend Fix48 operator * (Fix48& f, int g);
|
||||
friend Fix48 operator * (int g, Fix48& f);
|
||||
friend Fix48 operator << (Fix48& f, int b);
|
||||
friend Fix48 operator >> (Fix48& f, int b);
|
||||
|
||||
friend Fix48 operator * (Fix24& f, Fix24& g);
|
||||
|
||||
Fix48& operator += (Fix48& f);
|
||||
Fix48& operator -= (Fix48& f);
|
||||
Fix48& operator *= (int b);
|
||||
Fix48& operator <<=(int b);
|
||||
Fix48& operator >>=(int b);
|
||||
|
||||
friend int operator == (Fix48& f, Fix48& g);
|
||||
friend int operator != (Fix48& f, Fix48& g);
|
||||
friend int operator >= (Fix48& f, Fix48& g);
|
||||
friend int operator <= (Fix48& f, Fix48& g);
|
||||
friend int operator > (Fix48& f, Fix48& g);
|
||||
friend int operator < (Fix48& f, Fix48& g);
|
||||
|
||||
friend istream& operator >> (istream& s, Fix48& f);
|
||||
friend ostream& operator << (ostream& s, Fix48& f);
|
||||
|
||||
void overflow(twolongs& i);
|
||||
void range_error(twolongs& i);
|
||||
};
|
||||
|
||||
|
||||
// active error handler declarations
|
||||
|
||||
typedef void (*Fix24_peh)(long&);
|
||||
typedef void (*Fix48_peh)(twolongs&);
|
||||
|
||||
extern Fix24_peh Fix24_overflow_handler;
|
||||
extern Fix48_peh Fix48_overflow_handler;
|
||||
|
||||
extern Fix24_peh Fix24_range_error_handler;
|
||||
extern Fix48_peh Fix48_range_error_handler;
|
||||
|
||||
|
||||
// error handler declarations
|
||||
|
||||
#if defined(SHORT_NAMES) || defined(VMS)
|
||||
#define set_overflow_handler sohndl
|
||||
#define set_range_error_handler srnghdl
|
||||
#endif
|
||||
|
||||
extern Fix24_peh set_Fix24_overflow_handler(Fix24_peh);
|
||||
extern Fix48_peh set_Fix48_overflow_handler(Fix48_peh);
|
||||
extern void set_overflow_handler(Fix24_peh, Fix48_peh);
|
||||
|
||||
extern Fix24_peh set_Fix24_range_error_handler(Fix24_peh);
|
||||
extern Fix48_peh set_Fix48_range_error_handler(Fix48_peh);
|
||||
extern void set_range_error_handler(Fix24_peh, Fix48_peh);
|
||||
|
||||
extern void
|
||||
Fix24_ignore(long&),
|
||||
Fix24_overflow_saturate(long&),
|
||||
Fix24_overflow_warning_saturate(long&),
|
||||
Fix24_warning(long&),
|
||||
Fix24_abort(long&);
|
||||
|
||||
extern void
|
||||
Fix48_ignore(twolongs&),
|
||||
Fix48_overflow_saturate(twolongs&),
|
||||
Fix48_overflow_warning_saturate(twolongs&),
|
||||
Fix48_warning(twolongs&),
|
||||
Fix48_abort(twolongs&);
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline Fix24::~Fix24() {}
|
||||
|
||||
inline Fix24::Fix24(long i)
|
||||
{
|
||||
m = i;
|
||||
}
|
||||
|
||||
inline Fix24::Fix24(int i)
|
||||
{
|
||||
m = i;
|
||||
}
|
||||
|
||||
inline Fix24::operator double()
|
||||
{
|
||||
return Fix24_div * m;
|
||||
}
|
||||
|
||||
inline Fix24::Fix24()
|
||||
{
|
||||
m = 0;
|
||||
}
|
||||
|
||||
inline Fix24::Fix24(Fix24& f)
|
||||
{
|
||||
m = f.m;
|
||||
}
|
||||
|
||||
inline Fix24::Fix24(double d)
|
||||
{
|
||||
m = assign(d);
|
||||
}
|
||||
|
||||
inline Fix24::Fix24(Fix48& f)
|
||||
{
|
||||
m = f.m.u;
|
||||
}
|
||||
|
||||
inline Fix24& Fix24::operator=(Fix24& f)
|
||||
{
|
||||
m = f.m;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Fix24& Fix24::operator=(double d)
|
||||
{
|
||||
m = assign(d);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Fix24& Fix24::operator=(Fix48& f)
|
||||
{
|
||||
m = f.m.u;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline long& mantissa(Fix24& f)
|
||||
{
|
||||
return f.m;
|
||||
}
|
||||
|
||||
inline double value(Fix24& f)
|
||||
{
|
||||
return double(f);
|
||||
}
|
||||
|
||||
inline Fix24 Fix24::operator+()
|
||||
{
|
||||
return m;
|
||||
}
|
||||
|
||||
inline Fix24 Fix24::operator-()
|
||||
{
|
||||
return -m;
|
||||
}
|
||||
|
||||
inline Fix24 operator+(Fix24& f, Fix24& g)
|
||||
{
|
||||
long sum = f.m + g.m;
|
||||
if ( (f.m ^ sum) & (g.m ^ sum) & Fix24_msb )
|
||||
f.overflow(sum);
|
||||
return sum;
|
||||
}
|
||||
|
||||
inline Fix24 operator-(Fix24& f, Fix24& g)
|
||||
{
|
||||
long sum = f.m - g.m;
|
||||
if ( (f.m ^ sum) & (-g.m ^ sum) & Fix24_msb )
|
||||
f.overflow(sum);
|
||||
return sum;
|
||||
}
|
||||
|
||||
inline Fix24 operator*(Fix24& a, int b)
|
||||
{
|
||||
return a.m * b;
|
||||
}
|
||||
|
||||
inline Fix24 operator*(int b, Fix24& a)
|
||||
{
|
||||
return a * b;
|
||||
}
|
||||
|
||||
inline Fix24 operator<<(Fix24& a, int b)
|
||||
{
|
||||
return a.m << b;
|
||||
}
|
||||
|
||||
inline Fix24 operator>>(Fix24& a, int b)
|
||||
{
|
||||
return (a.m >> b) & 0xffffff00L;
|
||||
}
|
||||
|
||||
inline Fix24& Fix24:: operator+=(Fix24& f)
|
||||
{
|
||||
return *this = *this + f;
|
||||
}
|
||||
|
||||
inline Fix24& Fix24:: operator-=(Fix24& f)
|
||||
{
|
||||
return *this = *this - f;
|
||||
}
|
||||
|
||||
inline Fix24& Fix24::operator*=(Fix24& f)
|
||||
{
|
||||
return *this = *this * f;
|
||||
}
|
||||
|
||||
inline Fix24& Fix24:: operator/=(Fix24& f)
|
||||
{
|
||||
return *this = *this / f;
|
||||
}
|
||||
|
||||
inline Fix24& Fix24:: operator<<=(int b)
|
||||
{
|
||||
return *this = *this << b;
|
||||
}
|
||||
|
||||
inline Fix24& Fix24:: operator>>=(int b)
|
||||
{
|
||||
return *this = *this >> b;
|
||||
}
|
||||
|
||||
inline Fix24& Fix24::operator*=(int b)
|
||||
{
|
||||
return *this = *this * b;
|
||||
}
|
||||
|
||||
inline int operator==(Fix24& f, Fix24& g)
|
||||
{
|
||||
return f.m == g.m;
|
||||
}
|
||||
|
||||
inline int operator!=(Fix24& f, Fix24& g)
|
||||
{
|
||||
return f.m != g.m;
|
||||
}
|
||||
|
||||
inline int operator>=(Fix24& f, Fix24& g)
|
||||
{
|
||||
return f.m >= g.m;
|
||||
}
|
||||
|
||||
inline int operator<=(Fix24& f, Fix24& g)
|
||||
{
|
||||
return f.m <= g.m;
|
||||
}
|
||||
|
||||
inline int operator>(Fix24& f, Fix24& g)
|
||||
{
|
||||
return f.m > g.m;
|
||||
}
|
||||
|
||||
inline int operator<(Fix24& f, Fix24& g)
|
||||
{
|
||||
return f.m < g.m;
|
||||
}
|
||||
|
||||
inline istream& operator>>(istream& s, Fix24& f)
|
||||
{
|
||||
double d;
|
||||
s >> d;
|
||||
f = d;
|
||||
return s;
|
||||
}
|
||||
|
||||
inline ostream& operator<<(ostream& s, Fix24& f)
|
||||
{
|
||||
return s << double(f);
|
||||
}
|
||||
|
||||
inline Fix48::~Fix48() {}
|
||||
|
||||
inline Fix48::Fix48(twolongs i)
|
||||
{
|
||||
m = i;
|
||||
}
|
||||
|
||||
inline Fix48:: operator double()
|
||||
{
|
||||
/*
|
||||
* Note: can't simply do Fix48_div_u * m.u + Fix48_div_l * m.l, because
|
||||
* m.u is signed and m.l is unsigned.
|
||||
*/
|
||||
return (m.u >= 0)? Fix48_div_u * m.u + Fix48_div_l * m.l :
|
||||
(Fix48_div_u * ((unsigned long)(m.u & 0xffffff00))
|
||||
+ Fix48_div_l * m.l) - 2;
|
||||
}
|
||||
|
||||
inline Fix48::Fix48()
|
||||
{
|
||||
m.u = 0;
|
||||
m.l = 0;
|
||||
}
|
||||
|
||||
inline Fix48::Fix48(Fix48& f)
|
||||
{
|
||||
m = f.m;
|
||||
}
|
||||
|
||||
inline Fix48::Fix48(Fix24& f)
|
||||
{
|
||||
m.u = f.m;
|
||||
m.l = 0;
|
||||
}
|
||||
|
||||
inline Fix48::Fix48(double d)
|
||||
{
|
||||
m = assign(d);
|
||||
}
|
||||
|
||||
inline Fix48& Fix48::operator=(Fix48& f)
|
||||
{
|
||||
m = f.m;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Fix48& Fix48::operator=(Fix24& f)
|
||||
{
|
||||
m.u = f.m;
|
||||
m.l = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Fix48& Fix48::operator=(double d)
|
||||
{
|
||||
m = assign(d);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline twolongs& mantissa(Fix48& f)
|
||||
{
|
||||
return f.m;
|
||||
}
|
||||
|
||||
inline double value(Fix48& f)
|
||||
{
|
||||
return double(f);
|
||||
}
|
||||
|
||||
inline Fix48 Fix48::operator+()
|
||||
{
|
||||
return m;
|
||||
}
|
||||
|
||||
inline Fix48 Fix48::operator-()
|
||||
{
|
||||
twolongs n;
|
||||
n.l = -m.l;
|
||||
n.u = ~m.u + ((n.l ^ m.l) & Fix24_msb ? 0 : Fix24_lsb);
|
||||
return Fix48(n);
|
||||
}
|
||||
|
||||
inline Fix48 operator*(int b, Fix48& a)
|
||||
{
|
||||
return a * b;
|
||||
}
|
||||
|
||||
inline Fix48& Fix48::operator+=(Fix48& f)
|
||||
{
|
||||
return *this = *this + f;
|
||||
}
|
||||
|
||||
inline Fix48& Fix48::operator-=(Fix48& f)
|
||||
{
|
||||
return *this = *this - f;
|
||||
}
|
||||
|
||||
inline Fix48& Fix48::operator*=(int b)
|
||||
{
|
||||
return *this = *this * b;
|
||||
}
|
||||
|
||||
inline Fix48& Fix48::operator<<=(int b)
|
||||
{
|
||||
return *this = *this << b;
|
||||
}
|
||||
|
||||
inline Fix48& Fix48::operator>>=(int b)
|
||||
{
|
||||
return *this = *this >> b;
|
||||
}
|
||||
|
||||
inline int operator==(Fix48& f, Fix48& g)
|
||||
{
|
||||
return f.m.u == g.m.u && f.m.l == g.m.l;
|
||||
}
|
||||
|
||||
inline int operator!=(Fix48& f, Fix48& g)
|
||||
{
|
||||
return f.m.u != g.m.u || f.m.l != g.m.l;
|
||||
}
|
||||
|
||||
inline int operator>=(Fix48& f, Fix48& g)
|
||||
{
|
||||
return f.m.u >= g.m.u || (f.m.u == g.m.u && f.m.l >= g.m.l);
|
||||
}
|
||||
|
||||
inline int operator<=(Fix48& f, Fix48& g)
|
||||
{
|
||||
return f.m.u <= g.m.u || (f.m.u == g.m.u && f.m.l <= g.m.l);
|
||||
}
|
||||
|
||||
inline int operator>(Fix48& f, Fix48& g)
|
||||
{
|
||||
return f.m.u > g.m.u || (f.m.u == g.m.u && f.m.l > g.m.l);
|
||||
}
|
||||
|
||||
inline int operator<(Fix48& f, Fix48& g)
|
||||
{
|
||||
return f.m.u < g.m.u || (f.m.u == g.m.u && f.m.l < g.m.l);
|
||||
}
|
||||
|
||||
inline istream& operator>>(istream& s, Fix48& f)
|
||||
{
|
||||
double d;
|
||||
s >> d;
|
||||
f = d;
|
||||
return s;
|
||||
}
|
||||
|
||||
inline ostream& operator<<(ostream& s, Fix48& f)
|
||||
{
|
||||
return s << double(f);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
|
||||
#ifndef _Fmodes_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Fmodes_h 1
|
||||
|
||||
enum io_mode // known unix file IO modes
|
||||
{
|
||||
io_readonly = 0,
|
||||
io_writeonly = 1,
|
||||
io_readwrite = 2,
|
||||
io_appendonly = 3,
|
||||
io_append = 4, // append, plus allow reads
|
||||
};
|
||||
|
||||
enum access_mode // ways to open a file
|
||||
{
|
||||
a_createonly = 0, // create, fail if file exists
|
||||
a_create = 1, // create if doesn't exist, else truncate
|
||||
a_useonly = 2, // use (no truncate) fail if doesn't exist
|
||||
a_use = 3, // use (no truncate), create if doesn't exist
|
||||
};
|
||||
|
||||
enum state_value // File states
|
||||
{
|
||||
_good = 0, // all is well
|
||||
_eof = 1, // at eof
|
||||
_fail = 2, // logical or physical IO error
|
||||
_bad = 4 // unopened/corrupted
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,59 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef _Geometric_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Geometric_h
|
||||
|
||||
#include <Random.h>
|
||||
|
||||
class Geometric: public Random {
|
||||
protected:
|
||||
double pMean;
|
||||
public:
|
||||
Geometric(double mean, RNG *gen);
|
||||
|
||||
double mean();
|
||||
double mean(double x);
|
||||
|
||||
virtual double operator()();
|
||||
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline Geometric::Geometric(double mean, RNG *gen) : (gen)
|
||||
{
|
||||
pMean = mean;
|
||||
}
|
||||
|
||||
|
||||
inline double Geometric::mean() { return pMean; }
|
||||
inline double Geometric::mean(double x) {
|
||||
double tmp = pMean; pMean = x; return tmp;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,131 +0,0 @@
|
|||
/* Getopt for GNU.
|
||||
Copyright (C) 1987, 1989 Free Software Foundation, Inc.
|
||||
(Modified by Douglas C. Schmidt for use with GNU G++.)
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 1, or (at your option)
|
||||
any later version.
|
||||
|
||||
This program 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 General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
||||
|
||||
|
||||
|
||||
/* This version of `getopt' appears to the caller like standard Unix `getopt'
|
||||
but it behaves differently for the user, since it allows the user
|
||||
to intersperse the options with the other arguments.
|
||||
|
||||
As `getopt' works, it permutes the elements of `argv' so that,
|
||||
when it is done, all the options precede everything else. Thus
|
||||
all application programs are extended to handle flexible argument order.
|
||||
|
||||
Setting the environment variable _POSIX_OPTION_ORDER disables permutation.
|
||||
Then the behavior is completely standard.
|
||||
|
||||
GNU application programs can use a third alternative mode in which
|
||||
they can distinguish the relative order of options and other arguments. */
|
||||
|
||||
#ifndef GetOpt_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define GetOpt_h 1
|
||||
|
||||
#include <std.h>
|
||||
#include <stdio.h>
|
||||
|
||||
class GetOpt
|
||||
{
|
||||
private:
|
||||
/* The next char to be scanned in the option-element
|
||||
in which the last option character we returned was found.
|
||||
This allows us to pick up the scan where we left off.
|
||||
|
||||
If this is zero, or a null string, it means resume the scan
|
||||
by advancing to the next ARGV-element. */
|
||||
|
||||
static char *nextchar;
|
||||
|
||||
|
||||
/* Describe how to deal with options that follow non-option ARGV-elements.
|
||||
|
||||
UNSPECIFIED means the caller did not specify anything;
|
||||
the default is then REQUIRE_ORDER if the environment variable
|
||||
_OPTIONS_FIRST is defined, PERMUTE otherwise.
|
||||
|
||||
REQUIRE_ORDER means don't recognize them as options.
|
||||
Stop option processing when the first non-option is seen.
|
||||
This is what Unix does.
|
||||
|
||||
PERMUTE is the default. We permute the contents of `argv' as we scan,
|
||||
so that eventually all the options are at the end. This allows options
|
||||
to be given in any order, even with programs that were not written to
|
||||
expect this.
|
||||
|
||||
RETURN_IN_ORDER is an option available to programs that were written
|
||||
to expect options and other ARGV-elements in any order and that care about
|
||||
the ordering of the two. We describe each non-option ARGV-element
|
||||
as if it were the argument of an option with character code zero.
|
||||
Using `-' as the first character of the list of option characters
|
||||
requests this mode of operation.
|
||||
|
||||
The special argument `--' forces an end of option-scanning regardless
|
||||
of the value of `ordering'. In the case of RETURN_IN_ORDER, only
|
||||
`--' can cause `getopt' to return EOF with `optind' != ARGC. */
|
||||
|
||||
static enum { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER } ordering;
|
||||
|
||||
/* Handle permutation of arguments. */
|
||||
|
||||
/* Describe the part of ARGV that contains non-options that have
|
||||
been skipped. `first_nonopt' is the index in ARGV of the first of them;
|
||||
`last_nonopt' is the index after the last of them. */
|
||||
|
||||
static int first_nonopt;
|
||||
static int last_nonopt;
|
||||
|
||||
void exchange (char **argv);
|
||||
public:
|
||||
/* For communication from `getopt' to the caller.
|
||||
When `getopt' finds an option that takes an argument,
|
||||
the argument value is returned here.
|
||||
Also, when `ordering' is RETURN_IN_ORDER,
|
||||
each non-option ARGV-element is returned here. */
|
||||
|
||||
char *optarg;
|
||||
|
||||
/* Index in ARGV of the next element to be scanned.
|
||||
This is used for communication to and from the caller
|
||||
and for communication between successive calls to `getopt'.
|
||||
On entry to `getopt', zero means this is the first call; initialize.
|
||||
|
||||
When `getopt' returns EOF, this is the index of the first of the
|
||||
non-option elements that the caller should itself scan.
|
||||
|
||||
Otherwise, `optind' communicates from one call to the next
|
||||
how much of ARGV has been scanned so far. */
|
||||
|
||||
int optind;
|
||||
|
||||
/* Callers store zero here to inhibit the error message
|
||||
for unrecognized options. */
|
||||
|
||||
int opterr;
|
||||
|
||||
int nargc;
|
||||
char **nargv;
|
||||
char *noptstring;
|
||||
|
||||
GetOpt (int argc, char **argv, char *optstring);
|
||||
int operator () (void);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,78 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef _HyperGeometric_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _HyperGeometric_h
|
||||
|
||||
#include <Random.h>
|
||||
|
||||
class HyperGeometric: public Random {
|
||||
protected:
|
||||
double pMean;
|
||||
double pVariance;
|
||||
double pP;
|
||||
void setState();
|
||||
|
||||
public:
|
||||
HyperGeometric(double mean, double variance, RNG *gen);
|
||||
|
||||
double mean();
|
||||
double mean(double x);
|
||||
double variance();
|
||||
double variance(double x);
|
||||
|
||||
virtual double operator()();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline void HyperGeometric::setState() {
|
||||
double z = pVariance / (pMean * pMean);
|
||||
pP = 0.5 * (1.0 - sqrt((z - 1.0) / ( z + 1.0 )));
|
||||
}
|
||||
|
||||
inline HyperGeometric::HyperGeometric(double mean, double variance, RNG *gen)
|
||||
: (gen) {
|
||||
pMean = mean; pVariance = variance;
|
||||
setState();
|
||||
}
|
||||
|
||||
inline double HyperGeometric::mean() { return pMean; };
|
||||
|
||||
inline double HyperGeometric::mean(double x) {
|
||||
double t = pMean; pMean = x;
|
||||
setState(); return t;
|
||||
}
|
||||
|
||||
inline double HyperGeometric::variance() { return pVariance; }
|
||||
|
||||
inline double HyperGeometric::variance(double x) {
|
||||
double t = pVariance; pVariance = x;
|
||||
setState(); return t;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,13 +0,0 @@
|
|||
#ifndef Incremental_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define Incremental_h
|
||||
#define DECLARE_INIT_FUNCTION(USER_INIT_FUNCTION) \
|
||||
static void USER_INIT_FUNCTION (); extern void (*_initfn)(); \
|
||||
static struct xyzzy { xyzzy () {_initfn = USER_INIT_FUNCTION;}; \
|
||||
~xyzzy () {};} __2xyzzy;
|
||||
#else
|
||||
#error Incremental.h was not the first file included in this module
|
||||
#endif
|
File diff suppressed because it is too large
Load Diff
|
@ -1,84 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef _LogNormal_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _LogNormal_h
|
||||
|
||||
#include "Normal.h"
|
||||
|
||||
class LogNormal: public Normal {
|
||||
protected:
|
||||
double logMean;
|
||||
double logVariance;
|
||||
void setState();
|
||||
public:
|
||||
LogNormal(double mean, double variance, RNG *gen);
|
||||
double mean();
|
||||
double mean(double x);
|
||||
double variance();
|
||||
double variance(double x);
|
||||
virtual double operator()();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline void LogNormal::setState()
|
||||
{
|
||||
double m2 = logMean * logMean;
|
||||
pMean = log(m2 / sqrt(logVariance + m2) );
|
||||
pVariance = log((sqrt(logVariance + m2)/m2 ));
|
||||
}
|
||||
|
||||
inline LogNormal::LogNormal(double mean, double variance, RNG *gen)
|
||||
: (mean, variance, gen)
|
||||
{
|
||||
logMean = mean;
|
||||
logVariance = variance;
|
||||
setState();
|
||||
}
|
||||
|
||||
inline double LogNormal::mean() {
|
||||
return logMean;
|
||||
}
|
||||
|
||||
inline double LogNormal::mean(double x)
|
||||
{
|
||||
double t=logMean; logMean = x; setState();
|
||||
return t;
|
||||
}
|
||||
|
||||
inline double LogNormal::variance() {
|
||||
return logVariance;
|
||||
}
|
||||
|
||||
inline double LogNormal::variance(double x)
|
||||
{
|
||||
double t=logVariance; logVariance = x; setState();
|
||||
return t;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,97 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef _MLCG_h
|
||||
#define _MLCG_h 1
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
|
||||
#include <RNG.h>
|
||||
#include <math.h>
|
||||
|
||||
//
|
||||
// Multiplicative Linear Conguential Generator
|
||||
//
|
||||
|
||||
class MLCG : public RNG {
|
||||
long initialSeedOne;
|
||||
long initialSeedTwo;
|
||||
long seedOne;
|
||||
long seedTwo;
|
||||
|
||||
protected:
|
||||
|
||||
public:
|
||||
MLCG(long seed1 = 0, long seed2 = 1);
|
||||
//
|
||||
// Return a long-words word of random bits
|
||||
//
|
||||
virtual unsigned long asLong();
|
||||
virtual void reset();
|
||||
long seed1();
|
||||
void seed1(long);
|
||||
long seed2();
|
||||
void seed2(long);
|
||||
void reseed(long, long);
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline long
|
||||
MLCG::seed1()
|
||||
{
|
||||
return(seedOne);
|
||||
}
|
||||
|
||||
inline void
|
||||
MLCG::seed1(long s)
|
||||
{
|
||||
initialSeedOne = s;
|
||||
reset();
|
||||
}
|
||||
|
||||
inline long
|
||||
MLCG::seed2()
|
||||
{
|
||||
return(seedTwo);
|
||||
}
|
||||
|
||||
inline void
|
||||
MLCG::seed2(long s)
|
||||
{
|
||||
initialSeedTwo = s;
|
||||
reset();
|
||||
}
|
||||
|
||||
inline void
|
||||
MLCG::reseed(long s1, long s2)
|
||||
{
|
||||
initialSeedOne = s1;
|
||||
initialSeedTwo = s2;
|
||||
reset();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,58 +0,0 @@
|
|||
# %W% (Berkeley) %G%
|
||||
#
|
||||
# Cloned from /usr/src/include/Makefile
|
||||
# Doing a make install builds /usr/include/g++
|
||||
#
|
||||
|
||||
all clean cleandir depend lint tags:
|
||||
|
||||
FILES= ACG.h AllocRing.h Binomial.h BitSet.h BitString.h \
|
||||
Complex.h CursesW.h DiscUnif.h Erlang.h File.h \
|
||||
Filebuf.h Fix.h Fix16.h Fix24.h Fmodes.h Geom.h \
|
||||
GetOpt.h HypGeom.h Incremental.h Integer.h LogNorm.h \
|
||||
MLCG.h NegExp.h Normal.h Obstack.h Pix.h PlotFile.h \
|
||||
Poisson.h RNG.h Random.h Rational.h Regex.h RndInt.h \
|
||||
SFile.h SmplHist.h SmplStat.h String.h Uniform.h \
|
||||
Weibull.h abs.h assert.h bool.h builtin.h compare.h \
|
||||
complex.h curses.h file.h filebuf.h generic.h \
|
||||
istream.h malloc.h max.h min.h minmax.h new.h open.h \
|
||||
osfcn.h ostream.h regex.h std.h strclass.h stream.h \
|
||||
streambuf.h swap.h values.h
|
||||
|
||||
DIRS= gen
|
||||
|
||||
NOOBJ= noobj
|
||||
|
||||
INCDIR= /usr/include/g++
|
||||
|
||||
install:
|
||||
@if [ ! -d ${DESTDIR}${INCDIR} ]; then \
|
||||
/bin/rm -f ${DESTDIR}${INCDIR} ; \
|
||||
mkdir -p ${DESTDIR}${INCDIR} ; \
|
||||
chown root.wheel ${DESTDIR}${INCDIR} ; \
|
||||
chmod 755 ${DESTDIR}${INCDIR} ; \
|
||||
else \
|
||||
true ; \
|
||||
fi
|
||||
@echo installing ${FILES}
|
||||
@-for i in ${FILES}; do \
|
||||
cmp -s $$i ${DESTDIR}${INCDIR}/$$i || \
|
||||
install -c -o ${BINOWN} -g ${BINGRP} -m 444 $$i \
|
||||
${DESTDIR}${INCDIR}/$$i; \
|
||||
done
|
||||
@echo installing ${DIRS}
|
||||
@-for i in ${DIRS}; do \
|
||||
if [ ! -d ${DESTDIR}${INCDIR}/$$i ]; \
|
||||
then \
|
||||
mkdir ${DESTDIR}${INCDIR}/$$i; \
|
||||
fi; \
|
||||
chown ${BINOWN}.${BINGRP} ${DESTDIR}${INCDIR}/$$i; \
|
||||
chmod 755 ${DESTDIR}${INCDIR}/$$i; \
|
||||
(cd $$i; for j in *.*P; do \
|
||||
cmp -s $$j ${DESTDIR}${INCDIR}/$$i/$$j || \
|
||||
install -c -o ${BINOWN} -g ${BINGRP} -m 444 $$j \
|
||||
${DESTDIR}${INCDIR}/$$i/$$j; \
|
||||
done); \
|
||||
done
|
||||
|
||||
.include <bsd.prog.mk>
|
|
@ -1,63 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef _NegativeExpntl_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _NegativeExpntl_h 1
|
||||
|
||||
|
||||
//
|
||||
// Negative Exponential Random Numbers
|
||||
//
|
||||
//
|
||||
|
||||
#include <Random.h>
|
||||
|
||||
class NegativeExpntl: public Random {
|
||||
protected:
|
||||
double pMean;
|
||||
public:
|
||||
NegativeExpntl(double xmean, RNG *gen);
|
||||
double mean();
|
||||
double mean(double x);
|
||||
|
||||
virtual double operator()();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline NegativeExpntl::NegativeExpntl(double xmean, RNG *gen)
|
||||
: (gen) {
|
||||
pMean = xmean;
|
||||
}
|
||||
|
||||
inline double NegativeExpntl::mean() { return pMean; }
|
||||
inline double NegativeExpntl::mean(double x) {
|
||||
double t = pMean; pMean = x;
|
||||
return t;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,74 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef _Normal_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Normal_h
|
||||
|
||||
#include <Random.h>
|
||||
|
||||
class Normal: public Random {
|
||||
char haveCachedNormal;
|
||||
double cachedNormal;
|
||||
|
||||
protected:
|
||||
double pMean;
|
||||
double pVariance;
|
||||
double pStdDev;
|
||||
|
||||
public:
|
||||
Normal(double xmean, double xvariance, RNG *gen);
|
||||
double mean();
|
||||
double mean(double x);
|
||||
double variance();
|
||||
double variance(double x);
|
||||
virtual double operator()();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline Normal::Normal(double xmean, double xvariance, RNG *gen)
|
||||
: (gen) {
|
||||
pMean = xmean;
|
||||
pVariance = xvariance;
|
||||
pStdDev = sqrt(pVariance);
|
||||
haveCachedNormal = 0;
|
||||
}
|
||||
|
||||
inline double Normal::mean() { return pMean; };
|
||||
inline double Normal::mean(double x) {
|
||||
double t=pMean; pMean = x;
|
||||
return t;
|
||||
}
|
||||
|
||||
inline double Normal::variance() { return pVariance; }
|
||||
inline double Normal::variance(double x) {
|
||||
double t=pVariance; pVariance = x;
|
||||
pStdDev = sqrt(pVariance);
|
||||
return t;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,226 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _Obstack_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Obstack_h 1
|
||||
|
||||
#include <std.h>
|
||||
|
||||
class Obstack
|
||||
{
|
||||
struct _obstack_chunk
|
||||
{
|
||||
char* limit;
|
||||
_obstack_chunk* prev;
|
||||
char contents[4];
|
||||
};
|
||||
|
||||
protected:
|
||||
long chunksize;
|
||||
_obstack_chunk* chunk;
|
||||
char* objectbase;
|
||||
char* nextfree;
|
||||
char* chunklimit;
|
||||
int alignmentmask;
|
||||
|
||||
void _free(void* obj);
|
||||
void newchunk(int size);
|
||||
|
||||
public:
|
||||
Obstack(int size = 4080, int alignment = 4); // 4080=4096-mallocslop
|
||||
|
||||
~Obstack();
|
||||
|
||||
void* base();
|
||||
void* next_free();
|
||||
int alignment_mask();
|
||||
int chunk_size();
|
||||
int size();
|
||||
int room();
|
||||
int contains(void* p); // does Obstack hold pointer p?
|
||||
|
||||
void grow(const void* data, int size);
|
||||
void grow(const void* data, int size, char terminator);
|
||||
void grow(const char* s);
|
||||
void grow(char c);
|
||||
void grow_fast(char c);
|
||||
void blank(int size);
|
||||
void blank_fast(int size);
|
||||
|
||||
void* finish();
|
||||
void* finish(char terminator);
|
||||
|
||||
void* copy(const void* data, int size);
|
||||
void* copy(const void* data, int size, char terminator);
|
||||
void* copy(const char* s);
|
||||
void* copy(char c);
|
||||
void* alloc(int size);
|
||||
|
||||
void free(void* obj);
|
||||
void shrink(int size = 1); // suggested by ken@cs.rochester.edu
|
||||
|
||||
int OK(); // rep invariant
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
|
||||
inline Obstack::~Obstack()
|
||||
{
|
||||
_free(0);
|
||||
}
|
||||
|
||||
inline void* Obstack::base()
|
||||
{
|
||||
return objectbase;
|
||||
}
|
||||
|
||||
inline void* Obstack::next_free()
|
||||
{
|
||||
return nextfree;
|
||||
}
|
||||
|
||||
inline int Obstack::alignment_mask()
|
||||
{
|
||||
return alignmentmask;
|
||||
}
|
||||
|
||||
inline int Obstack::chunk_size()
|
||||
{
|
||||
return chunksize;
|
||||
}
|
||||
|
||||
inline int Obstack::size()
|
||||
{
|
||||
return nextfree - objectbase;
|
||||
}
|
||||
|
||||
inline int Obstack::room()
|
||||
{
|
||||
return chunklimit - nextfree;
|
||||
}
|
||||
|
||||
inline void Obstack:: grow(const void* data, int size)
|
||||
{
|
||||
if (nextfree+size > chunklimit)
|
||||
newchunk(size);
|
||||
bcopy(data, nextfree, size);
|
||||
nextfree += size;
|
||||
}
|
||||
|
||||
inline void Obstack:: grow(const void* data, int size, char terminator)
|
||||
{
|
||||
if (nextfree+size+1 > chunklimit)
|
||||
newchunk(size+1);
|
||||
bcopy(data, nextfree, size);
|
||||
nextfree += size;
|
||||
*(nextfree)++ = terminator;
|
||||
}
|
||||
|
||||
inline void Obstack:: grow(const char* s)
|
||||
{
|
||||
grow((void*)s, strlen(s), 0);
|
||||
}
|
||||
|
||||
inline void Obstack:: grow(char c)
|
||||
{
|
||||
if (nextfree+1 > chunklimit)
|
||||
newchunk(1);
|
||||
*(nextfree)++ = c;
|
||||
}
|
||||
|
||||
inline void Obstack:: blank(int size)
|
||||
{
|
||||
if (nextfree+size > chunklimit)
|
||||
newchunk(size);
|
||||
nextfree += size;
|
||||
}
|
||||
|
||||
inline void* Obstack::finish(char terminator)
|
||||
{
|
||||
grow(terminator);
|
||||
return finish();
|
||||
}
|
||||
|
||||
inline void* Obstack::copy(const void* data, int size)
|
||||
{
|
||||
grow (data, size);
|
||||
return finish();
|
||||
}
|
||||
|
||||
inline void* Obstack::copy(const void* data, int size, char terminator)
|
||||
{
|
||||
grow(data, size, terminator);
|
||||
return finish();
|
||||
}
|
||||
|
||||
inline void* Obstack::copy(const char* s)
|
||||
{
|
||||
grow((void*)s, strlen(s), 0);
|
||||
return finish();
|
||||
}
|
||||
|
||||
inline void* Obstack::copy(char c)
|
||||
{
|
||||
grow(c);
|
||||
return finish();
|
||||
}
|
||||
|
||||
inline void* Obstack::alloc(int size)
|
||||
{
|
||||
blank(size);
|
||||
return finish();
|
||||
}
|
||||
|
||||
inline void Obstack:: free(void* obj)
|
||||
{
|
||||
if (obj >= (void*)chunk && obj<(void*)chunklimit)
|
||||
nextfree = objectbase = (char *) obj;
|
||||
else
|
||||
_free(obj);
|
||||
}
|
||||
|
||||
inline void Obstack:: grow_fast(char c)
|
||||
{
|
||||
*(nextfree)++ = c;
|
||||
}
|
||||
|
||||
inline void Obstack:: blank_fast(int size)
|
||||
{
|
||||
nextfree += size;
|
||||
}
|
||||
|
||||
inline void Obstack:: shrink(int size) // from ken@cs.rochester.edu
|
||||
{
|
||||
if (nextfree >= objectbase + size)
|
||||
nextfree -= size;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,9 +0,0 @@
|
|||
|
||||
#ifndef _Pix_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Pix_h 1
|
||||
typedef void* Pix;
|
||||
#endif
|
|
@ -1,154 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
/*
|
||||
a very simple implementation of a class to output unix "plot"
|
||||
format plotter files. See corresponding unix man pages for
|
||||
more details.
|
||||
*/
|
||||
|
||||
#ifndef _PlotFile_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _PlotFile_h
|
||||
|
||||
#include <File.h>
|
||||
|
||||
/*
|
||||
Some plot libraries have the `box' command to draw boxes. Some don't.
|
||||
`box' is included here via moves & lines to allow both possiblilties.
|
||||
*/
|
||||
|
||||
|
||||
class PlotFile : private File
|
||||
{
|
||||
protected:
|
||||
PlotFile& cmd(char c);
|
||||
PlotFile& operator << (const int x);
|
||||
PlotFile& operator << (const char *s);
|
||||
|
||||
public:
|
||||
|
||||
PlotFile();
|
||||
PlotFile(const char* filename, io_mode m, access_mode a);
|
||||
PlotFile(const char* filename, const char* m);
|
||||
PlotFile(int filedesc, const io_mode m = io_writeonly);
|
||||
PlotFile(FILE* fileptr);
|
||||
|
||||
~PlotFile();
|
||||
|
||||
operator void*();
|
||||
|
||||
PlotFile& close() { File::close(); return *this; }
|
||||
PlotFile& remove() { File::remove(); return *this; }
|
||||
|
||||
int filedesc() { return File::filedesc(); }
|
||||
const char* name() { return File::name(); }
|
||||
void setname(const char* newname) { File::setname(newname); }
|
||||
int iocount() { return File::iocount(); }
|
||||
|
||||
int rdstate() { return File::rdstate(); }
|
||||
int eof() { return File::eof(); }
|
||||
int fail() { return File::fail(); }
|
||||
int bad() { return File::bad(); }
|
||||
int good() { return File::good(); }
|
||||
|
||||
// other status queries
|
||||
|
||||
int readable() { return File::readable(); }
|
||||
int writable() { return File::writable(); }
|
||||
int is_open() { return File::is_open(); }
|
||||
|
||||
void error() { File::error(); }
|
||||
void clear(state_value f = _good) { File::clear(f); }
|
||||
void set(state_value f) { File::set(f); }
|
||||
void unset(state_value f) { File::unset(f); }
|
||||
PlotFile& failif(int cond) { File::failif(cond); return *this; }
|
||||
void check_state() { File::check_state(); }
|
||||
|
||||
PlotFile& raw() { File::raw(); return *this; }
|
||||
|
||||
PlotFile& open(const char* filename, io_mode m, access_mode a);
|
||||
PlotFile& open(const char* filename, const char* m);
|
||||
PlotFile& open(int filedesc, io_mode m);
|
||||
PlotFile& open(FILE* fileptr);
|
||||
PlotFile& setbuf(const int buffer_kind); // vals: _IONBF, _IOFBF, _IOLBF
|
||||
PlotFile& setbuf(const int size, char* buf);
|
||||
|
||||
PlotFile& arc(const int xi, const int yi,
|
||||
const int x0, const int y0,
|
||||
const int x1, const int y1);
|
||||
PlotFile& box(const int x0, const int y0,
|
||||
const int x1, const int y1);
|
||||
PlotFile& circle(const int x, const int y, const int r);
|
||||
PlotFile& cont(const int xi, const int yi);
|
||||
PlotFile& dot(const int xi, const int yi, const int dx,
|
||||
int n, const int* pat);
|
||||
PlotFile& erase();
|
||||
PlotFile& label(const char* s);
|
||||
PlotFile& line(const int x0, const int y0,
|
||||
const int x1, const int y1);
|
||||
PlotFile& linemod(const char* s);
|
||||
PlotFile& move(const int xi, const int yi);
|
||||
PlotFile& point(const int xi, const int yi);
|
||||
PlotFile& space(const int x0, const int y0,
|
||||
const int x1, const int y1);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,60 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef _Poisson_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Poisson_h
|
||||
|
||||
#include <Random.h>
|
||||
|
||||
class Poisson: public Random {
|
||||
protected:
|
||||
double pMean;
|
||||
public:
|
||||
Poisson(double mean, RNG *gen);
|
||||
|
||||
double mean();
|
||||
double mean(double x);
|
||||
|
||||
virtual double operator()();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline Poisson::Poisson(double mean, RNG *gen)
|
||||
: (gen) {
|
||||
pMean = mean;
|
||||
}
|
||||
|
||||
inline double Poisson::mean() { return pMean; }
|
||||
inline double Poisson::mean(double x) {
|
||||
double t = pMean;
|
||||
pMean = x;
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,88 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef _RNG_h
|
||||
#define _RNG_h 1
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
|
||||
union PrivateRNGSingleType { // used to access floats as unsigneds
|
||||
float s;
|
||||
unsigned long u;
|
||||
};
|
||||
|
||||
union PrivateRNGDoubleType { // used to access doubles as unsigneds
|
||||
double d;
|
||||
unsigned long u[2];
|
||||
};
|
||||
|
||||
//
|
||||
// Base class for Random Number Generators. See ACG and MLCG for instances.
|
||||
//
|
||||
class RNG {
|
||||
static PrivateRNGSingleType singleMantissa; // mantissa bit vector
|
||||
static PrivateRNGDoubleType doubleMantissa; // mantissa bit vector
|
||||
public:
|
||||
RNG();
|
||||
//
|
||||
// Return a long-words word of random bits
|
||||
//
|
||||
virtual unsigned long asLong() = 0;
|
||||
virtual void reset() = 0;
|
||||
//
|
||||
// Return random bits converted to either a float or a double
|
||||
//
|
||||
float asFloat();
|
||||
double asDouble();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
|
||||
inline float RNG::asFloat()
|
||||
{
|
||||
PrivateRNGSingleType result;
|
||||
result.s = 1.0;
|
||||
result.u |= (asLong() & singleMantissa.u);
|
||||
result.s -= 1.0;
|
||||
assert( result.s < 1.0 && result.s >= 0);
|
||||
return( result.s );
|
||||
}
|
||||
|
||||
inline double RNG::asDouble()
|
||||
{
|
||||
PrivateRNGDoubleType result;
|
||||
result.d = 1.0;
|
||||
result.u[0] |= (asLong() & doubleMantissa.u[0]);
|
||||
result.u[1] |= (asLong() & doubleMantissa.u[1]);
|
||||
result.d -= 1.0;
|
||||
assert( result.d < 1.0 && result.d >= 0);
|
||||
return( result.d );
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,67 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef _Random_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Random_h 1
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
|
||||
#include <math.h>
|
||||
#include "RNG.h"
|
||||
|
||||
class Random {
|
||||
protected:
|
||||
RNG *pGenerator;
|
||||
public:
|
||||
Random(RNG *generator);
|
||||
virtual double operator()() = 0;
|
||||
|
||||
RNG *generator();
|
||||
void generator(RNG *p);
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline Random::Random(RNG *gen)
|
||||
{
|
||||
pGenerator = gen;
|
||||
}
|
||||
|
||||
inline RNG *Random::generator()
|
||||
{
|
||||
return(pGenerator);
|
||||
}
|
||||
|
||||
inline void Random::generator(RNG *p)
|
||||
{
|
||||
pGenerator = p;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,270 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifndef _Rational_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Rational_h 1
|
||||
|
||||
#include <Integer.h>
|
||||
#include <math.h>
|
||||
|
||||
class Rational
|
||||
{
|
||||
protected:
|
||||
Integer num;
|
||||
Integer den;
|
||||
|
||||
void normalize();
|
||||
|
||||
public:
|
||||
Rational();
|
||||
Rational(double);
|
||||
Rational(long n, long d = 1);
|
||||
Rational(const Integer& n);
|
||||
Rational(const Integer& n, const Integer& d);
|
||||
Rational(const Rational&);
|
||||
|
||||
~Rational();
|
||||
|
||||
void operator = (const Rational& y);
|
||||
|
||||
friend int operator == (const Rational& x, const Rational& y);
|
||||
friend int operator != (const Rational& x, const Rational& y);
|
||||
friend int operator < (const Rational& x, const Rational& y);
|
||||
friend int operator <= (const Rational& x, const Rational& y);
|
||||
friend int operator > (const Rational& x, const Rational& y);
|
||||
friend int operator >= (const Rational& x, const Rational& y);
|
||||
|
||||
friend Rational operator + (const Rational& x, const Rational& y);
|
||||
friend Rational operator - (const Rational& x, const Rational& y);
|
||||
friend Rational operator * (const Rational& x, const Rational& y);
|
||||
friend Rational operator / (const Rational& x, const Rational& y);
|
||||
|
||||
void operator += (const Rational& y);
|
||||
void operator -= (const Rational& y);
|
||||
void operator *= (const Rational& y);
|
||||
void operator /= (const Rational& y);
|
||||
|
||||
#ifdef __GNUG__
|
||||
friend Rational operator <? (const Rational& x, const Rational& y); // min
|
||||
friend Rational operator >? (const Rational& x, const Rational& y); // max
|
||||
#endif
|
||||
|
||||
friend Rational operator - (const Rational& x);
|
||||
|
||||
|
||||
// builtin Rational functions
|
||||
|
||||
|
||||
void negate(); // x = -x
|
||||
void invert(); // x = 1/x
|
||||
|
||||
friend int sign(const Rational& x); // -1, 0, or +1
|
||||
friend Rational abs(const Rational& x); // absolute value
|
||||
friend Rational sqr(const Rational& x); // square
|
||||
friend Rational pow(const Rational& x, long y);
|
||||
friend Rational pow(const Rational& x, Integer& y);
|
||||
const Integer& numerator() const;
|
||||
const Integer& denominator() const;
|
||||
|
||||
// coercion & conversion
|
||||
|
||||
operator double() const;
|
||||
friend Integer floor(const Rational& x);
|
||||
friend Integer ceil(const Rational& x);
|
||||
friend Integer trunc(const Rational& x);
|
||||
friend Integer round(const Rational& x);
|
||||
|
||||
friend istream& operator >> (istream& s, Rational& y);
|
||||
friend ostream& operator << (ostream& s, const Rational& y);
|
||||
|
||||
|
||||
// procedural versions of operators
|
||||
|
||||
friend int compare(const Rational& x, const Rational& y);
|
||||
friend void add(const Rational& x, const Rational& y, Rational& dest);
|
||||
friend void sub(const Rational& x, const Rational& y, Rational& dest);
|
||||
friend void mul(const Rational& x, const Rational& y, Rational& dest);
|
||||
friend void div(const Rational& x, const Rational& y, Rational& dest);
|
||||
|
||||
// error detection
|
||||
|
||||
volatile void error(const char* msg) const;
|
||||
int OK() const;
|
||||
|
||||
};
|
||||
|
||||
typedef Rational RatTmp; // backwards compatibility
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline Rational::Rational() {}
|
||||
inline Rational::~Rational() {}
|
||||
|
||||
inline Rational::Rational(const Rational& y) :num(y.num), den(y.den) {}
|
||||
|
||||
inline Rational::Rational(const Integer& n) :num(n), den(1) {}
|
||||
|
||||
inline Rational::Rational(const Integer& n, const Integer& d) :num(n),den(d)
|
||||
{
|
||||
normalize();
|
||||
}
|
||||
|
||||
inline Rational::Rational(long n, long d) :num(n), den(d)
|
||||
{
|
||||
normalize();
|
||||
}
|
||||
|
||||
inline void Rational::operator = (const Rational& y)
|
||||
{
|
||||
num = y.num; den = y.den;
|
||||
}
|
||||
|
||||
inline int operator == (const Rational& x, const Rational& y)
|
||||
{
|
||||
return compare(x.num, y.num) == 0 && compare(x.den, y.den) == 0;
|
||||
}
|
||||
|
||||
inline int operator != (const Rational& x, const Rational& y)
|
||||
{
|
||||
return compare(x.num, y.num) != 0 || compare(x.den, y.den) != 0;
|
||||
}
|
||||
|
||||
inline int operator < (const Rational& x, const Rational& y)
|
||||
{
|
||||
return compare(x, y) < 0;
|
||||
}
|
||||
|
||||
inline int operator <= (const Rational& x, const Rational& y)
|
||||
{
|
||||
return compare(x, y) <= 0;
|
||||
}
|
||||
|
||||
inline int operator > (const Rational& x, const Rational& y)
|
||||
{
|
||||
return compare(x, y) > 0;
|
||||
}
|
||||
|
||||
inline int operator >= (const Rational& x, const Rational& y)
|
||||
{
|
||||
return compare(x, y) >= 0;
|
||||
}
|
||||
|
||||
inline int sign(const Rational& x)
|
||||
{
|
||||
return sign(x.num);
|
||||
}
|
||||
|
||||
inline void Rational::negate()
|
||||
{
|
||||
num.negate();
|
||||
}
|
||||
|
||||
|
||||
inline void Rational::operator += (const Rational& y)
|
||||
{
|
||||
add(*this, y, *this);
|
||||
}
|
||||
|
||||
inline void Rational::operator -= (const Rational& y)
|
||||
{
|
||||
sub(*this, y, *this);
|
||||
}
|
||||
|
||||
inline void Rational::operator *= (const Rational& y)
|
||||
{
|
||||
mul(*this, y, *this);
|
||||
}
|
||||
|
||||
inline void Rational::operator /= (const Rational& y)
|
||||
{
|
||||
div(*this, y, *this);
|
||||
}
|
||||
|
||||
inline const Integer& Rational::numerator() const { return num; }
|
||||
inline const Integer& Rational::denominator() const { return den; }
|
||||
inline Rational::operator double() const { return ratio(num, den); }
|
||||
|
||||
#ifdef __GNUG__
|
||||
inline Rational operator <? (const Rational& x, const Rational& y)
|
||||
{
|
||||
if (compare(x, y) <= 0) return x; else return y;
|
||||
}
|
||||
|
||||
inline Rational operator >? (const Rational& x, const Rational& y)
|
||||
{
|
||||
if (compare(x, y) >= 0) return x; else return y;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__GNUG__) && !defined(NO_NRV)
|
||||
|
||||
inline Rational operator + (const Rational& x, const Rational& y) return r
|
||||
{
|
||||
add(x, y, r);
|
||||
}
|
||||
|
||||
inline Rational operator - (const Rational& x, const Rational& y) return r
|
||||
{
|
||||
sub(x, y, r);
|
||||
}
|
||||
|
||||
inline Rational operator * (const Rational& x, const Rational& y) return r
|
||||
{
|
||||
mul(x, y, r);
|
||||
}
|
||||
|
||||
inline Rational operator / (const Rational& x, const Rational& y) return r
|
||||
{
|
||||
div(x, y, r);
|
||||
}
|
||||
|
||||
#else /* NO_NRV */
|
||||
|
||||
inline Rational operator + (const Rational& x, const Rational& y)
|
||||
{
|
||||
Rational r; add(x, y, r); return r;
|
||||
}
|
||||
|
||||
inline Rational operator - (const Rational& x, const Rational& y)
|
||||
{
|
||||
Rational r; sub(x, y, r); return r;
|
||||
}
|
||||
|
||||
inline Rational operator * (const Rational& x, const Rational& y)
|
||||
{
|
||||
Rational r; mul(x, y, r); return r;
|
||||
}
|
||||
|
||||
inline Rational operator / (const Rational& x, const Rational& y)
|
||||
{
|
||||
Rational r; div(x, y, r); return r;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,76 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _Regex_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Regex_h 1
|
||||
|
||||
struct re_pattern_buffer; // defined elsewhere
|
||||
struct re_registers;
|
||||
|
||||
class Regex
|
||||
{
|
||||
private:
|
||||
|
||||
Regex(const Regex&) {} // no X(X&)
|
||||
void operator = (const Regex&) {} // no assignment
|
||||
|
||||
protected:
|
||||
re_pattern_buffer* buf;
|
||||
re_registers* reg;
|
||||
|
||||
public:
|
||||
Regex(const char* t,
|
||||
int fast = 0,
|
||||
int bufsize = 40,
|
||||
const char* transtable = 0);
|
||||
|
||||
~Regex();
|
||||
|
||||
int match(const char* s, int len, int pos = 0) const;
|
||||
int search(const char* s, int len,
|
||||
int& matchlen, int startpos = 0) const;
|
||||
int match_info(int& start, int& length, int nth = 0) const;
|
||||
|
||||
int OK() const; // representation invariant
|
||||
};
|
||||
|
||||
// some built in regular expressions
|
||||
|
||||
extern const Regex RXwhite; // = "[ \n\t\r\v\f]+"
|
||||
extern const Regex RXint; // = "-?[0-9]+"
|
||||
extern const Regex RXdouble; // = "-?\\(\\([0-9]+\\.[0-9]*\\)\\|
|
||||
// \\([0-9]+\\)\\|\\(\\.[0-9]+\\)\\)
|
||||
// \\([eE][---+]?[0-9]+\\)?"
|
||||
extern const Regex RXalpha; // = "[A-Za-z]+"
|
||||
extern const Regex RXlowercase; // = "[a-z]+"
|
||||
extern const Regex RXuppercase; // = "[A-Z]+"
|
||||
extern const Regex RXalphanum; // = "[0-9A-Za-z]+"
|
||||
extern const Regex RXidentifier; // = "[A-Za-z_][A-Za-z0-9_]*"
|
||||
|
||||
|
||||
#endif
|
|
@ -1,169 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1990 Free Software Foundation
|
||||
adapted from a submission from John Reidl <riedl@cs.purdue.edu>
|
||||
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifndef _RandomInteger_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _RandomInteger_h 1
|
||||
|
||||
// RandomInteger uses a random number generator to generate an integer
|
||||
// in a specified range. By default the range is 0..1. Since in my
|
||||
// experience random numbers are often needed for a wide variety of
|
||||
// ranges in the same program, this generator accepts a new low or high value
|
||||
// as an argument to the asLong and operator() methods to temporarily
|
||||
// override stored values
|
||||
|
||||
#include <math.h>
|
||||
#include "RNG.h"
|
||||
|
||||
class RandomInteger
|
||||
{
|
||||
protected:
|
||||
RNG *pGenerator;
|
||||
long pLow;
|
||||
long pHigh;
|
||||
|
||||
long _asLong(long, long);
|
||||
|
||||
public:
|
||||
|
||||
RandomInteger(long low, long high, RNG *gen);
|
||||
RandomInteger(long high, RNG *gen);
|
||||
RandomInteger(RNG *gen);
|
||||
|
||||
// read params
|
||||
|
||||
long low() const;
|
||||
long high() const;
|
||||
RNG* generator() const;
|
||||
|
||||
// change params
|
||||
|
||||
long low(long x);
|
||||
long high(long x);
|
||||
RNG* generator(RNG *gen);
|
||||
|
||||
// get a random number
|
||||
|
||||
long asLong();
|
||||
long operator()(); // synonym for asLong
|
||||
int asInt(); // (possibly) truncate as int
|
||||
|
||||
// override params for one shot
|
||||
|
||||
long asLong(long high);
|
||||
long asLong(long low, long high);
|
||||
|
||||
long operator () (long high); // synonyms
|
||||
long operator () (long low, long high);
|
||||
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline RandomInteger::RandomInteger(long low, long high, RNG *gen)
|
||||
: pLow((low < high) ? low : high),
|
||||
pHigh((low < high) ? high : low),
|
||||
pGenerator(gen)
|
||||
{}
|
||||
|
||||
inline RandomInteger::RandomInteger(long high, RNG *gen)
|
||||
: pLow((0 < high) ? 0 : high),
|
||||
pHigh((0 < high) ? high : 0),
|
||||
pGenerator(gen)
|
||||
{}
|
||||
|
||||
|
||||
inline RandomInteger::RandomInteger(RNG *gen)
|
||||
: pLow(0),
|
||||
pHigh(1),
|
||||
pGenerator(gen)
|
||||
{}
|
||||
|
||||
inline RNG* RandomInteger::generator() const { return pGenerator;}
|
||||
inline long RandomInteger::low() const { return pLow; }
|
||||
inline long RandomInteger::high() const { return pHigh; }
|
||||
|
||||
inline RNG* RandomInteger::generator(RNG *gen)
|
||||
{
|
||||
RNG *tmp = pGenerator; pGenerator = gen; return tmp;
|
||||
}
|
||||
|
||||
inline long RandomInteger::low(long x)
|
||||
{
|
||||
long tmp = pLow; pLow = x; return tmp;
|
||||
}
|
||||
|
||||
inline long RandomInteger:: high(long x)
|
||||
{
|
||||
long tmp = pHigh; pHigh = x; return tmp;
|
||||
}
|
||||
|
||||
inline long RandomInteger:: _asLong(long low, long high)
|
||||
{
|
||||
return (pGenerator->asLong() % (high-low+1)) + low;
|
||||
}
|
||||
|
||||
|
||||
inline long RandomInteger:: asLong()
|
||||
{
|
||||
return _asLong(pLow, pHigh);
|
||||
}
|
||||
|
||||
inline long RandomInteger:: asLong(long high)
|
||||
{
|
||||
return _asLong(pLow, high);
|
||||
}
|
||||
|
||||
inline long RandomInteger:: asLong(long low, long high)
|
||||
{
|
||||
return _asLong(low, high);
|
||||
}
|
||||
|
||||
inline long RandomInteger:: operator () ()
|
||||
{
|
||||
return _asLong(pLow, pHigh);
|
||||
}
|
||||
|
||||
inline long RandomInteger:: operator () (long high)
|
||||
{
|
||||
return _asLong(pLow, high);
|
||||
}
|
||||
|
||||
inline long RandomInteger:: operator () (long low, long high)
|
||||
{
|
||||
return _asLong(low, high);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
inline int RandomInteger:: asInt()
|
||||
{
|
||||
return int(asLong());
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,88 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifndef _SFile_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _SFile_h 1
|
||||
|
||||
#include <File.h>
|
||||
|
||||
class SFile: public File
|
||||
{
|
||||
protected:
|
||||
int sz; // unit size for structured binary IO
|
||||
|
||||
public:
|
||||
SFile();
|
||||
SFile(const char* filename, int size, io_mode m, access_mode a);
|
||||
SFile(const char* filename, int size, const char* m);
|
||||
SFile(int filedesc, int size, io_mode m);
|
||||
SFile(FILE* fileptr, int size);
|
||||
|
||||
~SFile();
|
||||
|
||||
int size();
|
||||
int setsize(int s);
|
||||
|
||||
SFile& get(void* x);
|
||||
SFile& put(void* x);
|
||||
SFile& operator[](long i);
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
|
||||
inline int SFile::size()
|
||||
{
|
||||
return sz;
|
||||
}
|
||||
|
||||
inline int SFile::setsize(int s)
|
||||
{
|
||||
int old = sz;
|
||||
sz = s;
|
||||
return old;
|
||||
}
|
||||
|
||||
inline SFile& SFile::get(void* x)
|
||||
{
|
||||
read(x, sz, 1); return *this;
|
||||
}
|
||||
|
||||
inline SFile& SFile::put(void* x)
|
||||
{
|
||||
write(x, sz, 1); return *this;
|
||||
}
|
||||
|
||||
inline SFile& SFile::operator[](long i)
|
||||
{
|
||||
seek(i * sz, 0); return *this;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,82 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifndef SampleHistogram_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define SampleHistogram_h 1
|
||||
|
||||
#include <stream.h>
|
||||
#include <SmplStat.h>
|
||||
|
||||
extern const int SampleHistogramMinimum;
|
||||
extern const int SampleHistogramMaximum;
|
||||
|
||||
class SampleHistogram : public SampleStatistic
|
||||
{
|
||||
protected:
|
||||
short howManyBuckets;
|
||||
int *bucketCount;
|
||||
double *bucketLimit;
|
||||
|
||||
public:
|
||||
|
||||
SampleHistogram(double low, double hi, double bucketWidth = -1.0);
|
||||
|
||||
~SampleHistogram();
|
||||
|
||||
virtual void reset();
|
||||
virtual void operator+=(double);
|
||||
|
||||
int similarSamples(double);
|
||||
|
||||
int buckets();
|
||||
|
||||
double bucketThreshold(int i);
|
||||
int inBucket(int i);
|
||||
void printBuckets(ostream&);
|
||||
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline int SampleHistogram:: buckets() { return(howManyBuckets); };
|
||||
|
||||
inline double SampleHistogram:: bucketThreshold(int i) {
|
||||
if (i < 0 || i >= howManyBuckets)
|
||||
error("invalid bucket access");
|
||||
return(bucketLimit[i]);
|
||||
}
|
||||
|
||||
inline int SampleHistogram:: inBucket(int i) {
|
||||
if (i < 0 || i >= howManyBuckets)
|
||||
error("invalid bucket access");
|
||||
return(bucketCount[i]);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,73 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef SampleStatistic_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define SampleStatistic_h 1
|
||||
|
||||
#include <builtin.h>
|
||||
|
||||
class SampleStatistic {
|
||||
protected:
|
||||
int n;
|
||||
double x;
|
||||
double x2;
|
||||
double minValue, maxValue;
|
||||
|
||||
public :
|
||||
|
||||
SampleStatistic();
|
||||
virtual void reset();
|
||||
|
||||
virtual void operator+=(double);
|
||||
int samples();
|
||||
double mean();
|
||||
double stdDev();
|
||||
double var();
|
||||
double min();
|
||||
double max();
|
||||
double confidence(int p_percentage);
|
||||
double confidence(double p_value);
|
||||
|
||||
void error(const char* msg);
|
||||
};
|
||||
|
||||
// error handlers
|
||||
|
||||
extern void default_SampleStatistic_error_handler(const char*);
|
||||
extern one_arg_error_handler_t SampleStatistic_error_handler;
|
||||
|
||||
extern one_arg_error_handler_t
|
||||
set_SampleStatistic_error_handler(one_arg_error_handler_t f);
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline SampleStatistic:: SampleStatistic(){ reset();}
|
||||
inline int SampleStatistic:: samples() {return(n);}
|
||||
inline double SampleStatistic:: min() {return(minValue);}
|
||||
inline double SampleStatistic:: max() {return(maxValue);}
|
||||
|
||||
#endif
|
||||
#endif
|
File diff suppressed because it is too large
Load Diff
|
@ -1,81 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef _Uniform_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Uniform_h 1
|
||||
|
||||
#include <Random.h>
|
||||
|
||||
//
|
||||
// The interval [lo..hi]
|
||||
//
|
||||
|
||||
class Uniform: public Random {
|
||||
double pLow;
|
||||
double pHigh;
|
||||
double delta;
|
||||
public:
|
||||
Uniform(double low, double high, RNG *gen);
|
||||
|
||||
double low();
|
||||
double low(double x);
|
||||
double high();
|
||||
double high(double x);
|
||||
|
||||
virtual double operator()();
|
||||
};
|
||||
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline Uniform::Uniform(double low, double high, RNG *gen):(gen)
|
||||
{
|
||||
pLow = (low < high) ? low : high;
|
||||
pHigh = (low < high) ? high : low;
|
||||
delta = pHigh - pLow;
|
||||
}
|
||||
|
||||
inline double Uniform::low() { return pLow; }
|
||||
|
||||
inline double Uniform::low(double x) {
|
||||
double tmp = pLow;
|
||||
pLow = x;
|
||||
delta = pHigh - pLow;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
inline double Uniform::high() { return pHigh; }
|
||||
|
||||
inline double Uniform::high(double x) {
|
||||
double tmp = pHigh;
|
||||
pHigh = x;
|
||||
delta = pHigh - pLow;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,83 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Dirk Grunwald (grunwald@cs.uiuc.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
#ifndef _Weibull_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _Weibull_h
|
||||
|
||||
#include <Random.h>
|
||||
|
||||
class Weibull: public Random {
|
||||
protected:
|
||||
double pAlpha;
|
||||
double pInvAlpha;
|
||||
double pBeta;
|
||||
|
||||
void setState();
|
||||
|
||||
public:
|
||||
Weibull(double alpha, double beta, RNG *gen);
|
||||
|
||||
double alpha();
|
||||
double alpha(double x);
|
||||
|
||||
double beta();
|
||||
double beta(double x);
|
||||
|
||||
virtual double operator()();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline void Weibull::setState() {
|
||||
pInvAlpha = 1.0 / pAlpha;
|
||||
}
|
||||
|
||||
inline Weibull::Weibull(double alpha, double beta, RNG *gen) : (gen)
|
||||
{
|
||||
pAlpha = alpha;
|
||||
pBeta = beta;
|
||||
setState();
|
||||
}
|
||||
|
||||
inline double Weibull::alpha() { return pAlpha; }
|
||||
|
||||
inline double Weibull::alpha(double x) {
|
||||
double tmp = pAlpha;
|
||||
pAlpha = x;
|
||||
setState();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
inline double Weibull::beta() { return pBeta; };
|
||||
inline double Weibull::beta(double x) {
|
||||
double tmp = pBeta;
|
||||
pBeta = x;
|
||||
return tmp;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1 +0,0 @@
|
|||
#include <builtin.h>
|
|
@ -1,10 +0,0 @@
|
|||
|
||||
#ifndef _bool_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#endif
|
||||
#define _bool_h 1
|
||||
|
||||
enum bool { FALSE = 0, TRUE = 1 };
|
||||
|
||||
#endif
|
|
@ -1,276 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
/*
|
||||
arithmetic, etc. functions on built in types
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _builtin_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _builtin_h 1
|
||||
|
||||
|
||||
typedef void (*one_arg_error_handler_t)(const char*);
|
||||
typedef void (*two_arg_error_handler_t)(const char*, const char*);
|
||||
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include <std.h>
|
||||
#include <math.h>
|
||||
|
||||
long gcd(long, long);
|
||||
long lg(unsigned long);
|
||||
double pow(double, long);
|
||||
long pow(long, long);
|
||||
|
||||
double start_timer();
|
||||
double return_elapsed_time(double last_time = 0.0);
|
||||
|
||||
char* itoa(long x, int base = 10, int width = 0);
|
||||
char* itoa(unsigned long x, int base = 10, int width = 0);
|
||||
#ifdef __GNUG__
|
||||
char* itoa(long long x, int base = 10, int width = 0);
|
||||
char* itoa(unsigned long long x, int base = 10, int width = 0);
|
||||
#endif
|
||||
char* dtoa(double x, char cvt = 'g', int width = 0, int prec = 6);
|
||||
|
||||
char* hex(long x, int width = 0);
|
||||
char* hex(unsigned long x, int width = 0);
|
||||
char* hex(int x, int width = 0);
|
||||
char* hex(short x, int width = 0);
|
||||
char* hex(unsigned int x, int width = 0);
|
||||
char* hex(unsigned short x, int width = 0);
|
||||
|
||||
char* oct(long x, int width = 0);
|
||||
char* oct(unsigned long x, int width = 0);
|
||||
char* oct(int x, int width = 0);
|
||||
char* oct(short x, int width = 0);
|
||||
char* oct(unsigned int x, int width = 0) ;
|
||||
char* oct(unsigned short x, int width = 0);
|
||||
|
||||
char* dec(long x, int width = 0);
|
||||
char* dec(unsigned long x, int width = 0);
|
||||
char* dec(int x, int width = 0);
|
||||
char* dec(short x, int width = 0);
|
||||
char* dec(unsigned int x, int width = 0) ;
|
||||
char* dec(unsigned short x, int width = 0);
|
||||
|
||||
char* form(const char* fmt ...);
|
||||
char* chr(char ch, int width = 0);
|
||||
char* str(const char* s, int width = 0);
|
||||
|
||||
unsigned int hashpjw(const char*);
|
||||
unsigned int multiplicativehash(int);
|
||||
unsigned int foldhash(double);
|
||||
|
||||
extern void default_one_arg_error_handler(const char*);
|
||||
extern void default_two_arg_error_handler(const char*, const char*);
|
||||
|
||||
extern two_arg_error_handler_t lib_error_handler;
|
||||
|
||||
extern two_arg_error_handler_t
|
||||
set_lib_error_handler(two_arg_error_handler_t f);
|
||||
|
||||
|
||||
double abs(double arg);
|
||||
float abs(float arg);
|
||||
short abs(short arg);
|
||||
long abs(long arg);
|
||||
int sign(long arg);
|
||||
int sign(double arg);
|
||||
long sqr(long arg);
|
||||
double sqr(double arg);
|
||||
int even(long arg);
|
||||
int odd(long arg);
|
||||
long lcm(long x, long y);
|
||||
void setbit(long& x, long b);
|
||||
void clearbit(long& x, long b);
|
||||
int testbit(long x, long b);
|
||||
|
||||
signed char min(signed char a, signed char b);
|
||||
unsigned char min(unsigned char a, unsigned char b);
|
||||
|
||||
signed short min(signed short a, signed short b);
|
||||
unsigned short min(unsigned short a, unsigned short b);
|
||||
|
||||
signed int min(signed int a, signed int b);
|
||||
unsigned int min(unsigned int a, unsigned int b);
|
||||
|
||||
signed long min(signed long a, signed long b);
|
||||
unsigned long min(unsigned long a, unsigned long b);
|
||||
|
||||
float min(float a, float b);
|
||||
|
||||
double min(double a, double b);
|
||||
|
||||
signed char max(signed char a, signed char b);
|
||||
unsigned char max(unsigned char a, unsigned char b);
|
||||
|
||||
signed short max(signed short a, signed short b);
|
||||
unsigned short max(unsigned short a, unsigned short b);
|
||||
|
||||
signed int max(signed int a, signed int b);
|
||||
unsigned int max(unsigned int a, unsigned int b);
|
||||
|
||||
signed long max(signed long a, signed long b);
|
||||
unsigned long max(unsigned long a, unsigned long b);
|
||||
|
||||
float max(float a, float b);
|
||||
|
||||
double max(double a, double b);
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
|
||||
inline double abs(double arg)
|
||||
{
|
||||
return (arg < 0.0)? -arg : arg;
|
||||
}
|
||||
|
||||
inline float abs(float arg)
|
||||
{
|
||||
return (arg < 0.0)? -arg : arg;
|
||||
}
|
||||
|
||||
inline short abs(short arg)
|
||||
{
|
||||
return (arg < 0)? -arg : arg;
|
||||
}
|
||||
|
||||
inline long abs(long arg)
|
||||
{
|
||||
return (arg < 0)? -arg : arg;
|
||||
}
|
||||
|
||||
inline int sign(long arg)
|
||||
{
|
||||
return (arg == 0) ? 0 : ( (arg > 0) ? 1 : -1 );
|
||||
}
|
||||
|
||||
inline int sign(double arg)
|
||||
{
|
||||
return (arg == 0.0) ? 0 : ( (arg > 0.0) ? 1 : -1 );
|
||||
}
|
||||
|
||||
inline long sqr(long arg)
|
||||
{
|
||||
return arg * arg;
|
||||
}
|
||||
|
||||
inline double sqr(double arg)
|
||||
{
|
||||
return arg * arg;
|
||||
}
|
||||
|
||||
inline int even(long arg)
|
||||
{
|
||||
return !(arg & 1);
|
||||
}
|
||||
|
||||
inline int odd(long arg)
|
||||
{
|
||||
return (arg & 1);
|
||||
}
|
||||
|
||||
inline long lcm(long x, long y)
|
||||
{
|
||||
return x / gcd(x, y) * y;
|
||||
}
|
||||
|
||||
inline void setbit(long& x, long b)
|
||||
{
|
||||
x |= (1 << b);
|
||||
}
|
||||
|
||||
inline void clearbit(long& x, long b)
|
||||
{
|
||||
x &= ~(1 << b);
|
||||
}
|
||||
|
||||
inline int testbit(long x, long b)
|
||||
{
|
||||
return ((x & (1 << b)) != 0);
|
||||
}
|
||||
|
||||
inline char* hex(int x, int width = 0) { return hex(long(x), width); }
|
||||
inline char* hex(short x, int width = 0) { return hex(long(x), width); }
|
||||
inline char* hex(unsigned int x, int width = 0)
|
||||
{ return hex((unsigned long)(x), width); }
|
||||
inline char* hex(unsigned short x, int width = 0)
|
||||
{ return hex((unsigned long)(x), width); }
|
||||
|
||||
inline char* oct(int x, int width = 0) { return oct(long(x), width); }
|
||||
inline char* oct(short x, int width = 0) { return oct(long(x), width); }
|
||||
inline char* oct(unsigned int x, int width = 0)
|
||||
{ return oct((unsigned long)(x), width); }
|
||||
inline char* oct(unsigned short x, int width = 0)
|
||||
{ return oct((unsigned long)(x), width); }
|
||||
|
||||
inline char* dec(int x, int width = 0) { return dec(long(x), width); }
|
||||
inline char* dec(short x, int width = 0) { return dec(long(x), width); }
|
||||
inline char* dec(unsigned int x, int width = 0)
|
||||
{ return dec((unsigned long)(x), width); }
|
||||
inline char* dec(unsigned short x, int width = 0)
|
||||
{ return dec((unsigned long)(x), width); }
|
||||
|
||||
inline signed char min(signed char a, signed char b) { return (a < b)?a:b;}
|
||||
inline unsigned char min(unsigned char a, unsigned char b) {return (a < b)?a:b;}
|
||||
|
||||
inline signed short min(signed short a, signed short b) {return (a < b) ?a:b;}
|
||||
inline unsigned short min(unsigned short a, unsigned short b) {return (a < b)?a:b;}
|
||||
|
||||
inline signed int min(signed int a, signed int b) {return (a < b)?a:b;}
|
||||
inline unsigned int min(unsigned int a, unsigned int b) {return (a < b)?a:b;}
|
||||
|
||||
inline signed long min(signed long a, signed long b) {return (a < b)?a:b;}
|
||||
inline unsigned long min(unsigned long a, unsigned long b) {return (a < b)?a:b;}
|
||||
|
||||
inline float min(float a, float b) {return (a < b)?a:b;}
|
||||
|
||||
inline double min(double a, double b) {return (a < b)?a:b;}
|
||||
|
||||
inline signed char max(signed char a, signed char b) { return (a > b)?a:b;}
|
||||
inline unsigned char max(unsigned char a, unsigned char b) {return (a > b)?a:b;}
|
||||
|
||||
inline signed short max(signed short a, signed short b) {return (a > b) ?a:b;}
|
||||
inline unsigned short max(unsigned short a, unsigned short b) {return (a > b)?a:b;}
|
||||
|
||||
inline signed int max(signed int a, signed int b) {return (a > b)?a:b;}
|
||||
inline unsigned int max(unsigned int a, unsigned int b) {return (a > b)?a:b;}
|
||||
|
||||
inline signed long max(signed long a, signed long b) {return (a > b)?a:b;}
|
||||
inline unsigned long max(unsigned long a, unsigned long b) {return (a > b)?a:b;}
|
||||
|
||||
inline float max(float a, float b) {return (a > b)?a:b;}
|
||||
|
||||
inline double max(double a, double b) {return (a > b)?a:b;}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,98 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifndef _compare_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _compare_h 1
|
||||
|
||||
#include <builtin.h>
|
||||
|
||||
int compare(int a, int b);
|
||||
int compare(short a, short b);
|
||||
int compare(char a, char b);
|
||||
int compare(unsigned long a, unsigned long b);
|
||||
int compare(unsigned int a, unsigned int b);
|
||||
int compare(unsigned short a, unsigned short b);
|
||||
int compare(unsigned char a, unsigned char b);
|
||||
int compare(float a, float b);
|
||||
int compare(double a, double b);
|
||||
int compare(const char* a, const char* b);
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline int compare(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
|
||||
inline int compare(short a, short b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
|
||||
inline int compare(char a, char b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
|
||||
inline int compare(unsigned long a, unsigned long b)
|
||||
{
|
||||
return (a < b)? -1 : (a > b)? 1 : 0;
|
||||
}
|
||||
|
||||
inline int compare(unsigned int a, unsigned int b)
|
||||
{
|
||||
return (a < b)? -1 : (a > b)? 1 : 0;
|
||||
}
|
||||
|
||||
inline int compare(unsigned short a, unsigned short b)
|
||||
{
|
||||
return (a < b)? -1 : (a > b)? 1 : 0;
|
||||
}
|
||||
|
||||
inline int compare(unsigned char a, unsigned char b)
|
||||
{
|
||||
return (a < b)? -1 : (a > b)? 1 : 0;
|
||||
}
|
||||
|
||||
inline int compare(float a, float b)
|
||||
{
|
||||
return (a < b)? -1 : (a > b)? 1 : 0;
|
||||
}
|
||||
|
||||
inline int compare(double a, double b)
|
||||
{
|
||||
return (a < b)? -1 : (a > b)? 1 : 0;
|
||||
}
|
||||
|
||||
inline int compare(const char* a, const char* b)
|
||||
{
|
||||
return strcmp(a,b);
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,9 +0,0 @@
|
|||
#ifndef _complex_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#endif
|
||||
#define _complex_h
|
||||
#define __ATT_complex__
|
||||
#include <Complex.h>
|
||||
typedef class Complex complex;
|
||||
#endif
|
|
@ -1,8 +0,0 @@
|
|||
|
||||
#ifndef file_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#include <sys/file.h>
|
||||
#endif
|
|
@ -1,63 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifndef _filebuf_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _filebuf_h 1
|
||||
|
||||
#include <streambuf.h>
|
||||
#include <stdio.h>
|
||||
|
||||
class filebuf: public streambuf
|
||||
{
|
||||
public:
|
||||
int fd;
|
||||
char opened;
|
||||
|
||||
int overflow(int c = EOF);
|
||||
int underflow();
|
||||
|
||||
filebuf();
|
||||
filebuf(int newfd);
|
||||
filebuf(int newfd, char* buf, int buflen);
|
||||
filebuf(const char* filename, io_mode m, access_mode a);
|
||||
filebuf(const char* filename, const char* m);
|
||||
filebuf(int filedesc, io_mode m);
|
||||
filebuf(FILE* fileptr);
|
||||
|
||||
~filebuf();
|
||||
|
||||
streambuf* open(const char* name, open_mode m);
|
||||
streambuf* open(const char* filename, io_mode m, access_mode a);
|
||||
streambuf* open(const char* filename, const char* m);
|
||||
streambuf* open(int filedesc, io_mode m);
|
||||
streambuf* open(FILE* fileptr);
|
||||
int is_open();
|
||||
int close();
|
||||
};
|
||||
|
||||
|
||||
#endif
|
|
@ -1,608 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation
|
||||
#endif
|
||||
#include <stream.h>
|
||||
#include <assert.h>
|
||||
#include "<T>.<C>.AVLMap.h"
|
||||
|
||||
|
||||
/*
|
||||
constants & inlines for maintaining balance & thread status in tree nodes
|
||||
*/
|
||||
|
||||
#define AVLBALANCEMASK 3
|
||||
#define AVLBALANCED 0
|
||||
#define AVLLEFTHEAVY 1
|
||||
#define AVLRIGHTHEAVY 2
|
||||
|
||||
#define LTHREADBIT 4
|
||||
#define RTHREADBIT 8
|
||||
|
||||
|
||||
static inline int bf(<T><C>AVLNode* t)
|
||||
{
|
||||
return t->stat & AVLBALANCEMASK;
|
||||
}
|
||||
|
||||
static inline void set_bf(<T><C>AVLNode* t, int b)
|
||||
{
|
||||
t->stat = (t->stat & ~AVLBALANCEMASK) | (b & AVLBALANCEMASK);
|
||||
}
|
||||
|
||||
|
||||
static inline int rthread(<T><C>AVLNode* t)
|
||||
{
|
||||
return t->stat & RTHREADBIT;
|
||||
}
|
||||
|
||||
static inline void set_rthread(<T><C>AVLNode* t, int b)
|
||||
{
|
||||
if (b)
|
||||
t->stat |= RTHREADBIT;
|
||||
else
|
||||
t->stat &= ~RTHREADBIT;
|
||||
}
|
||||
|
||||
static inline int lthread(<T><C>AVLNode* t)
|
||||
{
|
||||
return t->stat & LTHREADBIT;
|
||||
}
|
||||
|
||||
static inline void set_lthread(<T><C>AVLNode* t, int b)
|
||||
{
|
||||
if (b)
|
||||
t->stat |= LTHREADBIT;
|
||||
else
|
||||
t->stat &= ~LTHREADBIT;
|
||||
}
|
||||
|
||||
/*
|
||||
traversal primitives
|
||||
*/
|
||||
|
||||
|
||||
<T><C>AVLNode* <T><C>AVLMap::leftmost()
|
||||
{
|
||||
<T><C>AVLNode* t = root;
|
||||
if (t != 0) while (t->lt != 0) t = t->lt;
|
||||
return t;
|
||||
}
|
||||
|
||||
<T><C>AVLNode* <T><C>AVLMap::rightmost()
|
||||
{
|
||||
<T><C>AVLNode* t = root;
|
||||
if (t != 0) while (t->rt != 0) t = t->rt;
|
||||
return t;
|
||||
}
|
||||
|
||||
<T><C>AVLNode* <T><C>AVLMap::succ(<T><C>AVLNode* t)
|
||||
{
|
||||
<T><C>AVLNode* r = t->rt;
|
||||
if (!rthread(t)) while (!lthread(r)) r = r->lt;
|
||||
return r;
|
||||
}
|
||||
|
||||
<T><C>AVLNode* <T><C>AVLMap::pred(<T><C>AVLNode* t)
|
||||
{
|
||||
<T><C>AVLNode* l = t->lt;
|
||||
if (!lthread(t)) while (!rthread(l)) l = l->rt;
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
Pix <T><C>AVLMap::seek(<T&> key)
|
||||
{
|
||||
<T><C>AVLNode* t = root;
|
||||
if (t == 0)
|
||||
return 0;
|
||||
for (;;)
|
||||
{
|
||||
int cmp = <T>CMP(key, t->item);
|
||||
if (cmp == 0)
|
||||
return Pix(t);
|
||||
else if (cmp < 0)
|
||||
{
|
||||
if (lthread(t))
|
||||
return 0;
|
||||
else
|
||||
t = t->lt;
|
||||
}
|
||||
else if (rthread(t))
|
||||
return 0;
|
||||
else
|
||||
t = t->rt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
The combination of threads and AVL bits make adding & deleting
|
||||
interesting, but very awkward.
|
||||
|
||||
We use the following statics to avoid passing them around recursively
|
||||
*/
|
||||
|
||||
static int _need_rebalancing; // to send back balance info from rec. calls
|
||||
static <T>* _target_item; // add/del_item target
|
||||
static <T><C>AVLNode* _found_node; // returned added/deleted node
|
||||
static int _already_found; // for deletion subcases
|
||||
|
||||
|
||||
void <T><C>AVLMap:: _add(<T><C>AVLNode*& t)
|
||||
{
|
||||
int cmp = <T>CMP(*_target_item, t->item);
|
||||
if (cmp == 0)
|
||||
{
|
||||
_found_node = t;
|
||||
return;
|
||||
}
|
||||
else if (cmp < 0)
|
||||
{
|
||||
if (lthread(t))
|
||||
{
|
||||
++count;
|
||||
_found_node = new <T><C>AVLNode(*_target_item, def);
|
||||
set_lthread(_found_node, 1);
|
||||
set_rthread(_found_node, 1);
|
||||
_found_node->lt = t->lt;
|
||||
_found_node->rt = t;
|
||||
t->lt = _found_node;
|
||||
set_lthread(t, 0);
|
||||
_need_rebalancing = 1;
|
||||
}
|
||||
else
|
||||
_add(t->lt);
|
||||
if (_need_rebalancing)
|
||||
{
|
||||
switch(bf(t))
|
||||
{
|
||||
case AVLRIGHTHEAVY:
|
||||
set_bf(t, AVLBALANCED);
|
||||
_need_rebalancing = 0;
|
||||
return;
|
||||
case AVLBALANCED:
|
||||
set_bf(t, AVLLEFTHEAVY);
|
||||
return;
|
||||
case AVLLEFTHEAVY:
|
||||
<T><C>AVLNode* l = t->lt;
|
||||
if (bf(l) == AVLLEFTHEAVY)
|
||||
{
|
||||
if (rthread(l))
|
||||
t->lt = l;
|
||||
else
|
||||
t->lt = l->rt;
|
||||
set_lthread(t, rthread(l));
|
||||
l->rt = t;
|
||||
set_rthread(l, 0);
|
||||
set_bf(t, AVLBALANCED);
|
||||
set_bf(l, AVLBALANCED);
|
||||
t = l;
|
||||
_need_rebalancing = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
<T><C>AVLNode* r = l->rt;
|
||||
set_rthread(l, lthread(r));
|
||||
if (lthread(r))
|
||||
l->rt = r;
|
||||
else
|
||||
l->rt = r->lt;
|
||||
r->lt = l;
|
||||
set_lthread(r, 0);
|
||||
set_lthread(t, rthread(r));
|
||||
if (rthread(r))
|
||||
t->lt = r;
|
||||
else
|
||||
t->lt = r->rt;
|
||||
r->rt = t;
|
||||
set_rthread(r, 0);
|
||||
if (bf(r) == AVLLEFTHEAVY)
|
||||
set_bf(t, AVLRIGHTHEAVY);
|
||||
else
|
||||
set_bf(t, AVLBALANCED);
|
||||
if (bf(r) == AVLRIGHTHEAVY)
|
||||
set_bf(l, AVLLEFTHEAVY);
|
||||
else
|
||||
set_bf(l, AVLBALANCED);
|
||||
set_bf(r, AVLBALANCED);
|
||||
t = r;
|
||||
_need_rebalancing = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rthread(t))
|
||||
{
|
||||
++count;
|
||||
_found_node = new <T><C>AVLNode(*_target_item, def);
|
||||
set_rthread(t, 0);
|
||||
set_lthread(_found_node, 1);
|
||||
set_rthread(_found_node, 1);
|
||||
_found_node->lt = t;
|
||||
_found_node->rt = t->rt;
|
||||
t->rt = _found_node;
|
||||
_need_rebalancing = 1;
|
||||
}
|
||||
else
|
||||
_add(t->rt);
|
||||
if (_need_rebalancing)
|
||||
{
|
||||
switch(bf(t))
|
||||
{
|
||||
case AVLLEFTHEAVY:
|
||||
set_bf(t, AVLBALANCED);
|
||||
_need_rebalancing = 0;
|
||||
return;
|
||||
case AVLBALANCED:
|
||||
set_bf(t, AVLRIGHTHEAVY);
|
||||
return;
|
||||
case AVLRIGHTHEAVY:
|
||||
<T><C>AVLNode* r = t->rt;
|
||||
if (bf(r) == AVLRIGHTHEAVY)
|
||||
{
|
||||
if (lthread(r))
|
||||
t->rt = r;
|
||||
else
|
||||
t->rt = r->lt;
|
||||
set_rthread(t, lthread(r));
|
||||
r->lt = t;
|
||||
set_lthread(r, 0);
|
||||
set_bf(t, AVLBALANCED);
|
||||
set_bf(r, AVLBALANCED);
|
||||
t = r;
|
||||
_need_rebalancing = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
<T><C>AVLNode* l = r->lt;
|
||||
set_lthread(r, rthread(l));
|
||||
if (rthread(l))
|
||||
r->lt = l;
|
||||
else
|
||||
r->lt = l->rt;
|
||||
l->rt = r;
|
||||
set_rthread(l, 0);
|
||||
set_rthread(t, lthread(l));
|
||||
if (lthread(l))
|
||||
t->rt = l;
|
||||
else
|
||||
t->rt = l->lt;
|
||||
l->lt = t;
|
||||
set_lthread(l, 0);
|
||||
if (bf(l) == AVLRIGHTHEAVY)
|
||||
set_bf(t, AVLLEFTHEAVY);
|
||||
else
|
||||
set_bf(t, AVLBALANCED);
|
||||
if (bf(l) == AVLLEFTHEAVY)
|
||||
set_bf(r, AVLRIGHTHEAVY);
|
||||
else
|
||||
set_bf(r, AVLBALANCED);
|
||||
set_bf(l, AVLBALANCED);
|
||||
t = l;
|
||||
_need_rebalancing = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
<C>& <T><C>AVLMap::operator [] (<T&> item)
|
||||
{
|
||||
if (root == 0)
|
||||
{
|
||||
++count;
|
||||
root = new <T><C>AVLNode(item, def);
|
||||
set_rthread(root, 1);
|
||||
set_lthread(root, 1);
|
||||
return root->cont;
|
||||
}
|
||||
else
|
||||
{
|
||||
_target_item = &item;
|
||||
_need_rebalancing = 0;
|
||||
_add(root);
|
||||
return _found_node->cont;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void <T><C>AVLMap::_del(<T><C>AVLNode* par, <T><C>AVLNode*& t)
|
||||
{
|
||||
int comp;
|
||||
if (_already_found)
|
||||
{
|
||||
if (rthread(t))
|
||||
comp = 0;
|
||||
else
|
||||
comp = 1;
|
||||
}
|
||||
else
|
||||
comp = <T>CMP(*_target_item, t->item);
|
||||
if (comp == 0)
|
||||
{
|
||||
if (lthread(t) && rthread(t))
|
||||
{
|
||||
_found_node = t;
|
||||
if (t == par->lt)
|
||||
{
|
||||
set_lthread(par, 1);
|
||||
par->lt = t->lt;
|
||||
}
|
||||
else
|
||||
{
|
||||
set_rthread(par, 1);
|
||||
par->rt = t->rt;
|
||||
}
|
||||
_need_rebalancing = 1;
|
||||
return;
|
||||
}
|
||||
else if (lthread(t))
|
||||
{
|
||||
_found_node = t;
|
||||
<T><C>AVLNode* s = succ(t);
|
||||
if (s != 0 && lthread(s))
|
||||
s->lt = t->lt;
|
||||
t = t->rt;
|
||||
_need_rebalancing = 1;
|
||||
return;
|
||||
}
|
||||
else if (rthread(t))
|
||||
{
|
||||
_found_node = t;
|
||||
<T><C>AVLNode* p = pred(t);
|
||||
if (p != 0 && rthread(p))
|
||||
p->rt = t->rt;
|
||||
t = t->lt;
|
||||
_need_rebalancing = 1;
|
||||
return;
|
||||
}
|
||||
else // replace item & find someone deletable
|
||||
{
|
||||
<T><C>AVLNode* p = pred(t);
|
||||
t->item = p->item;
|
||||
t->cont = p->cont;
|
||||
_already_found = 1;
|
||||
comp = -1; // fall through below to left
|
||||
}
|
||||
}
|
||||
|
||||
if (comp < 0)
|
||||
{
|
||||
if (lthread(t))
|
||||
return;
|
||||
_del(t, t->lt);
|
||||
if (!_need_rebalancing)
|
||||
return;
|
||||
switch (bf(t))
|
||||
{
|
||||
case AVLLEFTHEAVY:
|
||||
set_bf(t, AVLBALANCED);
|
||||
return;
|
||||
case AVLBALANCED:
|
||||
set_bf(t, AVLRIGHTHEAVY);
|
||||
_need_rebalancing = 0;
|
||||
return;
|
||||
case AVLRIGHTHEAVY:
|
||||
<T><C>AVLNode* r = t->rt;
|
||||
switch (bf(r))
|
||||
{
|
||||
case AVLBALANCED:
|
||||
if (lthread(r))
|
||||
t->rt = r;
|
||||
else
|
||||
t->rt = r->lt;
|
||||
set_rthread(t, lthread(r));
|
||||
r->lt = t;
|
||||
set_lthread(r, 0);
|
||||
set_bf(t, AVLRIGHTHEAVY);
|
||||
set_bf(r, AVLLEFTHEAVY);
|
||||
_need_rebalancing = 0;
|
||||
t = r;
|
||||
return;
|
||||
case AVLRIGHTHEAVY:
|
||||
if (lthread(r))
|
||||
t->rt = r;
|
||||
else
|
||||
t->rt = r->lt;
|
||||
set_rthread(t, lthread(r));
|
||||
r->lt = t;
|
||||
set_lthread(r, 0);
|
||||
set_bf(t, AVLBALANCED);
|
||||
set_bf(r, AVLBALANCED);
|
||||
t = r;
|
||||
return;
|
||||
case AVLLEFTHEAVY:
|
||||
<T><C>AVLNode* l = r->lt;
|
||||
set_lthread(r, rthread(l));
|
||||
if (rthread(l))
|
||||
r->lt = l;
|
||||
else
|
||||
r->lt = l->rt;
|
||||
l->rt = r;
|
||||
set_rthread(l, 0);
|
||||
set_rthread(t, lthread(l));
|
||||
if (lthread(l))
|
||||
t->rt = l;
|
||||
else
|
||||
t->rt = l->lt;
|
||||
l->lt = t;
|
||||
set_lthread(l, 0);
|
||||
if (bf(l) == AVLRIGHTHEAVY)
|
||||
set_bf(t, AVLLEFTHEAVY);
|
||||
else
|
||||
set_bf(t, AVLBALANCED);
|
||||
if (bf(l) == AVLLEFTHEAVY)
|
||||
set_bf(r, AVLRIGHTHEAVY);
|
||||
else
|
||||
set_bf(r, AVLBALANCED);
|
||||
set_bf(l, AVLBALANCED);
|
||||
t = l;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rthread(t))
|
||||
return;
|
||||
_del(t, t->rt);
|
||||
if (!_need_rebalancing)
|
||||
return;
|
||||
switch (bf(t))
|
||||
{
|
||||
case AVLRIGHTHEAVY:
|
||||
set_bf(t, AVLBALANCED);
|
||||
return;
|
||||
case AVLBALANCED:
|
||||
set_bf(t, AVLLEFTHEAVY);
|
||||
_need_rebalancing = 0;
|
||||
return;
|
||||
case AVLLEFTHEAVY:
|
||||
<T><C>AVLNode* l = t->lt;
|
||||
switch (bf(l))
|
||||
{
|
||||
case AVLBALANCED:
|
||||
if (rthread(l))
|
||||
t->lt = l;
|
||||
else
|
||||
t->lt = l->rt;
|
||||
set_lthread(t, rthread(l));
|
||||
l->rt = t;
|
||||
set_rthread(l, 0);
|
||||
set_bf(t, AVLLEFTHEAVY);
|
||||
set_bf(l, AVLRIGHTHEAVY);
|
||||
_need_rebalancing = 0;
|
||||
t = l;
|
||||
return;
|
||||
case AVLLEFTHEAVY:
|
||||
if (rthread(l))
|
||||
t->lt = l;
|
||||
else
|
||||
t->lt = l->rt;
|
||||
set_lthread(t, rthread(l));
|
||||
l->rt = t;
|
||||
set_rthread(l, 0);
|
||||
set_bf(t, AVLBALANCED);
|
||||
set_bf(l, AVLBALANCED);
|
||||
t = l;
|
||||
return;
|
||||
case AVLRIGHTHEAVY:
|
||||
<T><C>AVLNode* r = l->rt;
|
||||
set_rthread(l, lthread(r));
|
||||
if (lthread(r))
|
||||
l->rt = r;
|
||||
else
|
||||
l->rt = r->lt;
|
||||
r->lt = l;
|
||||
set_lthread(r, 0);
|
||||
set_lthread(t, rthread(r));
|
||||
if (rthread(r))
|
||||
t->lt = r;
|
||||
else
|
||||
t->lt = r->rt;
|
||||
r->rt = t;
|
||||
set_rthread(r, 0);
|
||||
if (bf(r) == AVLLEFTHEAVY)
|
||||
set_bf(t, AVLRIGHTHEAVY);
|
||||
else
|
||||
set_bf(t, AVLBALANCED);
|
||||
if (bf(r) == AVLRIGHTHEAVY)
|
||||
set_bf(l, AVLLEFTHEAVY);
|
||||
else
|
||||
set_bf(l, AVLBALANCED);
|
||||
set_bf(r, AVLBALANCED);
|
||||
t = r;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void <T><C>AVLMap::del(<T&> item)
|
||||
{
|
||||
if (root == 0) return;
|
||||
_need_rebalancing = 0;
|
||||
_already_found = 0;
|
||||
_found_node = 0;
|
||||
_target_item = &item;
|
||||
_del(root, root);
|
||||
if (_found_node)
|
||||
{
|
||||
delete(_found_node);
|
||||
if (--count == 0)
|
||||
root = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void <T><C>AVLMap::_kill(<T><C>AVLNode* t)
|
||||
{
|
||||
if (t != 0)
|
||||
{
|
||||
if (!lthread(t)) _kill(t->lt);
|
||||
if (!rthread(t)) _kill(t->rt);
|
||||
delete t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
<T><C>AVLMap::<T><C>AVLMap(<T><C>AVLMap& b) :<T><C>Map(b.def)
|
||||
{
|
||||
root = 0;
|
||||
count = 0;
|
||||
for (Pix i = b.first(); i != 0; b.next(i))
|
||||
(*this)[b.key(i)] = b.contents(i);
|
||||
}
|
||||
|
||||
|
||||
int <T><C>AVLMap::OK()
|
||||
{
|
||||
int v = 1;
|
||||
if (root == 0)
|
||||
v = count == 0;
|
||||
else
|
||||
{
|
||||
int n = 1;
|
||||
<T><C>AVLNode* trail = leftmost();
|
||||
<T><C>AVLNode* t = succ(trail);
|
||||
while (t != 0)
|
||||
{
|
||||
++n;
|
||||
v &= <T>CMP(trail->item, t->item) < 0;
|
||||
trail = t;
|
||||
t = succ(t);
|
||||
}
|
||||
v &= n == count;
|
||||
}
|
||||
if (!v) error("invariant failure");
|
||||
return v;
|
||||
}
|
|
@ -1,157 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _<T><C>AVLMap_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _<T><C>AVLMap_h 1
|
||||
|
||||
#include "<T>.<C>.Map.h"
|
||||
|
||||
struct <T><C>AVLNode
|
||||
{
|
||||
<T><C>AVLNode* lt;
|
||||
<T><C>AVLNode* rt;
|
||||
<T> item;
|
||||
<C> cont;
|
||||
char stat;
|
||||
<T><C>AVLNode(<T&> h, <C&> c,
|
||||
<T><C>AVLNode* l=0, <T><C>AVLNode* r=0);
|
||||
~<T><C>AVLNode();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T><C>AVLNode::<T><C>AVLNode(<T&> h, <C&> c,
|
||||
<T><C>AVLNode* l, <T><C>AVLNode* r)
|
||||
:item(h), cont(c), lt(l), rt(r), stat(0) {}
|
||||
|
||||
inline <T><C>AVLNode::~<T><C>AVLNode() {}
|
||||
|
||||
#endif
|
||||
|
||||
typedef <T><C>AVLNode* <T><C>AVLNodePtr;
|
||||
|
||||
|
||||
class <T><C>AVLMap : public <T><C>Map
|
||||
{
|
||||
protected:
|
||||
<T><C>AVLNode* root;
|
||||
|
||||
<T><C>AVLNode* leftmost();
|
||||
<T><C>AVLNode* rightmost();
|
||||
<T><C>AVLNode* pred(<T><C>AVLNode* t);
|
||||
<T><C>AVLNode* succ(<T><C>AVLNode* t);
|
||||
void _kill(<T><C>AVLNode* t);
|
||||
void _add(<T><C>AVLNode*& t);
|
||||
void _del(<T><C>AVLNode* p, <T><C>AVLNode*& t);
|
||||
|
||||
public:
|
||||
<T><C>AVLMap(<C&> dflt);
|
||||
<T><C>AVLMap(<T><C>AVLMap& a);
|
||||
~<T><C>AVLMap();
|
||||
|
||||
<C>& operator [] (<T&> key);
|
||||
|
||||
void del(<T&> key);
|
||||
|
||||
Pix first();
|
||||
void next(Pix& i);
|
||||
<T>& key(Pix i);
|
||||
<C>& contents(Pix i);
|
||||
|
||||
Pix seek(<T&> key);
|
||||
int contains(<T&> key);
|
||||
|
||||
void clear();
|
||||
|
||||
Pix last();
|
||||
void prev(Pix& i);
|
||||
|
||||
int OK();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T><C>AVLMap::~<T><C>AVLMap()
|
||||
{
|
||||
_kill(root);
|
||||
}
|
||||
|
||||
inline <T><C>AVLMap::<T><C>AVLMap(<C&> dflt) :(dflt)
|
||||
{
|
||||
root = 0;
|
||||
}
|
||||
|
||||
|
||||
inline Pix <T><C>AVLMap::first()
|
||||
{
|
||||
return Pix(leftmost());
|
||||
}
|
||||
|
||||
inline Pix <T><C>AVLMap::last()
|
||||
{
|
||||
return Pix(rightmost());
|
||||
}
|
||||
|
||||
inline void <T><C>AVLMap::next(Pix& i)
|
||||
{
|
||||
if (i != 0) i = Pix(succ((<T><C>AVLNode*)i));
|
||||
}
|
||||
|
||||
inline void <T><C>AVLMap::prev(Pix& i)
|
||||
{
|
||||
if (i != 0) i = Pix(pred((<T><C>AVLNode*)i));
|
||||
}
|
||||
|
||||
inline <T>& <T><C>AVLMap::key(Pix i)
|
||||
{
|
||||
if (i == 0) error("null Pix");
|
||||
return ((<T><C>AVLNode*)i)->item;
|
||||
}
|
||||
|
||||
inline <C>& <T><C>AVLMap::contents(Pix i)
|
||||
{
|
||||
if (i == 0) error("null Pix");
|
||||
return ((<T><C>AVLNode*)i)->cont;
|
||||
}
|
||||
|
||||
inline void <T><C>AVLMap::clear()
|
||||
{
|
||||
_kill(root);
|
||||
count = 0;
|
||||
root = 0;
|
||||
}
|
||||
|
||||
inline int <T><C>AVLMap::contains(<T&> key)
|
||||
{
|
||||
return seek(key) != 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,885 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation
|
||||
#endif
|
||||
#include <stream.h>
|
||||
#include <assert.h>
|
||||
#include "<T>.AVLSet.h"
|
||||
|
||||
|
||||
/*
|
||||
constants & inlines for maintaining balance & thread status in tree nodes
|
||||
*/
|
||||
|
||||
#define AVLBALANCEMASK 3
|
||||
#define AVLBALANCED 0
|
||||
#define AVLLEFTHEAVY 1
|
||||
#define AVLRIGHTHEAVY 2
|
||||
|
||||
#define LTHREADBIT 4
|
||||
#define RTHREADBIT 8
|
||||
|
||||
|
||||
static inline int bf(<T>AVLNode* t)
|
||||
{
|
||||
return t->stat & AVLBALANCEMASK;
|
||||
}
|
||||
|
||||
static inline void set_bf(<T>AVLNode* t, int b)
|
||||
{
|
||||
t->stat = (t->stat & ~AVLBALANCEMASK) | (b & AVLBALANCEMASK);
|
||||
}
|
||||
|
||||
|
||||
static inline int rthread(<T>AVLNode* t)
|
||||
{
|
||||
return t->stat & RTHREADBIT;
|
||||
}
|
||||
|
||||
static inline void set_rthread(<T>AVLNode* t, int b)
|
||||
{
|
||||
if (b)
|
||||
t->stat |= RTHREADBIT;
|
||||
else
|
||||
t->stat &= ~RTHREADBIT;
|
||||
}
|
||||
|
||||
static inline int lthread(<T>AVLNode* t)
|
||||
{
|
||||
return t->stat & LTHREADBIT;
|
||||
}
|
||||
|
||||
static inline void set_lthread(<T>AVLNode* t, int b)
|
||||
{
|
||||
if (b)
|
||||
t->stat |= LTHREADBIT;
|
||||
else
|
||||
t->stat &= ~LTHREADBIT;
|
||||
}
|
||||
|
||||
/*
|
||||
traversal primitives
|
||||
*/
|
||||
|
||||
|
||||
<T>AVLNode* <T>AVLSet::leftmost()
|
||||
{
|
||||
<T>AVLNode* t = root;
|
||||
if (t != 0) while (t->lt != 0) t = t->lt;
|
||||
return t;
|
||||
}
|
||||
|
||||
<T>AVLNode* <T>AVLSet::rightmost()
|
||||
{
|
||||
<T>AVLNode* t = root;
|
||||
if (t != 0) while (t->rt != 0) t = t->rt;
|
||||
return t;
|
||||
}
|
||||
|
||||
<T>AVLNode* <T>AVLSet::succ(<T>AVLNode* t)
|
||||
{
|
||||
<T>AVLNode* r = t->rt;
|
||||
if (!rthread(t)) while (!lthread(r)) r = r->lt;
|
||||
return r;
|
||||
}
|
||||
|
||||
<T>AVLNode* <T>AVLSet::pred(<T>AVLNode* t)
|
||||
{
|
||||
<T>AVLNode* l = t->lt;
|
||||
if (!lthread(t)) while (!rthread(l)) l = l->rt;
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
Pix <T>AVLSet::seek(<T&> key)
|
||||
{
|
||||
<T>AVLNode* t = root;
|
||||
if (t == 0)
|
||||
return 0;
|
||||
for (;;)
|
||||
{
|
||||
int cmp = <T>CMP(key, t->item);
|
||||
if (cmp == 0)
|
||||
return Pix(t);
|
||||
else if (cmp < 0)
|
||||
{
|
||||
if (lthread(t))
|
||||
return 0;
|
||||
else
|
||||
t = t->lt;
|
||||
}
|
||||
else if (rthread(t))
|
||||
return 0;
|
||||
else
|
||||
t = t->rt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
The combination of threads and AVL bits make adding & deleting
|
||||
interesting, but very awkward.
|
||||
|
||||
We use the following statics to avoid passing them around recursively
|
||||
*/
|
||||
|
||||
static int _need_rebalancing; // to send back balance info from rec. calls
|
||||
static <T>* _target_item; // add/del_item target
|
||||
static <T>AVLNode* _found_node; // returned added/deleted node
|
||||
static int _already_found; // for deletion subcases
|
||||
|
||||
static <T>AVLNode** _hold_nodes; // used for rebuilding trees
|
||||
static int _max_hold_index; // # elements-1 in _hold_nodes
|
||||
|
||||
|
||||
void <T>AVLSet:: _add(<T>AVLNode*& t)
|
||||
{
|
||||
int cmp = <T>CMP(*_target_item, t->item);
|
||||
if (cmp == 0)
|
||||
{
|
||||
_found_node = t;
|
||||
return;
|
||||
}
|
||||
else if (cmp < 0)
|
||||
{
|
||||
if (lthread(t))
|
||||
{
|
||||
++count;
|
||||
_found_node = new <T>AVLNode(*_target_item);
|
||||
set_lthread(_found_node, 1);
|
||||
set_rthread(_found_node, 1);
|
||||
_found_node->lt = t->lt;
|
||||
_found_node->rt = t;
|
||||
t->lt = _found_node;
|
||||
set_lthread(t, 0);
|
||||
_need_rebalancing = 1;
|
||||
}
|
||||
else
|
||||
_add(t->lt);
|
||||
if (_need_rebalancing)
|
||||
{
|
||||
switch(bf(t))
|
||||
{
|
||||
case AVLRIGHTHEAVY:
|
||||
set_bf(t, AVLBALANCED);
|
||||
_need_rebalancing = 0;
|
||||
return;
|
||||
case AVLBALANCED:
|
||||
set_bf(t, AVLLEFTHEAVY);
|
||||
return;
|
||||
case AVLLEFTHEAVY:
|
||||
<T>AVLNode* l = t->lt;
|
||||
if (bf(l) == AVLLEFTHEAVY)
|
||||
{
|
||||
if (rthread(l))
|
||||
t->lt = l;
|
||||
else
|
||||
t->lt = l->rt;
|
||||
set_lthread(t, rthread(l));
|
||||
l->rt = t;
|
||||
set_rthread(l, 0);
|
||||
set_bf(t, AVLBALANCED);
|
||||
set_bf(l, AVLBALANCED);
|
||||
t = l;
|
||||
_need_rebalancing = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
<T>AVLNode* r = l->rt;
|
||||
set_rthread(l, lthread(r));
|
||||
if (lthread(r))
|
||||
l->rt = r;
|
||||
else
|
||||
l->rt = r->lt;
|
||||
r->lt = l;
|
||||
set_lthread(r, 0);
|
||||
set_lthread(t, rthread(r));
|
||||
if (rthread(r))
|
||||
t->lt = r;
|
||||
else
|
||||
t->lt = r->rt;
|
||||
r->rt = t;
|
||||
set_rthread(r, 0);
|
||||
if (bf(r) == AVLLEFTHEAVY)
|
||||
set_bf(t, AVLRIGHTHEAVY);
|
||||
else
|
||||
set_bf(t, AVLBALANCED);
|
||||
if (bf(r) == AVLRIGHTHEAVY)
|
||||
set_bf(l, AVLLEFTHEAVY);
|
||||
else
|
||||
set_bf(l, AVLBALANCED);
|
||||
set_bf(r, AVLBALANCED);
|
||||
t = r;
|
||||
_need_rebalancing = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rthread(t))
|
||||
{
|
||||
++count;
|
||||
_found_node = new <T>AVLNode(*_target_item);
|
||||
set_rthread(t, 0);
|
||||
set_lthread(_found_node, 1);
|
||||
set_rthread(_found_node, 1);
|
||||
_found_node->lt = t;
|
||||
_found_node->rt = t->rt;
|
||||
t->rt = _found_node;
|
||||
_need_rebalancing = 1;
|
||||
}
|
||||
else
|
||||
_add(t->rt);
|
||||
if (_need_rebalancing)
|
||||
{
|
||||
switch(bf(t))
|
||||
{
|
||||
case AVLLEFTHEAVY:
|
||||
set_bf(t, AVLBALANCED);
|
||||
_need_rebalancing = 0;
|
||||
return;
|
||||
case AVLBALANCED:
|
||||
set_bf(t, AVLRIGHTHEAVY);
|
||||
return;
|
||||
case AVLRIGHTHEAVY:
|
||||
<T>AVLNode* r = t->rt;
|
||||
if (bf(r) == AVLRIGHTHEAVY)
|
||||
{
|
||||
if (lthread(r))
|
||||
t->rt = r;
|
||||
else
|
||||
t->rt = r->lt;
|
||||
set_rthread(t, lthread(r));
|
||||
r->lt = t;
|
||||
set_lthread(r, 0);
|
||||
set_bf(t, AVLBALANCED);
|
||||
set_bf(r, AVLBALANCED);
|
||||
t = r;
|
||||
_need_rebalancing = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
<T>AVLNode* l = r->lt;
|
||||
set_lthread(r, rthread(l));
|
||||
if (rthread(l))
|
||||
r->lt = l;
|
||||
else
|
||||
r->lt = l->rt;
|
||||
l->rt = r;
|
||||
set_rthread(l, 0);
|
||||
set_rthread(t, lthread(l));
|
||||
if (lthread(l))
|
||||
t->rt = l;
|
||||
else
|
||||
t->rt = l->lt;
|
||||
l->lt = t;
|
||||
set_lthread(l, 0);
|
||||
if (bf(l) == AVLRIGHTHEAVY)
|
||||
set_bf(t, AVLLEFTHEAVY);
|
||||
else
|
||||
set_bf(t, AVLBALANCED);
|
||||
if (bf(l) == AVLLEFTHEAVY)
|
||||
set_bf(r, AVLRIGHTHEAVY);
|
||||
else
|
||||
set_bf(r, AVLBALANCED);
|
||||
set_bf(l, AVLBALANCED);
|
||||
t = l;
|
||||
_need_rebalancing = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Pix <T>AVLSet::add(<T&> item)
|
||||
{
|
||||
if (root == 0)
|
||||
{
|
||||
++count;
|
||||
root = new <T>AVLNode(item);
|
||||
set_rthread(root, 1);
|
||||
set_lthread(root, 1);
|
||||
return Pix(root);
|
||||
}
|
||||
else
|
||||
{
|
||||
_target_item = &item;
|
||||
_need_rebalancing = 0;
|
||||
_add(root);
|
||||
return Pix(_found_node);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void <T>AVLSet::_del(<T>AVLNode* par, <T>AVLNode*& t)
|
||||
{
|
||||
int comp;
|
||||
if (_already_found)
|
||||
{
|
||||
if (rthread(t))
|
||||
comp = 0;
|
||||
else
|
||||
comp = 1;
|
||||
}
|
||||
else
|
||||
comp = <T>CMP(*_target_item, t->item);
|
||||
if (comp == 0)
|
||||
{
|
||||
if (lthread(t) && rthread(t))
|
||||
{
|
||||
_found_node = t;
|
||||
if (t == par->lt)
|
||||
{
|
||||
set_lthread(par, 1);
|
||||
par->lt = t->lt;
|
||||
}
|
||||
else
|
||||
{
|
||||
set_rthread(par, 1);
|
||||
par->rt = t->rt;
|
||||
}
|
||||
_need_rebalancing = 1;
|
||||
return;
|
||||
}
|
||||
else if (lthread(t))
|
||||
{
|
||||
_found_node = t;
|
||||
<T>AVLNode* s = succ(t);
|
||||
if (s != 0 && lthread(s))
|
||||
s->lt = t->lt;
|
||||
t = t->rt;
|
||||
_need_rebalancing = 1;
|
||||
return;
|
||||
}
|
||||
else if (rthread(t))
|
||||
{
|
||||
_found_node = t;
|
||||
<T>AVLNode* p = pred(t);
|
||||
if (p != 0 && rthread(p))
|
||||
p->rt = t->rt;
|
||||
t = t->lt;
|
||||
_need_rebalancing = 1;
|
||||
return;
|
||||
}
|
||||
else // replace item & find someone deletable
|
||||
{
|
||||
<T>AVLNode* p = pred(t);
|
||||
t->item = p->item;
|
||||
_already_found = 1;
|
||||
comp = -1; // fall through below to left
|
||||
}
|
||||
}
|
||||
|
||||
if (comp < 0)
|
||||
{
|
||||
if (lthread(t))
|
||||
return;
|
||||
_del(t, t->lt);
|
||||
if (!_need_rebalancing)
|
||||
return;
|
||||
switch (bf(t))
|
||||
{
|
||||
case AVLLEFTHEAVY:
|
||||
set_bf(t, AVLBALANCED);
|
||||
return;
|
||||
case AVLBALANCED:
|
||||
set_bf(t, AVLRIGHTHEAVY);
|
||||
_need_rebalancing = 0;
|
||||
return;
|
||||
case AVLRIGHTHEAVY:
|
||||
<T>AVLNode* r = t->rt;
|
||||
switch (bf(r))
|
||||
{
|
||||
case AVLBALANCED:
|
||||
if (lthread(r))
|
||||
t->rt = r;
|
||||
else
|
||||
t->rt = r->lt;
|
||||
set_rthread(t, lthread(r));
|
||||
r->lt = t;
|
||||
set_lthread(r, 0);
|
||||
set_bf(t, AVLRIGHTHEAVY);
|
||||
set_bf(r, AVLLEFTHEAVY);
|
||||
_need_rebalancing = 0;
|
||||
t = r;
|
||||
return;
|
||||
case AVLRIGHTHEAVY:
|
||||
if (lthread(r))
|
||||
t->rt = r;
|
||||
else
|
||||
t->rt = r->lt;
|
||||
set_rthread(t, lthread(r));
|
||||
r->lt = t;
|
||||
set_lthread(r, 0);
|
||||
set_bf(t, AVLBALANCED);
|
||||
set_bf(r, AVLBALANCED);
|
||||
t = r;
|
||||
return;
|
||||
case AVLLEFTHEAVY:
|
||||
<T>AVLNode* l = r->lt;
|
||||
set_lthread(r, rthread(l));
|
||||
if (rthread(l))
|
||||
r->lt = l;
|
||||
else
|
||||
r->lt = l->rt;
|
||||
l->rt = r;
|
||||
set_rthread(l, 0);
|
||||
set_rthread(t, lthread(l));
|
||||
if (lthread(l))
|
||||
t->rt = l;
|
||||
else
|
||||
t->rt = l->lt;
|
||||
l->lt = t;
|
||||
set_lthread(l, 0);
|
||||
if (bf(l) == AVLRIGHTHEAVY)
|
||||
set_bf(t, AVLLEFTHEAVY);
|
||||
else
|
||||
set_bf(t, AVLBALANCED);
|
||||
if (bf(l) == AVLLEFTHEAVY)
|
||||
set_bf(r, AVLRIGHTHEAVY);
|
||||
else
|
||||
set_bf(r, AVLBALANCED);
|
||||
set_bf(l, AVLBALANCED);
|
||||
t = l;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rthread(t))
|
||||
return;
|
||||
_del(t, t->rt);
|
||||
if (!_need_rebalancing)
|
||||
return;
|
||||
switch (bf(t))
|
||||
{
|
||||
case AVLRIGHTHEAVY:
|
||||
set_bf(t, AVLBALANCED);
|
||||
return;
|
||||
case AVLBALANCED:
|
||||
set_bf(t, AVLLEFTHEAVY);
|
||||
_need_rebalancing = 0;
|
||||
return;
|
||||
case AVLLEFTHEAVY:
|
||||
<T>AVLNode* l = t->lt;
|
||||
switch (bf(l))
|
||||
{
|
||||
case AVLBALANCED:
|
||||
if (rthread(l))
|
||||
t->lt = l;
|
||||
else
|
||||
t->lt = l->rt;
|
||||
set_lthread(t, rthread(l));
|
||||
l->rt = t;
|
||||
set_rthread(l, 0);
|
||||
set_bf(t, AVLLEFTHEAVY);
|
||||
set_bf(l, AVLRIGHTHEAVY);
|
||||
_need_rebalancing = 0;
|
||||
t = l;
|
||||
return;
|
||||
case AVLLEFTHEAVY:
|
||||
if (rthread(l))
|
||||
t->lt = l;
|
||||
else
|
||||
t->lt = l->rt;
|
||||
set_lthread(t, rthread(l));
|
||||
l->rt = t;
|
||||
set_rthread(l, 0);
|
||||
set_bf(t, AVLBALANCED);
|
||||
set_bf(l, AVLBALANCED);
|
||||
t = l;
|
||||
return;
|
||||
case AVLRIGHTHEAVY:
|
||||
<T>AVLNode* r = l->rt;
|
||||
set_rthread(l, lthread(r));
|
||||
if (lthread(r))
|
||||
l->rt = r;
|
||||
else
|
||||
l->rt = r->lt;
|
||||
r->lt = l;
|
||||
set_lthread(r, 0);
|
||||
set_lthread(t, rthread(r));
|
||||
if (rthread(r))
|
||||
t->lt = r;
|
||||
else
|
||||
t->lt = r->rt;
|
||||
r->rt = t;
|
||||
set_rthread(r, 0);
|
||||
if (bf(r) == AVLLEFTHEAVY)
|
||||
set_bf(t, AVLRIGHTHEAVY);
|
||||
else
|
||||
set_bf(t, AVLBALANCED);
|
||||
if (bf(r) == AVLRIGHTHEAVY)
|
||||
set_bf(l, AVLLEFTHEAVY);
|
||||
else
|
||||
set_bf(l, AVLBALANCED);
|
||||
set_bf(r, AVLBALANCED);
|
||||
t = r;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void <T>AVLSet::del(<T&> item)
|
||||
{
|
||||
if (root == 0) return;
|
||||
_need_rebalancing = 0;
|
||||
_already_found = 0;
|
||||
_found_node = 0;
|
||||
_target_item = &item;
|
||||
_del(root, root);
|
||||
if (_found_node)
|
||||
{
|
||||
delete(_found_node);
|
||||
if (--count == 0)
|
||||
root = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// build an ordered array of pointers to tree nodes back into a tree
|
||||
// we know that at least one element exists
|
||||
|
||||
static <T>AVLNode* _do_treeify(int lo, int hi, int& h)
|
||||
{
|
||||
int lh, rh;
|
||||
int mid = (lo + hi) / 2;
|
||||
<T>AVLNode* t = _hold_nodes[mid];
|
||||
if (lo > mid - 1)
|
||||
{
|
||||
set_lthread(t, 1);
|
||||
if (mid == 0)
|
||||
t->lt = 0;
|
||||
else
|
||||
t->lt = _hold_nodes[mid-1];
|
||||
lh = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
set_lthread(t, 0);
|
||||
t->lt = _do_treeify(lo, mid-1, lh);
|
||||
}
|
||||
if (hi < mid + 1)
|
||||
{
|
||||
set_rthread(t, 1);
|
||||
if (mid == _max_hold_index)
|
||||
t->rt = 0;
|
||||
else
|
||||
t->rt = _hold_nodes[mid+1];
|
||||
rh = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
set_rthread(t, 0);
|
||||
t->rt = _do_treeify(mid+1, hi, rh);
|
||||
}
|
||||
if (lh == rh)
|
||||
{
|
||||
set_bf(t, AVLBALANCED);
|
||||
h = lh + 1;
|
||||
}
|
||||
else if (lh == rh - 1)
|
||||
{
|
||||
set_bf(t, AVLRIGHTHEAVY);
|
||||
h = rh + 1;
|
||||
}
|
||||
else if (rh == lh - 1)
|
||||
{
|
||||
set_bf(t, AVLLEFTHEAVY);
|
||||
h = lh + 1;
|
||||
}
|
||||
else // can't happen
|
||||
abort();
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
static <T>AVLNode* _treeify(int n)
|
||||
{
|
||||
<T>AVLNode* t;
|
||||
if (n == 0)
|
||||
t = 0;
|
||||
else
|
||||
{
|
||||
int b;
|
||||
_max_hold_index = n-1;
|
||||
t = _do_treeify(0, _max_hold_index, b);
|
||||
}
|
||||
delete _hold_nodes;
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
void <T>AVLSet::_kill(<T>AVLNode* t)
|
||||
{
|
||||
if (t != 0)
|
||||
{
|
||||
if (!lthread(t)) _kill(t->lt);
|
||||
if (!rthread(t)) _kill(t->rt);
|
||||
delete t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
<T>AVLSet::<T>AVLSet(<T>AVLSet& b)
|
||||
{
|
||||
if ((count = b.count) == 0)
|
||||
{
|
||||
root = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_hold_nodes = new <T>AVLNodePtr [count];
|
||||
<T>AVLNode* t = b.leftmost();
|
||||
int i = 0;
|
||||
while (t != 0)
|
||||
{
|
||||
_hold_nodes[i++] = new <T>AVLNode(t->item);
|
||||
t = b.succ(t);
|
||||
}
|
||||
root = _treeify(count);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int <T>AVLSet::operator == (<T>AVLSet& y)
|
||||
{
|
||||
if (count != y.count)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
<T>AVLNode* t = leftmost();
|
||||
<T>AVLNode* u = y.leftmost();
|
||||
for (;;)
|
||||
{
|
||||
if (t == 0)
|
||||
return 1;
|
||||
else if (!(<T>EQ(t->item, u->item)))
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
t = succ(t);
|
||||
u = y.succ(u);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int <T>AVLSet::operator <= (<T>AVLSet& y)
|
||||
{
|
||||
if (count > y.count)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
<T>AVLNode* t = leftmost();
|
||||
<T>AVLNode* u = y.leftmost();
|
||||
for (;;)
|
||||
{
|
||||
if (t == 0)
|
||||
return 1;
|
||||
else if (u == 0)
|
||||
return 0;
|
||||
int cmp = <T>CMP(t->item, u->item);
|
||||
if (cmp == 0)
|
||||
{
|
||||
t = succ(t);
|
||||
u = y.succ(u);
|
||||
}
|
||||
else if (cmp < 0)
|
||||
return 0;
|
||||
else
|
||||
u = y.succ(u);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void <T>AVLSet::operator |=(<T>AVLSet& y)
|
||||
{
|
||||
<T>AVLNode* t = leftmost();
|
||||
<T>AVLNode* u = y.leftmost();
|
||||
int rsize = count + y.count;
|
||||
_hold_nodes = new <T>AVLNodePtr [rsize];
|
||||
int k = 0;
|
||||
for (;;)
|
||||
{
|
||||
if (t == 0)
|
||||
{
|
||||
while (u != 0)
|
||||
{
|
||||
_hold_nodes[k++] = new <T>AVLNode(u->item);
|
||||
u = y.succ(u);
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if (u == 0)
|
||||
{
|
||||
while (t != 0)
|
||||
{
|
||||
_hold_nodes[k++] = t;
|
||||
t = succ(t);
|
||||
}
|
||||
break;
|
||||
}
|
||||
int cmp = <T>CMP(t->item, u->item);
|
||||
if (cmp == 0)
|
||||
{
|
||||
_hold_nodes[k++] = t;
|
||||
t = succ(t);
|
||||
u = y.succ(u);
|
||||
}
|
||||
else if (cmp < 0)
|
||||
{
|
||||
_hold_nodes[k++] = t;
|
||||
t = succ(t);
|
||||
}
|
||||
else
|
||||
{
|
||||
_hold_nodes[k++] = new <T>AVLNode(u->item);
|
||||
u = y.succ(u);
|
||||
}
|
||||
}
|
||||
root = _treeify(k);
|
||||
count = k;
|
||||
}
|
||||
|
||||
void <T>AVLSet::operator &= (<T>AVLSet& y)
|
||||
{
|
||||
<T>AVLNode* t = leftmost();
|
||||
<T>AVLNode* u = y.leftmost();
|
||||
int rsize = (count < y.count)? count : y.count;
|
||||
_hold_nodes = new <T>AVLNodePtr [rsize];
|
||||
int k = 0;
|
||||
for (;;)
|
||||
{
|
||||
if (t == 0)
|
||||
break;
|
||||
if (u == 0)
|
||||
{
|
||||
while (t != 0)
|
||||
{
|
||||
<T>AVLNode* tmp = succ(t);
|
||||
delete t;
|
||||
t = tmp;
|
||||
}
|
||||
break;
|
||||
}
|
||||
int cmp = <T>CMP(t->item, u->item);
|
||||
if (cmp == 0)
|
||||
{
|
||||
_hold_nodes[k++] = t;
|
||||
t = succ(t);
|
||||
u = y.succ(u);
|
||||
}
|
||||
else if (cmp < 0)
|
||||
{
|
||||
<T>AVLNode* tmp = succ(t);
|
||||
delete t;
|
||||
t = tmp;
|
||||
}
|
||||
else
|
||||
u = y.succ(u);
|
||||
}
|
||||
root = _treeify(k);
|
||||
count = k;
|
||||
}
|
||||
|
||||
|
||||
void <T>AVLSet::operator -=(<T>AVLSet& y)
|
||||
{
|
||||
<T>AVLNode* t = leftmost();
|
||||
<T>AVLNode* u = y.leftmost();
|
||||
int rsize = count;
|
||||
_hold_nodes = new <T>AVLNodePtr [rsize];
|
||||
int k = 0;
|
||||
for (;;)
|
||||
{
|
||||
if (t == 0)
|
||||
break;
|
||||
else if (u == 0)
|
||||
{
|
||||
while (t != 0)
|
||||
{
|
||||
_hold_nodes[k++] = t;
|
||||
t = succ(t);
|
||||
}
|
||||
break;
|
||||
}
|
||||
int cmp = <T>CMP(t->item, u->item);
|
||||
if (cmp == 0)
|
||||
{
|
||||
<T>AVLNode* tmp = succ(t);
|
||||
delete t;
|
||||
t = tmp;
|
||||
u = y.succ(u);
|
||||
}
|
||||
else if (cmp < 0)
|
||||
{
|
||||
_hold_nodes[k++] = t;
|
||||
t = succ(t);
|
||||
}
|
||||
else
|
||||
u = y.succ(u);
|
||||
}
|
||||
root = _treeify(k);
|
||||
count = k;
|
||||
}
|
||||
|
||||
int <T>AVLSet::owns(Pix i)
|
||||
{
|
||||
if (i == 0) return 0;
|
||||
for (<T>AVLNode* t = leftmost(); t != 0; t = succ(t))
|
||||
if (Pix(t) == i) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int <T>AVLSet::OK()
|
||||
{
|
||||
int v = 1;
|
||||
if (root == 0)
|
||||
v = count == 0;
|
||||
else
|
||||
{
|
||||
int n = 1;
|
||||
<T>AVLNode* trail = leftmost();
|
||||
<T>AVLNode* t = succ(trail);
|
||||
while (t != 0)
|
||||
{
|
||||
++n;
|
||||
v &= <T>CMP(trail->item, t->item) < 0;
|
||||
trail = t;
|
||||
t = succ(t);
|
||||
}
|
||||
v &= n == count;
|
||||
}
|
||||
if (!v) error("invariant failure");
|
||||
return v;
|
||||
}
|
|
@ -1,167 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _<T>AVL_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _<T>AVL_h 1
|
||||
|
||||
#include "<T>.Set.h"
|
||||
|
||||
struct <T>AVLNode
|
||||
{
|
||||
<T>AVLNode* lt;
|
||||
<T>AVLNode* rt;
|
||||
<T> item;
|
||||
char stat;
|
||||
<T>AVLNode(<T&> h, <T>AVLNode* l=0, <T>AVLNode* r=0);
|
||||
~<T>AVLNode();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T>AVLNode::<T>AVLNode(<T&> h, <T>AVLNode* l, <T>AVLNode* r)
|
||||
:item(h), lt(l), rt(r), stat(0) {}
|
||||
|
||||
inline <T>AVLNode::~<T>AVLNode() {}
|
||||
|
||||
#endif
|
||||
|
||||
typedef <T>AVLNode* <T>AVLNodePtr;
|
||||
|
||||
|
||||
class <T>AVLSet : public <T>Set
|
||||
{
|
||||
protected:
|
||||
<T>AVLNode* root;
|
||||
|
||||
<T>AVLSet(<T>AVLNode* p, int l);
|
||||
|
||||
<T>AVLNode* leftmost();
|
||||
<T>AVLNode* rightmost();
|
||||
<T>AVLNode* pred(<T>AVLNode* t);
|
||||
<T>AVLNode* succ(<T>AVLNode* t);
|
||||
void _kill(<T>AVLNode* t);
|
||||
void _add(<T>AVLNode*& t);
|
||||
void _del(<T>AVLNode* p, <T>AVLNode*& t);
|
||||
|
||||
public:
|
||||
<T>AVLSet();
|
||||
<T>AVLSet(<T>AVLSet& a);
|
||||
~<T>AVLSet();
|
||||
|
||||
Pix add(<T&> item);
|
||||
void del(<T&> item);
|
||||
int contains(<T&> item);
|
||||
|
||||
void clear();
|
||||
|
||||
Pix first();
|
||||
void next(Pix& i);
|
||||
<T>& operator () (Pix i);
|
||||
int owns(Pix i);
|
||||
Pix seek(<T&> item);
|
||||
|
||||
Pix last();
|
||||
void prev(Pix& i);
|
||||
|
||||
void operator |= (<T>AVLSet& b);
|
||||
void operator -= (<T>AVLSet& b);
|
||||
void operator &= (<T>AVLSet& b);
|
||||
|
||||
int operator == (<T>AVLSet& b);
|
||||
int operator != (<T>AVLSet& b);
|
||||
int operator <= (<T>AVLSet& b);
|
||||
|
||||
int OK();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T>AVLSet::~<T>AVLSet()
|
||||
{
|
||||
_kill(root);
|
||||
}
|
||||
|
||||
inline <T>AVLSet::<T>AVLSet()
|
||||
{
|
||||
root = 0;
|
||||
count = 0;
|
||||
}
|
||||
|
||||
inline <T>AVLSet::<T>AVLSet(<T>AVLNode* p, int l)
|
||||
{
|
||||
root = p;
|
||||
count = l;
|
||||
}
|
||||
|
||||
inline int <T>AVLSet::operator != (<T>AVLSet& b)
|
||||
{
|
||||
return ! ((*this) == b);
|
||||
}
|
||||
|
||||
inline Pix <T>AVLSet::first()
|
||||
{
|
||||
return Pix(leftmost());
|
||||
}
|
||||
|
||||
inline Pix <T>AVLSet::last()
|
||||
{
|
||||
return Pix(rightmost());
|
||||
}
|
||||
|
||||
inline void <T>AVLSet::next(Pix& i)
|
||||
{
|
||||
if (i != 0) i = Pix(succ((<T>AVLNode*)i));
|
||||
}
|
||||
|
||||
inline void <T>AVLSet::prev(Pix& i)
|
||||
{
|
||||
if (i != 0) i = Pix(pred((<T>AVLNode*)i));
|
||||
}
|
||||
|
||||
inline <T>& <T>AVLSet::operator () (Pix i)
|
||||
{
|
||||
if (i == 0) error("null Pix");
|
||||
return ((<T>AVLNode*)i)->item;
|
||||
}
|
||||
|
||||
inline void <T>AVLSet::clear()
|
||||
{
|
||||
_kill(root);
|
||||
count = 0;
|
||||
root = 0;
|
||||
}
|
||||
|
||||
inline int <T>AVLSet::contains(<T&> key)
|
||||
{
|
||||
return seek(key) != 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,401 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation
|
||||
#endif
|
||||
#include <stream.h>
|
||||
#include "<T>.AVec.h"
|
||||
|
||||
/*
|
||||
The following brought to you by the department of redundancy department
|
||||
*/
|
||||
|
||||
<T>AVec& <T>AVec::operator = (<T>AVec& v)
|
||||
{
|
||||
if (len != 0 && len != v.capacity())
|
||||
error("nonconformant vectors.");
|
||||
if (len == 0)
|
||||
s = new <T> [len = v.capacity()];
|
||||
if (s != v.vec())
|
||||
{
|
||||
for (int i = 0; i < len; ++i)
|
||||
s[i] = v.vec()[i];
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
<T>AVec& <T>AVec::operator = (<T&> f)
|
||||
{
|
||||
for (int i = 0; i < len; ++i) s[i] = f;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
<T>AVec concat(<T>AVec & a, <T>AVec & b)
|
||||
{
|
||||
int newl = a.capacity() + b.capacity();
|
||||
<T>* news = new <T> [newl];
|
||||
<T>* p = news;
|
||||
<T>* top = &(a.vec()[a.capacity()]);
|
||||
<T>* t = a.vec();
|
||||
while (t < top) *p++ = *t++;
|
||||
top = &(b.vec()[b.capacity()]);
|
||||
t = b.vec();
|
||||
while (t < top) *p++ = *t++;
|
||||
return <T>AVec(newl, news);
|
||||
}
|
||||
|
||||
|
||||
<T>AVec combine(<T>Combiner f, <T>AVec& a, <T>AVec& b)
|
||||
{
|
||||
int newl = (a.capacity() < b.capacity())? a.capacity() : b.capacity();
|
||||
<T>* news = new <T> [newl];
|
||||
<T>* p = news;
|
||||
<T>* top = &(a.vec()[newl]);
|
||||
<T>* t = a.vec();
|
||||
<T>* u = b.vec();
|
||||
while (t < top) *p++ = (*f)(*t++, *u++);
|
||||
return <T>AVec(newl, news);
|
||||
}
|
||||
|
||||
<T>AVec reverse(<T>AVec& a)
|
||||
{
|
||||
<T>* news = new <T> [a.capacity()];
|
||||
if (a.capacity() != 0)
|
||||
{
|
||||
<T>* lo = news;
|
||||
<T>* hi = &(news[a.capacity() - 1]);
|
||||
while (lo < hi)
|
||||
{
|
||||
<T> tmp = *lo;
|
||||
*lo++ = *hi;
|
||||
*hi-- = tmp;
|
||||
}
|
||||
}
|
||||
return <T>AVec(a.capacity(), news);
|
||||
}
|
||||
|
||||
<T>AVec map(<T>Mapper f, <T>AVec& a)
|
||||
{
|
||||
<T>* news = new <T> [a.capacity()];
|
||||
<T>* p = news;
|
||||
<T>* top = &(a.vec()[a.capacity()]);
|
||||
<T>* t = a.vec();
|
||||
while(t < top) *p++ = (*f)(*t++);
|
||||
return <T>AVec(a.capacity(), news);
|
||||
}
|
||||
|
||||
<T>AVec <T>AVec::at(int from, int n)
|
||||
{
|
||||
int to;
|
||||
if (n < 0)
|
||||
{
|
||||
n = len - from;
|
||||
to = len - 1;
|
||||
}
|
||||
else
|
||||
to = from + n - 1;
|
||||
if ((unsigned)from > to)
|
||||
range_error();
|
||||
<T>* news = new <T> [n];
|
||||
<T>* p = news;
|
||||
<T>* t = &(s[from]);
|
||||
<T>* top = &(s[to]);
|
||||
while (t <= top) *p++ = *t++;
|
||||
return <T>AVec(n, news);
|
||||
}
|
||||
|
||||
<T>AVec merge(<T>AVec & a, <T>AVec & b, <T>Comparator f)
|
||||
{
|
||||
int newl = a.capacity() + b.capacity();
|
||||
<T>* news = new <T> [newl];
|
||||
<T>* p = news;
|
||||
<T>* topa = &(a.vec()[a.capacity()]);
|
||||
<T>* as = a.vec();
|
||||
<T>* topb = &(b.vec()[b.capacity()]);
|
||||
<T>* bs = b.vec();
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (as >= topa)
|
||||
{
|
||||
while (bs < topb) *p++ = *bs++;
|
||||
break;
|
||||
}
|
||||
else if (bs >= topb)
|
||||
{
|
||||
while (as < topa) *p++ = *as++;
|
||||
break;
|
||||
}
|
||||
else if ((*f)(*as, *bs) <= 0)
|
||||
*p++ = *as++;
|
||||
else
|
||||
*p++ = *bs++;
|
||||
}
|
||||
return <T>AVec(newl, news);
|
||||
}
|
||||
|
||||
<T>AVec operator + (<T>AVec& a, <T>AVec& b)
|
||||
{
|
||||
a.check_len(b.capacity());
|
||||
<T>* news = new <T> [a.capacity()];
|
||||
<T>* p = news;
|
||||
<T>* top = &(a.vec()[a.capacity()]);
|
||||
<T>* t = a.vec();
|
||||
<T>* u = b.vec();
|
||||
while (t < top) *p++ = *t++ + *u++;
|
||||
return <T>AVec(a.capacity(), news);
|
||||
}
|
||||
|
||||
<T>AVec operator - (<T>AVec& a, <T>AVec& b)
|
||||
{
|
||||
a.check_len(b.capacity());
|
||||
<T>* news = new <T> [a.capacity()];
|
||||
<T>* p = news;
|
||||
<T>* top = &(a.vec()[a.capacity()]);
|
||||
<T>* t = a.vec();
|
||||
<T>* u = b.vec();
|
||||
while (t < top) *p++ = *t++ - *u++;
|
||||
return <T>AVec(a.capacity(), news);
|
||||
}
|
||||
|
||||
<T>AVec product (<T>AVec& a, <T>AVec& b)
|
||||
{
|
||||
a.check_len(b.capacity());
|
||||
<T>* news = new <T> [a.capacity()];
|
||||
<T>* p = news;
|
||||
<T>* top = &(a.vec()[a.capacity()]);
|
||||
<T>* t = a.vec();
|
||||
<T>* u = b.vec();
|
||||
while (t < top) *p++ = *t++ * *u++;
|
||||
return <T>AVec(a.capacity(), news);
|
||||
}
|
||||
|
||||
<T>AVec quotient(<T>AVec& a, <T>AVec& b)
|
||||
{
|
||||
a.check_len(b.capacity());
|
||||
<T>* news = new <T> [a.capacity()];
|
||||
<T>* p = news;
|
||||
<T>* top = &(a.vec()[a.capacity()]);
|
||||
<T>* t = a.vec();
|
||||
<T>* u = b.vec();
|
||||
while (t < top) *p++ = *t++ / *u++;
|
||||
return <T>AVec(a.capacity(), news);
|
||||
}
|
||||
|
||||
<T>AVec operator + (<T>AVec& a, <T&> b)
|
||||
{
|
||||
<T>* news = new <T> [a.capacity()];
|
||||
<T>* p = news;
|
||||
<T>* top = &(a.vec()[a.capacity()]);
|
||||
<T>* t = a.vec();
|
||||
while (t < top) *p++ = *t++ + b;
|
||||
return <T>AVec(a.capacity(), news);
|
||||
}
|
||||
|
||||
<T>AVec operator - (<T>AVec& a, <T&> b)
|
||||
{
|
||||
<T>* news = new <T> [a.capacity()];
|
||||
<T>* p = news;
|
||||
<T>* top = &(a.vec()[a.capacity()]);
|
||||
<T>* t = a.vec();
|
||||
while (t < top) *p++ = *t++ - b;
|
||||
return <T>AVec(a.capacity(), news);
|
||||
}
|
||||
|
||||
<T>AVec operator * (<T>AVec& a, <T&> b)
|
||||
{
|
||||
<T>* news = new <T> [a.capacity()];
|
||||
<T>* p = news;
|
||||
<T>* top = &(a.vec()[a.capacity()]);
|
||||
<T>* t = a.vec();
|
||||
while (t < top) *p++ = *t++ * b;
|
||||
return <T>AVec(a.capacity(), news);
|
||||
}
|
||||
|
||||
<T>AVec operator / (<T>AVec& a, <T&> b)
|
||||
{
|
||||
<T>* news = new <T> [a.capacity()];
|
||||
<T>* p = news;
|
||||
<T>* top = &(a.vec()[a.capacity()]);
|
||||
<T>* t = a.vec();
|
||||
while (t < top) *p++ = *t++ / b;
|
||||
return <T>AVec(a.capacity(), news);
|
||||
}
|
||||
|
||||
<T>AVec <T>AVec::operator - ()
|
||||
{
|
||||
<T>* news = new <T> [len];
|
||||
<T>* p = news;
|
||||
<T>* top = &(s[len]);
|
||||
<T>* t = s;
|
||||
while (t < top) *p++ = -(*t++);
|
||||
return <T>AVec(len, news);
|
||||
}
|
||||
|
||||
<T>AVec& <T>AVec::operator += (<T>AVec& b)
|
||||
{
|
||||
check_len(b.capacity());
|
||||
<T>* u = b.vec();
|
||||
<T>* top = &(s[len]);
|
||||
<T>* t = s;
|
||||
while (t < top) *t++ += *u++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
<T>AVec& <T>AVec::operator -= (<T>AVec& b)
|
||||
{
|
||||
check_len(b.capacity());
|
||||
<T>* u = b.vec();
|
||||
<T>* top = &(s[len]);
|
||||
<T>* t = s;
|
||||
while (t < top) *t++ -= *u++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
<T>AVec& <T>AVec::product(<T>AVec& b)
|
||||
{
|
||||
check_len(b.capacity());
|
||||
<T>* u = b.vec();
|
||||
<T>* top = &(s[len]);
|
||||
<T>* t = s;
|
||||
while (t < top) *t++ *= *u++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
<T>AVec& <T>AVec::quotient(<T>AVec& b)
|
||||
{
|
||||
check_len(b.capacity());
|
||||
<T>* u = b.vec();
|
||||
<T>* top = &(s[len]);
|
||||
<T>* t = s;
|
||||
while (t < top) *t++ /= *u++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
<T>AVec& <T>AVec::operator += (<T&> b)
|
||||
{
|
||||
<T>* top = &(s[len]);
|
||||
<T>* t = s;
|
||||
while (t < top) *t++ += b;
|
||||
return *this;
|
||||
}
|
||||
|
||||
<T>AVec& <T>AVec::operator -= (<T&> b)
|
||||
{
|
||||
<T>* top = &(s[len]);
|
||||
<T>* t = s;
|
||||
while (t < top) *t++ -= b;
|
||||
return *this;
|
||||
}
|
||||
|
||||
<T>AVec& <T>AVec::operator *= (<T&> b)
|
||||
{
|
||||
<T>* top = &(s[len]);
|
||||
<T>* t = s;
|
||||
while (t < top) *t++ *= b;
|
||||
return *this;
|
||||
}
|
||||
|
||||
<T>AVec& <T>AVec::operator /= (<T&> b)
|
||||
{
|
||||
<T>* top = &(s[len]);
|
||||
<T>* t = s;
|
||||
while (t < top) *t++ /= b;
|
||||
return *this;
|
||||
}
|
||||
|
||||
<T> <T>AVec::max()
|
||||
{
|
||||
if (len == 0)
|
||||
return 0;
|
||||
<T>* top = &(s[len]);
|
||||
<T>* t = s;
|
||||
<T> res = *t++;
|
||||
for (; t < top; ++t) if (*t > res) res = *t;
|
||||
return res;
|
||||
}
|
||||
|
||||
int <T>AVec::max_index()
|
||||
{
|
||||
if (len == 0)
|
||||
return -1;
|
||||
int ind = 0;
|
||||
for (int i = 1; i < len; ++i)
|
||||
if (s[i] > s[ind])
|
||||
ind = i;
|
||||
return ind;
|
||||
}
|
||||
|
||||
<T> <T>AVec::min()
|
||||
{
|
||||
if (len == 0)
|
||||
return 0;
|
||||
<T>* top = &(s[len]);
|
||||
<T>* t = s;
|
||||
<T> res = *t++;
|
||||
for (; t < top; ++t) if (*t < res) res = *t;
|
||||
return res;
|
||||
}
|
||||
|
||||
int <T>AVec::min_index()
|
||||
{
|
||||
if (len == 0)
|
||||
return -1;
|
||||
int ind = 0;
|
||||
for (int i = 1; i < len; ++i)
|
||||
if (s[i] < s[ind])
|
||||
ind = i;
|
||||
return ind;
|
||||
}
|
||||
|
||||
<T> <T>AVec::sum()
|
||||
{
|
||||
<T> res = 0;
|
||||
<T>* top = &(s[len]);
|
||||
<T>* t = s;
|
||||
while (t < top) res += *t++;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
<T> <T>AVec::sumsq()
|
||||
{
|
||||
<T> res = 0;
|
||||
<T>* top = &(s[len]);
|
||||
<T>* t = s;
|
||||
for (; t < top; ++t) res += *t * *t;
|
||||
return res;
|
||||
}
|
||||
|
||||
<T> operator * (<T>AVec& a, <T>AVec& b)
|
||||
{
|
||||
a.check_len(b.capacity());
|
||||
<T>* top = &(a.vec()[a.capacity()]);
|
||||
<T>* t = a.vec();
|
||||
<T>* u = b.vec();
|
||||
<T> res = 0;
|
||||
while (t < top) res += *t++ * *u++;
|
||||
return res;
|
||||
}
|
|
@ -1,123 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _<T>AVec_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _<T>AVec_h 1
|
||||
|
||||
#include "<T>.Vec.h"
|
||||
|
||||
class <T>AVec : public <T>Vec
|
||||
{
|
||||
protected:
|
||||
void check_len(int l);
|
||||
<T>* vec();
|
||||
<T>AVec(int l, <T>* d);
|
||||
public:
|
||||
<T>AVec ();
|
||||
<T>AVec (int l);
|
||||
<T>AVec (int l, <T&> fill_value);
|
||||
<T>AVec (<T>AVec&);
|
||||
~<T>AVec ();
|
||||
|
||||
<T>AVec& operator = (<T>AVec& a);
|
||||
<T>AVec& operator = (<T&> fill_value);
|
||||
|
||||
// vector by scalar -> vector operations
|
||||
|
||||
friend <T>AVec operator + (<T>AVec& a, <T&> b);
|
||||
friend <T>AVec operator - (<T>AVec& a, <T&> b);
|
||||
friend <T>AVec operator * (<T>AVec& a, <T&> b);
|
||||
friend <T>AVec operator / (<T>AVec& a, <T&> b);
|
||||
|
||||
<T>AVec& operator += (<T&> b);
|
||||
<T>AVec& operator -= (<T&> b);
|
||||
<T>AVec& operator *= (<T&> b);
|
||||
<T>AVec& operator /= (<T&> b);
|
||||
|
||||
// vector by vector -> vector operations
|
||||
|
||||
friend <T>AVec operator + (<T>AVec& a, <T>AVec& b);
|
||||
friend <T>AVec operator - (<T>AVec& a, <T>AVec& b);
|
||||
<T>AVec& operator += (<T>AVec& b);
|
||||
<T>AVec& operator -= (<T>AVec& b);
|
||||
|
||||
<T>AVec operator - ();
|
||||
|
||||
friend <T>AVec product(<T>AVec& a, <T>AVec& b);
|
||||
<T>AVec& product(<T>AVec& b);
|
||||
friend <T>AVec quotient(<T>AVec& a, <T>AVec& b);
|
||||
<T>AVec& quotient(<T>AVec& b);
|
||||
|
||||
// vector -> scalar operations
|
||||
|
||||
friend <T> operator * (<T>AVec& a, <T>AVec& b);
|
||||
|
||||
<T> sum();
|
||||
<T> min();
|
||||
<T> max();
|
||||
<T> sumsq();
|
||||
|
||||
// indexing
|
||||
|
||||
int min_index();
|
||||
int max_index();
|
||||
|
||||
// redundant but necesssary
|
||||
friend <T>AVec concat(<T>AVec& a, <T>AVec& b);
|
||||
friend <T>AVec map(<T>Mapper f, <T>AVec& a);
|
||||
friend <T>AVec merge(<T>AVec& a, <T>AVec& b, <T>Comparator f);
|
||||
friend <T>AVec combine(<T>Combiner f, <T>AVec& a, <T>AVec& b);
|
||||
friend <T>AVec reverse(<T>AVec& a);
|
||||
<T>AVec at(int from = 0, int n = -1);
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T>AVec::<T>AVec() {}
|
||||
inline <T>AVec::<T>AVec(int l) :<T>Vec(l) {}
|
||||
inline <T>AVec::<T>AVec(int l, <T&> fill_value) : <T>Vec (l, fill_value) {}
|
||||
inline <T>AVec::<T>AVec(<T>AVec& v) :(v) {}
|
||||
inline <T>AVec::<T>AVec(int l, <T>* d) :<T>Vec(l, d) {}
|
||||
inline <T>AVec::~<T>AVec() {}
|
||||
|
||||
|
||||
inline <T>* <T>AVec::vec()
|
||||
{
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
inline void <T>AVec::check_len(int l)
|
||||
{
|
||||
if (l != len)
|
||||
error("nonconformant vectors.");
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,383 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation
|
||||
#endif
|
||||
#include <stream.h>
|
||||
#include "<T>.BSTSet.h"
|
||||
|
||||
|
||||
/*
|
||||
traversal primitives
|
||||
*/
|
||||
|
||||
|
||||
<T>BSTNode* <T>BSTSet::leftmost()
|
||||
{
|
||||
<T>BSTNode* t = root;
|
||||
if (t != 0) while (t->lt != 0) t = t->lt;
|
||||
return t;
|
||||
}
|
||||
|
||||
<T>BSTNode* <T>BSTSet::rightmost()
|
||||
{
|
||||
<T>BSTNode* t = root;
|
||||
if (t != 0) while (t->rt != 0) t = t->rt;
|
||||
return t;
|
||||
}
|
||||
|
||||
<T>BSTNode* <T>BSTSet::succ(<T>BSTNode* t)
|
||||
{
|
||||
if (t == 0)
|
||||
return 0;
|
||||
if (t->rt != 0)
|
||||
{
|
||||
t = t->rt;
|
||||
while (t->lt != 0) t = t->lt;
|
||||
return t;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
if (t->par == 0 || t == t->par->lt)
|
||||
return t->par;
|
||||
else
|
||||
t = t->par;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<T>BSTNode* <T>BSTSet::pred(<T>BSTNode* t)
|
||||
{
|
||||
if (t == 0)
|
||||
return 0;
|
||||
else if (t->lt != 0)
|
||||
{
|
||||
t = t->lt;
|
||||
while (t->rt != 0) t = t->rt;
|
||||
return t;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
if (t->par == 0 || t == t->par->rt)
|
||||
return t->par;
|
||||
else
|
||||
t = t->par;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Pix <T>BSTSet::seek(<T&> key)
|
||||
{
|
||||
<T>BSTNode* t = root;
|
||||
for (;;)
|
||||
{
|
||||
if (t == 0)
|
||||
return 0;
|
||||
int comp = <T>CMP(key, t->item);
|
||||
if (comp == 0)
|
||||
return Pix(t);
|
||||
else if (comp < 0)
|
||||
t = t->lt;
|
||||
else
|
||||
t = t->rt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Pix <T>BSTSet::add(<T&> item)
|
||||
{
|
||||
if (root == 0)
|
||||
{
|
||||
++count;
|
||||
root = new <T>BSTNode(item);
|
||||
return Pix(root);
|
||||
}
|
||||
|
||||
<T>BSTNode* t = root;
|
||||
<T>BSTNode* p = root;
|
||||
int comp;
|
||||
for (;;)
|
||||
{
|
||||
if (t == 0)
|
||||
{
|
||||
++count;
|
||||
t = new <T>BSTNode(item);
|
||||
if (comp > 0)
|
||||
p->lt = t;
|
||||
else
|
||||
p->rt = t;
|
||||
t->par = p;
|
||||
return Pix(t);
|
||||
}
|
||||
p = t;
|
||||
comp = <T>CMP(t->item, item);
|
||||
if (comp == 0)
|
||||
return Pix(t);
|
||||
else if (comp > 0)
|
||||
t = t->lt;
|
||||
else
|
||||
t = t->rt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void <T>BSTSet::del(<T&> key)
|
||||
{
|
||||
<T>BSTNode* t = root;
|
||||
<T>BSTNode* p = root;
|
||||
int comp;
|
||||
for (;;)
|
||||
{
|
||||
if (t == 0)
|
||||
return;
|
||||
comp = <T>CMP(key, t->item);
|
||||
if (comp == 0)
|
||||
{
|
||||
--count;
|
||||
<T>BSTNode* repl;
|
||||
if (t->lt == 0)
|
||||
repl = t->rt;
|
||||
else if (t->rt == 0)
|
||||
repl = t->lt;
|
||||
else
|
||||
{
|
||||
<T>BSTNode* prepl = t;
|
||||
repl = t->lt;
|
||||
while (repl->rt != 0)
|
||||
{
|
||||
prepl = repl;
|
||||
repl = repl->rt;
|
||||
}
|
||||
if (prepl != t)
|
||||
{
|
||||
prepl->rt = repl->lt;
|
||||
if (prepl->rt != 0) prepl->rt->par = prepl;
|
||||
repl->lt = t->lt;
|
||||
if (repl->lt != 0) repl->lt->par = repl;
|
||||
}
|
||||
repl->rt = t->rt;
|
||||
if (repl->rt != 0) repl->rt->par = repl;
|
||||
}
|
||||
if (t == root)
|
||||
{
|
||||
root = repl;
|
||||
if (repl != 0) repl->par = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (t == p->lt)
|
||||
p->lt = repl;
|
||||
else
|
||||
p->rt = repl;
|
||||
if (repl != 0) repl->par = p;
|
||||
}
|
||||
delete t;
|
||||
return;
|
||||
}
|
||||
p = t;
|
||||
if (comp < 0)
|
||||
t = t->lt;
|
||||
else
|
||||
t = t->rt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void <T>BSTSet::_kill(<T>BSTNode* t)
|
||||
{
|
||||
if (t != 0)
|
||||
{
|
||||
_kill(t->lt);
|
||||
_kill(t->rt);
|
||||
delete t;
|
||||
}
|
||||
}
|
||||
|
||||
<T>BSTNode* <T>BSTSet::_copy(<T>BSTNode* t)
|
||||
{
|
||||
if (t == 0)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
<T>BSTNode* u = new <T>BSTNode(t->item, _copy(t->lt), _copy(t->rt));
|
||||
if (u->lt != 0) u->lt->par = u;
|
||||
if (u->rt != 0) u->rt->par = u;
|
||||
return u;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int <T>BSTSet::operator == (<T>BSTSet& y)
|
||||
{
|
||||
if (count != y.count)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
<T>BSTNode* t = leftmost();
|
||||
<T>BSTNode* u = y.leftmost();
|
||||
for (;;)
|
||||
{
|
||||
if (t == 0)
|
||||
return 1;
|
||||
else if (!<T>EQ(t->item, u->item))
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
t = succ(t);
|
||||
u = y.succ(u);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int <T>BSTSet::operator <= (<T>BSTSet& y)
|
||||
{
|
||||
if (count > y.count)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
<T>BSTNode* t = leftmost();
|
||||
<T>BSTNode* u = y.leftmost();
|
||||
for (;;)
|
||||
{
|
||||
if (t == 0)
|
||||
return 1;
|
||||
else if (u == 0)
|
||||
return 0;
|
||||
int cmp = <T>CMP(t->item, u->item);
|
||||
if (cmp == 0)
|
||||
{
|
||||
t = succ(t);
|
||||
u = y.succ(u);
|
||||
}
|
||||
else if (cmp < 0)
|
||||
return 0;
|
||||
else
|
||||
u = y.succ(u);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// linear-time, zero space overhead binary tree rebalancing from
|
||||
// Stout & Warren, ``Tree rebalancing in linear space and time''
|
||||
// CACM, Sept, 1986, p902.
|
||||
|
||||
|
||||
void <T>BSTSet::balance()
|
||||
{
|
||||
if (count <= 2) return; // don't bother --
|
||||
// also we assume non-null root, below
|
||||
|
||||
// make re-attaching the root easy via trickery
|
||||
|
||||
struct _fake_node { _fake_node *lt, *rt, *par; } fake_root;
|
||||
|
||||
fake_root.rt = (_fake_node*)root;
|
||||
fake_root.par = 0;
|
||||
<T>BSTNode* pseudo_root = (<T>BSTNode*)&fake_root;
|
||||
|
||||
// phase 1: tree-to-vine
|
||||
|
||||
<T>BSTNode* vine_tail = pseudo_root;
|
||||
<T>BSTNode* remainder = root;
|
||||
|
||||
while (remainder != 0)
|
||||
{
|
||||
if (remainder->lt == 0)
|
||||
{
|
||||
vine_tail = remainder;
|
||||
remainder = remainder->rt;
|
||||
}
|
||||
else
|
||||
{
|
||||
<T>BSTNode* tmp = remainder->lt;
|
||||
remainder->lt = tmp->rt;
|
||||
if (remainder->lt != 0) remainder->lt->par = remainder;
|
||||
tmp->rt = remainder;
|
||||
remainder->par = tmp;
|
||||
vine_tail->rt = remainder = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
// phase 2: vine-to-tree
|
||||
|
||||
// Uses the slightly simpler version adapted from
|
||||
// Day ``Balancing a binary tree'' Computer Journal, Nov. 1976,
|
||||
// since it's not generally important whether the `stray' leaves are
|
||||
// on the left or on the right.
|
||||
|
||||
unsigned int spines = count - 1;
|
||||
while (spines > 1)
|
||||
{
|
||||
int compressions = spines >> 1; // compress every other node
|
||||
spines -= compressions + 1; // halve for next time
|
||||
|
||||
<T>BSTNode* scanner = pseudo_root;
|
||||
while (compressions-- > 0)
|
||||
{
|
||||
<T>BSTNode* child = scanner->rt;
|
||||
<T>BSTNode* grandchild = child->rt;
|
||||
scanner->rt = grandchild;
|
||||
grandchild->par = scanner;
|
||||
child->rt = grandchild->lt;
|
||||
if (child->rt != 0) child->rt->par = child;
|
||||
grandchild->lt = child;
|
||||
child->par = grandchild;
|
||||
scanner = grandchild;
|
||||
}
|
||||
}
|
||||
|
||||
root = pseudo_root->rt;
|
||||
root->par = 0;
|
||||
}
|
||||
|
||||
|
||||
int <T>BSTSet::OK()
|
||||
{
|
||||
int v = 1;
|
||||
if (root == 0)
|
||||
v = count == 0;
|
||||
else
|
||||
{
|
||||
int n = 1;
|
||||
<T>BSTNode* trail = leftmost();
|
||||
<T>BSTNode* t = succ(trail);
|
||||
while (t != 0)
|
||||
{
|
||||
++n;
|
||||
v &= <T>CMP(trail->item, t->item) < 0;
|
||||
trail = t;
|
||||
t = succ(t);
|
||||
}
|
||||
v &= n == count;
|
||||
}
|
||||
if (!v) error("invariant failure");
|
||||
return v;
|
||||
}
|
|
@ -1,166 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _<T>BSTSet_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _<T>BSTSet_h 1
|
||||
|
||||
#include "<T>.Set.h"
|
||||
|
||||
#ifndef _<T>BSTNode
|
||||
#define _<T>BSTNode 1
|
||||
|
||||
struct <T>BSTNode
|
||||
{
|
||||
<T>BSTNode* lt;
|
||||
<T>BSTNode* rt;
|
||||
<T>BSTNode* par;
|
||||
<T> item;
|
||||
<T>BSTNode(<T&> h, <T>BSTNode* l=0, <T>BSTNode* r=0,
|
||||
<T>BSTNode* p = 0);
|
||||
~<T>BSTNode();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T>BSTNode::<T>BSTNode(<T&> h, <T>BSTNode* l, <T>BSTNode* r,
|
||||
<T>BSTNode* p)
|
||||
:item(h), lt(l), rt(r), par(p) {}
|
||||
|
||||
inline <T>BSTNode::~<T>BSTNode() {}
|
||||
|
||||
#endif
|
||||
|
||||
typedef <T>BSTNode* <T>BSTNodePtr;
|
||||
|
||||
#endif
|
||||
|
||||
class <T>BSTSet : public <T>Set
|
||||
{
|
||||
protected:
|
||||
<T>BSTNode* root;
|
||||
|
||||
<T>BSTNode* leftmost();
|
||||
<T>BSTNode* rightmost();
|
||||
<T>BSTNode* pred(<T>BSTNode* t);
|
||||
<T>BSTNode* succ(<T>BSTNode* t);
|
||||
void _kill(<T>BSTNode* t);
|
||||
<T>BSTNode* _copy(<T>BSTNode* t);
|
||||
|
||||
public:
|
||||
<T>BSTSet();
|
||||
<T>BSTSet(<T>BSTSet& a);
|
||||
~<T>BSTSet();
|
||||
|
||||
Pix add(<T&> item);
|
||||
void del(<T&> item);
|
||||
int contains(<T&> item);
|
||||
|
||||
void clear();
|
||||
|
||||
Pix first();
|
||||
void next(Pix& i);
|
||||
<T>& operator () (Pix i);
|
||||
Pix seek(<T&> item);
|
||||
|
||||
Pix last();
|
||||
void prev(Pix& i);
|
||||
|
||||
int operator == (<T>BSTSet& b);
|
||||
int operator != (<T>BSTSet& b);
|
||||
int operator <= (<T>BSTSet& b);
|
||||
|
||||
void balance();
|
||||
int OK();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T>BSTSet::~<T>BSTSet()
|
||||
{
|
||||
_kill(root);
|
||||
}
|
||||
|
||||
inline <T>BSTSet::<T>BSTSet()
|
||||
{
|
||||
root = 0;
|
||||
count = 0;
|
||||
}
|
||||
|
||||
|
||||
inline <T>BSTSet::<T>BSTSet(<T>BSTSet& a)
|
||||
{
|
||||
count = a.count;
|
||||
root = _copy(a.root);
|
||||
}
|
||||
|
||||
inline int <T>BSTSet::operator != (<T>BSTSet& b)
|
||||
{
|
||||
return ! (*this == b);
|
||||
}
|
||||
|
||||
inline Pix <T>BSTSet::first()
|
||||
{
|
||||
return Pix(leftmost());
|
||||
}
|
||||
|
||||
inline Pix <T>BSTSet::last()
|
||||
{
|
||||
return Pix(rightmost());
|
||||
}
|
||||
|
||||
inline void <T>BSTSet::next(Pix& i)
|
||||
{
|
||||
if (i != 0) i = Pix(succ((<T>BSTNode*)i));
|
||||
}
|
||||
|
||||
inline void <T>BSTSet::prev(Pix& i)
|
||||
{
|
||||
if (i != 0) i = Pix(pred((<T>BSTNode*)i));
|
||||
}
|
||||
|
||||
inline <T>& <T>BSTSet::operator () (Pix i)
|
||||
{
|
||||
if (i == 0) error("null Pix");
|
||||
return ((<T>BSTNode*)i)->item;
|
||||
}
|
||||
|
||||
inline void <T>BSTSet::clear()
|
||||
{
|
||||
_kill(root);
|
||||
count = 0;
|
||||
root = 0;
|
||||
}
|
||||
|
||||
inline int <T>BSTSet::contains(<T&> key)
|
||||
{
|
||||
return seek(key) != 0;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,79 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation
|
||||
#endif
|
||||
#include <builtin.h>
|
||||
#include "<T>.Bag.h"
|
||||
|
||||
// error handling
|
||||
|
||||
void <T>Bag::error(const char* msg)
|
||||
{
|
||||
(*lib_error_handler)("Bag", msg);
|
||||
}
|
||||
|
||||
|
||||
Pix <T>Bag::seek(<T&> item, Pix i)
|
||||
{
|
||||
if (i == 0)
|
||||
i = first();
|
||||
else
|
||||
next(i);
|
||||
for (;i != 0 && (!(<T>EQ((*this)(i), item))); next(i));
|
||||
return i;
|
||||
}
|
||||
|
||||
int <T>Bag::owns(Pix p)
|
||||
{
|
||||
if (p == 0) return 0;
|
||||
for (Pix i = first(); i != 0; next(i)) if (i == p) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void <T>Bag::remove(<T&> item)
|
||||
{
|
||||
int i = nof(item);
|
||||
while (i-- > 0) del(item);
|
||||
}
|
||||
|
||||
|
||||
int <T>Bag::nof(<T&> item)
|
||||
{
|
||||
int n = 0;
|
||||
for (Pix p = first(); p; next(p)) if (<T>EQ((*this)(p), item)) ++n;
|
||||
return n;
|
||||
}
|
||||
|
||||
void <T>Bag::clear()
|
||||
{
|
||||
Pix i = first();
|
||||
while (i != 0)
|
||||
{
|
||||
del((*this)(i));
|
||||
i = first();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,87 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _<T>Bag_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _<T>Bag_h 1
|
||||
|
||||
#include <Pix.h>
|
||||
#include "<T>.defs.h"
|
||||
|
||||
class <T>Bag
|
||||
{
|
||||
protected:
|
||||
int count;
|
||||
|
||||
public:
|
||||
virtual ~<T>Bag();
|
||||
|
||||
int length(); // current number of items
|
||||
int empty();
|
||||
|
||||
virtual Pix add(<T&> item) = 0; // add item; return Pix
|
||||
|
||||
virtual void del(<T&> item) = 0; // delete 1 occurrence of item
|
||||
virtual void remove(<T&> item); // delete all occurrences
|
||||
virtual void clear(); // delete all items
|
||||
|
||||
virtual int contains(<T&> item); // is item in Bag?
|
||||
virtual int nof(<T&> item); // how many in Bag?
|
||||
|
||||
virtual Pix first() = 0; // Pix of first item or 0
|
||||
virtual void next(Pix& i) = 0; // advance to next or 0
|
||||
|
||||
virtual <T>& operator () (Pix i) = 0; // access item at i
|
||||
|
||||
virtual Pix seek(<T&> item, Pix from=0); // Pix of next occurrence
|
||||
virtual int owns(Pix i); // is i a valid Pix ?
|
||||
|
||||
void error(const char* msg);
|
||||
virtual int OK() = 0; // rep invariant
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T>Bag::~<T>Bag() {}
|
||||
|
||||
inline int <T>Bag::length()
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
inline int <T>Bag::empty()
|
||||
{
|
||||
return count == 0;
|
||||
}
|
||||
|
||||
inline int <T>Bag::contains(<T&> item)
|
||||
{
|
||||
return seek(item) != 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,214 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation
|
||||
#endif
|
||||
#include "<T>.CHBag.h"
|
||||
|
||||
// The nodes are linked together serially via a version
|
||||
// of a trick used in some vtables: odd pointers are
|
||||
// actually links to the next table entry.
|
||||
// Not terrible, but not wonderful either
|
||||
|
||||
static inline int goodCHptr(<T>CHNode* t)
|
||||
{
|
||||
return ((((unsigned)t) & 1) == 0);
|
||||
}
|
||||
|
||||
static inline <T>CHNode* index_to_CHptr(int i)
|
||||
{
|
||||
return (<T>CHNode*)((i << 1) + 1);
|
||||
}
|
||||
|
||||
static inline int CHptr_to_index(<T>CHNode* t)
|
||||
{
|
||||
return ( ((unsigned) t) >> 1);
|
||||
}
|
||||
|
||||
<T>CHBag::<T>CHBag(unsigned int sz)
|
||||
{
|
||||
tab = (<T>CHNode**)(new <T>CHNodePtr[size = sz]);
|
||||
for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
|
||||
count = 0;
|
||||
}
|
||||
|
||||
<T>CHBag::<T>CHBag(<T>CHBag& a)
|
||||
{
|
||||
tab = (<T>CHNode**)(new <T>CHNodePtr[size = a.size]);
|
||||
for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
|
||||
count = 0;
|
||||
for (Pix p = a.first(); p; a.next(p)) add(a(p));
|
||||
}
|
||||
|
||||
|
||||
Pix <T>CHBag::seek(<T&> key, Pix i)
|
||||
{
|
||||
<T>CHNode* p = (<T>CHNode*)i;
|
||||
if (p == 0 || !<T>EQ(p->hd, key))
|
||||
{
|
||||
unsigned int h = <T>HASH(key) % size;
|
||||
for (<T>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
|
||||
if (<T>EQ(key, t->hd))
|
||||
return Pix(t);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (p = p->tl; goodCHptr(p); p = p->tl)
|
||||
if (<T>EQ(p->hd, key))
|
||||
return Pix(p);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int <T>CHBag::nof(<T&> key)
|
||||
{
|
||||
int n = 0;
|
||||
unsigned int h = <T>HASH(key) % size;
|
||||
for (<T>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
|
||||
if (<T>EQ(key, t->hd)) ++n;
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
Pix <T>CHBag::add(<T&> item)
|
||||
{
|
||||
unsigned int h = <T>HASH(item) % size;
|
||||
<T>CHNode* t = new <T>CHNode(item);
|
||||
t->tl = tab[h];
|
||||
tab[h] = t;
|
||||
++count;
|
||||
return Pix(t);
|
||||
}
|
||||
|
||||
void <T>CHBag::del(<T&> key)
|
||||
{
|
||||
unsigned int h = <T>HASH(key) % size;
|
||||
|
||||
<T>CHNode* t = tab[h];
|
||||
<T>CHNode* trail = t;
|
||||
while (goodCHptr(t))
|
||||
{
|
||||
if (<T>EQ(key, t->hd))
|
||||
{
|
||||
if (trail == t)
|
||||
tab[h] = t->tl;
|
||||
else
|
||||
trail->tl = t->tl;
|
||||
delete t;
|
||||
--count;
|
||||
return;
|
||||
}
|
||||
trail = t;
|
||||
t = t->tl;
|
||||
}
|
||||
}
|
||||
|
||||
void <T>CHBag::remove(<T&> key)
|
||||
{
|
||||
unsigned int h = <T>HASH(key) % size;
|
||||
|
||||
<T>CHNode* t = tab[h];
|
||||
<T>CHNode* trail = t;
|
||||
while (goodCHptr(t))
|
||||
{
|
||||
if (<T>EQ(key, t->hd))
|
||||
{
|
||||
--count;
|
||||
if (trail == t)
|
||||
{
|
||||
tab[h] = t->tl;
|
||||
delete t;
|
||||
t = trail = tab[h];
|
||||
}
|
||||
else
|
||||
{
|
||||
trail->tl = t->tl;
|
||||
delete t;
|
||||
t = trail->tl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
trail = t;
|
||||
t = t->tl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void <T>CHBag::clear()
|
||||
{
|
||||
for (unsigned int i = 0; i < size; ++i)
|
||||
{
|
||||
<T>CHNode* p = tab[i];
|
||||
tab[i] = index_to_CHptr(i+1);
|
||||
while (goodCHptr(p))
|
||||
{
|
||||
<T>CHNode* nxt = p->tl;
|
||||
delete(p);
|
||||
p = nxt;
|
||||
}
|
||||
}
|
||||
count = 0;
|
||||
}
|
||||
|
||||
Pix <T>CHBag::first()
|
||||
{
|
||||
for (unsigned int i = 0; i < size; ++i) if (goodCHptr(tab[i])) return Pix(tab[i]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void <T>CHBag::next(Pix& p)
|
||||
{
|
||||
if (p == 0) return;
|
||||
<T>CHNode* t = ((<T>CHNode*)p)->tl;
|
||||
if (goodCHptr(t))
|
||||
p = Pix(t);
|
||||
else
|
||||
{
|
||||
for (unsigned int i = CHptr_to_index(t); i < size; ++i)
|
||||
{
|
||||
if (goodCHptr(tab[i]))
|
||||
{
|
||||
p = Pix(tab[i]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
p = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int <T>CHBag::OK()
|
||||
{
|
||||
int v = tab != 0;
|
||||
int n = 0;
|
||||
for (unsigned int i = 0; i < size; ++i)
|
||||
{
|
||||
for (<T>CHNode* p = tab[i]; goodCHptr(p); p = p->tl) ++n;
|
||||
v &= CHptr_to_index(p) == i + 1;
|
||||
}
|
||||
v &= count == n;
|
||||
if (!v) error("invariant failure");
|
||||
return v;
|
||||
}
|
|
@ -1,110 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _<T>CHBag_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _<T>CHBag_h 1
|
||||
|
||||
#include "<T>.Bag.h"
|
||||
|
||||
|
||||
#ifndef _<T>CHNode_h
|
||||
#define _<T>CHNode_h 1
|
||||
|
||||
struct <T>CHNode
|
||||
{
|
||||
<T>CHNode* tl;
|
||||
<T> hd;
|
||||
<T>CHNode();
|
||||
<T>CHNode(<T&> h, <T>CHNode* t = 0);
|
||||
~<T>CHNode();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T>CHNode::<T>CHNode() {}
|
||||
|
||||
inline <T>CHNode::<T>CHNode(<T&> h, <T>CHNode* t) :hd(h), tl(t) {}
|
||||
|
||||
inline <T>CHNode::~<T>CHNode() {}
|
||||
|
||||
#endif
|
||||
|
||||
typedef <T>CHNode* <T>CHNodePtr;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
class <T>CHBag : public <T>Bag
|
||||
{
|
||||
protected:
|
||||
<T>CHNode** tab;
|
||||
unsigned int size;
|
||||
|
||||
public:
|
||||
<T>CHBag(unsigned int sz = DEFAULT_INITIAL_CAPACITY);
|
||||
<T>CHBag(<T>CHBag& a);
|
||||
~<T>CHBag();
|
||||
|
||||
Pix add(<T&> item);
|
||||
void del(<T&> item);
|
||||
void remove(<T&>item);
|
||||
int nof(<T&> item);
|
||||
int contains(<T&> item);
|
||||
|
||||
void clear();
|
||||
|
||||
Pix first();
|
||||
void next(Pix& i);
|
||||
<T>& operator () (Pix i);
|
||||
Pix seek(<T&> item, Pix from = 0);
|
||||
|
||||
int OK();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T>CHBag::~<T>CHBag()
|
||||
{
|
||||
clear();
|
||||
delete tab;
|
||||
}
|
||||
|
||||
inline int <T>CHBag::contains(<T&> key)
|
||||
{
|
||||
return seek(key) != 0;
|
||||
}
|
||||
|
||||
inline <T>& <T>CHBag::operator () (Pix i)
|
||||
{
|
||||
if (i == 0) error("null Pix");
|
||||
return ((<T>CHNode*)i)->hd;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,171 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation
|
||||
#endif
|
||||
#include "<T>.<C>.CHMap.h"
|
||||
|
||||
// The nodes are linked together serially via a version
|
||||
// of a trick used in some vtables: odd pointers are
|
||||
// actually links to the next table entry.
|
||||
// Not terrible, but not wonderful either
|
||||
|
||||
static inline int goodCHptr(<T><C>CHNode* t)
|
||||
{
|
||||
return ((((unsigned)t) & 1) == 0);
|
||||
}
|
||||
|
||||
static inline <T><C>CHNode* index_to_CHptr(int i)
|
||||
{
|
||||
return (<T><C>CHNode*)((i << 1) + 1);
|
||||
}
|
||||
|
||||
static inline int CHptr_to_index(<T><C>CHNode* t)
|
||||
{
|
||||
return ( ((unsigned) t) >> 1);
|
||||
}
|
||||
|
||||
<T><C>CHMap::<T><C>CHMap(<C&> dflt, unsigned int sz)
|
||||
:<T><C>Map(dflt)
|
||||
{
|
||||
tab = (<T><C>CHNode**)(new <T><C>CHNodePtr[size = sz]);
|
||||
for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
|
||||
count = 0;
|
||||
}
|
||||
|
||||
<T><C>CHMap::<T><C>CHMap(<T><C>CHMap& a) :<T><C>Map(a.def)
|
||||
{
|
||||
tab = (<T><C>CHNode**)(new <T><C>CHNodePtr[size = a.size]);
|
||||
for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
|
||||
count = 0;
|
||||
for (Pix p = a.first(); p; a.next(p)) (*this)[a.key(p)] = a.contents(p);
|
||||
}
|
||||
|
||||
|
||||
Pix <T><C>CHMap::seek(<T&> key)
|
||||
{
|
||||
unsigned int h = <T>HASH(key) % size;
|
||||
|
||||
for (<T><C>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
|
||||
if (<T>EQ(key, t->hd))
|
||||
return Pix(t);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
<C>& <T><C>CHMap::operator [](<T&> item)
|
||||
{
|
||||
unsigned int h = <T>HASH(item) % size;
|
||||
|
||||
for (<T><C>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
|
||||
if (<T>EQ(item, t->hd))
|
||||
return t->cont;
|
||||
|
||||
t = new <T><C>CHNode(item, def, tab[h]);
|
||||
tab[h] = t;
|
||||
++count;
|
||||
return t->cont;
|
||||
}
|
||||
|
||||
|
||||
void <T><C>CHMap::del(<T&> key)
|
||||
{
|
||||
unsigned int h = <T>HASH(key) % size;
|
||||
|
||||
<T><C>CHNode* t = tab[h];
|
||||
<T><C>CHNode* trail = t;
|
||||
while (goodCHptr(t))
|
||||
{
|
||||
if (<T>EQ(key, t->hd))
|
||||
{
|
||||
if (trail == t)
|
||||
tab[h] = t->tl;
|
||||
else
|
||||
trail->tl = t->tl;
|
||||
delete t;
|
||||
--count;
|
||||
return;
|
||||
}
|
||||
trail = t;
|
||||
t = t->tl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void <T><C>CHMap::clear()
|
||||
{
|
||||
for (unsigned int i = 0; i < size; ++i)
|
||||
{
|
||||
<T><C>CHNode* p = tab[i];
|
||||
tab[i] = index_to_CHptr(i+1);
|
||||
while (goodCHptr(p))
|
||||
{
|
||||
<T><C>CHNode* nxt = p->tl;
|
||||
delete(p);
|
||||
p = nxt;
|
||||
}
|
||||
}
|
||||
count = 0;
|
||||
}
|
||||
|
||||
Pix <T><C>CHMap::first()
|
||||
{
|
||||
for (unsigned int i = 0; i < size; ++i) if (goodCHptr(tab[i])) return Pix(tab[i]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void <T><C>CHMap::next(Pix& p)
|
||||
{
|
||||
<T><C>CHNode* t = ((<T><C>CHNode*)p)->tl;
|
||||
if (goodCHptr(t))
|
||||
p = Pix(t);
|
||||
else
|
||||
{
|
||||
for (unsigned int i = CHptr_to_index(t); i < size; ++i)
|
||||
{
|
||||
if (goodCHptr(tab[i]))
|
||||
{
|
||||
p = Pix(tab[i]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
p = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int <T><C>CHMap::OK()
|
||||
{
|
||||
int v = tab != 0;
|
||||
int n = 0;
|
||||
for (unsigned int i = 0; i < size; ++i)
|
||||
{
|
||||
for (<T><C>CHNode* p = tab[i]; goodCHptr(p); p = p->tl) ++n;
|
||||
v &= CHptr_to_index(p) == i + 1;
|
||||
}
|
||||
v &= count == n;
|
||||
if (!v) error("invariant failure");
|
||||
return v;
|
||||
}
|
|
@ -1,118 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _<T><C>CHMap_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _<T><C>CHMap_h 1
|
||||
|
||||
#include "<T>.<C>.Map.h"
|
||||
|
||||
#ifndef _<T><C>CHNode_h
|
||||
#define _<T><C>CHNode_h 1
|
||||
|
||||
struct <T><C>CHNode
|
||||
{
|
||||
<T><C>CHNode* tl;
|
||||
<T> hd;
|
||||
<C> cont;
|
||||
<T><C>CHNode();
|
||||
<T><C>CHNode(<T&> h, <C&> c, <T><C>CHNode* t = 0);
|
||||
~<T><C>CHNode();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T><C>CHNode::<T><C>CHNode() {}
|
||||
|
||||
inline <T><C>CHNode::<T><C>CHNode(<T&> h, <C&> c, <T><C>CHNode* t)
|
||||
: hd(h), cont(c), tl(t) {}
|
||||
|
||||
inline <T><C>CHNode::~<T><C>CHNode() {}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
typedef <T><C>CHNode* <T><C>CHNodePtr;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
class <T><C>CHMap : public <T><C>Map
|
||||
{
|
||||
protected:
|
||||
<T><C>CHNode** tab;
|
||||
unsigned int size;
|
||||
|
||||
public:
|
||||
<T><C>CHMap(<C&> dflt,unsigned int sz=DEFAULT_INITIAL_CAPACITY);
|
||||
<T><C>CHMap(<T><C>CHMap& a);
|
||||
~<T><C>CHMap();
|
||||
|
||||
<C>& operator [] (<T&> key);
|
||||
|
||||
void del(<T&> key);
|
||||
|
||||
Pix first();
|
||||
void next(Pix& i);
|
||||
<T>& key(Pix i);
|
||||
<C>& contents(Pix i);
|
||||
|
||||
Pix seek(<T&> key);
|
||||
int contains(<T&> key);
|
||||
|
||||
void clear();
|
||||
int OK();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T><C>CHMap::~<T><C>CHMap()
|
||||
{
|
||||
clear();
|
||||
delete tab;
|
||||
}
|
||||
|
||||
inline int <T><C>CHMap::contains(<T&> key)
|
||||
{
|
||||
return seek(key) != 0;
|
||||
}
|
||||
|
||||
inline <T>& <T><C>CHMap::key(Pix p)
|
||||
{
|
||||
if (p == 0) error("null Pix");
|
||||
return ((<T><C>CHNode*)p)->hd;
|
||||
}
|
||||
|
||||
inline <C>& <T><C>CHMap::contents(Pix p)
|
||||
{
|
||||
if (p == 0) error("null Pix");
|
||||
return ((<T><C>CHNode*)p)->cont;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,276 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation
|
||||
#endif
|
||||
#include "<T>.CHSet.h"
|
||||
|
||||
// A CHSet is implemented as an array (tab) of buckets, each of which
|
||||
// contains a pointer to a list of <T>CHNodes. Each node contains a
|
||||
// pointer to the next node in the list, and a pointer to the <T>.
|
||||
// The end of the list is marked by a next node pointer which is odd
|
||||
// when considered as an integer (least significant bit = 1). The
|
||||
// assumption is that CHNodes will all begin on even addresses. If
|
||||
// the odd pointer is right-shifted by one bit, it becomes the index
|
||||
// within the tab array of the next bucket (that is, bucket i has
|
||||
// next bucket pointer 2*(i+1)+1).
|
||||
|
||||
// The bucket pointers are initialized by the constructor and
|
||||
// used to support the next(Pix&) method.
|
||||
|
||||
// This implementation is not portable to machines with different
|
||||
// pointer and integer sizes, or on which CHNodes might be aligned on
|
||||
// odd byte boundaries, but allows the same pointer to be used for
|
||||
// chaining within a bucket and to the next bucket.
|
||||
|
||||
|
||||
static inline int goodCHptr(<T>CHNode* t)
|
||||
{
|
||||
return ((((unsigned)t) & 1) == 0);
|
||||
}
|
||||
|
||||
static inline <T>CHNode* index_to_CHptr(int i)
|
||||
{
|
||||
return (<T>CHNode*)((i << 1) + 1);
|
||||
}
|
||||
|
||||
static inline int CHptr_to_index(<T>CHNode* t)
|
||||
{
|
||||
return ( ((unsigned) t) >> 1);
|
||||
}
|
||||
|
||||
<T>CHSet::<T>CHSet(unsigned int sz)
|
||||
{
|
||||
tab = (<T>CHNode**)(new <T>CHNodePtr[size = sz]);
|
||||
for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
|
||||
count = 0;
|
||||
}
|
||||
|
||||
<T>CHSet::<T>CHSet(<T>CHSet& a)
|
||||
{
|
||||
tab = (<T>CHNode**)(new <T>CHNodePtr[size = a.size]);
|
||||
for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
|
||||
count = 0;
|
||||
for (Pix p = a.first(); p; a.next(p)) add(a(p));
|
||||
}
|
||||
|
||||
|
||||
Pix <T>CHSet::seek(<T&> key)
|
||||
{
|
||||
unsigned int h = <T>HASH(key) % size;
|
||||
|
||||
for (<T>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
|
||||
if (<T>EQ(key, t->hd))
|
||||
return Pix(t);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Pix <T>CHSet::add(<T&> item)
|
||||
{
|
||||
unsigned int h = <T>HASH(item) % size;
|
||||
|
||||
for (<T>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
|
||||
if (<T>EQ(item, t->hd))
|
||||
return Pix(t);
|
||||
|
||||
++count;
|
||||
t = new <T>CHNode(item, tab[h]);
|
||||
tab[h] = t;
|
||||
return Pix(t);
|
||||
}
|
||||
|
||||
|
||||
void <T>CHSet::del(<T&> key)
|
||||
{
|
||||
unsigned int h = <T>HASH(key) % size;
|
||||
|
||||
<T>CHNode* t = tab[h];
|
||||
<T>CHNode* trail = t;
|
||||
while (goodCHptr(t))
|
||||
{
|
||||
if (<T>EQ(key, t->hd))
|
||||
{
|
||||
if (trail == t)
|
||||
tab[h] = t->tl;
|
||||
else
|
||||
trail->tl = t->tl;
|
||||
delete t;
|
||||
--count;
|
||||
return;
|
||||
}
|
||||
trail = t;
|
||||
t = t->tl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void <T>CHSet::clear()
|
||||
{
|
||||
for (unsigned int i = 0; i < size; ++i)
|
||||
{
|
||||
<T>CHNode* p = tab[i];
|
||||
tab[i] = index_to_CHptr(i+1);
|
||||
while (goodCHptr(p))
|
||||
{
|
||||
<T>CHNode* nxt = p->tl;
|
||||
delete(p);
|
||||
p = nxt;
|
||||
}
|
||||
}
|
||||
count = 0;
|
||||
}
|
||||
|
||||
Pix <T>CHSet::first()
|
||||
{
|
||||
for (unsigned int i = 0; i < size; ++i) if (goodCHptr(tab[i])) return Pix(tab[i]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void <T>CHSet::next(Pix& p)
|
||||
{
|
||||
if (p == 0) return;
|
||||
<T>CHNode* t = ((<T>CHNode*)p)->tl;
|
||||
if (goodCHptr(t))
|
||||
p = Pix(t);
|
||||
else
|
||||
{
|
||||
for (unsigned int i = CHptr_to_index(t); i < size; ++i)
|
||||
{
|
||||
if (goodCHptr(tab[i]))
|
||||
{
|
||||
p = Pix(tab[i]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
p = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int <T>CHSet::operator == (<T>CHSet& b)
|
||||
{
|
||||
if (count != b.count)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
<T>CHNode* p;
|
||||
for (unsigned int i = 0; i < size; ++i)
|
||||
for (p = tab[i]; goodCHptr(p); p = p->tl)
|
||||
if (b.seek(p->hd) == 0)
|
||||
return 0;
|
||||
for (i = 0; i < b.size; ++i)
|
||||
for (p = b.tab[i]; goodCHptr(p); p = p->tl)
|
||||
if (seek(p->hd) == 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int <T>CHSet::operator <= (<T>CHSet& b)
|
||||
{
|
||||
if (count > b.count)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
for (unsigned int i = 0; i < size; ++i)
|
||||
for (<T>CHNode* p = tab[i]; goodCHptr(p); p = p->tl)
|
||||
if (b.seek(p->hd) == 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void <T>CHSet::operator |= (<T>CHSet& b)
|
||||
{
|
||||
if (&b == this || b.count == 0)
|
||||
return;
|
||||
for (unsigned int i = 0; i < b.size; ++i)
|
||||
for (<T>CHNode* p = b.tab[i]; goodCHptr(p); p = p->tl)
|
||||
add(p->hd);
|
||||
}
|
||||
|
||||
void <T>CHSet::operator &= (<T>CHSet& b)
|
||||
{
|
||||
for (unsigned int i = 0; i < size; ++i)
|
||||
{
|
||||
<T>CHNode* t = tab[i];
|
||||
<T>CHNode* trail = t;
|
||||
while (goodCHptr(t))
|
||||
{
|
||||
<T>CHNode* nxt = t->tl;
|
||||
if (b.seek(t->hd) == 0)
|
||||
{
|
||||
if (trail == tab[i])
|
||||
trail = tab[i] = nxt;
|
||||
else
|
||||
trail->tl = nxt;
|
||||
delete t;
|
||||
--count;
|
||||
}
|
||||
else
|
||||
trail = t;
|
||||
t = nxt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void <T>CHSet::operator -= (<T>CHSet& b)
|
||||
{
|
||||
for (unsigned int i = 0; i < size; ++i)
|
||||
{
|
||||
<T>CHNode* t = tab[i];
|
||||
<T>CHNode* trail = t;
|
||||
while (goodCHptr(t))
|
||||
{
|
||||
<T>CHNode* nxt = t->tl;
|
||||
if (b.seek(t->hd) != 0)
|
||||
{
|
||||
if (trail == tab[i])
|
||||
trail = tab[i] = nxt;
|
||||
else
|
||||
trail->tl = nxt;
|
||||
delete t;
|
||||
--count;
|
||||
}
|
||||
else
|
||||
trail = t;
|
||||
t = nxt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int <T>CHSet::OK()
|
||||
{
|
||||
int v = tab != 0;
|
||||
int n = 0;
|
||||
for (unsigned int i = 0; i < size; ++i)
|
||||
{
|
||||
for (<T>CHNode* p = tab[i]; goodCHptr(p); p = p->tl) ++n;
|
||||
v &= CHptr_to_index(p) == i + 1;
|
||||
}
|
||||
v &= count == n;
|
||||
if (!v) error("invariant failure");
|
||||
return v;
|
||||
}
|
|
@ -1,121 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _<T>CHSet_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _<T>CHSet_h 1
|
||||
|
||||
#include "<T>.Set.h"
|
||||
|
||||
|
||||
#ifndef _<T>CHNode_h
|
||||
#define _<T>CHNode_h 1
|
||||
|
||||
struct <T>CHNode
|
||||
{
|
||||
<T>CHNode* tl;
|
||||
<T> hd;
|
||||
<T>CHNode();
|
||||
<T>CHNode(<T&> h, <T>CHNode* t = 0);
|
||||
~<T>CHNode();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T>CHNode::<T>CHNode() {}
|
||||
|
||||
inline <T>CHNode::<T>CHNode(<T&> h, <T>CHNode* t) : hd(h), tl(t) {}
|
||||
|
||||
inline <T>CHNode::~<T>CHNode() {}
|
||||
|
||||
#endif
|
||||
|
||||
typedef <T>CHNode* <T>CHNodePtr;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
class <T>CHSet : public <T>Set
|
||||
{
|
||||
protected:
|
||||
<T>CHNode** tab;
|
||||
unsigned int size;
|
||||
|
||||
public:
|
||||
<T>CHSet(unsigned int sz = DEFAULT_INITIAL_CAPACITY);
|
||||
<T>CHSet(<T>CHSet& a);
|
||||
~<T>CHSet();
|
||||
|
||||
Pix add(<T&> item);
|
||||
void del(<T&> item);
|
||||
int contains(<T&> item);
|
||||
|
||||
void clear();
|
||||
|
||||
Pix first();
|
||||
void next(Pix& i);
|
||||
<T>& operator () (Pix i);
|
||||
Pix seek(<T&> item);
|
||||
|
||||
void operator |= (<T>CHSet& b);
|
||||
void operator -= (<T>CHSet& b);
|
||||
void operator &= (<T>CHSet& b);
|
||||
|
||||
int operator == (<T>CHSet& b);
|
||||
int operator != (<T>CHSet& b);
|
||||
int operator <= (<T>CHSet& b);
|
||||
|
||||
int OK();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T>CHSet::~<T>CHSet()
|
||||
{
|
||||
clear();
|
||||
delete tab;
|
||||
}
|
||||
|
||||
inline int <T>CHSet::contains(<T&> key)
|
||||
{
|
||||
return seek(key) != 0;
|
||||
}
|
||||
|
||||
inline <T>& <T>CHSet::operator () (Pix i)
|
||||
{
|
||||
if (i == 0) error("null Pix");
|
||||
return ((<T>CHNode*)i)->hd;
|
||||
}
|
||||
|
||||
inline int <T>CHSet::operator != (<T>CHSet& b)
|
||||
{
|
||||
return ! ((*this) == b);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,4 +0,0 @@
|
|||
#ifdef __GNUG__
|
||||
#pragma implementation
|
||||
#endif
|
||||
#include "<T>.DLDeque.h"
|
|
@ -1,138 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _<T>DLDeque_h
|
||||
#ifdef __GNUG__
|
||||
#pragma once
|
||||
#pragma interface
|
||||
#endif
|
||||
#define _<T>DLDeque_h
|
||||
|
||||
#include "<T>.DLList.h"
|
||||
#include "<T>.Deque.h"
|
||||
|
||||
class <T>DLDeque : public <T>Deque
|
||||
{
|
||||
<T>DLList p;
|
||||
|
||||
public:
|
||||
<T>DLDeque();
|
||||
<T>DLDeque(const <T>DLDeque& d);
|
||||
~<T>DLDeque();
|
||||
|
||||
void operator = (const <T>DLDeque&);
|
||||
|
||||
void push(<T&> item); // insert at front
|
||||
void enq(<T&> item); // insert at rear
|
||||
|
||||
<T>& front();
|
||||
<T>& rear();
|
||||
|
||||
<T> deq();
|
||||
void del_front();
|
||||
void del_rear();
|
||||
|
||||
void clear();
|
||||
int empty();
|
||||
int full();
|
||||
int length();
|
||||
|
||||
int OK();
|
||||
};
|
||||
|
||||
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
|
||||
|
||||
inline <T>DLDeque::<T>DLDeque() : p() {}
|
||||
inline <T>DLDeque::<T>DLDeque(const <T>DLDeque& d) : p(d.p) {}
|
||||
|
||||
inline <T>DLDeque::~<T>DLDeque() {}
|
||||
|
||||
inline void <T>DLDeque::push(<T&>item)
|
||||
{
|
||||
p.prepend(item);
|
||||
}
|
||||
|
||||
inline void <T>DLDeque::enq(<T&>item)
|
||||
{
|
||||
p.append(item);
|
||||
}
|
||||
|
||||
inline <T> <T>DLDeque::deq()
|
||||
{
|
||||
return p.remove_front();
|
||||
}
|
||||
|
||||
inline <T>& <T>DLDeque::front()
|
||||
{
|
||||
return p.front();
|
||||
}
|
||||
|
||||
inline <T>& <T>DLDeque::rear()
|
||||
{
|
||||
return p.rear();
|
||||
}
|
||||
|
||||
inline void <T>DLDeque::del_front()
|
||||
{
|
||||
p.del_front();
|
||||
}
|
||||
|
||||
inline void <T>DLDeque::del_rear()
|
||||
{
|
||||
p.del_rear();
|
||||
}
|
||||
|
||||
inline void <T>DLDeque::operator =(const <T>DLDeque& s)
|
||||
{
|
||||
p.operator = (s.p);
|
||||
}
|
||||
|
||||
|
||||
inline int <T>DLDeque::empty()
|
||||
{
|
||||
return p.empty();
|
||||
}
|
||||
|
||||
inline int <T>DLDeque::full()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline int <T>DLDeque::length()
|
||||
{
|
||||
return p.length();
|
||||
}
|
||||
|
||||
inline int <T>DLDeque::OK()
|
||||
{
|
||||
return p.OK();
|
||||
}
|
||||
|
||||
inline void <T>DLDeque::clear()
|
||||
{
|
||||
p.clear();
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,317 +0,0 @@
|
|||
// This may look like C code, but it is really -*- C++ -*-
|
||||
/*
|
||||
Copyright (C) 1988 Free Software Foundation
|
||||
written by Doug Lea (dl@rocky.oswego.edu)
|
||||
|
||||
This file is part of GNU CC.
|
||||
|
||||
GNU CC is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY. No author or distributor
|
||||
accepts responsibility to anyone for the consequences of using it
|
||||
or for whether it serves any particular purpose or works at all,
|
||||
unless he says so in writing. Refer to the GNU CC General Public
|
||||
License for full details.
|
||||
|
||||
Everyone is granted permission to copy, modify and redistribute
|
||||
GNU CC, but only under the conditions described in the
|
||||
GNU CC General Public License. A copy of this license is
|
||||
supposed to have been given to you along with GNU CC so you
|
||||
can know your rights and responsibilities. It should be in a
|
||||
file named COPYING. Among other things, the copyright notice
|
||||
and this notice must be preserved on all copies.
|
||||
*/
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation
|
||||
#endif
|
||||
#include <values.h>
|
||||
#include <stream.h>
|
||||
#include "<T>.DLList.h"
|
||||
|
||||
// error handling
|
||||
|
||||
|
||||
|
||||
void <T>DLList::error(const char* msg)
|
||||
{
|
||||
(*lib_error_handler)("DLList", msg);
|
||||
}
|
||||
|
||||
int <T>DLList::length()
|
||||
{
|
||||
int l = 0;
|
||||
<T>DLListNode* t = h;
|
||||
if (t != 0) do { ++l; t = t->fd; } while (t != h);
|
||||
return l;
|
||||
}
|
||||
|
||||
<T>DLList::<T>DLList(<T>DLList& a)
|
||||
{
|
||||
if (a.h == 0)
|
||||
h = 0;
|
||||
else
|
||||
{
|
||||
<T>DLListNode* p = a.h;
|
||||
<T>DLListNode* t = new <T>DLListNode(p->hd);
|
||||
h = t;
|
||||
p = p->fd;
|
||||
while (p != a.h)
|
||||
{
|
||||
<T>DLListNode* n = new <T>DLListNode(p->hd);
|
||||
t->fd = n;
|
||||
n->bk = t;
|
||||
t = n;
|
||||
p = p->fd;
|
||||
}
|
||||
t->fd = h;
|
||||
h->bk = t;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
<T>DLList& <T>DLList::operator = (<T>DLList& a)
|
||||
{
|
||||
if (h != a.h)
|
||||
{
|
||||
clear();
|
||||
if (a.h != 0)
|
||||
{
|
||||
<T>DLListNode* p = a.h;
|
||||
<T>DLListNode* t = new <T>DLListNode(p->hd);
|
||||
h = t;
|
||||
p = p->fd;
|
||||
while (p != a.h)
|
||||
{
|
||||
<T>DLListNode* n = new <T>DLListNode(p->hd);
|
||||
t->fd = n;
|
||||
n->bk = t;
|
||||
t = n;
|
||||
p = p->fd;
|
||||
}
|
||||
t->fd = h;
|
||||
h->bk = t;
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void <T>DLList::clear()
|
||||
{
|
||||
if (h == 0)
|
||||
return;
|
||||
|
||||
<T>DLListNode* p = h->fd;
|
||||
h->fd = 0;
|
||||
h = 0;
|
||||
|
||||
while (p != 0)
|
||||
{
|
||||
<T>DLListNode* nxt = p->fd;
|
||||
delete(p);
|
||||
p = nxt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Pix <T>DLList::prepend(<T&> item)
|
||||
{
|
||||
<T>DLListNode* t = new <T>DLListNode(item);
|
||||
if (h == 0)
|
||||
t->fd = t->bk = h = t;
|
||||
else
|
||||
{
|
||||
t->fd = h;
|
||||
t->bk = h->bk;
|
||||
h->bk->fd = t;
|
||||
h->bk = t;
|
||||
h = t;
|
||||
}
|
||||
return Pix(t);
|
||||
}
|
||||
|
||||
Pix <T>DLList::append(<T&> item)
|
||||
{
|
||||
<T>DLListNode* t = new <T>DLListNode(item);
|
||||
if (h == 0)
|
||||
t->fd = t->bk = h = t;
|
||||
else
|
||||
{
|
||||
t->bk = h->bk;
|
||||
t->bk->fd = t;
|
||||
t->fd = h;
|
||||
h->bk = t;
|
||||
}
|
||||
return Pix(t);
|
||||
}
|
||||
|
||||
Pix <T>DLList::ins_after(Pix p, <T&> item)
|
||||
{
|
||||
if (p == 0) return prepend(item);
|
||||
<T>DLListNode* u = (<T>DLListNode*) p;
|
||||
<T>DLListNode* t = new <T>DLListNode(item, u, u->fd);
|
||||
u->fd->bk = t;
|
||||
u->fd = t;
|
||||
return Pix(t);
|
||||
}
|
||||
|
||||
Pix <T>DLList::ins_before(Pix p, <T&> item)
|
||||
{
|
||||
if (p == 0) error("null Pix");
|
||||
<T>DLListNode* u = (<T>DLListNode*) p;
|
||||
<T>DLListNode* t = new <T>DLListNode(item, u->bk, u);
|
||||
u->bk->fd = t;
|
||||
u->bk = t;
|
||||
if (u == h) h = t;
|
||||
return Pix(t);
|
||||
}
|
||||
|
||||
void <T>DLList::join(<T>DLList& b)
|
||||
{
|
||||
<T>DLListNode* t = b.h;
|
||||
b.h = 0;
|
||||
if (h == 0)
|
||||
h = t;
|
||||
else if (t != 0)
|
||||
{
|
||||
<T>DLListNode* l = t->bk;
|
||||
h->bk->fd = t;
|
||||
t->bk = h->bk;
|
||||
h->bk = l;
|
||||
l->fd = h;
|
||||
}
|
||||
}
|
||||
|
||||
int <T>DLList::owns(Pix p)
|
||||
{
|
||||
<T>DLListNode* t = h;
|
||||
if (t != 0 && p != 0)
|
||||
{
|
||||
do
|
||||
{
|
||||
if (Pix(t) == p) return 1;
|
||||
t = t->fd;
|
||||
} while (t != h);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void <T>DLList::del(Pix& p, int dir)
|
||||
{
|
||||
if (p == 0) error("null Pix");
|
||||
<T>DLListNode* t = (<T>DLListNode*) p;
|
||||
if (t->fd == t)
|
||||
{
|
||||
h = 0;
|
||||
p = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (dir < 0)
|
||||
{
|
||||
if (t == h)
|
||||
p = 0;
|
||||
else
|
||||
p = Pix(t->bk);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (t == h->bk)
|
||||
p = 0;
|
||||
else
|
||||
p = Pix(t->fd);
|
||||
}
|
||||
t->bk->fd = t->fd;
|
||||
t->fd->bk = t->bk;
|
||||
if (t == h) h = t->fd;
|
||||
}
|
||||
delete t;
|
||||
}
|
||||
|
||||
<T> <T>DLList::remove_front()
|
||||
{
|
||||
if (h == 0)
|
||||
error("remove_front of empty list");
|
||||
<T>DLListNode* t = h;
|
||||
<T> res = t->hd;
|
||||
if (h->fd == h)
|
||||
h = 0;
|
||||
else
|
||||
{
|
||||
h->fd->bk = h->bk;
|
||||
h->bk->fd = h->fd;
|
||||
h = h->fd;
|
||||
}
|
||||
delete t;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
void <T>DLList::del_front()
|
||||
{
|
||||
if (h == 0)
|
||||
error("del_front of empty list");
|
||||
<T>DLListNode* t = h;
|
||||
if (h->fd == h)
|
||||
h = 0;
|
||||
else
|
||||
{
|
||||
h->fd->bk = h->bk;
|
||||
h->bk->fd = h->fd;
|
||||
h = h->fd;
|
||||
}
|
||||
delete t;
|
||||
}
|
||||
|
||||
<T> <T>DLList::remove_rear()
|
||||
{
|
||||
if (h == 0)
|
||||
error("remove_rear of empty list");
|
||||
<T>DLListNode* t = h->bk;
|
||||
<T> res = t->hd;
|
||||
if (h->fd == h)
|
||||
h = 0;
|
||||
else
|
||||
{
|
||||
t->fd->bk = t->bk;
|
||||
t->bk->fd = t->fd;
|
||||
}
|
||||
delete t;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
void <T>DLList::del_rear()
|
||||
{
|
||||
if (h == 0)
|
||||
error("del_rear of empty list");
|
||||
<T>DLListNode* t = h->bk;
|
||||
if (h->fd == h)
|
||||
h = 0;
|
||||
else
|
||||
{
|
||||
t->fd->bk = t->bk;
|
||||
t->bk->fd = t->fd;
|
||||
}
|
||||
delete t;
|
||||
}
|
||||
|
||||
|
||||
int <T>DLList::OK()
|
||||
{
|
||||
int v = 1;
|
||||
if (h != 0)
|
||||
{
|
||||
<T>DLListNode* t = h;
|
||||
long count = MAXLONG; // Lots of chances to find h!
|
||||
do
|
||||
{
|
||||
count--;
|
||||
v &= t->bk->fd == t;
|
||||
v &= t->fd->bk == t;
|
||||
t = t->fd;
|
||||
} while (v && count > 0 && t != h);
|
||||
v &= count > 0;
|
||||
}
|
||||
if (!v) error("invariant failure");
|
||||
return v;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue