source: trunk/libdjvu/Arrays.cpp @ 76

Last change on this file since 76 was 17, checked in by Eugene Romanenko, 16 years ago

update makefiles, remove absolute paths, update djvulibre to version 3.5.17

File size: 9.2 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, Version 2. The license should have
9//C- accompanied the software or you may obtain a copy of the license
10//C- from the Free Software Foundation at http://www.fsf.org .
11//C-
12//C- This program is distributed in the hope that it will be useful,
13//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
14//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15//C- GNU General Public License for more details.
16//C-
17//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
18//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech
19//C- Software authorized us to replace the original DjVu(r) Reference
20//C- Library notice by the following text (see doc/lizard2002.djvu):
21//C-
22//C-  ------------------------------------------------------------------
23//C- | DjVu (r) Reference Library (v. 3.5)
24//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
25//C- | The DjVu Reference Library is protected by U.S. Pat. No.
26//C- | 6,058,214 and patents pending.
27//C- |
28//C- | This software is subject to, and may be distributed under, the
29//C- | GNU General Public License, Version 2. The license should have
30//C- | accompanied the software or you may obtain a copy of the license
31//C- | from the Free Software Foundation at http://www.fsf.org .
32//C- |
33//C- | The computer code originally released by LizardTech under this
34//C- | license and unmodified by other parties is deemed "the LIZARDTECH
35//C- | ORIGINAL CODE."  Subject to any third party intellectual property
36//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
37//C- | non-exclusive license to make, use, sell, or otherwise dispose of
38//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
39//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
40//C- | General Public License.   This grant only confers the right to
41//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
42//C- | the extent such infringement is reasonably necessary to enable
43//C- | recipient to make, have made, practice, sell, or otherwise dispose
44//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
45//C- | any greater extent that may be necessary to utilize further
46//C- | modifications or combinations.
47//C- |
48//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
49//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
50//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
51//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
52//C- +------------------------------------------------------------------
53//
54// $Id: Arrays.cpp,v 1.8 2003/11/07 22:08:20 leonb Exp $
55// $Name:  $
56
57#ifdef HAVE_CONFIG_H
58# include "config.h"
59#endif
60#if NEED_GNUG_PRAGMAS
61# pragma implementation
62#endif
63
64#include "Arrays.h"
65#include "GException.h"
66
67
68#ifdef HAVE_NAMESPACES
69namespace DJVU {
70# ifdef NOT_DEFINED // Just to fool emacs c++ mode
71}
72#endif
73#endif
74
75ArrayRep::ArrayRep(int xelsize,
76                   void (* xdestroy)(void *, int, int),
77                   void (* xinit1)(void *, int, int),
78                   void (* xinit2)(void *, int, int, const void *, int, int),
79                   void (* xcopy)(void *, int, int, const void *, int, int),
80                   void (* xinsert)(void *, int, int, const void *, int)) :
81      data(0), minlo(0), maxhi(-1), lobound(0), hibound(-1),
82      elsize(xelsize), destroy(xdestroy), init1(xinit1),
83      init2(xinit2), copy(xcopy), insert(xinsert)
84{
85}
86
87ArrayRep::ArrayRep(int xelsize,
88                   void (* xdestroy)(void *, int, int),
89                   void (* xinit1)(void *, int, int),
90                   void (* xinit2)(void *, int, int, const void *, int, int),
91                   void (* xcopy)(void *, int, int, const void *, int, int),
92                   void (* xinsert)(void *, int, int, const void *, int),
93                   int hi) : data(0), minlo(0), maxhi(-1),
94   lobound(0), hibound(-1), elsize(xelsize), destroy(xdestroy), init1(xinit1),
95   init2(xinit2), copy(xcopy), insert(xinsert)
96{
97   resize(0, hi);
98}
99
100ArrayRep::ArrayRep(int xelsize,
101                   void (* xdestroy)(void *, int, int),
102                   void (* xinit1)(void *, int, int),
103                   void (* xinit2)(void *, int, int, const void *, int, int),
104                   void (* xcopy)(void *, int, int, const void *, int, int),
105                   void (* xinsert)(void *, int, int, const void *, int),
106                   int lo, int hi) : data(0), minlo(0), maxhi(-1),
107   lobound(0), hibound(-1), elsize(xelsize), destroy(xdestroy), init1(xinit1),
108   init2(xinit2), copy(xcopy), insert(xinsert)
109{
110   resize(lo,hi);
111}
112
113ArrayRep::ArrayRep(const ArrayRep & arr) : data(0), minlo(0), maxhi(-1),
114   lobound(0), hibound(-1), elsize(arr.elsize), destroy(arr.destroy),
115   init1(arr.init1), init2(arr.init2), copy(arr.copy), insert(arr.insert)
116{
117   resize(arr.lobound, arr.hibound);
118   arr.copy(data, lobound-minlo, hibound-minlo,
119            arr.data, arr.lobound-arr.minlo, arr.hibound-arr.minlo);
120}
121
122ArrayRep::~ArrayRep()
123{
124   destroy(data, lobound-minlo, hibound-minlo);
125   operator delete(data);
126   data=0;
127}
128
129ArrayRep & 
130ArrayRep::operator= (const ArrayRep & rep)
131{
132   if (&rep == this) return *this;
133   empty();
134   resize(rep.lobound, rep.hibound);
135   copy(data, lobound-minlo, hibound-minlo,
136        rep.data, rep.lobound-rep.minlo, rep.hibound-rep.minlo);
137   return *this;
138}
139
140void
141ArrayRep::resize(int lo, int hi)
142{
143  int nsize = hi - lo + 1;
144  // Validation
145  if (nsize < 0)
146    G_THROW( ERR_MSG("arrays.resize") );
147  // Destruction
148  if (nsize == 0)
149    {
150      destroy(data, lobound-minlo, hibound-minlo);
151      operator delete(data);
152      data = 0;
153      lobound = minlo = lo; 
154      hibound = maxhi = hi; 
155      return;
156    }
157  // Simple extension
158  if (lo >= minlo && hi <= maxhi)
159    {
160      init1(data, lo-minlo, lobound-1-minlo);
161      destroy(data, lobound-minlo, lo-1-minlo);
162      init1(data, hibound+1-minlo, hi-minlo);
163      destroy(data, hi+1-minlo, hibound-minlo);
164      lobound = lo;
165      hibound = hi;
166      return;
167    }
168  // General case
169  int nminlo = minlo;
170  int nmaxhi = maxhi;
171  if (nminlo > nmaxhi)
172    nminlo = nmaxhi = lo;
173  while (nminlo > lo) {
174    int incr = nmaxhi - nminlo;
175    nminlo -= (incr < 8 ? 8 : (incr > 32768 ? 32768 : incr));
176  }
177  while (nmaxhi < hi) {
178    int incr = nmaxhi - nminlo;
179    nmaxhi += (incr < 8 ? 8 : (incr > 32768 ? 32768 : incr));
180  }
181  // allocate
182  int bytesize=elsize*(nmaxhi-nminlo+1);
183  void * ndata;
184  GPBufferBase gndata(ndata,bytesize,1);
185  memset(ndata, 0, bytesize);
186  // initialize
187  init1(ndata, lo-nminlo, lobound-1-nminlo);
188  init2(ndata, lobound-nminlo, hibound-nminlo,
189        data, lobound-minlo, hibound-minlo);
190  init1(ndata, hibound+1-nminlo, hi-nminlo);
191  destroy(data, lobound-minlo, hibound-minlo);
192
193  // free and replace
194  void *tmp=data;
195  data = ndata;
196  ndata=tmp;
197
198  minlo = nminlo;
199  maxhi = nmaxhi;
200  lobound = lo;
201  hibound = hi;
202}
203
204void
205ArrayRep::shift(int disp)
206{
207   lobound += disp;
208   hibound += disp;
209   minlo += disp;
210   maxhi += disp;
211}
212
213void
214ArrayRep::del(int n, unsigned int howmany)
215{
216   if (howmany == 0)
217      return;
218   if ((int)(n + howmany) > hibound +1)
219      G_THROW( ERR_MSG("arrays.ill_arg") );
220   copy(data, n-minlo, hibound-howmany-minlo,
221        data, n+howmany-minlo, hibound-minlo);
222   destroy(data, hibound+1-howmany-minlo, hibound-minlo);
223   hibound = hibound - howmany;
224}
225
226void
227ArrayRep::ins(int n, const void * what, unsigned int howmany)
228{
229   int nhi = hibound + howmany;
230   if (howmany == 0) return;
231   if (maxhi < nhi)
232   {
233      int nmaxhi = maxhi;
234      while (nmaxhi < nhi)
235         nmaxhi += (nmaxhi < 8 ? 8 : (nmaxhi > 32768 ? 32768 : nmaxhi));
236      int bytesize = elsize*(nmaxhi-minlo+1);
237      void *ndata;
238      GPBufferBase gndata(ndata,bytesize,1);
239      memset(ndata, 0, bytesize);
240      copy(ndata, lobound-minlo, hibound-minlo,
241           data, lobound-minlo, hibound-minlo);
242      destroy(data, lobound-minlo, hibound-minlo);
243      void *tmp=data;
244      data=ndata;
245      tmp=data;
246      maxhi = nmaxhi;
247   }
248
249   insert(data, hibound+1-minlo, n-minlo, what, howmany);
250   hibound=nhi;
251}
252
253
254
255#ifdef HAVE_NAMESPACES
256}
257# ifndef NOT_USING_DJVU_NAMESPACE
258using namespace DJVU;
259# endif
260#endif
261
262
263// ---------------------------------------
264// BEGIN HACK
265// ---------------------------------------
266// Included here to avoid dependency
267// from ByteStream.o to Arrays.o
268
269#ifndef DO_NOT_MOVE_GET_DATA_TO_ARRAYS_CPP
270#include "ByteStream.h"
271
272#ifdef HAVE_NAMESPACES
273namespace DJVU {
274# ifdef NOT_DEFINED // Just to fool emacs c++ mode
275}
276#endif
277#endif
278TArray<char>
279ByteStream::get_data(void)
280{
281   const int s=size();
282   if(s > 0)
283   {
284     TArray<char> data(0, s-1);
285     readat((char*)data, s, 0);
286     return data;
287   }else
288   {
289     TArray<char> data(0, -1);
290     return data;
291   }
292}
293
294#ifdef HAVE_NAMESPACES
295}
296# ifndef NOT_USING_DJVU_NAMESPACE
297using namespace DJVU;
298# endif
299#endif
300#endif
301
302// ---------------------------------------
303// END HACK
304// ---------------------------------------
305
Note: See TracBrowser for help on using the repository browser.