#include<stdio.h>
#include<stdlib.h>
class allocIndex{
private:
int size;
int pos;
int *indexArr;
int *posArr;
public:
allocIndex(int vSize)
{
size = vSize;
pos = 0;
indexArr = (int *)malloc(vSize*sizeof(int));
posArr = (int *)malloc(vSize*sizeof(int));
for (int i = 0; i < vSize; i++)
{
indexArr[i] = i;
posArr[i] = i;
}
}
int getUnUsedIndex()
{
int index = 0xffff;
if (pos < size)
{
index = indexArr[pos];
posArr[index] = pos;
pos++;
return index;
}
return 0xffff;
}
int isExistIndex(int vIndex)
{
int currPos = 0;
if (vIndex >= size)
{
return 0xffff;
}
currPos = posArr[vIndex];
if (currPos >= pos)
{
return 0xffff;
}
return 0;
}
void freeIndex(int vIndex)
{
int position = 0xffff;
pos--;
printf("del:%d \n", vIndex);
if (isExistIndex(vIndex) != 0)
{
return;
}
position = posArr[vIndex];
if (pos > position)
{
int index1 = indexArr[position];
indexArr[position] = indexArr[pos];
indexArr[pos] = index1;
posArr[index1] = pos;
posArr[indexArr[position]] = position;
}
else
{
//pos == position;
}
}
void print()
{
for (int i = 0; i < size; i++)
{
printf("%d ",indexArr[i]);
}
printf("\n");
for (int i = 0; i < size; i++)
{
printf("%d ", posArr[i]);
}
printf("\n");
printf("pos:%d\n", pos);
}
};
索引分配,查找时间为1
最新推荐文章于 2023-06-13 11:34:14 发布