SIMLIB/C++  3.07
list.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 //! \file list.cc internal double-linked list implementation
3 //
4 // Copyright (c) 1991-2004 Petr Peringer
5 //
6 // This library is licensed under GNU Library GPL. See the file COPYING.
7 //
8 
9 //
10 // implementation of double-linked list
11 // base for Queue abstraction (and simple Calendar)
12 //
13 
14 ////////////////////////////////////////////////////////////////////////////
15 // interface
16 //
17 
18 #include "simlib.h"
19 #include "internal.h"
20 
21 
22 ////////////////////////////////////////////////////////////////////////////
23 // implementation
24 //
25 
26 namespace simlib3 {
27 
29 
30 ////////////////////////////////////////////////////////////////////////////
31 // constructors
32 //
33 List::List() : Link(this,this,this), n(0)
34 {
35  Dprintf(("List::List()"));
36 }
37 
38 List::List(const char *name) : Link(this,this,this), n(0)
39 {
40  Dprintf(("List::List(\"%s\")",name));
41  SetName(name);
42 }
43 
44 ////////////////////////////////////////////////////////////////////////////
45 // destructor
46 //
48  Dprintf(("List::~List() // \"%s\" ",Name().c_str()));
49  clear();
50  head = 0; // this is important due to check in ~Link
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////
54 // InsFirst --- insert first
55 //
56 void List::InsFirst(Link *ent)
57 {
58  if(ent->head!=0)
60  ent->pred = this;
61  ent->succ = Link::succ;
62  Link::succ->pred = ent;
63  Link::succ = ent;
64  ent->head = this;
65  n++; // # of list items
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////
69 // InsLast - insert last
70 //
71 void List::InsLast(Link *ent)
72 {
73  if(ent->head!=0)
75  ent->pred = Link::pred;
76  ent->succ = this;
77  Link::pred->succ = ent;
78  Link::pred = ent;
79  ent->head = this;
80  n++; // # of list items
81 }
82 
83 ////////////////////////////////////////////////////////////////////////////
84 // PostIns - insert after
85 //
86 void List::PostIns(Link *ent, iterator pos)
87 {
88  if(pos==end())
89  SIMLIB_error(ListActivityError); /// change !!!!
90  Link *act = *pos;
91  if(act->head != this) // logic-error
92  SIMLIB_error(ListActivityError); /// change !!!!
93  if(ent->head!=0)
95  ent->pred = act;
96  ent->succ = act->succ;
97  act->succ->pred = ent;
98  act->succ = ent;
99  ent->head = this;
100  n++; // # of list items
101 }
102 
103 ////////////////////////////////////////////////////////////////////////////
104 // PredIns --- insert before
105 //
106 void List::PredIns(Link *ent, iterator pos)
107 {
108  if(ent->head!=0)
110  Link *act = *pos;
111  if(act->head != this)
112  SIMLIB_error(ListActivityError); /// change !!!!
113  ent->pred = act->pred;
114  ent->succ = act;
115  act->pred->succ = ent;
116  act->pred = ent;
117  ent->head = this;
118  n++; // # of list items
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////
122 // GetFirst --- remove first item from list
123 //
125 {
126  return Get(begin());
127 }
128 
129 ////////////////////////////////////////////////////////////////////////////
130 // GetLast --- remove last item from list
131 //
133 {
134  return Get(--end());
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////
138 // Get --- remove selected item from list (virtual)
139 //
141 {
142  if(empty())
144  if(pos==end())
145  SIMLIB_error(ListActivityError); // change !!
146  Link *x = *pos;
147  if (x->head != this)
148  SIMLIB_error(LinkOutError); // can't remove if not in list
149 
150  (x->pred->succ = x->succ)->pred = x->pred;
151  x->head = 0; // ---- not in list
152  n--; // # of items in list
153  return (x);
154 }
155 
156 ////////////////////////////////////////////////////////////////////////////
157 // clear --- list initialization (remove all items)
158 //
160 {
161  while(!empty()) {
162  Link *e = GetFirst(); // remove entity from list
163  if(e->isAllocated()) delete e; // free memory ?????????????###
164  }
165 }
166 
167 }
168 // end
169 
void PredIns(Link *e, iterator pos)
Definition: list.cc:106
void SIMLIB_error(const enum _ErrEnum N)
print error message and abort program
Definition: error.cc:38
void SetName(const std::string &name)
assign the name
Definition: object.cc:125
virtual Link * Get(iterator pos)
remove at position
Definition: list.cc:140
void InsLast(Link *e)
Definition: list.cc:71
bool isAllocated() const
Definition: simlib.h:318
Implementation of class CalendarList interface is static - using global functions in SQS namespace...
Definition: algloop.cc:32
Link * GetFirst()
remove first
Definition: list.cc:124
void clear()
Definition: list.cc:159
unsigned n
Definition: simlib.h:633
bool empty()
Definition: simlib.h:676
#define SIMLIB_internal_error()
Definition: internal.h:167
Internal header file for SIMLIB/C++.
Main SIMLIB/C++ interface.
virtual std::string Name() const
get object name
Definition: object.cc:134
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
void PostIns(Link *e, iterator pos)
Definition: list.cc:86
iterator begin()
Definition: simlib.h:672
Link * GetLast()
remove last
Definition: list.cc:132
#define Dprintf(f)
Definition: internal.h:100
iterator end()
Definition: simlib.h:673
void InsFirst(Link *e)
Definition: list.cc:56