Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Efficient Resource Tracking

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,312
US
This is one question that I repeatedly have encountered as of late: What is the best way to handle a finite resource tracking algorithm?

For example, I might have a specific number of spots set up for working with TBitmap.

Code:
bitmap_table: array[1..10] of TBitmap;

Now I could generally start from 1 when I create this object. However, if I have more objects (let's say 3), and I'm done with 1, I can Free the object.

Now what I'm talking about is handling things to be able to reclaim that spot. In other words, use it efficiently? What's generally the best approach? Or am on the wrong track and there's a better way to handle this?

Measurement is not management.
 
I've always used TList, or TObjectList, or TCollection to manage dynamic collections of objects. When you delete a member, everything moves around so you are always referring to them in the list index from 0.
 
^^^^^

+1

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I'm not quite understanding something on how this would be useful. If I were to use TList and delete a specific element, wouldn't that remove the element, and therefore change the indexing if I were to try to subsequently access any of the other resources by a specific index? (i.e. element 4 becomes element 3, and so on)

Measurement is not management.
 
well it depends on your needs.

if you use the objects sequentially, you can use TList / TObjectlist like a stack (FIFO).

I never mess with arrays.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I stand by my original suggestion, but if you did want to track the 'holes' in the original static array, you could use a dynamic array of Integer. ie.

Code:
var
  Avail: array of Integer;

Each time an array member becomes free or available to be reused, you extend the length of Avail, and the last member is set to the the available index of your static array.

When you need to find an available spot in your static array, grab Avail[High(Avail)], use that index in the static array, then reduce the length of Avail so that you don't reuse it. Your Avail array becomes a FILO stack.
 
The Accpac Plus data tables used a method whereby the header of the table pointed to the next available record number. With a new table this pointed to the end of the table. When a record was deleted the data in it's place pointed to the next available record number and then the header was updated with the location of the deleted record. In a sense it became a linked list. When adding a record you looked in the header for the location of the next open spot and, before filling that spot with data, if it pointed somewhere, you update the header with the next available spot and then write the data.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top