+#endif
+
+
+
+void CollReplaceExpand (Collection* C, void* Item, unsigned Index)
+/* If Index is a valid index for the collection, replace the item at this
+ * position by the one passed. If the collection is too small, expand it,
+ * filling unused pointers with NULL, then add the new item at the given
+ * position.
+ */
+{
+ if (Index < C->Count) {
+ /* Collection is already large enough */
+ C->Items[Index] = Item;
+ } else {
+ /* Must expand the collection */
+ unsigned Size = C->Size;
+ if (Size == 0) {
+ Size = 4;
+ }
+ while (Index >= Size) {
+ Size *= 2;
+ }
+ CollGrow (C, Size);
+
+ /* Fill up unused slots with NULL */
+ while (C->Count < Index) {
+ C->Items[C->Count++] = 0;
+ }
+
+ /* Fill in the item */
+ C->Items[C->Count++] = 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);
+}
+
+
+
+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);
+}