1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
//C- -------------------------------------------------------------------
//C- DjVuLibre-3.5
//C- Copyright (c) 2002 Leon Bottou and Yann Le Cun.
//C- Copyright (c) 2001 AT&T
//C-
//C- This software is subject to, and may be distributed under, the
//C- GNU General Public License, Version 2. The license should have
//C- accompanied the software or you may obtain a copy of the license
//C- from the Free Software Foundation at http://www.fsf.org .
//C-
//C- This program is distributed in the hope that it will be useful,
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//C- GNU General Public License for more details.
//C-
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
//C- distributed by Lizardtech Software. On July 19th 2002, Lizardtech
//C- Software authorized us to replace the original DjVu(r) Reference
//C- Library notice by the following text (see doc/lizard2002.djvu):
//C-
//C- ------------------------------------------------------------------
//C- | DjVu (r) Reference Library (v. 3.5)
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
//C- | 6,058,214 and patents pending.
//C- |
//C- | This software is subject to, and may be distributed under, the
//C- | GNU General Public License, Version 2. The license should have
//C- | accompanied the software or you may obtain a copy of the license
//C- | from the Free Software Foundation at http://www.fsf.org .
//C- |
//C- | The computer code originally released by LizardTech under this
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
//C- | ORIGINAL CODE." Subject to any third party intellectual property
//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
//C- | non-exclusive license to make, use, sell, or otherwise dispose of
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
//C- | General Public License. This grant only confers the right to
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
//C- | the extent such infringement is reasonably necessary to enable
//C- | recipient to make, have made, practice, sell, or otherwise dispose
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
//C- | any greater extent that may be necessary to utilize further
//C- | modifications or combinations.
//C- |
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//C- +------------------------------------------------------------------
//
// $Id: GContainer.cpp,v 1.12 2003/11/07 22:08:21 leonb Exp $
// $Name: release_3_5_15 $
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if NEED_GNUG_PRAGMAS
# pragma implementation
#endif
#include "GContainer.h"
#ifdef HAVE_NAMESPACES
namespace DJVU {
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
}
#endif
#endif
// ------------------------------------------------------------
// DYNAMIC ARRAYS
// ------------------------------------------------------------
GArrayBase::GArrayBase(const GArrayBase &ref)
: traits(ref.traits),
gdata(data,0,1),
minlo(ref.minlo), maxhi(ref.maxhi),
lobound(ref.lobound), hibound(ref.hibound)
{
if (maxhi >= minlo)
gdata.resize(traits.size * (maxhi - minlo + 1),1);
if (hibound >= lobound)
traits.copy(traits.lea(data, lobound-minlo),
traits.lea(ref.data, lobound-minlo),
hibound - lobound + 1, 0);
}
GArrayBase::GArrayBase(const GCONT Traits &traits)
: traits(traits),
gdata(data,0,1),
minlo(0), maxhi(-1),
lobound(0), hibound(-1)
{
}
GArrayBase::GArrayBase(const GCONT Traits &traits, int lobound, int hibound)
: traits(traits),
gdata(data,0,1),
minlo(0), maxhi(-1),
lobound(0), hibound(-1)
{
resize(lobound, hibound);
}
GArrayBase::~GArrayBase()
{
G_TRY { empty(); } G_CATCH_ALL { } G_ENDCATCH;
}
GArrayBase &
GArrayBase::operator= (const GArrayBase &ga)
{
if (this == &ga)
return *this;
empty();
if (ga.hibound >= ga.lobound)
{
resize(ga.lobound, ga.hibound);
traits.copy( traits.lea(data, lobound-minlo),
traits.lea(ga.data, ga.lobound-ga.minlo),
hibound - lobound + 1, 0 );
}
return *this;
}
void
GArrayBase::steal(GArrayBase &ga)
{
if (this != &ga)
{
empty();
lobound = ga.lobound;
hibound = ga.hibound;
minlo = ga.minlo;
maxhi = ga.maxhi;
data = ga.data;
ga.data = 0;
ga.lobound = ga.minlo = 0;
ga.hibound = ga.maxhi = -1;
}
}
void
GArrayBase::empty()
{
resize(0, -1);
}
void
GArrayBase::touch(int n)
{
int nlo = (n<lobound ? n : lobound);
int nhi = (n>hibound ? n : hibound);
if (hibound < lobound)
nlo = nhi = n;
resize(nlo, nhi);
}
void
GArrayBase::resize(int lo, int hi)
{
// Validation
int nsize = hi - lo + 1;
if (nsize < 0)
G_THROW( ERR_MSG("GContainer.bad_args") );
// Destruction
if (nsize == 0)
{
if (hibound >= lobound)
traits.fini( traits.lea(data, lobound-minlo), hibound-lobound+1 );
if (data)
gdata.resize(0,1);
lobound = minlo = 0;
hibound = maxhi = -1;
return;
}
// Simple extension
if (lo >= minlo && hi <= maxhi)
{
if (lobound > lo)
traits.init( traits.lea(data,lo-minlo), lobound-lo );
else if (lo > lobound)
traits.fini( traits.lea(data,lobound-minlo), lo-lobound );
if (hi > hibound)
traits.init( traits.lea(data,hibound-minlo+1), hi-hibound );
else if (hibound > hi)
traits.fini( traits.lea(data,hi-minlo+1), hibound-hi );
lobound = lo;
hibound = hi;
return;
}
// General case
int nminlo = minlo;
int nmaxhi = maxhi;
if (nminlo > nmaxhi)
nminlo = nmaxhi = lo;
while (nminlo > lo) {
int incr = nmaxhi - nminlo;
nminlo -= (incr < 8 ? 8 : (incr > 32768 ? 32768 : incr));
}
while (nmaxhi < hi) {
int incr = nmaxhi - nminlo;
nmaxhi += (incr < 8 ? 8 : (incr > 32768 ? 32768 : incr));
}
// allocate and move
int beg = lo;
int end = hi;
int bytesize = traits.size * (nmaxhi-nminlo+1);
void *ndata;
GPBufferBase gndata(ndata,bytesize,1);
#if GCONTAINER_ZERO_FILL
memset(ndata, 0, bytesize); // slower but cleaner
#endif
if (lo < lobound)
{ traits.init( traits.lea(ndata,lo-nminlo), lobound-lo ); beg=lobound; }
else if (lobound < lo)
{ traits.fini( traits.lea(data,lobound-minlo), lo-lobound); }
if (hibound < hi)
{ traits.init( traits.lea(ndata,hibound-nminlo+1), hi-hibound ); end=hibound; }
else if (hi < hibound)
{ traits.fini( traits.lea(data, hi-minlo+1), hibound-hi ); }
if (end >= beg)
{ traits.copy( traits.lea(ndata, beg-nminlo),
traits.lea(data, beg-minlo),
end-beg+1, 1 ); }
// free and replace
void *tmp=data;
data=ndata;
ndata=tmp;
minlo = nminlo;
maxhi = nmaxhi;
lobound = lo;
hibound = hi;
}
void
GArrayBase::shift(int disp)
{
lobound += disp;
hibound += disp;
minlo += disp;
maxhi += disp;
}
void
GArrayBase::del(int n, int howmany)
{
if (howmany < 0)
G_THROW( ERR_MSG("GContainer.bad_howmany") );
if (howmany == 0)
return;
if ( n < lobound || n+(int)howmany-1 > hibound)
G_THROW( ERR_MSG("GContainer.bad_sub2") );
traits.fini( traits.lea(data, n-minlo), howmany );
if ( n+howmany-1 < hibound)
traits.copy( traits.lea(data, n-minlo),
traits.lea(data, n-minlo+howmany),
hibound - (n+howmany-1), 1 );
hibound = hibound - howmany;
}
static inline void *
nextptr(void *p, int elsize)
{
return (void*)(((char*)p) + elsize);
}
static inline void *
prevptr(void *p, int elsize)
{
return (void*)(((char*)p) - elsize);
}
void
GArrayBase::ins(int n, const void *src, int howmany)
{
if (howmany < 0)
G_THROW( ERR_MSG("GContainer.bad_howmany") );
if (howmany == 0)
return;
// Make enough room
if (hibound+howmany > maxhi)
{
int nmaxhi = maxhi;
while (nmaxhi < hibound+howmany)
nmaxhi += (nmaxhi < 8 ? 8 : (nmaxhi > 32768 ? 32768 : nmaxhi));
int bytesize = traits.size * (nmaxhi-minlo+1);
void *ndata; // = operator new (bytesize);
GPBufferBase gndata(ndata,bytesize,1);
memset(ndata, 0, bytesize); // slower but cleaner
if (hibound >= lobound)
traits.copy( traits.lea(ndata, lobound-minlo),
traits.lea(data, lobound-minlo),
hibound-lobound+1, 1 );
maxhi = nmaxhi;
void *tmp=data;
data = ndata;
ndata=tmp;
}
// Shift data
int elsize = traits.size;
void *pdst = traits.lea(data, hibound+howmany-minlo);
void *psrc = traits.lea(data, hibound-minlo);
void *pend = traits.lea(data, n-minlo);
while ((char*)psrc >= (char*)pend)
{
traits.copy( pdst, psrc, 1, 1 );
pdst = prevptr(pdst, elsize);
psrc = prevptr(psrc, elsize);
}
hibound += howmany;
// Initialize new data
if (! src)
{
traits.init( traits.lea(data, n-minlo), howmany );
hibound += howmany;
return;
}
// Initialize new data with copy constructor
pdst = traits.lea(data, n-minlo);
pend = traits.lea(data, n+howmany-minlo);
while ((char*)pdst < (char*)pend)
{
traits.copy( pdst, src, 1, 0);
pdst = nextptr(pdst, elsize);
}
}
// ------------------------------------------------------------
// GPOSITION
// ------------------------------------------------------------
void
GPosition::throw_invalid(void *c) const
{
if (c != cont)
G_THROW( ERR_MSG("GContainer.bad_pos_cont") );
else if (! ptr)
G_THROW( ERR_MSG("GContainer.bad_pos_null") );
else
G_THROW( ERR_MSG("GContainer.bad_pos") );
}
// ------------------------------------------------------------
// DOUBLY LINKED LISTS
// ------------------------------------------------------------
GListBase::GListBase(const Traits& traits)
: traits(traits)
{
nelem = 0;
head.next = head.prev = 0;
}
GListBase::GListBase(const GListBase &ref)
: traits(ref.traits)
{
nelem = 0;
head.next = head.prev = 0;
GListBase::operator= (ref);
}
#include <stdio.h>
GListBase::~GListBase()
{
G_TRY
{
empty();
}
G_CATCH_ALL
{
}
G_ENDCATCH;
}
void
GListBase::append(Node *n)
{
// Link
n->next = 0;
n->prev = head.prev;
head.prev = n;
if (n->prev)
n->prev->next = n;
else
head.next = n;
// Finish
nelem += 1;
}
void
GListBase::prepend(Node *n)
{
// Link
n->next = head.next;
n->prev = 0;
head.next = n;
if (n->next)
n->next->prev = n;
else
head.prev = n;
// Finish
nelem += 1;
}
void
GListBase::insert_after(GPosition pos, Node *n)
{
// Prepare
if (pos.ptr)
{
if (pos.cont != (void*)this)
pos.throw_invalid((void*)this);
Node *p = pos.ptr;
n->prev = p;
n->next = p->next;
}
else
{
n->prev = 0;
n->next = head.next;
}
// Link
if (n->prev)
n->prev->next = n;
else
head.next = n;
if (n->next)
n->next->prev = n;
else
head.prev = n;
// Finish
nelem += 1;
}
void
GListBase::insert_before(GPosition pos, Node *n)
{
// Prepare
if (pos.ptr)
{
if (pos.cont != (void*)this)
pos.throw_invalid((void*)this);
Node *p = pos.ptr;
n->prev = p->prev;
n->next = p;
}
else
{
n->prev = head.prev;
n->next = 0;
}
// Link
if (n->prev)
n->prev->next = n;
else
head.next = n;
if (n->next)
n->next->prev = n;
else
head.prev = n;
// Finish
nelem += 1;
}
void
GListBase::insert_before(GPosition pos, GListBase &fromlist, GPosition &frompos)
{
// Check
if (!frompos.ptr || frompos.cont != (void*)&fromlist)
frompos.throw_invalid((void*)&fromlist);
if (pos.ptr && pos.cont != (void*)this)
pos.throw_invalid((void*)this);
// Update frompos
Node *n = frompos.ptr;
frompos.ptr = n->next;
if (pos.ptr == n) return;
// Unlink
if (n->next)
n->next->prev = n->prev;
else
fromlist.head.prev = n->prev;
if (n->prev)
n->prev->next = n->next;
else
fromlist.head.next = n->next;
fromlist.nelem -= 1;
// Prepare insertion
if (pos.ptr)
{
Node *p = pos.ptr;
n->prev = p->prev;
n->next = p;
}
else
{
n->prev = head.prev;
n->next = 0;
}
// Link
if (n->prev)
n->prev->next = n;
else
head.next = n;
if (n->next)
n->next->prev = n;
else
head.prev = n;
nelem += 1;
}
void
GListBase::del(GPosition &pos)
{
// Check
if (!pos.ptr || pos.cont != (void*)this) return;
// Unlink
Node *n = pos.ptr;
if (n->next)
n->next->prev = n->prev;
else
head.prev = n->prev;
if (n->prev)
n->prev->next = n->next;
else
head.next = n->next;
// Finish
nelem -= 1;
traits.fini( (void*)n, 1);
operator delete ( (void*)n );
pos.ptr = 0;
}
GPosition
GListBase::nth(unsigned int n) const
{
Node *p = 0;
if ((int)n < nelem)
for (p=head.next; p; p=p->next)
if ( n-- == 0)
break;
return GPosition(p, (void*)this);
}
void
GListBase::empty()
{
Node *n=head.next;
while (n)
{
Node *p = n->next;
traits.fini( (void*)n, 1 );
operator delete ( (void*)n );
n = p;
}
head.next = head.prev = 0;
nelem = 0;
}
GListBase &
GListBase::operator= (const GListBase & ref)
{
if (this == &ref)
return *this;
empty();
for(Node *n = ref.head.next; n; n=n->next)
{
Node *m = (Node*) operator new (traits.size);
traits.copy( (void*)m, (void*)n, 1, 0);
append(m);
}
return *this;
}
// ------------------------------------------------------------
// ASSOCIATIVE MAPS
// ------------------------------------------------------------
GSetBase::GSetBase(const Traits &traits)
: traits(traits), nelems(0), nbuckets(0),
gtable(table), first(0)
{
rehash(17);
}
GSetBase::GSetBase(const GSetBase &ref)
: traits(ref.traits),
nelems(0), nbuckets(0), gtable(table), first(0)
{
GSetBase::operator= (ref);
}
GSetBase::~GSetBase()
{
G_TRY { empty(); } G_CATCH_ALL { } G_ENDCATCH;
// delete [] table;
}
GCONT HNode *
GSetBase::hashnode(unsigned int hashcode) const
{
int bucket = hashcode % nbuckets;
return table[bucket];
}
GCONT HNode *
GSetBase::installnode(HNode *n)
{
// Rehash if table is more than 60% full
if (nelems*3 > nbuckets*2)
rehash( 2*nbuckets - 1 );
// Create and insert
insertnode(n);
return n;
}
void
GSetBase::insertnode(HNode *n)
{
int bucket = n->hashcode % nbuckets;
n->prev = n->hprev = table[bucket];
if (n->prev)
{
// bucket was not empty
n->next = n->prev->next;
n->prev->next = n;
if (n->next)
n->next->prev = n;
}
else
{
// bucket was empty.
n->next = first;
first = n;
if (n->next)
n->next->prev = n;
}
// finish
table[bucket] = n;
nelems += 1;
}
void
GSetBase::deletenode(GCONT HNode *n)
{
if (n == 0)
return;
int bucket = n->hashcode % nbuckets;
// Regular links
if (n->next)
n->next->prev = n->prev;
if (n->prev)
n->prev->next = n->next;
else
first = (HNode*)(n->next);
// HPrev links
if (table[bucket] == n)
table[bucket] = n->hprev;
else
((HNode*)(n->next))->hprev = n->hprev;
// Delete entry
traits.fini( (void*)n, 1 );
operator delete ( (void*)n );
nelems -= 1;
}
void
GSetBase::rehash(int newbuckets)
{
// Save chain of nodes
Node *n = first;
// Simulate an empty map
nelems = 0;
first = 0;
// Allocate a new empty bucket table
// delete [] table;
gtable.resize(0);
nbuckets = newbuckets;
typedef HNode *HNodePtr;
// table = new HNodePtr[nbuckets];
gtable.resize(nbuckets);
gtable.clear();
// for (int i=0; i<nbuckets; i++)
// table[i] = 0;
// Insert saved nodes
while (n)
{
Node *p = n->next;
insertnode((HNode*)n);
n = p;
}
}
GSetBase&
GSetBase::operator=(const GSetBase &ref)
{
if (this == &ref)
return *this;
empty();
rehash(ref.nbuckets);
for (Node *n = ref.first; n; n=n->next)
{
HNode *m = (HNode*) operator new (traits.size);
traits.copy( (void*)m, (void*)n, 1, 0);
insertnode(m);
}
return *this;
}
GPosition
GSetBase::firstpos() const
{
return GPosition(first, (void*)this);
}
void
GSetBase::del(GPosition &pos)
{
if (pos.ptr && pos.cont==(void*)this)
{
deletenode((HNode*)pos.ptr);
pos.ptr = 0;
}
}
void
GSetBase::empty()
{
HNode *n = first;
while (n)
{
HNode *p = (HNode*)(n->next);
traits.fini( (void*)n, 1 );
operator delete ( (void*)n );
n = p;
}
first = 0;
nelems = 0;
gtable.clear();
// for (int i=0; i<nbuckets; i++)
// table[i] = 0;
}
#ifdef HAVE_NAMESPACES
}
# ifndef NOT_USING_DJVU_NAMESPACE
using namespace DJVU;
# endif
#endif
|