//delete the given record from the indices
getline(in, nameKey, '\n');
Record nameRecord(nameKey);
idx = strHash(nameKey, tableSize);
//if record exists
if ( hash.get(idx, nameRecord) && hash.remove(idx, nameRecord) ) {
logFile << setw(8) << "delete "
<< setw(20) << nameKey
<< setw(12) << "succeeded"
<< "the record at file position: "
<< nameRecord.getLocation() << endl
<< setw(40) << ""
<< "has been removed from the indices" << endl << endl;
//get the zip code and remove the corresponding record from the inverted table
textFile.seekg(nameRecord.getLocation()); //goto record on file
textFile.ignore(100, '\n'); //skip the name line
textFile.ignore(100, '\n'); //skip the street line
textFile.ignore(100, '\t'); //skip to the state field
textFile.ignore(100, '\t'); //skip to the zip code field
textFile >> zipKey; //read the zip key
//get list of zipRecords from the iverted table
Record zipRecord(zipKey);
list<Record> recordList = invT.get(zipRecord);
//remove all zipRecords whos key == zipKey from inverted table
while ( invT.remove(zipRecord) );
//remove the zipRecord with a byte offset equal to the nameRecord's
//byte offset from the list of records previously gotten with invT.get(...)
//and re-insert the zipRecords with a byte offset NOT EQUAL to the
//nameRecord's byte offset.
for ( list<Record>::iterator i = recordList.begin();
i != recordList.end(); i++ ) {
//get the location from a zipRecord and compare it with the
//nameRecord that was deleted
if ( nameRecord != i->getLocation() )
invT.insert(*i);
}
}
else {
logFile << setw(8) << "delete "
<< setw(20) << nameKey
<< setw(12) << "failed"
<< "the record does not exist" << endl << endl;
}
|