#include "paragraph.h"
+#ifdef NO_STD_LIST
+
////////// The ParagraphList::iterator
ParagraphList::iterator::iterator()
ParagraphList::iterator &
ParagraphList::iterator::operator++()
{
-#ifndef NO_NEXT
- ptr = ptr->next_;
-#else
ptr = ptr->next_par_;
-#endif
return *this;
}
ParagraphList::iterator &
ParagraphList::iterator::operator--()
{
-#ifndef NO_NEXT
- ptr = ptr->previous_;
-#else
ptr = ptr->prev_par_;
-#endif
return *this;
}
return !(i1 == i2);
}
+////////// The ParagraphList::const_iterator
+
+ParagraphList::const_iterator::const_iterator()
+ : ptr(0)
+{}
+
+
+ParagraphList::const_iterator::const_iterator(Paragraph * p)
+ : ptr(p)
+{}
+
+
+ParagraphList::const_iterator::const_reference
+ParagraphList::const_iterator::operator*()
+{
+ return *ptr;
+}
+
+
+ParagraphList::const_iterator::const_pointer
+ParagraphList::const_iterator::operator->()
+{
+ return ptr;
+}
+
+
+ParagraphList::const_iterator &
+ParagraphList::const_iterator::operator++()
+{
+ ptr = ptr->next_par_;
+ return *this;
+}
+
+
+ParagraphList::const_iterator
+ParagraphList::const_iterator::operator++(int)
+{
+ const_iterator tmp = *this;
+ ++*this;
+ return tmp;
+}
+
+
+ParagraphList::const_iterator &
+ParagraphList::const_iterator::operator--()
+{
+ ptr = ptr->prev_par_;
+ return *this;
+}
+
+
+ParagraphList::const_iterator
+ParagraphList::const_iterator::operator--(int)
+{
+ const_iterator tmp = *this;
+ --*this;
+ return tmp;
+}
+
+
+bool operator==(ParagraphList::const_iterator const & i1,
+ ParagraphList::const_iterator const & i2)
+{
+ return &(*const_cast<ParagraphList::const_iterator&>(i1))
+ == &(*const_cast<ParagraphList::const_iterator&>(i2));
+}
+
+
+bool operator!=(ParagraphList::const_iterator const & i1,
+ ParagraphList::const_iterator const & i2)
+{
+ return !(i1 == i2);
+}
+
//////////
////////// The ParagraphList proper
//////////
: parlist(0)
{
// Deep copy.
- ParagraphList::iterator it = pl.begin();
- ParagraphList::iterator end = pl.end();
+ ParagraphList::const_iterator it = pl.begin();
+ ParagraphList::const_iterator end = pl.end();
for (; it != end; ++it) {
- push_back(new Paragraph(*it, false));
+ push_back(*it);
}
}
ParagraphList::iterator
-ParagraphList::insert(ParagraphList::iterator it, Paragraph * par)
+ParagraphList::insert(ParagraphList::iterator it, Paragraph const & p)
{
-#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
+ Paragraph * par = new Paragraph(p);
+
if (it != end()) {
Paragraph * prev = it->prev_par_;
par->next_par_ = &*it;
par->prev_par_ = last;
}
return iterator(par);
-#endif
}
void ParagraphList::insert(iterator pos, iterator beg, iterator end)
{
for (; beg != end; ++beg) {
- insert(pos, new Paragraph(*beg, false));
+ insert(pos, *beg);
}
}
{
clear();
for (; beg != end; ++beg) {
- push_back(new Paragraph(*beg, false));
+ push_back(*beg);
}
}
+void ParagraphList::splice(iterator pos, ParagraphList & pl)
+{
+ if (pl.parlist == 0)
+ return;
+
+ Paragraph * first = pl.parlist;
+ Paragraph * last = first;
+ while (last->next_par_)
+ last = last->next_par_;
+
+ if (pos == end()) {
+ if (parlist == 0) {
+ parlist = first;
+ } else {
+ Paragraph * last_par = &back();
+ last_par->next_par_ = first;
+ first->prev_par_ = last_par;
+ }
+ } else if (pos == begin()) {
+ last->next_par_ = parlist;
+ parlist->prev_par_ = last;
+ parlist = first;
+ } else {
+ Paragraph * pos_par = &*pos;
+ Paragraph * before_pos = pos_par->prev_par_;
+
+ before_pos->next_par_ = first;
+ first->prev_par_ = before_pos;
+ last->next_par_ = pos_par;
+ pos_par->prev_par_ = last;
+ }
+ pl.parlist = 0;
+}
+
void ParagraphList::clear()
{
-#ifndef NO_NEXT
- while (parlist) {
- 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)
+ParagraphList::iterator 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_;
next->prev_par_ = prev;
delete &*it;
-#endif
+ return next;
+}
+
+
+ParagraphList::iterator ParagraphList::erase(ParagraphList::iterator first,
+ ParagraphList::iterator last)
+{
+ while (first != last) {
+ erase(first++);
+ }
+ return last;
}
}
-ParagraphList::iterator ParagraphList::begin() const
+ParagraphList::const_iterator ParagraphList::begin() const
{
- return iterator(parlist);
+ return const_iterator(parlist);
}
}
-ParagraphList::iterator ParagraphList::end() const
+ParagraphList::const_iterator ParagraphList::end() const
{
- return iterator();
+ return const_iterator();
}
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)
+void ParagraphList::push_back(Paragraph const & pr)
{
- parlist = p;
-}
-
-
-void ParagraphList::push_back(Paragraph * p)
-{
-#ifndef NO_NEXT
- if (!parlist) {
- parlist = p;
- return;
- }
+ Paragraph * p = new Paragraph(pr);
- Paragraph * pos = parlist;
- while (pos->next_)
- pos = pos->next_;
- pos->next_ = p;
- p->previous_ = pos;
-#else
if (!parlist) {
parlist = p;
return;
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;
tmp = tmp->next_par_;
}
return c;
-#endif
}
{
return parlist == 0;
}
+
+#endif