📄 table.cpp
字号:
const void *value
)
{
Check_Object(this);
size_t n = numItems;
size_t i = 0, j;
IteratorPosition k;
while (i < n)
{
j = (i + n - 1) >> 1;
Check_Pointer(array);
Verify(j < numItems);
if ((k = CompareValueToTableEntry(value, array[j])) == 0)
break;
if (k < 0)
n = j;
else
i = j + 1;
}
return j;
}
#endif
//
//###########################################################################
// SearchForTableEntry
//###########################################################################
//
IteratorPosition
Table::SearchForTableEntry(
TableEntry *link
)
{
Check_Object(this);
CollectionSize i;
for (i = 0; i < numItems; i++)
{
Check_Pointer(array);
VERIFY_INDEX(i);
if (array[i] == link)
return i;
}
return TableNullIndex;
}
//
//###########################################################################
// RemoveNthTableEntry
//###########################################################################
//
void
Table::RemoveNthTableEntry(
CollectionSize index
)
{
Check_Object(this);
char *itemPtr, *lastItem;
size_t width;
Check_Pointer(array);
VERIFY_INDEX(index);
/*
* Find the location of the item
*/
itemPtr = Cast_Pointer(char*, &array[index]);
/*
* Remove the item from the array
*/
width = sizeof(void*);
lastItem = Cast_Pointer(char*, array) + (numItems - 1) * width;
if (itemPtr < lastItem)
{
memmove(itemPtr, itemPtr + width, (size_t)(lastItem - itemPtr));
}
/*
* Resize the array
*/
CollectionSize new_num_items = numItems-1;
if (new_num_items == 0)
{
Unregister_Pointer(array);
delete[] array;
array = NULL;
}
numItems = new_num_items;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~ TableIterator inlines ~~~~~~~~~~~~~~~~~~~~~~~~~~~
#if defined(_ARMOR)
TableEntry*
TableIterator::NthEntry(
CollectionSize index
)
{
Check_Object(this);
Check_Pointer(array);
Verify(0 <= index && index < numItems);
Check_Object(array[index]);
return array[index];
}
#endif
//
//###########################################################################
// TableIterator
//###########################################################################
//
TableIterator::TableIterator(Table *table):
SortedIterator(table)
{
array = table->array;
numItems = table->numItems;
if (array != NULL)
currentPosition = 0;
else
currentPosition = TableNullIndex;
}
//
//###########################################################################
//###########################################################################
//
TableIterator::~TableIterator()
{
}
//
//###########################################################################
// TestInstance
//###########################################################################
//
void
TableIterator::TestInstance()
{
SortedIterator::TestInstance();
if (array != NULL)
{
Check_Pointer(array);
Verify(0 < numItems);
Verify(currentPosition < numItems);
}
}
//
//###########################################################################
// First
//###########################################################################
//
void
TableIterator::First()
{
if (array != NULL)
currentPosition = 0;
}
//
//###########################################################################
// Last
//###########################################################################
//
void
TableIterator::Last()
{
if (array != NULL)
currentPosition = numItems - 1;
}
//
//###########################################################################
// Next
//###########################################################################
//
void
TableIterator::Next()
{
IncrementPosition();
}
//
//###########################################################################
// Previous
//###########################################################################
//
void
TableIterator::Previous()
{
DecrementPosition();
}
//
//###########################################################################
// ReadAndNextImplementation
//###########################################################################
//
void
*TableIterator::ReadAndNextImplementation()
{
if (currentPosition != TableNullIndex)
{
Plug *plug;
plug = NthEntry(currentPosition)->GetPlug();
IncrementPosition();
return plug;
}
return NULL;
}
//
//###########################################################################
// ReadAndPreviousImplementation
//###########################################################################
//
void
*TableIterator::ReadAndPreviousImplementation()
{
if (currentPosition != TableNullIndex)
{
Plug *plug;
plug = NthEntry(currentPosition)->GetPlug();
DecrementPosition();
return plug;
}
return NULL;
}
//
//###########################################################################
// GetCurrentImplementation
//###########################################################################
//
void
*TableIterator::GetCurrentImplementation()
{
if (currentPosition != TableNullIndex)
{
return NthEntry(currentPosition)->GetPlug();
}
return NULL;
}
//
//###########################################################################
// GetSize
//###########################################################################
//
CollectionSize
TableIterator::GetSize()
{
return numItems;
}
//
//###########################################################################
// GetNthImplementation
//###########################################################################
//
void
*TableIterator::GetNthImplementation(
CollectionSize index
)
{
if (index < numItems)
{
return NthEntry(currentPosition = index)->GetPlug();
}
return NULL;
}
//
//###########################################################################
// Remove
//###########################################################################
//
void
TableIterator::Remove()
{
if (currentPosition != TableNullIndex)
{
Unregister_Object(NthEntry(currentPosition));
delete NthEntry(currentPosition);
}
}
//
//###########################################################################
// FindImplementation
//###########################################################################
//
Plug
*TableIterator::FindImplementation(
const void *value
)
{
IteratorPosition index;
Table *table = Cast_Object(Table*, socket);
if ((index = table->SearchForValue(value)) != TableNullIndex)
{
if (!table->HasUniqueEntries())
{
while (
index-1 >= 0 &&
table->CompareTableEntries(NthEntry(index-1), NthEntry(index)) == 0
)
{
index--;
}
}
return (NthEntry(currentPosition = index)->GetPlug());
}
currentPosition = TableNullIndex;
return NULL;
}
//
//###########################################################################
// ReceiveMemo
//###########################################################################
//
void
TableIterator::ReceiveMemo(
IteratorMemo memo,
void *content
)
{
switch (memo)
{
case PlugAdded:
{
Table *table = Cast_Object(Table*, socket);
Check_Object(table);
array = table->array;
numItems = table->numItems;
//
// If a plug is added before or at the current position then
// the current position should be incremented one forward,
// otherwise, no action is necessary
//
TableEntry *link;
IteratorPosition index;
link = Cast_Object(TableEntry*, content);
index = table->SearchForTableEntry(link);
VERIFY_INDEX(index);
if (index <= currentPosition)
{
currentPosition++;
}
Verify(TableNullIndex <= currentPosition && currentPosition < numItems);
}
break;
case PlugRemoved:
{
Table *table = Cast_Object(Table*, socket);
Check_Object(table);
array = table->array;
numItems = table->numItems;
//
// If a plug is removed before the current position then decrement
// the current position, else if the current position is at the end
// of the table then decrement the counter
//
IteratorPosition index;
index = *Cast_Pointer(IteratorPosition*,content);
if (index < currentPosition)
{
currentPosition--;
}
else if (numItems <= currentPosition)
{
currentPosition--;
}
Verify(TableNullIndex <= currentPosition && currentPosition < numItems);
}
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -