Artifact c1c41296e0eaea11f16f1490d5a46be138e6baf5:
File
src/bag.c
part of check-in
[e15fe43153]
- A new decendent finding algorithm is (hopefully) faster. Changes to
the timeline are in process and might not yet work.
by
drh on
2007-08-31 20:14:33.
Also file
src/bag.c
part of check-in
[bbcb6326c9]
- Pulled in the navbar and timeline changes.
by
aku on
2007-09-17 00:58:51.
0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20 /*.** Copyright
0010: 28 63 29 20 32 30 30 37 20 44 2e 20 52 69 63 68 (c) 2007 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54 ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66 his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75 u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20 te it and/or.**
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20 modify it under
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76 lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65 ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73 s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20 tributed in the
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20 l be useful,.**
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20 but WITHOUT ANY
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75 WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69 t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54 * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52 Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55 A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20 RPOSE. See the
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50 GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 d have received
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72 g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69 ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65 te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20 ation, Inc., 59
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53 Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73 uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31 ton, MA 02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20 307, USA..**.**
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69 Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20 nformation:.**
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68 * http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 contains code u
0380: 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 sed to implement
0390: 20 61 20 22 62 61 67 22 20 6f 66 20 69 6e 74 65 a "bag" of inte
03a0: 67 65 72 73 2e 0a 2a 2a 20 41 20 62 61 67 20 69 gers..** A bag i
03b0: 73 20 61 6e 20 75 6e 6f 72 64 65 72 65 64 20 63 s an unordered c
03c0: 6f 6c 6c 65 63 74 69 6f 6e 20 77 69 74 68 6f 75 ollection withou
03d0: 74 20 64 75 70 6c 69 63 61 74 65 73 2e 20 20 49 t duplicates. I
03e0: 6e 20 74 68 69 73 0a 2a 2a 20 69 6d 70 6c 65 6d n this.** implem
03f0: 65 6e 74 61 74 69 6f 6e 2c 20 61 6c 6c 20 65 6c entation, all el
0400: 65 6d 65 6e 74 73 20 6d 75 73 74 20 62 65 20 70 ements must be p
0410: 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 73 ositive integers
0420: 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 63 ..*/.#include "c
0430: 6f 6e 66 69 67 2e 68 22 0a 23 69 6e 63 6c 75 64 onfig.h".#includ
0440: 65 20 22 62 61 67 2e 68 22 0a 23 69 6e 63 6c 75 e "bag.h".#inclu
0450: 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a 0a 0a de <assert.h>...
0460: 23 69 66 20 49 4e 54 45 52 46 41 43 45 0a 2f 2a #if INTERFACE./*
0470: 0a 2a 2a 20 41 6e 20 69 6e 74 65 67 65 72 20 63 .** An integer c
0480: 61 6e 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 an appear in the
0490: 20 62 61 67 20 61 74 20 6d 6f 73 74 20 6f 6e 63 bag at most onc
04a0: 65 2e 0a 2a 2a 20 49 6e 74 65 67 65 72 73 20 6d e..** Integers m
04b0: 75 73 74 20 62 65 20 70 6f 73 69 74 69 76 65 2e ust be positive.
04c0: 0a 2a 2f 0a 73 74 72 75 63 74 20 42 61 67 20 7b .*/.struct Bag {
04d0: 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 2f 2a . int cnt; /*
04e0: 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e 74 65 67 Number of integ
04f0: 65 72 73 20 69 6e 20 74 68 65 20 62 61 67 20 2a ers in the bag *
0500: 2f 0a 20 20 69 6e 74 20 73 7a 3b 20 20 20 20 2f /. int sz; /
0510: 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 * Number of slot
0520: 73 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20 20 69 6e s in a[] */. in
0530: 74 20 75 73 65 64 3b 20 20 2f 2a 20 4e 75 6d 62 t used; /* Numb
0540: 65 72 20 6f 66 20 75 73 65 64 20 73 6c 6f 74 73 er of used slots
0550: 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 in a[] */. int
0560: 20 2a 61 3b 20 20 20 20 2f 2a 20 48 61 73 68 20 *a; /* Hash
0570: 74 61 62 6c 65 20 6f 66 20 69 6e 74 65 67 65 72 table of integer
0580: 73 20 74 68 61 74 20 61 72 65 20 69 6e 20 74 68 s that are in th
0590: 65 20 62 61 67 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 e bag */.};.#end
05a0: 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 if../*.** Initia
05b0: 6c 69 7a 65 20 61 20 42 61 67 20 73 74 72 75 63 lize a Bag struc
05c0: 74 75 72 65 0a 2a 2f 0a 76 6f 69 64 20 62 61 67 ture.*/.void bag
05d0: 5f 69 6e 69 74 28 42 61 67 20 2a 70 29 7b 0a 20 _init(Bag *p){.
05e0: 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 memset(p, 0, si
05f0: 7a 65 6f 66 28 2a 70 29 29 3b 0a 7d 0a 0a 2f 2a zeof(*p));.}../*
0600: 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 42 61 .** Destroy a Ba
0610: 67 2e 20 20 44 65 6c 65 74 65 20 61 6c 6c 20 6f g. Delete all o
0620: 66 20 69 74 73 20 63 6f 6e 74 65 6e 74 2e 0a 2a f its content..*
0630: 2f 0a 76 6f 69 64 20 62 61 67 5f 63 6c 65 61 72 /.void bag_clear
0640: 28 42 61 67 20 2a 70 29 7b 0a 20 20 66 72 65 65 (Bag *p){. free
0650: 28 70 2d 3e 61 29 3b 0a 20 20 62 61 67 5f 69 6e (p->a);. bag_in
0660: 69 74 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 it(p);.}../*.**
0670: 54 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f The hash functio
0680: 6e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 62 61 67 n.*/.#define bag
0690: 5f 68 61 73 68 28 69 29 20 20 28 69 2a 31 30 31 _hash(i) (i*101
06a0: 29 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 )../*.** Change
06b0: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
06c0: 68 61 73 68 20 74 61 62 6c 65 20 6f 6e 20 61 20 hash table on a
06d0: 62 61 67 20 73 6f 20 74 68 61 74 0a 2a 2a 20 69 bag so that.** i
06e0: 74 20 63 6f 6e 74 61 69 6e 73 20 4e 20 73 6c 6f t contains N slo
06f0: 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ts.*/.static voi
0700: 64 20 62 61 67 5f 72 65 73 69 7a 65 28 42 61 67 d bag_resize(Bag
0710: 20 2a 70 2c 20 69 6e 74 20 6e 65 77 53 69 7a 65 *p, int newSize
0720: 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 61 ){. int i;. Ba
0730: 67 20 6f 6c 64 3b 0a 0a 20 20 6f 6c 64 20 3d 20 g old;.. old =
0740: 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 65 *p;. assert( ne
0750: 77 53 69 7a 65 3e 6f 6c 64 2e 63 6e 74 20 29 3b wSize>old.cnt );
0760: 0a 20 20 70 2d 3e 61 20 3d 20 6d 61 6c 6c 6f 63 . p->a = malloc
0770: 28 20 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d ( sizeof(p->a[0]
0780: 29 2a 6e 65 77 53 69 7a 65 20 29 3b 0a 20 20 70 )*newSize );. p
0790: 2d 3e 73 7a 20 3d 20 6e 65 77 53 69 7a 65 3b 0a ->sz = newSize;.
07a0: 20 20 6d 65 6d 73 65 74 28 70 2d 3e 61 2c 20 30 memset(p->a, 0
07b0: 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d , sizeof(p->a[0]
07c0: 29 2a 6e 65 77 53 69 7a 65 20 29 3b 0a 20 20 66 )*newSize );. f
07d0: 6f 72 28 69 3d 30 3b 20 69 3c 6f 6c 64 2e 73 7a or(i=0; i<old.sz
07e0: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 ; i++){. int
07f0: 65 20 3d 20 6f 6c 64 2e 61 5b 69 5d 3b 0a 20 20 e = old.a[i];.
0800: 20 20 69 66 28 20 65 3e 30 20 29 7b 0a 20 20 20 if( e>0 ){.
0810: 20 20 20 75 6e 73 69 67 6e 65 64 20 68 20 3d 20 unsigned h =
0820: 62 61 67 5f 68 61 73 68 28 65 29 25 6e 65 77 53 bag_hash(e)%newS
0830: 69 7a 65 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 ize;. while
0840: 28 20 70 2d 3e 61 5b 68 5d 20 29 7b 0a 20 20 20 ( p->a[h] ){.
0850: 20 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 20 20 h++;.
0860: 20 20 69 66 28 20 68 3d 3d 6e 65 77 53 69 7a 65 if( h==newSize
0870: 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 ) h = 0;.
0880: 7d 0a 20 20 20 20 20 20 70 2d 3e 61 5b 68 5d 20 }. p->a[h]
0890: 3d 20 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 = e;. }. }.
08a0: 20 70 2d 3e 75 73 65 64 20 3d 20 70 2d 3e 63 6e p->used = p->cn
08b0: 74 3b 0a 20 20 62 61 67 5f 63 6c 65 61 72 28 26 t;. bag_clear(&
08c0: 6f 6c 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 old);.}../*.** I
08d0: 6e 73 65 72 74 20 65 6c 65 6d 65 6e 74 20 65 20 nsert element e
08e0: 69 6e 74 6f 20 74 68 65 20 62 61 67 20 69 66 20 into the bag if
08f0: 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 it is not there
0900: 61 6c 72 65 61 64 79 2e 0a 2a 2a 20 52 65 74 75 already..** Retu
0910: 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 69 rn TRUE if the i
0920: 6e 73 65 72 74 20 61 63 74 75 61 6c 6c 79 20 6f nsert actually o
0930: 63 63 75 72 72 65 64 2e 20 20 52 65 74 75 72 6e ccurred. Return
0940: 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20 74 68 65 FALSE.** if the
0950: 20 65 6c 65 6d 65 6e 74 20 77 61 73 20 61 6c 72 element was alr
0960: 65 61 64 79 20 69 6e 20 74 68 65 20 62 61 67 2e eady in the bag.
0970: 0a 2a 2f 0a 69 6e 74 20 62 61 67 5f 69 6e 73 65 .*/.int bag_inse
0980: 72 74 28 42 61 67 20 2a 70 2c 20 69 6e 74 20 65 rt(Bag *p, int e
0990: 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 68 3b ){. unsigned h;
09a0: 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 . int rc = 0;.
09b0: 20 61 73 73 65 72 74 28 20 65 3e 30 20 29 3b 0a assert( e>0 );.
09c0: 20 20 69 66 28 20 70 2d 3e 75 73 65 64 2b 31 20 if( p->used+1
09d0: 3e 3d 20 70 2d 3e 73 7a 2f 32 20 29 7b 0a 20 20 >= p->sz/2 ){.
09e0: 20 20 62 61 67 5f 72 65 73 69 7a 65 28 70 2c 20 bag_resize(p,
09f0: 20 70 2d 3e 63 6e 74 2a 32 20 2b 20 32 30 20 29 p->cnt*2 + 20 )
0a00: 3b 0a 20 20 7d 0a 20 20 68 20 3d 20 62 61 67 5f ;. }. h = bag_
0a10: 68 61 73 68 28 65 29 25 70 2d 3e 73 7a 3b 0a 20 hash(e)%p->sz;.
0a20: 20 77 68 69 6c 65 28 20 70 2d 3e 61 5b 68 5d 20 while( p->a[h]
0a30: 26 26 20 70 2d 3e 61 5b 68 5d 21 3d 65 20 29 7b && p->a[h]!=e ){
0a40: 0a 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69 66 . h++;. if
0a50: 28 20 68 3e 3d 70 2d 3e 73 7a 20 29 20 68 20 3d ( h>=p->sz ) h =
0a60: 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 0;. }. if( p-
0a70: 3e 61 5b 68 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 >a[h]==0 ){.
0a80: 70 2d 3e 61 5b 68 5d 20 3d 20 65 3b 0a 20 20 20 p->a[h] = e;.
0a90: 20 70 2d 3e 75 73 65 64 2b 2b 3b 0a 20 20 20 20 p->used++;.
0aa0: 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 20 20 72 63 p->cnt++;. rc
0ab0: 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 1;. }. retu
0ac0: 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
0ad0: 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 65 Return true if e
0ae0: 20 69 6e 20 74 68 65 20 62 61 67 2e 20 20 52 65 in the bag. Re
0af0: 74 75 72 6e 20 66 61 6c 73 65 20 69 66 20 69 74 turn false if it
0b00: 20 69 73 20 6e 6f 2e 0a 2a 2f 0a 69 6e 74 20 62 is no..*/.int b
0b10: 61 67 5f 66 69 6e 64 28 42 61 67 20 2a 70 2c 20 ag_find(Bag *p,
0b20: 69 6e 74 20 65 29 7b 0a 20 20 75 6e 73 69 67 6e int e){. unsign
0b30: 65 64 20 68 3b 0a 20 20 61 73 73 65 72 74 28 20 ed h;. assert(
0b40: 65 3e 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e e>0 );. if( p->
0b50: 73 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 sz==0 ){. ret
0b60: 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 68 20 3d urn 0;. }. h =
0b70: 20 62 61 67 5f 68 61 73 68 28 65 29 25 70 2d 3e bag_hash(e)%p->
0b80: 73 7a 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e sz;. while( p->
0b90: 61 5b 68 5d 20 26 26 20 70 2d 3e 61 5b 68 5d 21 a[h] && p->a[h]!
0ba0: 3d 65 20 29 7b 0a 20 20 20 20 68 2b 2b 3b 0a 20 =e ){. h++;.
0bb0: 20 20 20 69 66 28 20 68 3e 3d 70 2d 3e 73 7a 20 if( h>=p->sz
0bc0: 29 20 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 ) h = 0;. }. r
0bd0: 65 74 75 72 6e 20 70 2d 3e 61 5b 68 5d 3d 3d 65 eturn p->a[h]==e
0be0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 ;.}../*.** Remov
0bf0: 65 20 65 6c 65 6d 65 6e 74 20 65 20 66 72 6f 6d e element e from
0c00: 20 74 68 65 20 62 61 67 20 69 66 20 69 74 20 65 the bag if it e
0c10: 78 69 73 74 73 20 69 6e 20 74 68 65 20 62 61 67 xists in the bag
0c20: 2e 0a 2a 2a 20 49 66 20 65 20 69 73 20 6e 6f 74 ..** If e is not
0c30: 20 69 6e 20 74 68 65 20 62 61 67 2c 20 74 68 69 in the bag, thi
0c40: 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f s is a no-op..*/
0c50: 0a 76 6f 69 64 20 62 61 67 5f 72 65 6d 6f 76 65 .void bag_remove
0c60: 28 42 61 67 20 2a 70 2c 20 69 6e 74 20 65 29 7b (Bag *p, int e){
0c70: 0a 20 20 75 6e 73 69 67 6e 65 64 20 68 3b 0a 20 . unsigned h;.
0c80: 20 61 73 73 65 72 74 28 20 65 3e 30 20 29 3b 0a assert( e>0 );.
0c90: 20 20 69 66 28 20 70 2d 3e 73 7a 3d 3d 30 20 29 if( p->sz==0 )
0ca0: 20 72 65 74 75 72 6e 3b 0a 20 20 68 20 3d 20 62 return;. h = b
0cb0: 61 67 5f 68 61 73 68 28 65 29 25 70 2d 3e 73 7a ag_hash(e)%p->sz
0cc0: 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 61 5b ;. while( p->a[
0cd0: 68 5d 20 26 26 20 70 2d 3e 61 5b 68 5d 21 3d 65 h] && p->a[h]!=e
0ce0: 20 29 7b 0a 20 20 20 20 68 2b 2b 3b 0a 20 20 20 ){. h++;.
0cf0: 20 69 66 28 20 68 3e 3d 70 2d 3e 73 7a 20 29 20 if( h>=p->sz )
0d00: 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 h = 0;. }. if(
0d10: 20 70 2d 3e 61 5b 68 5d 20 29 7b 0a 20 20 20 20 p->a[h] ){.
0d20: 70 2d 3e 61 5b 68 5d 20 3d 20 2d 31 3b 0a 20 20 p->a[h] = -1;.
0d30: 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 20 20 p->cnt--;.
0d40: 69 66 28 20 70 2d 3e 73 7a 3e 32 30 20 26 26 20 if( p->sz>20 &&
0d50: 70 2d 3e 63 6e 74 3c 70 2d 3e 73 7a 2f 38 20 29 p->cnt<p->sz/8 )
0d60: 7b 0a 20 20 20 20 20 20 62 61 67 5f 72 65 73 69 {. bag_resi
0d70: 7a 65 28 70 2c 20 70 2d 3e 73 7a 2f 32 29 3b 0a ze(p, p->sz/2);.
0d80: 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
0d90: 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 69 ** Return the fi
0da0: 72 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 rst element in t
0db0: 68 65 20 62 61 67 2e 20 20 52 65 74 75 72 6e 20 he bag. Return
0dc0: 30 20 69 66 20 74 68 65 20 62 61 67 0a 2a 2a 20 0 if the bag.**
0dd0: 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 69 6e 74 is empty..*/.int
0de0: 20 62 61 67 5f 66 69 72 73 74 28 42 61 67 20 2a bag_first(Bag *
0df0: 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 p){. int i;. f
0e00: 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 73 7a 20 or(i=0; i<p->sz
0e10: 26 26 20 70 2d 3e 61 5b 69 5d 3c 3d 30 3b 20 69 && p->a[i]<=0; i
0e20: 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69 3c 70 2d ++){}. if( i<p-
0e30: 3e 73 7a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 >sz ){. retur
0e40: 6e 20 70 2d 3e 61 5b 69 5d 3b 0a 20 20 7d 65 6c n p->a[i];. }el
0e50: 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 se{. return 0
0e60: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ;. }.}../*.** R
0e70: 65 74 75 72 6e 20 74 68 65 20 6e 65 78 74 20 65 eturn the next e
0e80: 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 62 61 lement in the ba
0e90: 67 20 61 66 74 65 72 20 65 2e 20 20 52 65 74 75 g after e. Retu
0ea0: 72 6e 20 30 20 69 66 0a 2a 2a 20 69 73 20 74 68 rn 0 if.** is th
0eb0: 65 20 6c 61 73 74 20 65 6c 65 6d 65 6e 74 20 69 e last element i
0ec0: 6e 20 74 68 65 20 62 61 67 2e 20 20 41 6e 79 20 n the bag. Any
0ed0: 69 6e 73 65 72 74 20 6f 72 20 72 65 6d 6f 76 61 insert or remova
0ee0: 6c 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62 61 l from.** the ba
0ef0: 67 20 6d 69 67 68 74 20 72 65 6f 72 64 65 72 20 g might reorder
0f00: 74 68 65 20 62 61 67 2e 0a 2a 2f 0a 69 6e 74 20 the bag..*/.int
0f10: 62 61 67 5f 6e 65 78 74 28 42 61 67 20 2a 70 2c bag_next(Bag *p,
0f20: 20 69 6e 74 20 65 29 7b 0a 20 20 75 6e 73 69 67 int e){. unsig
0f30: 6e 65 64 20 68 3b 0a 20 20 61 73 73 65 72 74 28 ned h;. assert(
0f40: 20 70 2d 3e 73 7a 3e 30 20 29 3b 0a 20 20 61 73 p->sz>0 );. as
0f50: 73 65 72 74 28 20 65 3e 30 20 29 3b 0a 20 20 68 sert( e>0 );. h
0f60: 20 3d 20 62 61 67 5f 68 61 73 68 28 65 29 25 70 = bag_hash(e)%p
0f70: 2d 3e 73 7a 3b 0a 20 20 77 68 69 6c 65 28 20 70 ->sz;. while( p
0f80: 2d 3e 61 5b 68 5d 20 26 26 20 70 2d 3e 61 5b 68 ->a[h] && p->a[h
0f90: 5d 21 3d 65 20 29 7b 0a 20 20 20 20 68 2b 2b 3b ]!=e ){. h++;
0fa0: 0a 20 20 20 20 69 66 28 20 68 3e 3d 70 2d 3e 73 . if( h>=p->s
0fb0: 7a 20 29 20 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 z ) h = 0;. }.
0fc0: 20 61 73 73 65 72 74 28 20 70 2d 3e 61 5b 68 5d assert( p->a[h]
0fd0: 20 29 3b 0a 20 20 68 2b 2b 3b 0a 20 20 77 68 69 );. h++;. whi
0fe0: 6c 65 28 20 68 3c 70 2d 3e 73 7a 20 26 26 20 70 le( h<p->sz && p
0ff0: 2d 3e 61 5b 68 5d 3c 3d 30 20 29 7b 0a 20 20 20 ->a[h]<=0 ){.
1000: 20 68 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 h++;. }. retu
1010: 72 6e 20 68 3c 70 2d 3e 73 7a 20 3f 20 70 2d 3e rn h<p->sz ? p->
1020: 61 5b 68 5d 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a a[h] : 0;.}../*.
1030: 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1040: 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 mber of elements
1050: 20 69 6e 20 74 68 65 20 62 61 67 2e 0a 2a 2f 0a in the bag..*/.
1060: 69 6e 74 20 62 61 67 5f 63 6f 75 6e 74 28 42 61 int bag_count(Ba
1070: 67 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 g *p){. return
1080: 70 2d 3e 63 6e 74 3b 0a 7d 0a p->cnt;.}.