Artifact c849b6ba261f655c2a5840e14d0be53df1a523da:
File
src/pqueue.c
part of check-in
[abce5105e2]
- Updates to the timeline routines. Added the pqueue module.
by
drh on
2007-09-01 01:55:50.
Also file
src/pqueue.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 70 72 69 6f 72 69 74 79 20 71 75 65 75 a priority queu
03a0: 65 2e 0a 2a 2a 20 41 20 70 72 69 6f 72 69 74 79 e..** A priority
03b0: 20 71 75 65 75 65 20 69 73 20 61 20 6c 69 73 74 queue is a list
03c0: 20 6f 66 20 69 74 65 6d 73 20 6f 72 64 65 72 20 of items order
03d0: 62 79 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f by a floating po
03e0: 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 57 int.** value. W
03f0: 65 20 63 61 6e 20 69 6e 73 65 72 74 20 69 6e 74 e can insert int
0400: 65 67 65 72 73 20 77 69 74 68 20 65 61 63 68 20 egers with each
0410: 69 6e 74 65 67 65 72 20 74 69 65 64 20 74 6f 20 integer tied to
0420: 69 74 73 0a 2a 2a 20 76 61 6c 75 65 20 74 68 65 its.** value the
0430: 6e 20 65 78 74 72 61 63 74 20 74 68 65 20 69 6e n extract the in
0440: 74 65 67 65 72 20 77 69 74 68 20 74 68 65 20 73 teger with the s
0450: 6d 61 6c 6c 65 73 74 20 76 61 6c 75 65 2e 0a 2a mallest value..*
0460: 2a 0a 2a 2a 20 54 68 65 20 77 61 79 20 74 68 69 *.** The way thi
0470: 73 20 71 75 65 75 65 20 69 73 20 75 73 65 64 2c s queue is used,
0480: 20 77 65 20 6e 65 76 65 72 20 65 78 70 65 63 74 we never expect
0490: 20 69 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 6d it to contain m
04a0: 6f 72 65 0a 2a 2a 20 74 68 61 6e 20 32 20 6f 72 ore.** than 2 or
04b0: 20 33 20 65 6c 65 6d 65 6e 74 73 2c 20 73 6f 20 3 elements, so
04c0: 61 20 73 69 6d 70 6c 65 20 61 72 72 61 79 20 69 a simple array i
04d0: 73 20 73 75 66 66 69 63 69 65 6e 74 20 61 73 20 s sufficient as
04e0: 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 the.** implement
04f0: 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 63 6f 75 ation. This cou
0500: 6c 64 20 67 69 76 65 20 77 6f 72 73 74 20 63 61 ld give worst ca
0510: 73 65 20 4f 28 4e 29 20 69 6e 73 65 72 74 20 74 se O(N) insert t
0520: 69 6d 65 73 2c 0a 2a 2a 20 62 75 74 20 62 65 63 imes,.** but bec
0530: 61 75 73 65 20 6f 66 20 74 68 65 20 6e 61 74 75 ause of the natu
0540: 72 65 20 6f 66 20 74 68 65 20 70 72 6f 62 6c 65 re of the proble
0550: 6d 20 77 65 20 65 78 70 65 63 74 20 4f 28 31 29 m we expect O(1)
0560: 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 0a 2a 2f performance..*/
0570: 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f 6e 66 69 .#include "confi
0580: 67 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 22 70 g.h".#include "p
0590: 71 75 65 75 65 2e 68 22 0a 23 69 6e 63 6c 75 64 queue.h".#includ
05a0: 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a 0a 0a 23 e <assert.h>...#
05b0: 69 66 20 49 4e 54 45 52 46 41 43 45 0a 2f 2a 0a if INTERFACE./*.
05c0: 2a 2a 20 41 6e 20 69 6e 74 65 67 65 72 20 63 61 ** An integer ca
05d0: 6e 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 n appear in the
05e0: 62 61 67 20 61 74 20 6d 6f 73 74 20 6f 6e 63 65 bag at most once
05f0: 2e 0a 2a 2a 20 49 6e 74 65 67 65 72 73 20 6d 75 ..** Integers mu
0600: 73 74 20 62 65 20 70 6f 73 69 74 69 76 65 2e 0a st be positive..
0610: 2a 2f 0a 73 74 72 75 63 74 20 50 51 75 65 75 65 */.struct PQueue
0620: 20 7b 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 {. int cnt;
0630: 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
0640: 72 69 65 73 20 69 6e 20 74 68 65 20 71 75 65 75 ries in the queu
0650: 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 3b 20 20 e */. int sz;
0660: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 /* Number of s
0670: 6c 6f 74 73 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20 lots in a[] */.
0680: 20 73 74 72 75 63 74 20 51 75 65 75 65 45 6c 65 struct QueueEle
0690: 6d 65 6e 74 20 7b 0a 20 20 20 20 69 6e 74 20 69 ment {. int i
06a0: 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 d; /* I
06b0: 44 20 6f 66 20 74 68 65 20 65 6c 65 6d 65 6e 74 D of the element
06c0: 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 */. double v
06d0: 61 6c 75 65 3b 20 20 20 20 2f 2a 20 56 61 6c 75 alue; /* Valu
06e0: 65 20 6f 66 20 65 6c 65 6d 65 6e 74 2e 20 20 4b e of element. K
06f0: 65 70 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 ept in ascending
0700: 20 6f 72 64 65 72 20 2a 2f 0a 20 20 7d 20 2a 61 order */. } *a
0710: 3b 0a 7d 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a ;.};.#endif../*.
0720: 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 ** Initialize a
0730: 50 51 75 65 75 65 20 73 74 72 75 63 74 75 72 65 PQueue structure
0740: 0a 2a 2f 0a 76 6f 69 64 20 70 71 75 65 75 65 5f .*/.void pqueue_
0750: 69 6e 69 74 28 50 51 75 65 75 65 20 2a 70 29 7b init(PQueue *p){
0760: 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 . memset(p, 0,
0770: 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 7d 0a 0a sizeof(*p));.}..
0780: 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 /*.** Destroy a
0790: 50 51 75 65 75 65 2e 20 20 44 65 6c 65 74 65 20 PQueue. Delete
07a0: 61 6c 6c 20 6f 66 20 69 74 73 20 63 6f 6e 74 65 all of its conte
07b0: 6e 74 2e 0a 2a 2f 0a 76 6f 69 64 20 70 71 75 65 nt..*/.void pque
07c0: 75 65 5f 63 6c 65 61 72 28 50 51 75 65 75 65 20 ue_clear(PQueue
07d0: 2a 70 29 7b 0a 20 20 66 72 65 65 28 70 2d 3e 61 *p){. free(p->a
07e0: 29 3b 0a 20 20 70 71 75 65 75 65 5f 69 6e 69 74 );. pqueue_init
07f0: 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 (p);.}../*.** Ch
0800: 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 ange the size of
0810: 20 74 68 65 20 71 75 65 75 65 20 73 6f 20 74 68 the queue so th
0820: 61 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 4e at it contains N
0830: 20 73 6c 6f 74 73 0a 2a 2f 0a 73 74 61 74 69 63 slots.*/.static
0840: 20 76 6f 69 64 20 70 71 75 65 75 65 5f 72 65 73 void pqueue_res
0850: 69 7a 65 28 50 51 75 65 75 65 20 2a 70 2c 20 69 ize(PQueue *p, i
0860: 6e 74 20 4e 29 7b 0a 20 20 70 2d 3e 61 20 3d 20 nt N){. p->a =
0870: 72 65 61 6c 6c 6f 63 28 70 2d 3e 61 2c 20 73 69 realloc(p->a, si
0880: 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 2a 4e 29 zeof(p->a[0])*N)
0890: 3b 0a 20 20 70 2d 3e 73 7a 20 3d 20 4e 3b 0a 7d ;. p->sz = N;.}
08a0: 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 65 ../*.** Insert e
08b0: 6c 65 6d 65 6e 74 20 65 20 69 6e 74 6f 20 74 68 lement e into th
08c0: 65 20 71 75 65 75 65 2e 0a 2a 2f 0a 76 6f 69 64 e queue..*/.void
08d0: 20 70 71 75 65 75 65 5f 69 6e 73 65 72 74 28 50 pqueue_insert(P
08e0: 51 75 65 75 65 20 2a 70 2c 20 69 6e 74 20 65 2c Queue *p, int e,
08f0: 20 64 6f 75 62 6c 65 20 76 29 7b 0a 20 20 69 6e double v){. in
0900: 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 70 2d t i, j;. if( p-
0910: 3e 63 6e 74 2b 31 3e 70 2d 3e 73 7a 20 29 7b 0a >cnt+1>p->sz ){.
0920: 20 20 20 20 70 71 75 65 75 65 5f 72 65 73 69 7a pqueue_resiz
0930: 65 28 70 2c 20 70 2d 3e 63 6e 74 2b 35 29 3b 0a e(p, p->cnt+5);.
0940: 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
0950: 3c 70 2d 3e 63 6e 74 3b 20 69 2b 2b 29 7b 0a 20 <p->cnt; i++){.
0960: 20 20 20 69 66 28 20 70 2d 3e 61 5b 69 5d 2e 76 if( p->a[i].v
0970: 61 6c 75 65 3e 76 20 29 7b 0a 20 20 20 20 20 20 alue>v ){.
0980: 66 6f 72 28 6a 3d 70 2d 3e 63 6e 74 3b 20 6a 3e for(j=p->cnt; j>
0990: 69 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20 i; j--){.
09a0: 20 70 2d 3e 61 5b 6a 5d 20 3d 20 70 2d 3e 61 5b p->a[j] = p->a[
09b0: 6a 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 j-1];. }.
09c0: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
09d0: 0a 20 20 7d 0a 20 20 70 2d 3e 61 5b 69 5d 2e 69 . }. p->a[i].i
09e0: 64 20 3d 20 65 3b 0a 20 20 70 2d 3e 61 5b 69 5d d = e;. p->a[i]
09f0: 2e 76 61 6c 75 65 20 3d 20 76 3b 0a 20 20 70 2d .value = v;. p-
0a00: 3e 63 6e 74 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a >cnt++;.}../*.**
0a10: 20 45 78 74 72 61 63 74 20 74 68 65 20 66 69 72 Extract the fir
0a20: 73 74 20 65 6c 65 6d 65 6e 74 20 66 72 6f 6d 20 st element from
0a30: 74 68 65 20 71 75 65 75 65 20 28 74 68 65 20 65 the queue (the e
0a40: 6c 65 6d 65 6e 74 20 77 69 74 68 0a 2a 2a 20 74 lement with.** t
0a50: 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c 75 he smallest valu
0a60: 65 29 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 e) and return it
0a70: 73 20 49 44 2e 20 20 52 65 74 75 72 6e 20 30 20 s ID. Return 0
0a80: 69 66 20 74 68 65 20 71 75 65 75 65 0a 2a 2a 20 if the queue.**
0a90: 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 69 6e 74 is empty..*/.int
0aa0: 20 70 71 75 65 75 65 5f 65 78 74 72 61 63 74 28 pqueue_extract(
0ab0: 50 51 75 65 75 65 20 2a 70 29 7b 0a 20 20 69 6e PQueue *p){. in
0ac0: 74 20 65 2c 20 69 3b 0a 20 20 69 66 28 20 70 2d t e, i;. if( p-
0ad0: 3e 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 >cnt==0 ){. r
0ae0: 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 65 eturn 0;. }. e
0af0: 20 3d 20 70 2d 3e 61 5b 30 5d 2e 69 64 3b 0a 20 = p->a[0].id;.
0b00: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 63 for(i=0; i<p->c
0b10: 6e 74 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nt-1; i++){.
0b20: 70 2d 3e 61 5b 69 5d 20 3d 20 70 2d 3e 61 5b 69 p->a[i] = p->a[i
0b30: 2b 31 5d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 63 6e +1];. }. p->cn
0b40: 74 2d 2d 3b 0a 20 20 72 65 74 75 72 6e 20 65 3b t--;. return e;
0b50: 0a 7d 0a .}.