diff --git a/src/Makefile.am b/src/Makefile.am index 78234a1bd4..7b5a9b53a1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -77,6 +77,8 @@ lyx_SOURCES = \ ParagraphParameters.h \ ParameterStruct.h \ PrinterParams.h \ + RowList.C \ + RowList.h \ ShareContainer.h \ Spacing.C \ Spacing.h \ diff --git a/src/RowList.C b/src/RowList.C new file mode 100644 index 0000000000..599afa3c7a --- /dev/null +++ b/src/RowList.C @@ -0,0 +1,230 @@ +#include + +#include "RowList.h" + +#include "lyxrow.h" + +////////// The RowList::iterator + +RowList::iterator::iterator() + : ptr(0) +{} + + +RowList::iterator::iterator(Row * p) + : ptr(p) +{} + + +RowList::iterator::reference +RowList::iterator::operator*() +{ + return *ptr; +} + + +RowList::iterator::pointer +RowList::iterator::operator->() +{ + return ptr; +} + + +RowList::iterator & +RowList::iterator::operator++() +{ + ptr = ptr->next(); + return *this; +} + + +RowList::iterator +RowList::iterator::operator++(int) +{ + iterator tmp = *this; + ++*this; + return tmp; +} + + +RowList::iterator & +RowList::iterator::operator--() +{ + ptr = ptr->previous(); + return *this; +} + + +RowList::iterator +RowList::iterator::operator--(int) +{ + iterator tmp = *this; + --*this; + return tmp; +} + + +bool operator==(RowList::iterator const & i1, + RowList::iterator const & i2) +{ + return &(*const_cast(i1)) + == &(*const_cast(i2)); +} + + +bool operator!=(RowList::iterator const & i1, + RowList::iterator const & i2) +{ + return !(i1 == i2); +} + + +////////// The RowList proper +RowList::RowList() + : rowlist(0) +{} + + +RowList::iterator +RowList::insert(RowList::iterator it, Row * row) +{ + if (it != end()) { + Row * prev = it->previous(); + row->next(&*it); + row->previous(prev); + prev->next(row); + it->previous(row); + } else if (rowlist == 0) { + rowlist = row; + } else { + // Find last par. + Row * last = rowlist; + while (last->next()) + last = last->next(); + last->next(row); + row->previous(last); + } + return iterator(row); +} + + +void RowList::clear() +{ + while (rowlist) { + Row * tmp = rowlist->next(); + delete rowlist; + rowlist = tmp; + } +} + + +void RowList::erase(RowList::iterator it) +{ + Row * prev = it->previous(); + Row * next = it->next(); + + if (prev) + prev->next(next); + else + rowlist = next; + + if (next) + next->previous(prev); + + delete &*it; +} + + +RowList::iterator RowList::begin() +{ + return iterator(rowlist); +} + + +RowList::iterator RowList::begin() const +{ + return iterator(rowlist); +} + + +RowList::iterator RowList::end() +{ + return iterator(); +} + + +RowList::iterator RowList::end() const +{ + return iterator(); +} + + +Row const & RowList::front() const +{ + return *rowlist; +} + + +Row & RowList::front() +{ + return *rowlist; +} + + +Row const & RowList::back() const +{ + Row * tmp = rowlist; + while (tmp->next()) + tmp = tmp->next(); + return *tmp; +} + + +Row & RowList::back() +{ + Row * tmp = rowlist; + while (tmp->next()) + tmp = tmp->next(); + return *tmp; +} + + +void RowList::set(Row * p) +{ + rowlist = p; +} + + +void RowList::push_back(Row * p) +{ + if (!rowlist) { + rowlist = p; + return; + } + + Row * pos = rowlist; + while (pos->next()) + pos = pos->next(); + pos->next(p); + p->previous(pos); +} + + +int RowList::size() const +{ + // When we switch to a std::container this will be O(1) + // instead of O(n). (Lgb) + Row * tmp = rowlist; + int c = 0; + while (tmp) { + ++c; + tmp = tmp->next(); + } + return c; +} + + +bool RowList::empty() const +{ + return rowlist == 0; +} diff --git a/src/RowList.h b/src/RowList.h new file mode 100644 index 0000000000..f5a6a457f7 --- /dev/null +++ b/src/RowList.h @@ -0,0 +1,90 @@ +// -*- C++ -*- + +#ifndef ROW_LIST_H +#define ROW_LIST_H + +#include + +class Row; + +/// +class RowList { +public: + /// + class iterator { + public: + /// + typedef std::bidirectional_iterator_tag iterator_category; + /// + typedef Row * value_type; + /// + typedef ptrdiff_t difference_type; + /// + typedef Row * pointer; + /// + typedef Row & reference; + /// + iterator(); + /// + iterator(value_type); + /// + reference operator*(); + /// + pointer operator->(); + /// + iterator & operator++(); + /// + iterator operator++(int); + /// + iterator & operator--(); + /// + iterator operator--(int); + private: + /// + Row * ptr; + }; + /// + RowList(); + /// + iterator insert(iterator it, Row * row); + /// + void clear(); + /// + void erase(iterator it); + /// + iterator begin(); + /// + iterator begin() const; + /// + iterator end(); + /// + iterator end() const; + /// + void set(Row *); + /// + void push_back(Row *); + /// + Row const & front() const; + /// + Row & front(); + /// + Row const & back() const; + /// + Row & back(); + /// + int size() const; + /// + bool empty() const; +private: + /// + Row * rowlist; +}; + +/// +bool operator==(RowList::iterator const & i1, + RowList::iterator const & i2); +/// +bool operator!=(RowList::iterator const & i1, + RowList::iterator const & i2); + +#endif