home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lyx-0.13.2.tar.gz / lyx-0.13.2.tar / lyx-0.13.2 / src / array.h next >
C/C++ Source or Header  |  1998-04-23  |  4KB  |  236 lines

  1. // -*- C++ -*-
  2. /*
  3.  *  File:        array.h
  4.  *  Purpose:     A general purpose resizable array.  
  5.  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
  6.  *  Created:     January 1996
  7.  *
  8.  *  Dependencies: None (almost)
  9.  *
  10.  *  Copyright: (c) 1996, Alejandro Aguilar Sierra
  11.  *                 1997  The LyX Team!
  12.  *
  13.  *   You are free to use and modify this code under the terms of
  14.  *   the GNU General Public Licence version 2 or later.
  15.  */
  16.  
  17. #include <string.h>
  18.  
  19. #ifndef byte
  20. #define byte unsigned char
  21. #endif
  22.  
  23. /*@Doc: A resizable array
  24.   Why is it called "LyXArrayBase" if it is generic? (Lgb)
  25.   Initially I thought it could be the base class for both mathed's
  26.   and LyX' kernels data buffer. (Ale)
  27.  
  28.   */
  29. class LyxArrayBase  {
  30. public:
  31.     ///
  32.     enum {
  33.         ///
  34.         ARRAY_SIZE = 256,
  35.         ///
  36.         ARRAY_STEP = 16,
  37.         ///
  38.         ARRAY_MIN_SIZE = 4
  39.     };
  40.  
  41.     ///
  42.     LyxArrayBase(int size=ARRAY_STEP);
  43.     ///
  44.     LyxArrayBase(const LyxArrayBase&);
  45.     ///
  46.     ~LyxArrayBase();
  47.    
  48.     
  49.     ///
  50.     int Empty() { return (last==0); }
  51.    
  52.     ///
  53.     int Last() { return last; }
  54.    
  55.     /// Fills with 0 the entire array and set last to 0
  56.     void Init();     
  57.    
  58.     /// Make the allocated memory fit the needed size
  59.     void Fit();     
  60.  
  61.     /// Remove dx elements from position pos. Don't changes the size
  62.     void Remove(int pos, int dx);   
  63.  
  64.     /// Merge dx elements from array a at pos. Changes the size if necessary.
  65.     void Merge(LyxArrayBase *a, int pos, int dx); 
  66.  
  67.     /// Same as Merge but doesn't changes the size (dangerous)
  68.     void MergeF(LyxArrayBase *a, int pos, int dx); 
  69.  
  70.     /// Copy dx byts from an array at position pos
  71.     void Copy(void *, int pos, int dx); 
  72.  
  73.     /// Constructs a new array with dx elements starting at pos 
  74.     LyxArrayBase* Extract(int pos, int dx); 
  75.  
  76.     /// Insert a character at position pos
  77.     void Insert(int pos, byte);
  78.  
  79.     /// Insert a string of lenght dx at position pos
  80.     void Insert(int pos, byte *, int dx);
  81.  
  82.     /// Constructs a new array with dx elements starting at pos 
  83.     byte operator[](const int);
  84.  
  85.     /// Constructs a new array with dx elements starting at pos 
  86.     LyxArrayBase& operator=(const LyxArrayBase&); 
  87.  
  88. protected:
  89.     ///
  90.     void Resize(int newsize);
  91.     ///
  92.     bool Move(int p, int shift);
  93.  
  94.     /// Buffer
  95.     byte *bf;
  96.     /// Last position inserted.
  97.     int last;
  98.     /// Max size of the array.
  99.     int maxsize;
  100. private:
  101.     ///
  102.     friend class MathedIter;
  103. };
  104.    
  105.  
  106.  
  107. /************************ Inline functions *****************************/
  108.  
  109. inline
  110. void LyxArrayBase::Init()
  111. {
  112.     memset(bf, 0, maxsize);
  113.     last = 0;
  114. }
  115.  
  116. inline // Hmmm, Hp-UX's CC can't handle this inline. Asger.
  117. void LyxArrayBase::Resize(int newsize)
  118. {
  119.     if (newsize<ARRAY_MIN_SIZE)
  120.         newsize = ARRAY_MIN_SIZE;
  121.     newsize += ARRAY_STEP - (newsize % ARRAY_STEP);
  122.     byte *nwbf = new byte[newsize];
  123.     if (last >= newsize) last = newsize-1;
  124.     maxsize = newsize;
  125.     memcpy(nwbf, bf, last);
  126.     delete[] bf;
  127.     bf = nwbf;
  128.     bf[last] = 0;
  129. }
  130.  
  131. inline
  132. LyxArrayBase::LyxArrayBase(int size) 
  133. {
  134.     maxsize = (size<ARRAY_MIN_SIZE) ? ARRAY_MIN_SIZE: size;
  135.     bf = new byte[maxsize]; // this leaks
  136.     Init();
  137. }
  138.  
  139. inline   
  140. LyxArrayBase::~LyxArrayBase() 
  141. {
  142.     delete[] bf;
  143. }
  144.  
  145. inline
  146. LyxArrayBase::LyxArrayBase(const LyxArrayBase& a) 
  147. {
  148.     maxsize = a.maxsize;
  149.     bf = new byte[maxsize];
  150.     memcpy(&bf[0], &a.bf[0], maxsize);
  151.     last = a.last;
  152. }
  153.  
  154. inline
  155. LyxArrayBase& LyxArrayBase::operator=(const LyxArrayBase& a)
  156. {
  157.     if (this != &a) {
  158.         Resize(a.maxsize);
  159.         memcpy(&bf[0], &a.bf[0], maxsize);
  160.     }
  161.     return *this;
  162. }
  163.  
  164. inline   
  165. bool LyxArrayBase::Move(int p, int shift) 
  166. {
  167.     bool result = false;
  168.     if (p<=last) {
  169.         if (last+shift>=maxsize) { 
  170.             Resize(last + shift);
  171.         }
  172.         memmove(&bf[p+shift], &bf[p], last-p);
  173.         last += shift;
  174.         bf[last] = 0;
  175.         result = true;
  176.     }
  177.     return result;
  178. }
  179.  
  180. inline
  181. void LyxArrayBase::Fit()
  182. {
  183.     Resize(last);
  184. }
  185.  
  186. inline
  187. void LyxArrayBase::Remove(int pos, int dx)
  188. {
  189.     Move(pos+dx, -dx);
  190. }    
  191.  
  192. inline
  193. void LyxArrayBase::Merge(LyxArrayBase *a, int p, int dx)
  194. {
  195.     Move(p, dx);
  196.     memcpy(&bf[p], &a->bf[0], dx);
  197. }
  198.  
  199. inline
  200. void LyxArrayBase::MergeF(LyxArrayBase *a, int p, int dx)
  201. {
  202.     memcpy(&bf[p], &a->bf[0], dx);
  203. }
  204.  
  205. inline
  206. void LyxArrayBase::Copy(void *a, int p, int dx)
  207. {
  208.     memcpy(&bf[p], a, dx);
  209. }
  210.  
  211. inline
  212. LyxArrayBase *LyxArrayBase::Extract(int, int dx)
  213. {
  214.     LyxArrayBase *a = new LyxArrayBase(dx);
  215.     a->Merge(this, 0, dx);
  216.     return a;
  217. }
  218.  
  219. inline
  220. byte LyxArrayBase::operator[](const int i)
  221. {
  222.     return bf[i];
  223. }
  224.  
  225.  
  226. inline
  227. void LyxArrayBase::Insert(int pos, byte c)
  228. {
  229.     if (pos<0) pos = last;
  230.     if (pos>=maxsize) 
  231.         Resize(maxsize+ARRAY_STEP);
  232.     bf[pos] = c;
  233.     if (pos>=last)
  234.         last = pos+1;
  235. }
  236.