secgateway/Product/user/user_manager/array_index.c

137 lines
3.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include "array_index.h"
#include "../../common/common_user.h"
ARRAY g_user_index_head = { 0 };
ARRAY g_group_index_head = { 0 };
#define INDEX_LEN_16
#ifdef INDEX_LEN_16
#define INDEX_LEN 16
#define INDEX_LOW_MASK 0x0000ffff
#define INDEX_HI_MASK 0xffff0000
#else
#endif
#define CUR_INDEX (head->cur)
#define MAX_INDEX (head->size)
#define HI_ELEMENT_GET(element) ((element) >> INDEX_LEN)
#define HI_ELEMENT_SET(element, value) (((element) & INDEX_LOW_MASK) | ((value) << INDEX_LEN))
#define LOW_ELEMENT_GET(element) ((element) & INDEX_LOW_MASK)
#define LOW_ELEMENT_SET(element, value) (((element) & INDEX_HI_MASK) | ((value) & INDEX_LOW_MASK))
/*
*初始化索引
*失败1
*成功0
*/
int init_array(ARRAY* head, int index_size)
{
int i;
head->array = malloc(sizeof(head->array) * index_size);
if (NULL == head->array)
{
return 1;
}
//head->cur = 1;
//head->size = index_size;
CUR_INDEX = 1;
MAX_INDEX = index_size;
/*初始化第一个值*/
head->array[0] = HI_ELEMENT_SET(head->array[0], 0);
head->array[0] = LOW_ELEMENT_SET(head->array[0], 1);
for (i = 1; i < index_size - 1; i++)
{
head->array[i] = HI_ELEMENT_SET(head->array[i], i - 1);
head->array[i] = LOW_ELEMENT_SET(head->array[i], i + 1);
}
/*初始化最后一个值*/
head->array[i] = HI_ELEMENT_SET(head->array[i], i - 1);
head->array[i] = LOW_ELEMENT_SET(head->array[i], 0);
return 0;
}
/* 顺序分配index */
unsigned short alloc_index(ARRAY* head)
{
unsigned short index = CUR_INDEX; //获取cur
/*判断index无效返回0*/
if ((0 == index) || ((MAX_INDEX - 1) <= index))
{
return INVALID_INDEX;
}
CUR_INDEX = LOW_ELEMENT_GET(head->array[index]); //移动cur
head->array[index] = 0; //被使用过的元素置0
return index; //返回被使用的下标作为ID
//if ((0 < index) || ((MAX_INDEX-1) > index))
}
/* 指定分配index
*成功返回index
*失败返回0
*/
unsigned short alloc_index_special(ARRAY* head, unsigned short index)
{
unsigned short hi_index, low_index;
/*判断index无效返回0*/
if ((0 == index) || ((MAX_INDEX - 1) <= index))
{
return INVALID_INDEX;
}
/* 判断已经被使用过了返回0 */
if (INVALID_INDEX == head->array[index])
{
return INVALID_INDEX;
}
/* 判断和顺序插入相同 */
if (CUR_INDEX == index)
{
return alloc_index(head);
}
low_index = LOW_ELEMENT_GET(head->array[index]);
hi_index = HI_ELEMENT_GET(head->array[index]);
head->array[low_index] = HI_ELEMENT_SET(head->array[low_index], hi_index);
head->array[hi_index] = LOW_ELEMENT_SET(head->array[hi_index], low_index);
head->array[index] = 0;
return index;
}
void free_index(ARRAY* head, unsigned short index)
{
unsigned short cur_index = CUR_INDEX;
if ((0 == index) || ((MAX_INDEX - 1) <= index))
{
return;
}
if (INVALID_INDEX != head->array[index])
{
return;
}
CUR_INDEX = index;
head->array[index] = LOW_ELEMENT_SET(head->array[index], cur_index);
head->array[cur_index] = HI_ELEMENT_SET(head->array[cur_index], index);
return;
}