[Phission-cvs] phission/base/src phList.cpp,NONE,1.1
Brought to you by:
pthoren
From: Philip T. <pt...@us...> - 2004-07-31 18:35:31
|
Update of /cvsroot/phission/phission/base/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3089/base/src Added Files: phList.cpp Log Message: Made a couple simple changes/updates to FLDisplay. Adding the list code so it can be debugged on Linux. --- NEW FILE: phList.cpp --- /* --------------------------------------------------------------------------- Phission : Realtime Vision Processing System Copyright (C) 2003 Philip D.S. Thoren (pt...@cs...) University of Massachusetts at Lowell, Laboratory for Artificial Intelligence and Robotics This file is part of Phission. Phission is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Phission is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Phission; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ---------------------------------------------------------------------------*/ #include <phList.h> #include <stdio.h> #include <stdlib.h> #include <phbase.h> /* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */ phListNode::phListNode() { const char *function = "phListNode::phListNode"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); setName("phListNode"); this->m_previous = this->m_next = NULL; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phListNode::~phListNode() { const char *function = "phListNode::~phListNode"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); this->m_next = this->m_previous = NULL; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ int phListNode::setNext ( phListNode *node ) { const char *function = "phListNode::setNext"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); this->m_next = node; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return SUCCESS; } /* ---------------------------------------------------------------------- */ int phListNode::setPrevious ( phListNode *node ) { const char *function = "phListNode::setPrevious"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); this->m_previous = node; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return SUCCESS; } /* ---------------------------------------------------------------------- */ phListNode *phListNode::getNext ( ) { const char *function = "phListNode::getNext"; int rc = 0; phListNode *next = NULL; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); next = this->m_next; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return next; } /* ---------------------------------------------------------------------- */ phListNode *phListNode::getPrevious ( ) { const char *function = "phListNode::getPrevious"; int rc = 0; phListNode *prev = NULL; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); prev = this->m_previous; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return prev; } /* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */ phDataNode::phDataNode() { const char *function = "phDataNode::phDataNode"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); setName("phDataNode"); this->m_obj = NULL; this->m_autoDelete = 0; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phDataNode::phDataNode(phObject *pobj, int autoDelete_on) { const char *function = "phDataNode::phDataNode(phObject *pobj,int autoDelete_on)"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); setName("phDataNode"); this->m_obj = NULL; this->m_autoDelete = autoDelete_on; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phDataNode::~phDataNode() { const char *function = "phDataNode::~phDataNode"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); if (this->m_autoDelete) { DELETE( this->m_obj ); this->m_obj = NULL; } rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ int phDataNode::setObject( phObject *obj ) { const char *function = "phDataNode::setObject"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); this->m_obj = obj; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return SUCCESS; } /* ---------------------------------------------------------------------- */ phObject *phDataNode::getObject() { const char *function = "phDataNode::getObject"; int rc = 0; phObject *obj = NULL; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); obj = this->m_obj; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return obj; } /* ---------------------------------------------------------------------- */ int phDataNode::enableAutoDelete() { const char *function = "phDataNode::enableAutoDelete"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); this->m_autoDelete = 1; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return SUCCESS; } /* ---------------------------------------------------------------------- */ int phDataNode::disableAutoDelete() { const char *function = "phDataNode::disableAutoDelete"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); this->m_autoDelete = 0; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return SUCCESS; } /* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */ phList::phList() { const char *function = "phList::phList"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); this->m_totalNodes = 0; this->m_head = this->m_tail = NULL; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phList::~phList() { const char *function = "phList::~phList"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); this->m_totalNodes = 0; this->m_head = this->m_tail = NULL; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ int32_t phList::getTotal() { const char *function = "phList::getTotal"; int rc = 0; int32_t total = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); total = this->m_totalNodes; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return total; } /* ---------------------------------------------------------------------- */ int phList::isEmpty() { const char *function = "phList::isEmpty"; int rc = 0; int isempty = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); isempty = (this->m_totalNodes > 0) ? 0 : 1; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phListNode *phList::insert( phObject *pobj, int32_t index ) { const char *function = "phList::add"; int rc = 0; phListNode *new_node = NULL; phListNode *insert_after = NULL; phListNode *insert_before = NULL; if (pobj == NULL) return NULL; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); new_node = new phDataNode( pobj ); /* head and tail should be NULL only at the same time */ if ((this->m_head == NULL) && (this->m_tail == NULL)) { this->m_tail = this->m_head = new_node; /* Adjust the total count of nodes in the list */ this->m_totalNodes++; } else { if (index <= 0) { /* this should always happen if the previous if block * isn't satisfied */ if (this->m_head != NULL) { insert_before = this->m_head; this->m_head = new_node; } } else if (index >= (this->m_totalNodes - 1)) { /* this should always happen if the previous if block * isn't satisfied */ if (this->m_tail != NULL) { insert_after = this->m_tail; this->m_tail = new_node; } } else if ((index < (this->m_totalNodes - 1)) && (index > 0)) { int i = 0; phListNode *insert_after = this->m_head; phListNode *insert_before = (insert_after != NULL) ? insert_after->getNext() : NULL; for ( i = 0; (i < (index-1)) && (insert_before != NULL); i++ ) { insert_after = insert_before; if (insert_after != NULL) insert_before = insert_after->getNext(); } } new_node->setNext( insert_before ); new_node->setPrevious( insert_after ); if (insert_after != NULL) insert_after->setNext( new_node ); if (insert_before != NULL) insert_before->setPrevious( new_node ); /* Adjust the total count of nodes in the list */ this->m_totalNodes++; } rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return new_node; } /* ---------------------------------------------------------------------- */ #define DECLARE_LOCK_CHECKS() \ int locked = 0; \ int node_locked = 0; \ int head_locked = 0; \ int tail_locked = 0; \ int prev_locked = 0; \ int next_locked = 0; #define LOCK() \ if (locked == 0) { \ rc = this->lock(); \ PRINT_RC(rc,NULL,"rc = this->lock()"); \ locked = 1; \ } \ else \ { \ PROGRESS("Locked already\n"); \ } #define LOCK_NODE() \ if (node_locked == 0) \ { \ rc = node->lock(); \ PRINT_RC(rc,NULL,"rc = node->lock()"); \ node_locked = 1; \ } \ else \ { \ PROGRESS("Node locked already\n"); \ } #define LOCK_HEAD() \ if (head_locked == 0) \ { \ rc = this->m_headMutex.lock(); \ PRINT_RC(rc,NULL,"rc = this->m_headMutex.lock();"); \ head_locked = 1; \ } \ else \ { \ PROGRESS("Head locked already\n"); \ } #define LOCK_TAIL() \ if (tail_locked == 0) \ { \ rc = this->m_tailMutex.lock(); \ PRINT_RC(rc,NULL,"rc = this->m_tailMutex.lock();"); \ tail_locked = 1; \ } \ else \ { \ PROGRESS("Tail locked already\n"); \ } #define LOCK_PREV() \ if (prev_locked == 0) \ { \ if (prev != NULL) \ { \ rc = prev.lock(); \ PRINT_RC(rc,NULL,"rc = prev.lock();"); \ } \ prev_locked = 1; \ } \ else \ { \ PROGRESS("Previous locked already\n"); \ } #define LOCK_NEXT() \ if (next_locked == 0) \ { \ if (next != NULL) \ { \ rc = next.lock(); \ PRINT_RC(rc,NULL,"rc = next.lock();"); \ } \ next_locked = 1; \ } \ else \ { \ PROGRESS("Next locked already\n"); \ } #define UNLOCK_PREV() \ if (prev_locked == 1) \ { \ if (prev != NULL) \ { \ rc = prev.unlock(); \ PRINT_RC(rc,NULL,"rc = prev.unlock();"); \ } \ prev_locked = 0; \ } \ else \ { \ PROGRESS("Previous unlocked already\n"); \ } #define UNLOCK_NEXT() \ if (next_locked == 1) \ { \ if (next != NULL) \ { \ rc = next.unlock(); \ PRINT_RC(rc,NULL,"rc = next.unlock();"); \ } \ next_locked = 0; \ } \ else \ { \ PROGRESS("Next unlocked already\n"); \ } #define UNLOCK_NODE() \ if (node_locked == 1) \ { \ rc = node->unlock(); \ PRINT_RC(rc,NULL,"rc = node->unlock()");\ node_locked = 0; \ } \ else \ { \ PROGRESS("Node unlocked already\n"); \ } #define UNLOCK_TAIL() \ if (tail_locked == 1) \ { \ rc = this->m_tailMutex.unlock(); \ PRINT_RC(rc,NULL,"rc = this->m_tailMutex.unlock();"); \ tail_locked = 0; \ } \ else \ { \ PROGRESS("Tail unlocked already\n"); \ } #define UNLOCK_HEAD() \ if (head_locked == 1) \ { \ rc = this->m_headMutex.unlock(); \ PRINT_RC(rc,NULL,"rc = this->m_headMutex.unlock();"); \ head_locked = 0; \ } \ else \ { \ PROGRESS("Head unlocked already\n"); \ } #define UNLOCK() \ if (locked == 1) \ { \ rc = this->unlock(); \ PRINT_RC(rc,NULL,"rc = this->unlock()"); \ locked = 0; \ } \ else \ { \ PROGRESS("Unlocked already\n"); \ } /* ---------------------------------------------------------------------- */ phListNode *phList::remove( phListNode *node ) { const char *function = "phList::add"; int rc = 0; if (node == NULL) return NULL; DECLARE_LOCK_CHECKS(); LOCK(); /* 1 */ LOCK_NODE(); /* 1,2 */ LOCK_HEAD(); /* 1,2,3 */ LOCK_TAIL(); /* 1,2,3,4 */ phListNode *prev = node->getPrevious(); phListNode *next = node->getNext(); LOCK_PREV(); /* 1,2,3,4,5 */ LOCK_NEXT(); /* 1,2,3,4,5,6 */ /* if there is no previous and next node, this is the only node */ if ((node->getPrevious() == NULL) && (node->getNext() == NULL) && (this->m_head == node) && (this->m_tail == node)) { /* Adjust the list's first node to this node's next node */ this->m_head = this->m_tail = NULL; UNLOCK_NEXT(); /* 1,2,3,4,5 */ UNLOCK_PREV(); /* 1,2,3,4 */ UNLOCK_TAIL(); /* 1,2,3 */ UNLOCK_HEAD(); /* 1,2 */ /* Adjust the total count of nodes in the list */ list->m_totalNodes--; UNLOCK(); /* 2 */ } /* "Dequeue"/"Pop" */ /* if there is no next pointer, this is the last node... */ else if ((node->getNext() == NULL) && (this->m_tail == node)) { UNLOCK_NEXT(); /* 1,2,3,4 */ UNLOCK_HEAD(); /* Adjust the total count of nodes in the list */ list->m_totalNodes--; UNLOCK(); /*... there must be a previous node, otherwise the first if block * * would have executed. So, Adjust the previous node */ prev->setNext(NULL); /* Adjust the list's last node to the removed node's previous */ this->m_tail = prev; UNLOCK_PREV(); UNLOCK_TAIL(); } /* "Shift" */ /* If there is no previous pointer, this is the first node ... */ else if (node->getPrevious() == NULL) { /*... there must be a next node, otherwise the first "if block" * * would have executed. So, Adjust the first node: */ next->setPrevious( NULL ); /* Adjust the list's first node to this node's next */ this->m_head = next; } /* The node is in the middle somewhere, neither first nor last */ else { phListNode *prev = node->getPrevious(); phListNode *next = node->getNext(); /* Adjust the previous's next, then the next's previous */ prev->setNext( next ); next->setPrevious( prev ); } /* adjust the removed node's pointers */ node->setNext( NULL ); node->setPrevious( NULL ); UNLOCK_NODE(); return node; } /* ---------------------------------------------------------------------- */ phListNode *phList::add( phObject *pobj ) { const char *function = "phList::add"; int rc = 0; phListNode *new_node = NULL; if (pobj == NULL) return NULL; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); new_node = this->insert( pobj, this->m_totalNodes ); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return new_node; } /* ---------------------------------------------------------------------- */ phListNode *phList::push( phObject *pobj ) { const char *function = "phList::push"; int rc = 0; phListNode *new_node = NULL; if (pobj == NULL) return NULL; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); new_node = this->insert( pobj, this->m_totalNodes ); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return new_node; } /* ---------------------------------------------------------------------- */ phObject *phList::dequeue( ) { const char *function = ""; int rc = 0; phObject *obj = NULL; phListNode *node = NULL; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); node = this->dequeueNode( ); obj = node->getObject(); node->setObject( NULL ); delete node; rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return obj; } /* ---------------------------------------------------------------------- */ phListNode *phList::dequeueNode( ) { const char *function = "phList::dequeueNode"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phObject *phList::pop( phObject *pobj ) { const char *function = "phList::pop"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phListNode *phList::popNode( ) { const char *function = "phList::popNode"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phObject *phList::shift( ) { const char *function = "phList::shift"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phListNode *phList::shiftNode( ) { const char *function = "phList::shiftNode"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phListNode *phList::enqueue( phObject *pobj ) { const char *function = "phList::enqueue"; int rc = 0; phListNode *new_node = NULL; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); new_node = this->insert( pobj, -1 ); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return new_node; } /* ---------------------------------------------------------------------- */ phListNode *phList::unshift( phObject *pobj ) { const char *function = "phList::unshift"; int rc = 0; phListNode *new_node = NULL; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); new_node = this->insert( pobj, -1 ); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); return new_node; } /* ---------------------------------------------------------------------- */ phObject *phList::remove( phListNode *node ) { const char *function = "phList::remove( phListNode *node )"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phListNode *phList::removeNode( phListNode *node ) { const char *function = "phList::removeNode( phListNode *node )"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phObject *phList::remove( phObject *node ) { const char *function = "phList::remove( phObject *node )"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phListNode *phList::removeNode( phObject *node ) { const char *function = "phList::removeNode( phObject *node )"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phObject *phList::removeTail() { const char *function = "phList::removeTail"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phObject *phList::removeHead() { const char *function = "phList::removeHead"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phObject *phList::getTail() { const char *function = "phList::getTail"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phListNode *phList::getTailNode() { const char *function = "phList::getTailNode"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phObject *phList::getHead() { const char *function = "phList::getHead"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ phListNode *phList::getHeadNode() { const char *function = "phList::getHeadNode"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } /* ---------------------------------------------------------------------- */ int phList::empty() { const char *function = "phList::empty"; int rc = 0; rc = this->lock(); PRINT_RC(rc,NULL,"rc = this->lock()"); rc = this->unlock(); PRINT_RC(rc,NULL,"rc = this->unlock()"); } |