From 42695dd3c3c69cdb67cea49f39acaf4ce7700148 Mon Sep 17 00:00:00 2001 From: cuz Date: Thu, 3 May 2001 20:45:26 +0000 Subject: [PATCH] Added CollIndex and CollDeleteItem git-svn-id: svn://svn.cc65.org/cc65/trunk@708 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/common/coll.c | 40 ++++++++++++++++++++++++++++++++++++++-- src/common/coll.h | 12 +++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/common/coll.c b/src/common/coll.c index 6748ff12d..f2cd52f7e 100644 --- a/src/common/coll.c +++ b/src/common/coll.c @@ -9,7 +9,7 @@ /* (C) 2000-2001 Ullrich von Bassewitz */ /* Wacholderweg 14 */ /* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -195,6 +195,26 @@ const void* CollConstLast (const Collection* C) +int CollIndex (Collection* C, const void* Item) +/* Return the index of the given item in the collection. Return -1 if the + * item was not found in the collection. + */ +{ + /* Linear search */ + unsigned I; + for (I = 0; I < C->Count; ++I) { + if (Item == C->Items[I]) { + /* Found */ + return (int)I; + } + } + + /* Not found */ + return -1; +} + + + void CollDelete (Collection* C, unsigned Index) /* Remove the item with the given index from the collection. This will not * free the item itself, just the pointer. All items with higher indices @@ -211,10 +231,26 @@ void CollDelete (Collection* C, unsigned Index) +void CollDeleteItem (Collection* C, const void* Item) +/* Delete the item pointer from the collection. The item must be in the + * collection, otherwise FAIL will be called. + */ +{ + /* Get the index of the entry */ + int Index = CollIndex (C, Item); + CHECK (Index >= 0); + + /* Delete the item with this index */ + --C->Count; + memmove (C->Items+Index, C->Items+Index+1, (C->Count-Index) * sizeof (void*)); +} + + + void CollDeleteAll (Collection* C) /* Delete all items from the given collection. This will not free the items * itself, it will only remove the pointers. - */ + */ { /* This one is easy... */ C->Count = 0; diff --git a/src/common/coll.h b/src/common/coll.h index 37c759ba8..bab2ffc48 100644 --- a/src/common/coll.h +++ b/src/common/coll.h @@ -9,7 +9,7 @@ /* (C) 2000-2001 Ullrich von Bassewitz */ /* Wacholderweg 14 */ /* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -102,12 +102,22 @@ void* CollLast (Collection* C); const void* CollConstLast (const Collection* C); /* Return the last item in a collection */ +int CollIndex (Collection* C, const void* Item); +/* Return the index of the given item in the collection. Return -1 if the + * item was not found in the collection. + */ + void CollDelete (Collection* C, unsigned Index); /* Remove the item with the given index from the collection. This will not * free the item itself, just the pointer. All items with higher indices * will get moved to a lower position. */ +void CollDeleteItem (Collection* C, const void* Item); +/* Delete the item pointer from the collection. The item must be in the + * collection, otherwise FAIL will be called. + */ + void CollDeleteAll (Collection* C); /* Delete all items from the given collection. This will not free the items * itself, it will only remove the pointers. -- 2.39.5