]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/array.h
add std support
[lyx.git] / src / mathed / array.h
index 4be93b9bfca5b82462863db3790c23d0a539c6a6..158affcdbfb3d6350021e58da569b5394702f70e 100644 (file)
@@ -1,6 +1,5 @@
 // -*- C++ -*-
 /*
- *  File:        array.h
  *  Purpose:     A general purpose resizable array.  
  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
  *  Created:     January 1996
  *   the GNU General Public Licence version 2 or later.
  */
 
-#include <string.h>
+#ifndef MATHEDARRAY_H
+#define MATHEDARRAY_H
 
-#ifndef byte
-#define byte unsigned char
+#include <vector>
+#include <iosfwd>
+
+#include "mathed/support.h"
+
+class MathedInset;
+
+#ifdef __GNUG__
+#pragma interface
 #endif
 
-/** A resizable array.
+/** \class MathedArray
+    \brief A resizable array.
+    
     A general purpose resizable array.
-    @author Alejandro Aguilar Sierra
-    @version January 1996
+    
+    \author Alejandro Aguilar Sierra
+    \author André Pönitz
+    \author Lars Gullik Bjønnes
+    \version February 2001
   */
-class LyxArrayBase  {
+class MathedArray  {
 public:
        ///
-       enum {
-               ///
-               ARRAY_SIZE = 256,
-               ///
-               ARRAY_STEP = 16,
-               ///
-               ARRAY_MIN_SIZE = 4
-       };
-
+       typedef std::vector<byte>           buffer_type;
+       typedef byte                        value_type;
+       typedef buffer_type::size_type      size_type;
+       typedef buffer_type::iterator       iterator;
+       typedef buffer_type::const_iterator const_iterator;
+       
        ///
-       explicit
-       LyxArrayBase(int size = ARRAY_STEP);
+       MathedArray();
        ///
-       LyxArrayBase(LyxArrayBase const &);
+       MathedArray(MathedArray const &);
        ///
-       ~LyxArrayBase();
-   
-       /// Constructs a new array with dx elements starting at pos 
-       LyxArrayBase & operator=(LyxArrayBase const &); 
+       MathedArray & operator=(MathedArray const &);
+       ///
+       ~MathedArray();
 
        ///
-       int empty() const { return (last == 0); }
-   
+       iterator begin();
        ///
-       int Last() { return last; }
-   
-       /// Fills with 0 the entire array and set last to 0
-       void Init();     
+       iterator end();
+       ///
+       const_iterator begin() const;
+       ///
+       const_iterator end() const;
+       
+       ///
+       int empty() const;
+       ///
+       void clear();
    
-       /// Make the allocated memory fit the needed size
-       void Fit();     
-
-       /// Remove dx elements from position pos. Don't changes the size
-       void Remove(int pos, int dx);   
-
-       /// Merge dx elements from array a at pos. Changes the size if necessary.
-       void Merge(LyxArrayBase * a, int pos, int dx); 
-
-       /// Same as Merge but doesn't changes the size (dangerous)
-       void MergeF(LyxArrayBase * a, int pos, int dx); 
-
-       /// Copy dx byts from an array at position pos
-       void Copy(void *, int pos, int dx); 
-
-       /// Constructs a new array with dx elements starting at pos 
-       LyxArrayBase * Extract(int pos, int dx); 
-
-       /// Insert a character at position pos
-       void Insert(int pos, byte);
-
-       /// Constructs a new array with dx elements starting at pos 
-       byte operator[](const int);
+       ///
+       int last() const;
+       ///
+       void last(int l);
 
-protected:
        ///
-       void Resize(int newsize);
+       void swap(MathedArray &);
        ///
-       bool Move(int p, int shift);
+       void shrink(int pos1, int pos2);
 
-       /// Buffer
-       byte * bf;
-       /// Last position inserted.
-       int last;
-       /// Max size of the array.
-       int maxsize;
+#if 0
+       ///
+       void insert(iterator pos, const_iterator beg, const_iterator end);
+#else
+       /// Merge \a dx elements from array \a a at \apos.
+       /// This doesn't changes the size (dangerous)
+       void merge(MathedArray const & a, int pos); 
+#endif
+       ///
+       void raw_pointer_copy(MathedInset ** p, int pos) const;
+#if 0
+       ///
+       void insertInset(int pos, MathedInset * p, int type);
+       ///
+       MathedInset * getInset(int pos);
+#else
+       ///
+       void raw_pointer_insert(void * p, int pos, int len);
+#endif
+       ///
+       void strange_copy(MathedArray * dest, int dpos, int spos, int len);
+       ///
+       byte operator[](int) const;
+       ///
+       byte & operator[](int i);
+       
+       ///
+       void move(int p, int shift);
+       ///
+       void resize(int newsize);
+       /// Make sure we can access at least \a needed elements
+       void need_size(int needed);
+       ///
+       void dump(std::ostream &) const;
 private:
+       /// Buffer
+       buffer_type bf_;
+#if 0
        ///
-       friend class MathedIter;
+       struct InsetTable {
+               ///
+               int pos;
+               ///
+               MathedInset * inset;
+               ///
+               InsetTable(int p, MathedInset * i)
+                       : pos(p), inset(i) {}
+               
+       };
+       /// 
+       typedef std::vector<InsetTable> InsetList;
+       /// The list of insets in this array.
+       InsetList insetList_;
+#endif
+       /// Last position inserted.
+       int last_;
 };
-   
-
-
-/************************ Inline functions *****************************/
-
-inline
-void LyxArrayBase::Init()
-{
-       memset(bf, 0, maxsize);
-       last = 0;
-}
-
-inline // Hmmm, Hp-UX's CC can't handle this inline. Asger.
-void LyxArrayBase::Resize(int newsize)
-{
-       if (newsize<ARRAY_MIN_SIZE)
-               newsize = ARRAY_MIN_SIZE;
-       newsize += ARRAY_STEP - (newsize % ARRAY_STEP);
-       byte *nwbf = new byte[newsize];
-       if (last >= newsize) last = newsize-1;
-       maxsize = newsize;
-       memcpy(nwbf, bf, last);
-       delete[] bf;
-       bf = nwbf;
-       bf[last] = 0;
-}
-
-inline
-LyxArrayBase::LyxArrayBase(int size) 
-{
-       maxsize = (size<ARRAY_MIN_SIZE) ? ARRAY_MIN_SIZE: size;
-       bf = new byte[maxsize]; // this leaks
-       Init();
-}
-
-inline   
-LyxArrayBase::~LyxArrayBase() 
-{
-       delete[] bf;
-}
-
-inline
-LyxArrayBase::LyxArrayBase(LyxArrayBase const & a) 
-{
-       maxsize = a.maxsize;
-       bf = new byte[maxsize];
-       memcpy(&bf[0], &a.bf[0], maxsize);
-       last = a.last;
-}
-
-inline
-LyxArrayBase & LyxArrayBase::operator=(LyxArrayBase const & a)
-{
-       if (this != &a) {
-               Resize(a.maxsize);
-               memcpy(&bf[0], &a.bf[0], maxsize);
-       }
-       return *this;
-}
-
-inline   
-bool LyxArrayBase::Move(int p, int shift) 
-{
-       bool result = false;
-       if (p <= last) {
-               if (last + shift >= maxsize) { 
-                   Resize(last + shift);
-               }
-               memmove(&bf[p + shift], &bf[p], last - p);
-               last += shift;
-               bf[last] = 0;
-               result = true;
-       }
-       return result;
-}
-
-inline
-void LyxArrayBase::Fit()
-{
-       Resize(last);
-}
-
-inline
-void LyxArrayBase::Remove(int pos, int dx)
-{
-       Move(pos + dx, -dx);
-}    
-
-inline
-void LyxArrayBase::Merge(LyxArrayBase * a, int p, int dx)
-{
-       Move(p, dx);
-       memcpy(&bf[p], &a->bf[0], dx);
-}
-inline
-void LyxArrayBase::MergeF(LyxArrayBase * a, int p, int dx)
-{
-       memcpy(&bf[p], &a->bf[0], dx);
-}
-inline
-void LyxArrayBase::Copy(void * a, int p, int dx)
-{
-       memcpy(&bf[p], a, dx);
-}
-
-inline
-LyxArrayBase * LyxArrayBase::Extract(int, int dx)
-{
-       LyxArrayBase * a = new LyxArrayBase(dx);
-       a->Merge(this, 0, dx);
-       return a;
-}
-inline
-byte LyxArrayBase::operator[](const int i)
-{
-       return bf[i];
-}
-
-
-inline
-void LyxArrayBase::Insert(int pos, byte c)
-{
-       if (pos < 0) pos = last;
-       if (pos >= maxsize) 
-               Resize(maxsize + ARRAY_STEP);
-       bf[pos] = c;
-       if (pos >= last)
-               last = pos + 1;
-}
+#endif