version 1.1.2.1, 2006/06/29 17:31:36
|
version 1.7, 2008/12/01 17:49:52
|
|
|
#include <cassert> |
//%LICENSE//////////////////////////////////////////////////////////////// |
#include "List.h" |
// |
|
// Licensed to The Open Group (TOG) under one or more contributor license |
#ifdef PEGASUS_LINKABLE_SANITY |
// agreements. Refer to the OpenPegasusNOTICE.txt file distributed with |
# define LIST_ASSERT(COND) assert(COND) |
// this work for additional information regarding copyright ownership. |
#else |
// Each contributor licenses this file to you under the OpenPegasus Open |
# define LIST_ASSERT(COND) /* empty */ |
// Source License; you may not use this file except in compliance with the |
#endif |
// License. |
|
// |
|
// Permission is hereby granted, free of charge, to any person obtaining a |
|
// copy of this software and associated documentation files (the "Software"), |
|
// to deal in the Software without restriction, including without limitation |
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
|
// and/or sell copies of the Software, and to permit persons to whom the |
|
// Software is furnished to do so, subject to the following conditions: |
|
// |
|
// The above copyright notice and this permission notice shall be included |
|
// in all copies or substantial portions of the Software. |
|
// |
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
|
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
// |
|
////////////////////////////////////////////////////////////////////////// |
|
// |
|
//%///////////////////////////////////////////////////////////////////////////// |
| |
#define PEGASUS_LIST_MAGIC 0x1234ABCD |
#include "List.h" |
|
#include "PegasusAssert.h" |
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
ListRep::ListRep(void (*destructor)(Linkable*)) : |
ListRep::ListRep(void (*destructor)(Linkable*)) |
_magic(PEGASUS_LIST_MAGIC), _front(0), _back(0), _size(0) |
: _front(0), _back(0), _size(0) |
{ | { |
if (destructor) | if (destructor) |
_destructor = destructor; | _destructor = destructor; |
|
|
| |
ListRep::~ListRep() | ListRep::~ListRep() |
{ | { |
LIST_ASSERT(_magic == PEGASUS_LIST_MAGIC); |
PEGASUS_DEBUG_ASSERT(_magic); |
| |
clear(); | clear(); |
#ifdef PEGASUS_LINKABLE_SANITY |
#ifdef PEGASUS_DEBUG |
memset(this, 0xDD, sizeof(ListRep)); | memset(this, 0xDD, sizeof(ListRep)); |
#endif | #endif |
} | } |
| |
void ListRep::clear() | void ListRep::clear() |
{ | { |
LIST_ASSERT(_magic == PEGASUS_LIST_MAGIC); |
PEGASUS_DEBUG_ASSERT(_magic); |
| |
if (_destructor) | if (_destructor) |
{ | { |
for (Linkable* p = _front; p; ) |
// Reset _front, _back, and _size in case the destructor calls |
|
// a method of List. |
|
|
|
Linkable* front = _front; |
|
|
|
_front = 0; |
|
_back = 0; |
|
_size = 0; |
|
|
|
for (Linkable* p = front; p; ) |
{ | { |
LIST_ASSERT(p->magic == PEGASUS_LINKABLE_MAGIC); |
PEGASUS_DEBUG_ASSERT(p->magic); |
Linkable* next = p->next; | Linkable* next = p->next; |
|
p->list = 0; |
_destructor(p); | _destructor(p); |
p = next; | p = next; |
} | } |
|
|
_front = 0; |
|
_back = 0; |
|
_size = 0; |
|
} | } |
} | } |
| |
void ListRep::insert_front(Linkable* elem) | void ListRep::insert_front(Linkable* elem) |
{ | { |
LIST_ASSERT(_magic == PEGASUS_LIST_MAGIC); |
PEGASUS_DEBUG_ASSERT(_magic); |
LIST_ASSERT(elem != 0); |
PEGASUS_DEBUG_ASSERT(elem != 0); |
LIST_ASSERT(elem->magic == PEGASUS_LINKABLE_MAGIC); |
PEGASUS_DEBUG_ASSERT(elem->magic); |
LIST_ASSERT(elem->list == 0); |
PEGASUS_DEBUG_ASSERT(elem->list == 0); |
| |
elem->list = this; | elem->list = this; |
elem->next = _front; | elem->next = _front; |
|
|
| |
void ListRep::insert_back(Linkable* elem) | void ListRep::insert_back(Linkable* elem) |
{ | { |
LIST_ASSERT(_magic == PEGASUS_LIST_MAGIC); |
PEGASUS_DEBUG_ASSERT(_magic); |
LIST_ASSERT(elem != 0); |
PEGASUS_DEBUG_ASSERT(elem != 0); |
LIST_ASSERT(elem->magic == PEGASUS_LINKABLE_MAGIC); |
PEGASUS_DEBUG_ASSERT(elem->magic); |
LIST_ASSERT(elem->list == 0); |
PEGASUS_DEBUG_ASSERT(elem->list == 0); |
| |
elem->list = this; | elem->list = this; |
elem->prev = _back; | elem->prev = _back; |
|
|
Linkable* pos, | Linkable* pos, |
Linkable* elem) | Linkable* elem) |
{ | { |
LIST_ASSERT(_magic == PEGASUS_LIST_MAGIC); |
PEGASUS_DEBUG_ASSERT(_magic); |
LIST_ASSERT(pos != 0); |
PEGASUS_DEBUG_ASSERT(pos != 0); |
LIST_ASSERT(elem != 0); |
PEGASUS_DEBUG_ASSERT(elem != 0); |
LIST_ASSERT(elem->magic == PEGASUS_LINKABLE_MAGIC); |
PEGASUS_DEBUG_ASSERT(elem->magic); |
LIST_ASSERT(pos->magic == PEGASUS_LINKABLE_MAGIC); |
PEGASUS_DEBUG_ASSERT(pos->magic); |
LIST_ASSERT(elem->list == 0); |
PEGASUS_DEBUG_ASSERT(elem->list == 0); |
| |
elem->list = this; | elem->list = this; |
elem->prev = pos; | elem->prev = pos; |
|
|
Linkable* pos, | Linkable* pos, |
Linkable* elem) | Linkable* elem) |
{ | { |
LIST_ASSERT(_magic == PEGASUS_LIST_MAGIC); |
PEGASUS_DEBUG_ASSERT(_magic); |
LIST_ASSERT(pos != 0); |
PEGASUS_DEBUG_ASSERT(pos != 0); |
LIST_ASSERT(elem != 0); |
PEGASUS_DEBUG_ASSERT(elem != 0); |
LIST_ASSERT(pos->magic == PEGASUS_LINKABLE_MAGIC); |
PEGASUS_DEBUG_ASSERT(pos->magic); |
LIST_ASSERT(elem->magic == PEGASUS_LINKABLE_MAGIC); |
PEGASUS_DEBUG_ASSERT(elem->magic); |
LIST_ASSERT(elem->list == 0); |
PEGASUS_DEBUG_ASSERT(elem->list == 0); |
| |
elem->list = this; | elem->list = this; |
elem->next = pos; | elem->next = pos; |
|
|
| |
void ListRep::remove(Linkable* pos) | void ListRep::remove(Linkable* pos) |
{ | { |
LIST_ASSERT(_magic == PEGASUS_LIST_MAGIC); |
PEGASUS_DEBUG_ASSERT(_magic); |
LIST_ASSERT(pos != 0); |
PEGASUS_DEBUG_ASSERT(pos != 0); |
LIST_ASSERT(pos->magic == PEGASUS_LINKABLE_MAGIC); |
PEGASUS_DEBUG_ASSERT(pos->magic); |
LIST_ASSERT(pos->list == this); |
PEGASUS_DEBUG_ASSERT(pos->list == this); |
LIST_ASSERT(_size != 0); |
PEGASUS_DEBUG_ASSERT(_size != 0); |
| |
if (_size == 0) | if (_size == 0) |
return; | return; |
|
|
| |
bool ListRep::contains(const Linkable* elem) | bool ListRep::contains(const Linkable* elem) |
{ | { |
LIST_ASSERT(_magic == PEGASUS_LIST_MAGIC); |
PEGASUS_DEBUG_ASSERT(_magic); |
LIST_ASSERT(elem != 0); |
PEGASUS_DEBUG_ASSERT(elem != 0); |
LIST_ASSERT(elem->magic == PEGASUS_LINKABLE_MAGIC); |
PEGASUS_DEBUG_ASSERT(elem->magic); |
|
|
return elem && elem->list == this; |
|
| |
#if 0 |
|
for (const Linkable* p = _front; p; p = p->next) | for (const Linkable* p = _front; p; p = p->next) |
{ | { |
if (p == elem) | if (p == elem) |
|
|
| |
// Not found! | // Not found! |
return false; | return false; |
#endif |
|
} | } |
| |
Linkable* ListRep::remove_front() | Linkable* ListRep::remove_front() |
{ | { |
LIST_ASSERT(_magic == PEGASUS_LIST_MAGIC); |
PEGASUS_DEBUG_ASSERT(_magic); |
| |
if (_size == 0) | if (_size == 0) |
return 0; | return 0; |
|
|
| |
Linkable* ListRep::remove_back() | Linkable* ListRep::remove_back() |
{ | { |
LIST_ASSERT(_magic == PEGASUS_LIST_MAGIC); |
PEGASUS_DEBUG_ASSERT(_magic); |
LIST_ASSERT(_size > 0); |
|
|
if (_size == 0) |
|
return 0; |
| |
Linkable* elem = _back; | Linkable* elem = _back; |
remove(elem); | remove(elem); |
|
|
| |
Linkable* ListRep::find(ListRep::Equal equal, const void* client_data) | Linkable* ListRep::find(ListRep::Equal equal, const void* client_data) |
{ | { |
LIST_ASSERT(_magic == PEGASUS_LIST_MAGIC); |
PEGASUS_DEBUG_ASSERT(_magic); |
LIST_ASSERT(equal != 0); |
PEGASUS_DEBUG_ASSERT(equal != 0); |
| |
for (Linkable* p = _front; p; p = p->next) | for (Linkable* p = _front; p; p = p->next) |
{ | { |
if ((*equal)(p, client_data)) | if ((*equal)(p, client_data)) |
{ | { |
LIST_ASSERT(p->magic == PEGASUS_LINKABLE_MAGIC); |
PEGASUS_DEBUG_ASSERT(p->magic); |
return p; | return p; |
} | } |
} | } |
|
|
| |
Linkable* ListRep::remove(ListRep::Equal equal, const void* client_data) | Linkable* ListRep::remove(ListRep::Equal equal, const void* client_data) |
{ | { |
LIST_ASSERT(_magic == PEGASUS_LIST_MAGIC); |
PEGASUS_DEBUG_ASSERT(_magic); |
LIST_ASSERT(equal != 0); |
PEGASUS_DEBUG_ASSERT(equal != 0); |
| |
Linkable* p = find(equal, client_data); | Linkable* p = find(equal, client_data); |
| |
if (p) | if (p) |
{ | { |
LIST_ASSERT(p->magic == PEGASUS_LINKABLE_MAGIC); |
PEGASUS_DEBUG_ASSERT(p->magic); |
remove(p); | remove(p); |
} | } |
| |
|
|
| |
void ListRep::apply(ListRep::Apply apply, const void* client_data) | void ListRep::apply(ListRep::Apply apply, const void* client_data) |
{ | { |
LIST_ASSERT(_magic == PEGASUS_LIST_MAGIC); |
PEGASUS_DEBUG_ASSERT(_magic); |
LIST_ASSERT(apply != 0); |
PEGASUS_DEBUG_ASSERT(apply != 0); |
| |
for (Linkable* p = _front; p; p = p->next) | for (Linkable* p = _front; p; p = p->next) |
(*apply)(p, client_data); | (*apply)(p, client_data); |