+#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.
+ */
+{
+ /* 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);
+}
+
+
+
+void CollMoveMultiple (Collection* C, unsigned Start, unsigned Count, unsigned Target)
+/* Move a range of items from one position to another. Start is the index
+ * of the first item to move, Count is the number of items and Target is
+ * the index of the target item. The item with the index Start will later
+ * have the index Target. All items with indices Target and above are moved
+ * to higher indices.
+ */
+{
+ void** TmpItems;
+ unsigned Bytes;
+
+ /* Check the range */
+ PRECONDITION (Start < C->Count && Start + Count <= C->Count && Target <= C->Count);
+
+ /* Check for trivial parameters */
+ if (Count == 0 || Start == Target) {
+ return;
+ }
+
+ /* Calculate the raw memory space used by the items to move */
+ Bytes = Count * sizeof (void*);
+
+ /* Allocate temporary storage for the items */
+ TmpItems = xmalloc (Bytes);
+
+ /* Copy the items we have to move to the temporary storage */
+ memcpy (TmpItems, C->Items + Start, Bytes);
+
+ /* Check if the range has to be moved upwards or downwards. Move the
+ * existing items to their final location, so that the space needed
+ * for the items now in temporary storage is unoccupied.
+ */
+ if (Target < Start) {
+
+ /* Move downwards */
+ unsigned BytesToMove = (Start - Target) * sizeof (void*);
+ memmove (C->Items+Target+Count, C->Items+Target, BytesToMove);
+
+ } else if (Target < Start + Count) {
+
+ /* Target is inside range */
+ FAIL ("Not supported");
+
+ } else {
+
+ /* Move upwards */
+ unsigned ItemsToMove = (Target - Start - Count);
+ unsigned BytesToMove = ItemsToMove * sizeof (void*);
+ memmove (C->Items+Start, C->Items+Target-ItemsToMove, BytesToMove);
+
+ /* Adjust the target index */
+ Target -= Count;
+ }
+
+ /* Move the old items to their final location */
+ memcpy (C->Items + Target, TmpItems, Bytes);
+
+ /* Delete the temporary item space */
+ xfree (TmpItems);
+}
+