📄 phb_pindex.c
字号:
* index [IN] The `index'th element in table to store `position'
* position [IN] The data to append
* RETURNS
* index appended.
*****************************************************************************/
kal_uint16 phb_pindex_insert(pindex_type *pindex, kal_uint16 index, kal_uint16 position)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
kal_trace(TRACE_FUNC, FUNC_PHB_PINDEX_INSERT);
ASSERT(pindex != NULL);
/**
* Being inserted index could be equal to used_count.
* Note that capacity must no be exceeded after insertion.
*/
if ((index > pindex->used_count) || (pindex->used_count >= pindex->slots))
{
return (kal_uint16) PHB_INVALID_VALUE;
}
/* If the entry to be inserted is head or tail, shift is not needed. */
if (!((pindex->used_count == 0) || (index >= pindex->used_count)))
{
/* Shift down one slot, so that the entry could be inserted. */
table_shift(&pindex->used_count, &pindex->slots, pindex->table, sizeof(pindex_struct), shift_down, index);
}
pindex->table[index].position = position;
++pindex->used_count;
return index;
} /* end of phb_pindex_insert */
/*****************************************************************************
* FUNCTION
* phb_pindex_update
* DESCRIPTION
* Update `old_index'th element of table to `new_index' with new value `new_pos'.
* PARAMETERS
* pindex [IN] The pindex
* old_index [IN] The old index
* new_index [IN] The new index
* new_pos [IN] The data to append
* RETURNS
* KAL_TRUE if success, KAL_FALSE else.
*****************************************************************************/
kal_uint16 phb_pindex_update(pindex_type *pindex, kal_uint16 old_index, kal_uint16 new_index, kal_uint16 new_pos)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
kal_trace(TRACE_FUNC, FUNC_PHB_PINDEX_UPDATE);
ASSERT(pindex != NULL);
if ((old_index > (pindex->used_count - 1)) || (new_index > (pindex->used_count - 1)))
{
return (kal_uint16) PHB_INVALID_VALUE;
}
/* Same index, nothing has to be done. */
if (new_index == old_index)
{
return new_index;
}
if (new_index > old_index)
{
table_range_shift(
&pindex->used_count,
&pindex->slots,
pindex->table,
sizeof(pindex_struct),
shift_up,
(kal_uint16) (old_index + 1),
new_index);
pindex->table[new_index].position = new_pos;
}
else
{
table_range_shift(
&pindex->used_count,
&pindex->slots,
pindex->table,
sizeof(pindex_struct),
shift_down,
new_index,
(kal_uint16) (old_index - 1));
pindex->table[new_index].position = new_pos;
}
return new_index;
} /* end of phb_pindex_insert */
/*****************************************************************************
* FUNCTION
* phb_pindex_delete
* DESCRIPTION
* Delete `index'th element of table of pindex
* PARAMETERS
* pindex [IN] The pindex
* index [IN] The index
* RETURNS
* KAL_TRUE if success, KAL_FALSE else.
*****************************************************************************/
kal_bool phb_pindex_delete(pindex_type *pindex, kal_uint16 index)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
kal_trace(TRACE_FUNC, FUNC_PHB_PINDEX_DELETE);
ASSERT(pindex != NULL);
if (index > (pindex->used_count - 1))
{
return KAL_FALSE;
}
table_shift(
&pindex->used_count,
&pindex->slots,
pindex->table,
sizeof(pindex_struct),
shift_up,
(kal_uint16) (index + 1));
pindex->table[--pindex->used_count].position = (kal_uint16) PHB_INVALID_VALUE;
return KAL_TRUE;
} /* end of phb_pindex_delete */
/*****************************************************************************
* FUNCTION
* phb_pindex_relink
* DESCRIPTION
* This function is used to relink with data_entry_table due to
* unmatched index when some entry of data_entry_table is deleted.
* PARAMETERS
* pindex [IN] The pindex
* old_position [IN]
* index(?) [IN] The index
* RETURNS
* KAL_TRUE if success, KAL_FALSE else.(?)
*****************************************************************************/
void phb_pindex_relink(pindex_type *pindex, kal_uint16 old_position)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_uint16 i;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
kal_trace(TRACE_FUNC, FUNC_PHB_PINDEX_RELINK);
ASSERT(pindex != NULL);
/* No other way except linear searching. *Sigh* */
for (i = 0; i < pindex->used_count; ++i)
if (pindex->table[i].position > old_position)
{
--pindex->table[i].position;
}
} /* end of phb_pindex_delete */
/*****************************************************************************
* FUNCTION
* phb_pindex_get_pos
* DESCRIPTION
* Return `index'th element of table of pindex
* PARAMETERS
* pindex [IN] The pindex
* index [IN] The index
* RETURNS
* position if success, PHB_INVALID_VALUE otherwise.
*****************************************************************************/
kal_uint16 phb_pindex_get_pos(pindex_type *pindex, kal_uint16 index)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
kal_trace(TRACE_FUNC, FUNC_PHB_PINDEX_GET_POS);
ASSERT(pindex != NULL);
if (--index < pindex->used_count)
{
return pindex->table[index].position;
}
return (kal_uint16) PHB_INVALID_VALUE;
} /* end of phb_pindex_get_pos */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -