📄 base_obj.cpp
字号:
//*************************************************************************************
// BASE_OBJ.CPP
// This file contains the implementation (member functions and stuff) for the
// basic general-purpose classes developed for the CTL_EXEC/TranRun4 project.
//
// Copyright (c) 1994-97, D.M.Auslander and J.R.Ridgely
// May be used and distributed for any non-commercial purposes as long as this
// copyright notice is included.
//
// Version
// 11-01-94 DMA Original program
// 5-26-95 JRR Updated for use with TranRun3
// 7-18-95 JRR Class base_obj eliminated and list objects revised
// 10-13-97 JRR Replaced 'boolean' with 'bool'
//*************************************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !defined (TR4_COMP_HPP)
#define bool int
#define true 1
#define false 0
#define DELETE_ARRAY delete
#endif
#include <base_obj.hpp>
//=====================================================================================
// Class: CListNode
// This is a template for the nodes in a linked list. These nodes contain poin-
// ters so that they can point to one another (the list is doubly linked) and
// data pointers so that each node can hold its own data object.
//=====================================================================================
//-------------------------------------------------------------------------------------
// Constructor: CListNode
// Here we create a node which can be inserted into the generic linked list which
// is implemented in class CBasicList below.
CListNode::CListNode (CListNode* aPrev, CListNode* aNext, void* aData, int aKey,
CBasicList* aParent)
{
pPrevNode = aPrev;
pNextNode = aNext;
pNodeData = aData;
SortKey = aKey;
pParent = aParent;
}
//-------------------------------------------------------------------------------------
// Destructor: CListNode
// Here we create a node which can be inserted into the generic linked list which
// is implemented in class CBasicList below.
CListNode::~CListNode (void)
{
}
//=====================================================================================
// Class: CBasicList
// This class implements a linked list, using CListNode objects to represent the
// actual nodes in the list.
//=====================================================================================
//-------------------------------------------------------------------------------------
// Constructor: CBasicList
// This constructor creates an empty list object which is ready to hold pointers
// to items.
CBasicList::CBasicList (void)
{
pFirst = NULL; // Set all the object pointers to NULL
pLast = NULL;
pCurrent = NULL;
NumEntries = 0; // Number of entries is obviously 0 now
CurrentIndex = 0; // And index of current item is zero too
}
//-------------------------------------------------------------------------------------
// Destructor: CBasicList
// This destructor goes through the list and removes the items in it one by one.
CBasicList::~CBasicList (void)
{
RemoveNodes ();
}
//-------------------------------------------------------------------------------------
// Function: RemoveNodes
// This function goes through the list and removes the items in it one by one.
void CBasicList::RemoveNodes (void)
{
CListNode* pTemp; // Temporarily store pointer to node being deleted
// Begin at the head of the list. Delete every node, then go get the next one
// Note that this deletes the node objects but NOT the data stored by each node
pCurrent = pFirst;
while (pCurrent != NULL)
{
pTemp = pCurrent; // Save a pointer to the current object,
pCurrent = pCurrent->pNextNode; // then get a new pointer to the next one
delete (pTemp); // in the list and zap the current one
}
pFirst = NULL; // There's no more data, so null the pointers
pLast = NULL;
pCurrent = NULL;
NumEntries = 0;
CurrentIndex = 0;
}
//-------------------------------------------------------------------------------------
// Function: Insert (unsorted)
// This function inserts an object into the list, placing it at the tail without
// sorting. The sort index of this item is set to a default sort of 0.
void CBasicList::Insert (void* pNewData)
{
CListNode* pNewNode; // Pointer to newly created node object
// If the list is empty, put the new object at the head...tail...whatever
if (pFirst == NULL)
{
pNewNode = new CListNode (NULL, NULL, pNewData, 0, this);
pFirst = pNewNode;
pLast = pNewNode;
pCurrent = pNewNode;
CurrentIndex = 0;
}
// The list already has entries, so put this new thing at the end
else
{
pNewNode = new CListNode (pLast, NULL, pNewData, 0, this);
pLast->pNextNode = pNewNode;
pLast = pNewNode;
}
// Regardless of whether there were objects before or not, there's one more entry
NumEntries++;
}
//-------------------------------------------------------------------------------------
// Function: Insert (sorted)
// This function inserts an object into the list at a position determined by its
// sort index.
void CBasicList::Insert (void* pNewData, int aSortKey)
{
CListNode* pNewNode; // Pointer to newly created node object
CListNode* pCur; // Pointer for stepping through the list
// If the list is empty, put the new object in the only possible place
if (pFirst == NULL)
{
pNewNode = new CListNode (NULL, NULL, pNewData, aSortKey, this);
pFirst = pNewNode;
pLast = pNewNode;
pCurrent = pNewNode;
CurrentIndex = 0;
}
// The list already has entries. See if this item has the highest sort key; in
// this case it goes at the head
else
{
pCur = pFirst;
if (aSortKey > pCur->SortKey)
{
pNewNode = new CListNode (NULL, pCur, pNewData, aSortKey, this);
pCur->pPrevNode = pNewNode;
pFirst = pNewNode;
}
// OK, this isn't going to be the first item; go find where it can be placed
else
{
while ((pCur->pNextNode != NULL) && (aSortKey < pCur->pNextNode->SortKey))
pCur = pCur->pNextNode;
// pCurrent points to the last item whose sort key is less than new one
pNewNode = new CListNode (pCur, pCur->pNextNode, pNewData, aSortKey, this);
// If there's an item following this in the list, set its previous node
// pointer to point to this item; also set next node pointer of current
if (pCur->pNextNode != NULL)
(pCur->pNextNode)->pPrevNode = pNewNode;
pCur->pNextNode = pNewNode;
}
}
// Update the counter to reflext one more entry in the list
NumEntries++;
}
//-------------------------------------------------------------------------------------
// Function: Remove (given a data pointer)
// This function removes an object from the linked list and resets the list
// pointers so that the list's integrity is maintained. If the current-object
// pointer pointed to this object, it is set to point to the next one.
void CBasicList::Remove (void* KillData)
{
CListNode* pCur; // Pointer for stepping through the list
// Find the object in the list which has this pointer - just like GoToObjWith()
pCur = pFirst;
while (pCur->pNodeData != KillData)
{
pCur = pCur->pNextNode;
if (pCur == NULL) // If we can't find it, there's nothing
return; // to delete so just return
}
// Now that we have a pointer to the node containing the data to be removed,
// call the function which does the actual deleting
Remove (pCur);
}
//-------------------------------------------------------------------------------------
// Function: Remove (given a node pointer)
// This function removes the object whose node is pointed to by the given pointer.
// The user wouldn't usually call this because users only deal with pointers to
// data, not pointers to node objects.
void CBasicList::Remove (CListNode* pKillNode)
{
// If this is the furst or last object, fix the pointer which points to it
if (pKillNode == pFirst)
pFirst = pKillNode->pNextNode;
if (pKillNode == pLast)
pLast = pKillNode->pPrevNode;
// Reset the pointers of the previous and next objects so that they can find each
// other without going through this object
if (pKillNode->pPrevNode != NULL)
(pKillNode->pPrevNode)->pNextNode = pKillNode->pNextNode;
if (pKillNode->pNextNode != NULL)
(pKillNode->pNextNode)->pPrevNode = pKillNode->pPrevNode;
// If the current object pointer points to this thing, set it to the next one or,
// if there is no next one, the previous one; if there's no other node it's NULL
if (pCurrent == pKillNode)
if (pKillNode->pNextNode == NULL)
pCurrent = pKillNode->pPrevNode;
else
pCurrent = pKillNode->pNextNode;
// Delete the node object
delete pKillNode;
}
//-------------------------------------------------------------------------------------
// Function: GetHead
// In this function we just set the current list pointer to point to the first
// item in the list and return that pointer.
void* CBasicList::GetHead (void)
{
pCurrent = pFirst;
CurrentIndex = 0;
if (pCurrent != NULL)
return (pCurrent->pNodeData);
else
return (NULL);
}
//-------------------------------------------------------------------------------------
// Function: GoToTail
// Here we move the current-object pointer to the tail of the list and return it.
void* CBasicList::GetTail (void)
{
pCurrent = pLast;
CurrentIndex = NumEntries - 1;
if (pCurrent != NULL)
return (pCurrent->pNodeData);
else
return (NULL);
}
//-------------------------------------------------------------------------------------
// Function: GetNext
// This function moves the current-object pointer to the next item in the list
// and returns it, if there *is* a next item. If there isn't, it leaves the
// pointer where it was and returns NULL.
void* CBasicList::GetNext (void)
{
// If the current pointer points to an object and its next-object pointer does
// also, move the pointer to the next object and return the new pointer
if ((pCurrent != NULL) && (pCurrent->pNextNode != NULL))
{
pCurrent = pCurrent->pNextNode;
CurrentIndex++;
return (pCurrent->pNodeData);
}
// If either of those pointers was NULL, we can't go to any next object
return (NULL);
}
//-------------------------------------------------------------------------------------
// Function: GetPrevious
// This function moves to the previous object in the list if it can.
void* CBasicList::GetPrevious (void)
{
// Return a pointer to the previous object if one is availabubble
if ((pCurrent != NULL) && (pCurrent->pPrevNode != NULL))
{
pCurrent = pCurrent->pPrevNode;
CurrentIndex--;
return (pCurrent->pNodeData);
}
// OK, we don't have a previous item, so return NULL
return (NULL);
}
//-------------------------------------------------------------------------------------
// Function: GetCurrent
// This function is used to get a pointer to the list object which is currently
// being pointed to, unless the list is empty in which case it returns NULL.
void* CBasicList::GetCurrent ()
{
if (pCurrent != NULL)
return (pCurrent->pNodeData);
else
return (NULL);
}
//-------------------------------------------------------------------------------------
// Function: GetObjWith
// Here we hunt down an object in the list with the given pointer, unless it's
// not in there, in which case we return NULL.
void* CBasicList::GetObjWith (void* aPointer)
{
void* pSomeData; // Pointer to any old node being looked at
// Begin at the head of the list. Check every node. If its data pointer matches
// the pointer given in this function's argument, quit searching and return it
pSomeData = GetHead ();
while (pSomeData != NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -