]> git.lyx.org Git - lyx.git/blobdiff - src/ParagraphList.C
Alfredo's second patch
[lyx.git] / src / ParagraphList.C
index 923cc78fd026b6a3ce17157dfad15a5d8a91c55d..4f4b50633b97c4835a99dc76f36fac9701489c2d 100644 (file)
@@ -33,7 +33,11 @@ ParagraphList::iterator::operator->()
 ParagraphList::iterator &
 ParagraphList::iterator::operator++()
 {
-       ptr = ptr->next();
+#ifndef NO_NEXT
+       ptr = ptr->next_;
+#else
+       ptr = ptr->next_par_;
+#endif
        return *this;
 }
 
@@ -50,7 +54,11 @@ ParagraphList::iterator::operator++(int)
 ParagraphList::iterator &
 ParagraphList::iterator::operator--()
 {
-       ptr = ptr->previous();
+#ifndef NO_NEXT
+       ptr = ptr->previous_;
+#else
+       ptr = ptr->prev_par_;
+#endif
        return *this;
 }
 
@@ -67,7 +75,8 @@ ParagraphList::iterator::operator--(int)
 bool operator==(ParagraphList::iterator const & i1,
                ParagraphList::iterator const & i2)
 {
-       return &(*const_cast<ParagraphList::iterator&>(i1)) == &(*const_cast<ParagraphList::iterator&>(i2));
+       return &(*const_cast<ParagraphList::iterator&>(i1))
+           == &(*const_cast<ParagraphList::iterator&>(i2));
 }
 
 
@@ -84,17 +93,102 @@ ParagraphList::ParagraphList()
 {}
 
 
+ParagraphList::iterator
+ParagraphList::insert(ParagraphList::iterator it, Paragraph * par)
+{
+#ifndef NO_NEXT
+       if (it != end()) {
+               Paragraph * prev = it->previous_;
+               par->next_ = &*it;
+               par->previous_ = prev;
+               prev->next_ = par;
+               it->previous_ = par;
+       } else if (parlist == 0) {
+               parlist = par;
+       } else {
+               // Find last par.
+               Paragraph * last = parlist;
+               while (last->next_)
+                       last = last->next_;
+               last->next_ = par;
+               par->previous_ = last;
+       }
+       return iterator(par);
+#else
+       if (it != end()) {
+               Paragraph * prev = it->prev_par_;
+               par->next_par_ = &*it;
+               par->prev_par_ = prev;
+               prev->next_par_ = par;
+               it->prev_par_= par;
+       } else if (parlist == 0) {
+               parlist = par;
+       } else {
+               // Find last par.
+               Paragraph * last = parlist;
+               while (last->next_par_)
+                       last = last->next_par_;
+               last->next_par_ = par;
+               par->prev_par_ = last;
+       }
+       return iterator(par);
+#endif
+}
+
+
 void ParagraphList::clear()
 {
+#ifndef NO_NEXT
        while (parlist) {
-               Paragraph * tmp = parlist->next();
+               Paragraph * tmp = parlist->next_;
                delete parlist;
                parlist = tmp;
        }
+#else
+       while (parlist) {
+               Paragraph * tmp = parlist->next_par_;
+               delete parlist;
+               parlist = tmp;
+       }
+#endif
+}
+
+
+void ParagraphList::erase(ParagraphList::iterator it)
+{
+#ifndef NO_NEXT
+       Paragraph * prev = it->previous_;
+       Paragraph * next = it->next_;
+
+       if (prev)
+               prev->next_ = next;
+       else
+               parlist = next;
+
+       if (next)
+               next->previous_ = prev;
+
+       it->previous_ = 0;
+       it->next_ = 0;
+       delete &*it;
+#else
+       Paragraph * prev = it->prev_par_;
+       Paragraph * next = it->next_par_;
+
+       if (prev)
+               prev->next_par_ = next;
+       else
+               parlist = next;
+
+       if (next)
+               next->prev_par_ = prev;
+
+       delete &*it;
+#endif
 }
 
 
-ParagraphList::iterator ParagraphList::begin() 
+ParagraphList::iterator ParagraphList::begin()
 {
        return iterator(parlist);
 }
@@ -118,23 +212,107 @@ ParagraphList::iterator ParagraphList::end() const
 }
 
 
+Paragraph const & ParagraphList::front() const
+{
+       return *parlist;
+}
+
+
+Paragraph & ParagraphList::front()
+{
+       return *parlist;
+}
+
+
+Paragraph const & ParagraphList::back() const
+{
+#ifndef NO_NEXT
+       Paragraph * tmp = parlist;
+       while (tmp->next_)
+               tmp = tmp->next_;
+       return *tmp;
+#else
+       Paragraph * tmp = parlist;
+       while (tmp->next_par_)
+               tmp = tmp->next_par_;
+       return *tmp;
+#endif
+}
+
+
+Paragraph & ParagraphList::back()
+{
+#ifndef NO_NEXT
+       Paragraph * tmp = parlist;
+       while (tmp->next_)
+               tmp = tmp->next_;
+       return *tmp;
+#else
+       Paragraph * tmp = parlist;
+       while (tmp->next_par_)
+               tmp = tmp->next_par_;
+       return *tmp;
+#endif
+}
+
+
 void ParagraphList::set(Paragraph * p)
 {
        parlist = p;
 }
 
 
+void ParagraphList::push_back(Paragraph * p)
+{
+#ifndef NO_NEXT
+       if (!parlist) {
+               parlist = p;
+               return;
+       }
+
+       Paragraph * pos = parlist;
+       while (pos->next_)
+               pos = pos->next_;
+       pos->next_ = p;
+       p->previous_ = pos;
+#else
+       if (!parlist) {
+               parlist = p;
+               return;
+       }
+
+       Paragraph * pos = parlist;
+       while (pos->next_par_)
+               pos = pos->next_par_;
+       pos->next_par_ = p;
+       p->prev_par_ = pos;
+#endif
+}
+
+
 int ParagraphList::size() const
 {
+#ifndef NO_NEXT
+       // When we switch to a std::container this will be O(1)
+       // instead of O(n). (Lgb)
+       Paragraph * tmp = parlist;
+       int c = 0;
+       while (tmp) {
+               ++c;
+               tmp = tmp->next_;
+       }
+       return c;
+#else
        // When we switch to a std::container this will be O(1)
        // instead of O(n). (Lgb)
        Paragraph * tmp = parlist;
        int c = 0;
        while (tmp) {
                ++c;
-               tmp = tmp->next();
+               tmp = tmp->next_par_;
        }
        return c;
+#endif
 }