source: trunk/libdjvu/Arrays.cpp @ 280

Last change on this file since 280 was 280, checked in by rbri, 11 years ago

DJVU plugin: djvulibre updated to version 3.5.22

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