From 5c46ebee60dd66dcbbd51c5267c5549a6a2fde00 Mon Sep 17 00:00:00 2001 From: cuz Date: Tue, 17 Jul 2001 11:54:30 +0000 Subject: [PATCH] Added CollMove git-svn-id: svn://svn.cc65.org/cc65/trunk@797 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/common/coll.c | 23 +++++++++++++++++++++++ src/common/coll.h | 7 +++++++ 2 files changed, 30 insertions(+) diff --git a/src/common/coll.c b/src/common/coll.c index 465a65987..e5a0e7eb8 100644 --- a/src/common/coll.c +++ b/src/common/coll.c @@ -182,6 +182,29 @@ void CollDeleteItem (Collection* C, const void* Item) +void CollMove (Collection* C, unsigned OldIndex, unsigned NewIndex) +/* Move an item from one position in the collection to another. OldIndex + * is the current position of the item, NewIndex is the new index after + * the function has done it's work. Existing entries with indices NewIndex + * and up are moved one position upwards. + */ +{ + /* Get the item and remove it from the collection */ + void* Item = CollAt (C, OldIndex); + CollDelete (C, OldIndex); + + /* Correct NewIndex if needed */ + if (NewIndex >= OldIndex) { + /* Position has changed with removal */ + --NewIndex; + } + + /* Now insert it at the new position */ + CollInsert (C, Item, NewIndex); +} + + + static void QuickSort (Collection* C, int Lo, int Hi, int (*Compare) (void*, const void*, const void*), void* Data) diff --git a/src/common/coll.h b/src/common/coll.h index 3dfdd5378..d925b39c9 100644 --- a/src/common/coll.h +++ b/src/common/coll.h @@ -248,6 +248,13 @@ INLINE void CollReplace (Collection* C, void* Item, unsigned Index) (C)->Items[(Index)] = (Item)) #endif +void CollMove (Collection* C, unsigned OldIndex, unsigned NewIndex); +/* Move an item from one position in the collection to another. OldIndex + * is the current position of the item, NewIndex is the new index after + * the function has done it's work. Existing entries with indices NewIndex + * and up are moved one position upwards. + */ + void CollSort (Collection* C, int (*Compare) (void*, const void*, const void*), void* Data); -- 2.39.5