| 1 |
/* |
| 2 |
* SimpleList.cpp |
| 3 |
* AgentJ |
| 4 |
* |
| 5 |
* Created by Ian Taylor on 17/07/2008. |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "SimpleList.h" |
| 10 |
|
| 11 |
ListItem::ListItem() : identifier(-1), portnumber(-1), address(-1) {} |
| 12 |
|
| 13 |
SimpleList::SimpleList() : head(NULL), tail(NULL) { |
| 14 |
} |
| 15 |
|
| 16 |
SimpleList::~SimpleList() { |
| 17 |
ListItem* next = head; |
| 18 |
ListItem* todelete; |
| 19 |
|
| 20 |
// empty the list and delete objects |
| 21 |
while (next) { |
| 22 |
todelete = next; |
| 23 |
next = next->getNext(); |
| 24 |
delete todelete; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
void SimpleList::print() { |
| 29 |
ListItem* next = head; |
| 30 |
int count=0; |
| 31 |
// empty the list and delete objects |
| 32 |
while (next) { |
| 33 |
PLOG(PL_INFO, "Item %i pointer = %u\n", count, next); |
| 34 |
PLOG(PL_INFO, "Prev = %u Next = %u\n", next->getPrev(), next->getNext()); |
| 35 |
next = next->getNext(); |
| 36 |
++count; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
void SimpleList::prepend(ListItem *proxy) { |
| 42 |
PLOG(PL_DEBUG, "Entering SimpleList::prepend\n"); |
| 43 |
proxy->setPrev(NULL); |
| 44 |
proxy->setNext(head); |
| 45 |
if (head) { |
| 46 |
head->setPrev(proxy); |
| 47 |
if (head->getNext()==NULL) tail=head; |
| 48 |
} else tail=proxy; |
| 49 |
|
| 50 |
head = proxy; |
| 51 |
|
| 52 |
PLOG(PL_DEBUG, "Leaving SimpleList::prepend\n"); |
| 53 |
} // end SimpleList::prepend() |
| 54 |
|
| 55 |
/** |
| 56 |
* Removes the object from the list but does not delete the object. This is |
| 57 |
* the responsibility of the caller. |
| 58 |
*/ |
| 59 |
void SimpleList::remove(ListItem *proxy) |
| 60 |
{ |
| 61 |
PLOG(PL_DEBUG, "Entering SimpleList::remove\n"); |
| 62 |
ListItem* prev = proxy->getPrev(); |
| 63 |
ListItem* next = proxy->getNext(); |
| 64 |
|
| 65 |
// PLOG(PL_DETAIL, "SimpleList, prev = %u, next =%u\n", prev, next); |
| 66 |
// PLOG(PL_DETAIL, "SimpleList, head = %u, tail =%u\n", head, tail); |
| 67 |
|
| 68 |
if (prev) |
| 69 |
prev->setNext(next); |
| 70 |
else |
| 71 |
head = next; |
| 72 |
if (next) |
| 73 |
next->setPrev(prev); |
| 74 |
else |
| 75 |
tail=prev; |
| 76 |
|
| 77 |
// PLOG(PL_DETAIL, "SimpleList, head = %u, tail =%u\n", head, tail); |
| 78 |
|
| 79 |
PLOG(PL_DEBUG, "Leaving SimpleList::remove\n"); |
| 80 |
} // end SimpleList::remove() |
| 81 |
|
| 82 |
ListItem* SimpleList::findProxyByPortAndAddress(unsigned int address, unsigned int port) { |
| 83 |
PLOG(PL_DEBUG, "Entering SimpleList::findProxyByPortAndAddress\n"); |
| 84 |
ListItem* next = head; |
| 85 |
while (next) { |
| 86 |
PLOG(PL_DEBUG, "SimpleList: comparing %u and port %u\n", next->getAddress(), next->getPort()); |
| 87 |
PLOG(PL_DEBUG, "SimpleList: with %u and port %u\n", address, port); |
| 88 |
if ((next->getPort() == port) && (next->getAddress()==address)) |
| 89 |
return next; |
| 90 |
else |
| 91 |
next = next->getNext(); |
| 92 |
} |
| 93 |
PLOG(PL_DEBUG, "Leaving SimpleList::findProxyByPortAndAddress\n"); |
| 94 |
return NULL; |
| 95 |
} // end SimpleList::findProxyByPortAndAddress() |
| 96 |
|
| 97 |
ListItem* SimpleList::findProxyByIdentifier(unsigned int identifier) |
| 98 |
{ |
| 99 |
PLOG(PL_DEBUG, "Entering SimpleList::findProxyByIdentifier\n"); |
| 100 |
ListItem* next = head; |
| 101 |
while (next) |
| 102 |
{ |
| 103 |
if (next->getIdentifier() == identifier) |
| 104 |
return next; |
| 105 |
else |
| 106 |
next = next->getNext(); |
| 107 |
} |
| 108 |
PLOG(PL_DEBUG, "Leaving SimpleList::findProxyByIdentifier\n"); |
| 109 |
return NULL; |
| 110 |
} // end SimpleList::findProxyByIdentifier() |