how to manage a "resource" array efficiently
- by Haiyuan Zhang
The senario of my question is that one need to use a fixed size of array to keep track of certain number of "objects" .
The object here can be as simply as a integer or as complex as very fancy data structure. And "keep track" here means to allocate one object when other part of the app need one instance of object and recyle it for future allocation when one instance of the object is returned .Finally ,let me use c++ to put my problme in a more descriptive way .
#define MAX 65535
/* 65535 just indicate that many items should be handled . performance demanding! */
typedef struct {
int item ;
}Item_t;
Item_t items[MAX] ;
class itemManager {
private :
/* up to you.... */
public :
int get() ; /* get one index to a free Item_t in items */
bool put(int index) ; /* recyle one Item_t indicate by one index in items */
}
how will you implement the two public functions of itemManager ? it's up to you to add any private member .