索引分配,查找时间为1


#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);
    }


};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值