📄 vmhashbucket.cpp
字号:
/*****************************************************************************/
/* SOURCE FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
$Date: $
$Author: $
Description: Implementation of the Doubly Linked List class that is used
by the hash table object
TOOL And XML FORMS License
==========================
Except where otherwise noted, all of the documentation
and software included in the TOOL package is
copyrighted by Michael Swartzendruber.
Copyright (C) 2005 Michael John Swartzendruber.
All rights reserved.
Access to this code, whether intentional or accidental,
does NOT IMPLY any transfer of rights.
This software is provided "as-is," without any express
or implied warranty. In no event shall the author be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for
any purpose, including commercial applications, and to
alter and redistribute it, provided that the following
conditions are met:
1. All redistributions of source code files must retain
all copyright notices that are currently in place,
and this list of conditions without modification.
2. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
3. If you use this software in another product, an acknowledgment
in the product documentation would be appreciated but is
not required.
4. Modified versions in source or binary form must be plainly
marked as such, and must not be misrepresented as being
the original software.
*/
static char OBJECT_ID[] = "$Revision: $ : $Date: $";
/*****************************************************************************/
#include "../../../stdafx.h"
#include "../VMException.h"
#include "../VMCoreGlobal.h"
#include "VMHashBucket.h"
// implement utility functions
void VMHashBucket::DeepCopy( const VMHashBucket& dlst )
{
if ( 0 == dlst.Count )
return;
Node* n = dlst.Head;
do
{
Append( n->Data );
n = n->Next;
}
while( n != NULL );
Work = Head;
Shallow = false;
Circular = dlst.Circular;
}
void VMHashBucket::ShallowCopy( const VMHashBucket& dlst )
{
Head = dlst.Head;
Tail = dlst.Tail;
Work = dlst.Work;
Count = dlst.Count;
Shallow = true;
Circular = dlst.Circular;
}
void VMHashBucket::SetNull( void )
{
Head = NULL;
Tail = NULL;
Work = NULL;
Count = 0;
}
// constructors
VMHashBucket::VMHashBucket( bool circ )
{
SetNull();
Shallow = false;
Circular = circ;
}
// copy constructor
VMHashBucket::VMHashBucket( const VMHashBucket& dlst, bool shallow )
{
if ( shallow )
ShallowCopy( dlst );
else
{
SetNull();
DeepCopy( dlst );
}
}
// construct from array
VMHashBucket::VMHashBucket( const VMHashtableEntry* array, size_t no )
{
SetNull();
Circular = false;
Shallow = false;
if ( ( array == NULL ) || ( no == 0 ) )
throw new VMException( __FILE__, __LINE__, VMException::NULLARRAY, VMException::fatal );
const VMHashtableEntry* aptr = array;
for ( size_t n = 0; n < no; ++n )
{
Append( *aptr );
++aptr;
}
}
// destructor
VMHashBucket::~VMHashBucket( void )
{
Erase();
}
// assignment operator (deep copy)
void VMHashBucket::operator = ( const VMHashBucket& dlst )
{
Erase();
DeepCopy( dlst );
}
// conversion to array operator
VMHashBucket::operator VMHashtableEntry*( void )
{
if ( 0 == Count )
return NULL;
VMHashtableEntry* result = new VMHashtableEntry[ Count ];
if ( NULL == result )
throw new VMException( __FILE__, __LINE__, VMException::ARRAYALLOC, VMException::fatal );
Node* temp = Head;
VMHashtableEntry* aptr = result;
while ( temp != NULL )
{
*aptr = temp->Data;
++aptr;
temp = temp->Next;
}
// note: caller must delete this array!
return( result );
}
// append new item
void VMHashBucket::Append( const VMHashtableEntry& item )
{
Node* n = new Node;
if ( NULL == n )
throw new VMException( __FILE__, __LINE__, VMException::ALLOC, VMException::fatal );
n->Data.Data.CloneValue( item.Data );
n->Data.Key.CloneValue( item.Key );
// n->Data = item;
n->Next = NULL;
n->Prev = Tail;
if ( 0 == Count )
{
Head = n;
Tail = n;
Work = n;
}
else
{
Tail->Next = n;
Tail = n;
}
++Count;
}
void VMHashBucket::Append( const VMHashtableEntry* array, size_t no )
{
if ( ( NULL == array ) || ( 0 == no ) )
throw new VMException( __FILE__, __LINE__, VMException::NULLARRAY, VMException::fatal );
const VMHashtableEntry* aptr = array;
for ( size_t n = 0; n < no; ++n )
{
Append( *aptr );
++aptr;
}
}
void VMHashBucket::Append( const VMHashBucket& dlst )
{
const Node* n = dlst.Head;
while( n != NULL )
{
Append( n->Data );
n = n->Next;
}
}
// delete current item
void VMHashBucket::Delete( void )
{
if ( 0 == Count )
throw new VMException( __FILE__, __LINE__, VMException::EMPTY, VMException::fatal );
if ( NULL == Work )
throw new VMException( __FILE__, __LINE__, VMException::NOTREADY, VMException::fatal );
if ( Work == Head )
{
if ( NULL == Work->Next )
{
delete Work;
SetNull();
}
else
{
Head = Work->Next;
Head->Prev = NULL;
delete Work;
Work = Head;
}
}
else
{
if ( Work == Tail )
{
if ( NULL == Work->Prev )
{
delete Work;
SetNull();
}
else
{
Tail = Work->Prev;
Tail->Next = NULL;
delete Work;
Work = Tail;
}
}
else
{
Node* n = Work->Next;
Work->Prev->Next = Work->Next;
Work->Next->Prev = Work->Prev;
delete Work;
Work = n;
}
}
--Count;
}
// insert new item in current location
void VMHashBucket::InsertBefore( const VMHashtableEntry& item )
{
if ( 0 == Count )
throw new VMException( __FILE__, __LINE__, VMException::EMPTY, VMException::fatal );
if ( NULL == Work )
throw new VMException( __FILE__, __LINE__, VMException::NOTREADY, VMException::fatal );
Node* newnode = new Node;
if ( NULL == newnode )
throw new VMException( __FILE__, __LINE__, VMException::ALLOC, VMException::fatal );
newnode->Data = item;
if ( Work == Head )
{
newnode->Next = Head;
newnode->Prev = NULL;
Head->Prev = newnode;
Head = newnode;
}
else
{
Work->Prev->Next = newnode;
newnode->Prev = Work->Prev;
newnode->Next = Work;
Work->Prev = newnode;
}
++Count;
}
void VMHashBucket::InsertBefore( const VMHashtableEntry* array, size_t no )
{
for ( size_t n = 0; n < no; ++n )
InsertBefore( array[ n ] );
}
void VMHashBucket::InsertBefore( const VMHashBucket& dlst )
{
const Node* n = dlst.Head;
while ( n != NULL )
{
InsertBefore( n->Data );
n = n->Next;
}
}
void VMHashBucket::InsertAfter( const VMHashtableEntry& item )
{
if ( 0 == Count )
throw new VMException( __FILE__, __LINE__, VMException::EMPTY, VMException::fatal );
if ( NULL == Work )
throw new VMException( __FILE__, __LINE__, VMException::NOTREADY, VMException::fatal );
Node* newnode = new Node;
if ( NULL == newnode )
throw new VMException( __FILE__, __LINE__, VMException::ALLOC, VMException::fatal );
newnode->Data = item;
if ( Work == Tail )
{
newnode->Next = NULL;
newnode->Prev = Tail;
Tail->Next = newnode;
Tail = newnode;
}
else
{
Work->Next->Prev = newnode;
newnode->Next = Work->Next;
newnode->Prev = Work;
Work->Next = newnode;
}
++Count;
}
void VMHashBucket::InsertAfter( const VMHashtableEntry* array, size_t no )
{
// insert in reverse order, so they're stored in order
for ( size_t n = no; n > 0; --n )
InsertAfter( array[ n - 1 ] );
}
void VMHashBucket::InsertAfter( const VMHashBucket& dlst )
{
const Node* n = dlst.Tail;
while( n != NULL )
{
InsertAfter( n->Data );
n = n->Prev;
}
}
// remove all items from the list
void VMHashBucket::Erase( void )
{
if ( !Shallow )
{
Work = Head;
while ( Work != NULL )
{
Head = Work->Next;
delete Work;
Work = Head;
}
}
SetNull();
Shallow = false;
}
// get current item
bool VMHashBucket::Get( VMHashtableEntry& roOutput )
{
if ( 0 == Count )
return( false );
if ( NULL == Work )
return( false );
roOutput = Work->Data;
return( true );
}
// directly reference current item
VMHashtableEntry& VMHashBucket::operator * ( void )
{
if ( 0 == Count )
throw new VMException( __FILE__, __LINE__, VMException::EMPTY, VMException::fatal );
if ( NULL == Work )
throw new VMException( __FILE__, __LINE__, VMException::NOTREADY, VMException::fatal );
return( Work->Data );
}
// TRUE if current item is last item
bool VMHashBucket::AtHead( void )
{
if ( Work == Head )
return( true );
else
return( false );
}
bool VMHashBucket::AtTail( void )
{
if ( Work == Tail )
return( true );
else
return( false );
}
// set current item to head of list
void VMHashBucket::ToHead( void )
{
Work = Head;
}
void VMHashBucket::ToTail( void )
{
Work = Tail;
}
// move to next item in list
bool VMHashBucket::ToNext( void )
{
if ( 0 == Count )
throw new VMException( __FILE__, __LINE__, VMException::EMPTY, VMException::fatal );
if ( NULL == Work )
throw new VMException( __FILE__, __LINE__, VMException::NOTREADY, VMException::fatal );
if ( Work == Tail )
{
if ( Circular )
Work = Head;
else
return( false );
}
else
Work = Work->Next;
return( true );
}
bool VMHashBucket::ToPrev( void )
{
if ( 0 == Count )
throw new VMException( __FILE__, __LINE__, VMException::EMPTY, VMException::fatal );
if ( NULL == Work )
throw new VMException( __FILE__, __LINE__, VMException::NOTREADY, VMException::fatal );
if ( Work == Head )
{
if ( Circular )
Work = Tail;
else
return( false );
}
else
Work = Work->Prev;
return( true );
}
// interrogate list
size_t VMHashBucket::GetCount( void )
{
return( Count );
}
bool VMHashBucket::IsShallow( void )
{
return( Shallow );
}
bool VMHashBucket::IsCircular( void )
{
return( Circular );
}
/*****************************************************************************/
/* Check-in history */
/*
*$Log: $
*/
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -