[C#] Pools and lists

Started by
3 comments, last by Narf the Mouse 14 years ago
If I have a pool of decals, which can be obtained as follows:

        public T GetNewValidObject()
        {
            if (numberOfInvalidObjects > 0)
            {
                return objects[--numberOfInvalidObjects];
            }
            else if (isWrappable)
            {
                numberOfInvalidObjects = objects.Length;
                return objects[--numberOfInvalidObjects];
            }

            return null;
        }

When obtaining an object from the pool and then storing it in a separate list, is there any way of checking the list for the object faster than as follows:

Decal decal = Game1.Instance.pool_Decal.GetNewValidObject();
                    int index = decals.IndexOf(decal);

                    if (index == -1)
                    {
                        decals.Add(decal);
                    }
                    else
                    {
                        decals[index] = decal;
                    }

Advertisement
Maybe try List<T>.Contains(T). Dunno though if this is really faster in case of List<>.
I wish there were some way of avoiding this altogether; this is what I find is the problem with pools.

If I want to create a separate list from the objects as they are created and the pool "IsWrappable" then I have to check the list each time for the decal index rather than just replacing the index directly based off the pool index.

The pool index decrements as the list index increments.

[Pool]
objects[--numberOfInvalidObjects];


    decals[++index];

    Is there any way I can set the change the list based off the pool index? A numerical trick that I'm missing perhaps?

    EDIT:

    This covers wrappable pools:

            public T GetNewValidObject(out int index)        {            index = objects.Length - numberOfInvalidObjects;            if (numberOfInvalidObjects > 0)            {                return objects[--numberOfInvalidObjects];            }            else if (isWrappable)            {                numberOfInvalidObjects = objects.Length;                return objects[--numberOfInvalidObjects];            }            return null;        }


    [Edited by - Spa8nky on April 12, 2010 10:47:48 AM]
I've rewrote the code to cover wrappable pools with correct indexing.

Ignore the previous method, it was rubbish ;)

        /// <summary>        /// Creates a new valid object to fill the spot of an invalidated        /// object if possible. Returns null if all objects are valid.        /// </summary>        /// <returns>A reference to the next valid object, or null if all space is being used.</returns>        public T GetNewValidObject()        {            if (numberOfInvalidObjects > 0)            {                // Reset the wrap index to oldest object when there are invalid (unused) objects                wrapIndex = objects.Length;                return objects[--numberOfInvalidObjects];            }            else if (isWrappable)            {                if (wrapIndex == 0)                {                    wrapIndex = objects.Length;                }                // Overwrite the oldest object in the pool if there are no free objects                return objects[--wrapIndex];            }            return null;        }


This problem can be considered solved if there are no objections.

Thanks guys.
Well, yes. ".Length" returns the number of items in the array. The problem is, this is always one greater than the maximum index of the array.

This topic is closed to new replies.

Advertisement