1998-06-29 12:44:36 +00:00
|
|
|
|
/*-*- c++ -*-********************************************************
|
|
|
|
|
* kbList.h : a double linked list *
|
|
|
|
|
* *
|
|
|
|
|
* (C) 1998 by Karsten Ball<EFBFBD>der (Ballueder@usa.net) *
|
|
|
|
|
* *
|
|
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
*******************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef KBLIST_H
|
|
|
|
|
# define KBLIST_H
|
|
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
|
# pragma interface "kbList.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef NULL
|
|
|
|
|
# define NULL 0
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/**@name Double linked list implementation. */
|
|
|
|
|
//@{
|
|
|
|
|
|
|
|
|
|
/** kbListNode is a class used by kbList. It represents a single
|
|
|
|
|
element in the list. It is not intended for general use outside
|
|
|
|
|
kbList functions.
|
|
|
|
|
*/
|
|
|
|
|
struct kbListNode
|
|
|
|
|
{
|
|
|
|
|
/// pointer to next node or NULL
|
|
|
|
|
struct kbListNode *next;
|
|
|
|
|
/// pointer to previous node or NULL
|
|
|
|
|
struct kbListNode *prev;
|
1998-11-19 20:34:59 +00:00
|
|
|
|
/// pointer to the actual data
|
1998-06-29 12:44:36 +00:00
|
|
|
|
void *element;
|
|
|
|
|
/** Constructor - it automatically links the node into the list, if
|
|
|
|
|
the iprev, inext parameters are given.
|
|
|
|
|
@param ielement pointer to the data for this node (i.e. the data itself)
|
|
|
|
|
@param iprev if not NULL, use this as previous element in list
|
|
|
|
|
@param inext if not NULL, use this as next element in list
|
|
|
|
|
*/
|
|
|
|
|
kbListNode( void *ielement,
|
1998-11-19 20:34:59 +00:00
|
|
|
|
kbListNode *iprev = NULL,
|
|
|
|
|
kbListNode *inext = NULL);
|
1998-06-29 12:44:36 +00:00
|
|
|
|
/// Destructor.
|
|
|
|
|
~kbListNode();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** The main list class, handling void pointers as data.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class kbList
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/// An iterator class for kbList, just like for the STL classes.
|
|
|
|
|
class iterator
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
/// the node to which this iterator points
|
|
|
|
|
kbListNode *node;
|
|
|
|
|
friend class kbList;
|
|
|
|
|
public:
|
|
|
|
|
/** Constructor.
|
|
|
|
|
@param n if not NULL, the node to which to point
|
|
|
|
|
*/
|
1998-11-19 20:34:59 +00:00
|
|
|
|
iterator(kbListNode *n = NULL);
|
1998-06-29 12:44:36 +00:00
|
|
|
|
/** Dereference operator.
|
|
|
|
|
@return the data pointer of the node belonging to this
|
|
|
|
|
iterator
|
|
|
|
|
*/
|
|
|
|
|
void * operator*();
|
|
|
|
|
|
1998-11-19 20:34:59 +00:00
|
|
|
|
/** This operator allows us to write if(i). It is <em>not</em> a
|
|
|
|
|
dereference operator and the result is always useless apart
|
|
|
|
|
from its logical value!
|
|
|
|
|
*/
|
|
|
|
|
operator void*() const { return node == NULL ? (void*)0 : (void*)(-1); }
|
|
|
|
|
|
1998-06-29 12:44:36 +00:00
|
|
|
|
/** Increment operator - prefix, goes to next node in list.
|
|
|
|
|
@return itself
|
|
|
|
|
*/
|
|
|
|
|
iterator & operator++();
|
|
|
|
|
|
|
|
|
|
/** Decrement operator - prefix, goes to previous node in list.
|
|
|
|
|
@return itself
|
|
|
|
|
*/
|
|
|
|
|
iterator & operator--();
|
|
|
|
|
|
|
|
|
|
/** Increment operator - prefix, goes to next node in list.
|
|
|
|
|
@return itself
|
|
|
|
|
*/
|
|
|
|
|
iterator & operator++(int); //postfix
|
|
|
|
|
|
|
|
|
|
/** Decrement operator - prefix, goes to previous node in list.
|
|
|
|
|
@return itself
|
|
|
|
|
*/
|
|
|
|
|
iterator & operator--(int); //postfix
|
|
|
|
|
|
|
|
|
|
/** Comparison operator.
|
|
|
|
|
@return true if not equal.
|
|
|
|
|
*/
|
1998-11-19 20:34:59 +00:00
|
|
|
|
bool operator !=(iterator const &) const;
|
1998-06-29 12:44:36 +00:00
|
|
|
|
|
|
|
|
|
/* Comparison operator.
|
|
|
|
|
@return true if equal
|
|
|
|
|
*/
|
1998-11-19 20:34:59 +00:00
|
|
|
|
bool operator ==(iterator const &) const;
|
1998-06-29 12:44:36 +00:00
|
|
|
|
|
|
|
|
|
/** Returns a pointer to the node associated with this iterator.
|
|
|
|
|
This function is not for general use and should be
|
|
|
|
|
protected. However, if protected, it cannot be called from
|
|
|
|
|
derived classes' iterators. (Is this a bug in gcc/egcs?)
|
|
|
|
|
@return the node pointer
|
|
|
|
|
*/
|
|
|
|
|
inline kbListNode * Node(void) const
|
|
|
|
|
{ return node; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Constructor.
|
|
|
|
|
@param ownsEntriesFlag if true, the list owns the entries and
|
|
|
|
|
will issue a delete on each of them when deleting them. If
|
|
|
|
|
false, the entries themselves will not get deleted. Do not use
|
|
|
|
|
this with array types!
|
|
|
|
|
*/
|
|
|
|
|
kbList(bool ownsEntriesFlag = true);
|
|
|
|
|
|
|
|
|
|
/** Destructor.
|
|
|
|
|
If entries are owned, they will all get deleted from here.
|
|
|
|
|
*/
|
|
|
|
|
~kbList();
|
|
|
|
|
|
|
|
|
|
/** Tell list whether it owns objects. If owned, they can be
|
|
|
|
|
deleted by list. See the constructor for more details.
|
|
|
|
|
@param ownsflag if true, list will own entries
|
|
|
|
|
*/
|
|
|
|
|
void ownsObjects(bool ownsflag = true)
|
|
|
|
|
{ ownsEntries = ownsflag; }
|
|
|
|
|
|
|
|
|
|
/** Query whether list owns entries.
|
|
|
|
|
@return true if list owns entries
|
|
|
|
|
*/
|
|
|
|
|
bool ownsObjects(void)
|
|
|
|
|
{ return ownsEntries; }
|
1998-11-19 20:34:59 +00:00
|
|
|
|
|
1998-06-29 12:44:36 +00:00
|
|
|
|
/** Add an entry at the end of the list.
|
|
|
|
|
@param element pointer to data
|
|
|
|
|
*/
|
|
|
|
|
void push_back(void *element);
|
|
|
|
|
|
|
|
|
|
/** Add an entry at the head of the list.
|
|
|
|
|
@param element pointer to data
|
|
|
|
|
*/
|
|
|
|
|
void push_front(void *element);
|
|
|
|
|
|
|
|
|
|
/** Get element from end of the list and delete it.
|
|
|
|
|
NOTE: In this case the element's data will not get deleted by
|
|
|
|
|
the list. It is the responsibility of the caller to free it.
|
|
|
|
|
@return the element data
|
|
|
|
|
*/
|
|
|
|
|
void *pop_back(void);
|
|
|
|
|
|
|
|
|
|
/** Get element from head of the list and delete it.
|
|
|
|
|
NOTE: In this case the element's data will not get deleted by
|
|
|
|
|
the list. It is the responsibility of the caller to free it.
|
|
|
|
|
@return the element data
|
|
|
|
|
*/
|
|
|
|
|
void *pop_front(void);
|
|
|
|
|
|
|
|
|
|
/** Insert an element into the list.
|
1998-11-19 20:34:59 +00:00
|
|
|
|
@param i an iterator pointing to the element, before which the new one should be inserted
|
1998-06-29 12:44:36 +00:00
|
|
|
|
@param element the element data
|
|
|
|
|
*/
|
|
|
|
|
void insert(iterator & i, void *element);
|
|
|
|
|
|
1998-11-19 20:34:59 +00:00
|
|
|
|
/** Remove an element from the list _without_ deleting the object.
|
|
|
|
|
@param i iterator pointing to the element to be deleted
|
|
|
|
|
@return the value of the element just removed
|
|
|
|
|
*/
|
|
|
|
|
void *remove(iterator& i) { void *p = *i; doErase(i); return p; }
|
|
|
|
|
|
1998-06-29 12:44:36 +00:00
|
|
|
|
/** Erase an element, move iterator to following element.
|
|
|
|
|
@param i iterator pointing to the element to be deleted
|
|
|
|
|
*/
|
1998-11-19 20:34:59 +00:00
|
|
|
|
void erase(iterator & i) { deleteContent(i); doErase(i); }
|
|
|
|
|
|
1998-06-29 12:44:36 +00:00
|
|
|
|
/* Get head of list.
|
|
|
|
|
@return iterator pointing to head of list
|
|
|
|
|
*/
|
|
|
|
|
iterator begin(void) const;
|
|
|
|
|
|
|
|
|
|
/* Get end of list.
|
|
|
|
|
@return iterator pointing after the end of the list. This is an
|
|
|
|
|
invalid iterator which cannot be dereferenced or decremented. It is
|
|
|
|
|
only of use in comparisons. NOTE: this is different from STL!
|
|
|
|
|
@see tail
|
|
|
|
|
*/
|
|
|
|
|
iterator end(void) const;
|
|
|
|
|
|
|
|
|
|
/* Get last element in list.
|
|
|
|
|
@return iterator pointing to the last element in the list.
|
|
|
|
|
@see end
|
|
|
|
|
*/
|
|
|
|
|
iterator tail(void) const;
|
|
|
|
|
|
|
|
|
|
/* Get the number of elements in the list.
|
|
|
|
|
@return number of elements in the list
|
|
|
|
|
*/
|
|
|
|
|
unsigned size(void) const;
|
|
|
|
|
|
|
|
|
|
/* Query whether list is empty.
|
|
|
|
|
@return true if list is empty
|
|
|
|
|
*/
|
1998-11-19 20:34:59 +00:00
|
|
|
|
inline bool empty(void) const
|
1998-06-29 12:44:36 +00:00
|
|
|
|
{ return first == NULL ; }
|
|
|
|
|
|
1998-08-12 08:33:34 +00:00
|
|
|
|
protected:
|
1998-06-29 12:44:36 +00:00
|
|
|
|
/// if true, list owns entries
|
|
|
|
|
bool ownsEntries;
|
|
|
|
|
/// pointer to first element in list
|
|
|
|
|
kbListNode *first;
|
|
|
|
|
/// pointer to last element in list
|
|
|
|
|
kbListNode *last;
|
1998-11-19 20:34:59 +00:00
|
|
|
|
protected:
|
|
|
|
|
/** Erase an element, move iterator to following element.
|
|
|
|
|
@param i iterator pointing to the element to be deleted
|
|
|
|
|
*/
|
|
|
|
|
void doErase(iterator & i);
|
|
|
|
|
|
|
|
|
|
/** Deletes the actual content if ownsflag is set.
|
|
|
|
|
param iterator i
|
|
|
|
|
*/
|
|
|
|
|
inline void deleteContent(iterator i)
|
|
|
|
|
{ if(ownsEntries) delete *i; }
|
|
|
|
|
|
1998-06-29 12:44:36 +00:00
|
|
|
|
|
1998-08-12 08:33:34 +00:00
|
|
|
|
private:
|
1998-06-29 12:44:36 +00:00
|
|
|
|
/// forbid copy construction
|
|
|
|
|
kbList(kbList const &foo);
|
|
|
|
|
/// forbid assignments
|
|
|
|
|
kbList& operator=(const kbList& foo);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// just for backward compatibility, will be removed soon
|
|
|
|
|
typedef kbList::iterator kbListIterator;
|
|
|
|
|
/// cast an iterator to a pointer, compatibility only to be removed
|
|
|
|
|
#define kbListICast(type, iterator) ((type *)*iterator)
|
|
|
|
|
/// cast an iterator to a const pointer, compatibility only to be removed
|
|
|
|
|
#define kbListIcCast(type, iterator) ((type const *)*iterator)
|
|
|
|
|
|
|
|
|
|
/** Macro to define a kbList with a given name, having elements of
|
|
|
|
|
pointer to the given type. I.e. KBLIST_DEFINE(Int,int) would
|
|
|
|
|
create a kbListInt type holding int pointers.
|
|
|
|
|
*/
|
|
|
|
|
#define KBLIST_DEFINE(name,type) \
|
|
|
|
|
class name : public kbList \
|
|
|
|
|
{ \
|
|
|
|
|
public: \
|
|
|
|
|
class iterator : public kbList::iterator \
|
|
|
|
|
{ \
|
|
|
|
|
protected: \
|
|
|
|
|
inline iterator(kbList::iterator const & i) \
|
|
|
|
|
{ node = i.Node(); } \
|
|
|
|
|
friend class name; \
|
|
|
|
|
public: \
|
1998-11-19 20:34:59 +00:00
|
|
|
|
inline iterator(kbListNode *n = NULL) \
|
1998-06-29 12:44:36 +00:00
|
|
|
|
: kbList::iterator(n) {} \
|
|
|
|
|
inline type * operator*() \
|
|
|
|
|
/* the cast is needed for MS VC++ 5.0 */ \
|
|
|
|
|
{ return (type *)((kbList::iterator *)this)->operator*() ; } \
|
|
|
|
|
}; \
|
1998-11-19 20:34:59 +00:00
|
|
|
|
inline name(bool ownsEntriesFlag = TRUE) \
|
1998-06-29 12:44:36 +00:00
|
|
|
|
: kbList(ownsEntriesFlag) {} \
|
|
|
|
|
\
|
|
|
|
|
inline type *pop_back(void) \
|
|
|
|
|
{ return (type *) kbList::pop_back(); } \
|
|
|
|
|
\
|
|
|
|
|
inline type *pop_front(void) \
|
|
|
|
|
{ return (type *) kbList::pop_front(); } \
|
|
|
|
|
\
|
1998-11-19 20:34:59 +00:00
|
|
|
|
type *remove(iterator& i) \
|
|
|
|
|
{ return (type *)kbList::remove(i); } \
|
1998-08-12 08:33:34 +00:00
|
|
|
|
inline void erase(iterator & i) \
|
1999-03-07 23:01:40 +00:00
|
|
|
|
{ deleteContent(i); doErase(i); } \
|
1998-06-29 12:44:36 +00:00
|
|
|
|
\
|
|
|
|
|
inline iterator begin(void) const \
|
|
|
|
|
{ return kbList::begin(); } \
|
|
|
|
|
\
|
|
|
|
|
inline iterator end(void) const \
|
|
|
|
|
{ return kbList::end(); } \
|
|
|
|
|
\
|
|
|
|
|
inline iterator tail(void) const \
|
|
|
|
|
{ return kbList::tail(); } \
|
1998-08-12 08:33:34 +00:00
|
|
|
|
~name() \
|
|
|
|
|
{ \
|
|
|
|
|
kbListNode *next; \
|
1998-11-19 20:34:59 +00:00
|
|
|
|
while ( first != NULL ) \
|
1998-08-12 08:33:34 +00:00
|
|
|
|
{ \
|
|
|
|
|
next = first->next; \
|
|
|
|
|
if(ownsEntries) \
|
1998-11-19 20:34:59 +00:00
|
|
|
|
delete (type *)first->element; \
|
1998-08-12 08:33:34 +00:00
|
|
|
|
delete first; \
|
|
|
|
|
first = next; \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
1998-11-19 20:34:59 +00:00
|
|
|
|
protected: \
|
|
|
|
|
inline void deleteContent(iterator i) \
|
|
|
|
|
{ if(ownsEntries) delete *i; } \
|
|
|
|
|
}
|
1998-06-29 12:44:36 +00:00
|
|
|
|
|
|
|
|
|
#ifdef MCONFIG_H
|
|
|
|
|
/// define the most commonly used list type once:
|
|
|
|
|
KBLIST_DEFINE(kbStringList, String);
|
|
|
|
|
#endif
|
1999-03-07 21:38:50 +00:00
|
|
|
|
//@}
|
1998-06-29 12:44:36 +00:00
|
|
|
|
#endif // KBLIST_H
|