00001 /* 00002 * Copyright 2004, 2005, 2006 PathScale, Inc. All Rights Reserved. 00003 */ 00004 00005 /* 00006 00007 Copyright (C) 2000, 2001 Silicon Graphics, Inc. All Rights Reserved. 00008 00009 This program is free software; you can redistribute it and/or modify it 00010 under the terms of version 2 of the GNU General Public License as 00011 published by the Free Software Foundation. 00012 00013 This program is distributed in the hope that it would be useful, but 00014 WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00016 00017 Further, this software is distributed without any warranty that it is 00018 free of the rightful claim of any third person regarding infringement 00019 or the like. Any license provided herein, whether implied or 00020 otherwise, applies only to this software file. Patent licenses, if 00021 any, provided herein do not apply to combinations of this program with 00022 other software, or any other product whatsoever. 00023 00024 You should have received a copy of the GNU General Public License along 00025 with this program; if not, write the Free Software Foundation, Inc., 59 00026 Temple Place - Suite 330, Boston MA 02111-1307, USA. 00027 00028 Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pky, 00029 Mountain View, CA 94043, or: 00030 00031 http://www.sgi.com 00032 00033 For further information regarding this notice, see: 00034 00035 http://oss.sgi.com/projects/GenInfo/NoticeExplan 00036 00037 */ 00038 00039 00040 #include <sys/types.h> 00041 #include <stdio.h> 00042 #include "cxx_memory.h" 00043 #include "errors.h" 00044 #include "cxx_queue.h" 00045 00046 #ifndef __GNUC__ 00047 // Implementation stuff excluded here because g++ 00048 // (rightly) doesn't do implicit .cxx file inclusion. 00049 00050 template <class ITEM_TYPE> 00051 QUEUE<ITEM_TYPE>::QUEUE (MEM_POOL *pool) 00052 { 00053 _pool = pool; 00054 _length = 0; 00055 _first = _last = NULL; 00056 } 00057 00058 template <class ITEM_TYPE> 00059 void 00060 QUEUE<ITEM_TYPE>::Add_Tail_Q(ITEM_TYPE item) 00061 { 00062 QUEUE_NODE<ITEM_TYPE> *node; 00063 00064 node = CXX_NEW (QUEUE_NODE<ITEM_TYPE>(item), _pool); 00065 if (0 == _length) 00066 _first = _last = node; 00067 else { 00068 _last->Qnode_Next(node); 00069 _last = node; 00070 } 00071 _length++; 00072 return; 00073 } 00074 00075 template<class ITEM_TYPE> 00076 ITEM_TYPE 00077 QUEUE<ITEM_TYPE>::Get_Q () 00078 { 00079 ITEM_TYPE item; 00080 QUEUE_NODE<ITEM_TYPE> *node; 00081 00082 if (0 == _length) 00083 return (ITEM_TYPE) 0; 00084 node = _first; 00085 item = node->Qnode_Item(); 00086 /* Remove node from queue */ 00087 _first = node->Qnode_Next(); 00088 _length--; 00089 if (0 == _length) 00090 _last = NULL; 00091 return item; 00092 } 00093 00094 template<class ITEM_TYPE> 00095 ITEM_TYPE 00096 QUEUE<ITEM_TYPE>::Get_Tail_Q () 00097 { 00098 ITEM_TYPE item; 00099 QUEUE_NODE<ITEM_TYPE> *node; 00100 INT32 count, i; 00101 00102 if (0 == _length) 00103 return (ITEM_TYPE) 0; 00104 else if (1 == _length) 00105 return this->Get_Q(); 00106 else { 00107 count = _length - 2; 00108 node = _first; 00109 for (i = 0; i < count; i++) { 00110 node = node->Qnode_Next(); 00111 } 00112 assert (node->Qnode_Next() == _last); 00113 item = _last->Qnode_Item(); 00114 node->Qnode_Next(NULL); 00115 _length--; 00116 _last = node; 00117 return item; 00118 } 00119 } 00120 00121 template<class ITEM_TYPE> 00122 INT32 00123 QUEUE<ITEM_TYPE>::Index(ITEM_TYPE item, BOOL Insert_If_Absent) 00124 { 00125 INT32 ret_val = 0; 00126 QUEUE_NODE<ITEM_TYPE> *node = _first; 00127 while (NULL != node) { 00128 if (node->Qnode_Item() == item) 00129 return ret_val; 00130 node = node->Qnode_Next(); 00131 ret_val++; 00132 } 00133 FmtAssert (ret_val == _length, ("Inconsistency in queue index function")); 00134 if (Insert_If_Absent) { 00135 this->Add_Tail_Q (item); 00136 return ret_val; 00137 } 00138 else 00139 return -1; 00140 } 00141 00142 #endif
1.5.6