00001 /* 00002 SagaEngine library 00003 Copyright (c) 2002-2006 Skalden Studio AS 00004 00005 This software is provided 'as-is', without any express or implied 00006 warranty. In no event will the authors be held liable for any 00007 damages arising from the use of this software. 00008 00009 Permission is granted to distribute the library under the terms of the 00010 Q Public License version 1.0. Be sure to read and understand the license 00011 before using the library. It should be included here, or you may read it 00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl 00013 00014 The original version of this library can be located at: 00015 http://www.sagaengine.com/ 00016 00017 Rune Myrland 00018 rune@skalden.com 00019 */ 00020 00021 00022 #include "CompositeList.hpp" 00023 #include "comp/Composite.hpp" 00024 #include "../schema/CompSchema.hpp" 00025 #include "util/error/Log.hpp" 00026 #include <cstdio> 00027 00028 00029 namespace se_core { 00030 00031 CompositeList 00032 ::CompositeList() 00033 : firstNode_(CompSchema::VoidList::end()) { 00034 } 00035 00036 00037 CompositeList 00038 ::~CompositeList() { 00039 CompSchema::voidList.removeChain(firstNode_); 00040 firstNode_ = CompSchema::VoidList::end(); 00041 } 00042 00043 00044 void CompositeList 00045 ::add(Composite& value) { 00046 Assert(&value); 00047 //LogWarning(value.name() << " added"); 00048 CompSchema::voidList.add(&value, firstNode_); 00049 } 00050 00051 00052 void CompositeList 00053 ::remove(Composite& value) { 00054 //LogWarning(value.name() << " removed"); 00055 CompSchema::voidList.remove(&value, firstNode_); 00056 } 00057 00058 00059 bool CompositeList 00060 ::contains(Composite& value) const { 00061 CompSchema::VoidList::iterator_type it = iterator(); 00062 while(it != CompSchema::VoidList::end()) { 00063 if(CompSchema::voidList.next(it) == &value) 00064 return true; 00065 } 00066 return false; 00067 } 00068 00069 00070 bool CompositeList 00071 ::contains(CompositeList& msc) const { 00072 CompSchema::VoidList::iterator_type it = msc.iterator(); 00073 while(it != CompSchema::VoidList::end()) { 00074 if(!contains(*static_cast<Composite*>(CompSchema::voidList.next(it)))) 00075 return false; 00076 } 00077 return true; 00078 } 00079 00080 00081 bool CompositeList 00082 ::sharesAny(CompositeList& msc) const { 00083 CompSchema::VoidList::iterator_type it = msc.iterator(); 00084 while(it != CompSchema::VoidList::end()) { 00085 if(contains(*static_cast<Composite*>(CompSchema::voidList.next(it)))) { 00086 return true; 00087 } 00088 } 00089 return false; 00090 } 00091 00092 00093 void CompositeList 00094 ::add(CompositeList& msc) { 00095 Assert(&msc); 00096 CompSchema::VoidList::iterator_type it = msc.iterator(); 00097 while(it != CompSchema::VoidList::end()) { 00098 add(*static_cast<Composite*>(CompSchema::voidList.next(it))); 00099 } 00100 } 00101 00102 00103 void CompositeList 00104 ::destroyMembersAndClear() { 00105 CompSchema::VoidList::iterator_type it = iterator(); 00106 while(it != CompSchema::VoidList::end()) { 00107 delete static_cast<Composite*>(CompSchema::voidList.next(it)); 00108 } 00109 clear(); 00110 } 00111 00112 00113 void CompositeList 00114 ::initIterator(CompSchema::VoidList::iterator_type& iterator) const { 00115 iterator = firstNode_; 00116 } 00117 00118 00119 bool CompositeList 00120 ::isEmpty() const { 00121 return (firstNode_ == CompSchema::VoidList::end()); 00122 } 00123 00124 00125 void CompositeList 00126 ::clear() { 00127 CompSchema::voidList.removeChain(firstNode_); 00128 } 00129 00130 00131 int CompositeList 00132 ::size() const { 00133 return CompSchema::voidList.size(firstNode_); 00134 } 00135 00136 00137 00138 CompositeList::Iterator 00139 ::Iterator() 00140 : it_(CompSchema::VoidList::end()) { 00141 } 00142 00143 00144 CompositeList::Iterator 00145 ::Iterator(const CompositeList& msc) { 00146 (*this).init(msc); 00147 } 00148 00149 00150 void CompositeList::Iterator 00151 ::init(const CompositeList& msc) { 00152 msc.initIterator(it_); 00153 } 00154 00155 00156 void CompositeList::Iterator 00157 ::init(short firstNode) { 00158 it_ = firstNode; 00159 } 00160 00161 00163 CompositeList::TreeIterator 00164 ::TreeIterator() 00165 : stackPointer_(-1) { 00166 } 00167 00168 00169 CompositeList::TreeIterator 00170 ::TreeIterator(const CompositeList& msc) { 00171 if(msc.isEmpty()) { 00172 stackPointer_ = -1; 00173 return; 00174 } 00175 itStack_[ 0 ].init(msc); 00176 stackPointer_ = 0; 00177 } 00178 00179 00180 void CompositeList::TreeIterator 00181 ::init(const CompositeList& msc) { 00182 if(msc.isEmpty()) { 00183 stackPointer_ = -1; 00184 return; 00185 } 00186 itStack_[ 0 ].init(msc); 00187 stackPointer_ = 0; 00188 } 00189 00190 00191 bool CompositeList::TreeIterator 00192 ::hasNext() { 00193 return (stackPointer_ >= 0); 00194 } 00195 00196 00197 Composite& CompositeList::TreeIterator 00198 ::next() { 00199 // Get next in chain 00200 Composite& c = itStack_[ stackPointer_ ].next(); 00201 00202 // Stack overflowed? 00203 Assert(stackPointer_ < MAX_STACK_DEPTH - 1); 00204 00205 // Push child chain as next chain on stack 00206 itStack_[ ++stackPointer_ ].init(c.children()); 00207 00208 // Pop all completed chains 00209 while(stackPointer_ >= 0 && !itStack_[ stackPointer_ ].hasNext()) { 00210 --stackPointer_; 00211 } 00212 00213 return c; 00214 } 00215 00216 }
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:10 2007 by Doxygen version 1.3.9.1.