Andre Poenitz <[EMAIL PROTECTED]> writes:
| I am trying to move more low-level functionality from math_iter to array.
| In the end it should be possible to get rid of last_ as well and use
| standard std::vector semantics (allocated to capacity(), filled to size())
| entirely.
Good, that is my plan as well.
| Anybody noticed we've closed a memory leak already? ;-}
We did?
| int MathedArray::maxsize() const
| {
| - return maxsize_;
| + return static_cast<int>(bf_.size());
| }
Why not change the prototype for maxsize at the same time?
MathedArray::size_type MathedArray::maxsize() const
{
return bf_.size();
}
|
|
| +void MathedArray::need_size(int needed)
void MathedArray::need_size(MathedArray::size_type needed)
| +{
| + if (needed >= maxsize())
| + resize(needed);
| +}
| +
| +
| void MathedArray::resize(int newsize)
MathedArray::resize(MathedArray::size_type newsize)
| {
| if (newsize < ARRAY_MIN_SIZE)
| @@ -51,15 +58,14 @@ void MathedArray::resize(int newsize)
| newsize += ARRAY_STEP - (newsize % ARRAY_STEP);
| bf_.resize(newsize);
| if (last_ >= newsize) last_ = newsize - 1;
| - maxsize_ = newsize;
| bf_[last_] = 0;
| }
|
|
| MathedArray::MathedArray(int size)
MathedArray::MathedArray(MathedArray::size_type size)
| {
| - maxsize_ = (size < ARRAY_MIN_SIZE) ? ARRAY_MIN_SIZE : size;
| - bf_.resize(maxsize_);
| + int newsize = (size < ARRAY_MIN_SIZE) ? ARRAY_MIN_SIZE : size;
MathedArray::size_type newsize = ...;
| + bf_.resize(newsize);
| last_ = 0;
| }
|
| @@ -67,9 +73,7 @@ MathedArray::MathedArray(int size)
| void MathedArray::move(int p, int shift)
| {
| if (p <= last_) {
| - if (last_ + shift >= maxsize_) {
| - resize(last_ + shift);
| - }
| + need_size(last_ + shift);
| memmove(&bf_[p + shift], &bf_[p], last_ - p);
| last_ += shift;
| bf_[last_] = 0;
| @@ -117,8 +121,11 @@ byte & MathedArray::operator[](int i)
| void MathedArray::insert(int pos, byte c)
| {
| if (pos < 0) pos = last_;
| - if (pos >= maxsize_)
| - resize(maxsize_ + ARRAY_STEP);
| +
| + // I think this should be replaced by need_size(pos). Note that the
| + // current code looks troublesome if pos > maxsize() + ARRAY_STEP.
| + if (pos >= maxsize())
| + resize(maxsize() + ARRAY_STEP);
| bf_[pos] = c;
| if (pos >= last_)
| last_ = pos + 1;
| Index: array.h
| ===================================================================
| RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/array.h,v
| retrieving revision 1.10
| diff -u -p -r1.10 array.h
| --- array.h 2001/02/12 08:55:14 1.10
| +++ array.h 2001/02/12 10:54:29
| @@ -91,13 +91,13 @@ public:
| void resize(int newsize);
| ///
| int maxsize() const;
| + /// Make sure we can access at least \a needed elements
| + void need_size(int needed);
Can't this be private?
| private:
|
| /// Buffer
| buffer_type bf_;
| /// Last position inserted.
| int last_;
| - /// Max size of the array.
| - int maxsize_;
| };
| #endif
| Index: math_iter.C
| ===================================================================
| RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_iter.C,v
| retrieving revision 1.27
| diff -u -p -r1.27 math_iter.C
| --- math_iter.C 2001/02/12 08:55:14 1.27
| +++ math_iter.C 2001/02/12 10:54:29
| @@ -194,9 +194,7 @@ void MathedIter::Insert(byte c, MathedTe
| if (pos < array->last())
| array->move(pos, shift);
| else {
| - if (array->last() + shift >= array->maxsize()) {
| - array->resize(array->last() + shift);
| - }
| + array->need_size(array->last() + shift);
Why not stil use the resize method? But do it unconditinally?
| array->last(array->last() + shift);
| (*array)[array->last()] = '\0';
| }
| @@ -230,9 +228,7 @@ void MathedIter::split(int shift)
| array->move(pos, shift);
| if (fg) (*array)[pos + shift - 1] = fcode;
| } else {
| - if (array->last() + shift >= array->maxsize()) {
| - array->resize(array->last() + shift);
| - }
| + array->need_size(array->last() + shift);
ditto.
| array->last(array->last() + shift);
| }
| (*array)[array->last()] = '\0';
Lgb