text cover

Data Abstraction and Problem Solving with C++

Walls and Mirrors

by Frank M. Carrano

Addison Wesley Logo

TableA.cpp

Go to the documentation of this file.
00001 
00020 #include "TableA.h"  // header file
00021 
00022 void Table::tableInsert(const TableItemType& newItem)
00023    throw(TableException)
00024 // Note: Insertion is unsuccessful if the table is full,
00025 // that is, if the table already contains MAX_TABLE items.
00026 // Calls: position.
00027 {
00028    if (size == MAX_TABLE)
00029       throw TableException("TableException: Table full");
00030 
00031    // there is room to insert;
00032    // locate the position where newItem belongs
00033    int spot = position(newItem.getKey());
00034 
00035    // shift up to make room for the new item
00036    for (int index = size-1; index >= spot; --index)
00037       items[index+1] = items[index];
00038 
00039    // make the insertion
00040    items[spot] = newItem;
00041    ++size;
00042 }  // end tableInsert
00043 
00044 void Table::tableDelete(KeyType searchKey)
00045    throw(TableException)
00046 // Calls: position.
00047 {
00048    // locate the position where searchKey exists/belongs
00049    int spot = position(searchKey);
00050 
00051    // is searchKey present in the table?
00052    if ((spot > size) || (items[spot].getKey() != searchKey))
00053       // searchKey not in table
00054       throw TableException("TableException: Item not found on delete");
00055    else
00056    {  // searchKey in table
00057       --size;  // delete the item
00058 
00059       // shift down to fill the gap
00060       for (int index = spot; index < size; ++index)
00061          items[index] = items[index+1];
00062    }  // end if
00063 }  // end tableDelete
00064 
00065 void Table::tableRetrieve(KeyType searchKey,
00066                           TableItemType& tableItem) const
00067    throw(TableException)
00068 // Calls: position.
00069 {
00070    // locate the position where searchKey exists/belongs
00071    int spot = position(searchKey);
00072 
00073    // is searchKey present in table?
00074    if ((spot > size) || (items[spot].getKey() != searchKey))
00075       // searchKey not in table
00076       throw TableException("TableException: Item not found on retrieve");
00077    else
00078       tableItem = items[spot];  // item present; retrieve it
00079 }  // end tableRetrieve
00080 
00081 void Table::traverseTable(FunctionType visit)
00082 {
00083    for (int index = 0; index < size; ++index)
00084        visit(items[index]);
00085 }  // end traverseTable
00086 // End of implementation file.

Generated on Sun Aug 27 22:04:01 2006 for AWLogo by  doxygen 1.4.6