00001 // circuitADT.cc 00002 // SuperMix version 1.0 C++ source file 00003 // 00004 // Copyright (c) 1999 California Institute of Technology. 00005 // All rights reserved. 00006 // 00007 // Redistribution and use in source and binary forms for noncommercial 00008 // purposes are permitted provided that the above copyright notice and 00009 // this paragraph are duplicated in all such forms and that any 00010 // documentation and other materials related to such distribution and 00011 // use acknowledge that the software was developed by California 00012 // Institute of Technology. Redistribution and/or use in source or 00013 // binary forms is not permitted for any commercial purpose. Use of 00014 // this software does not include a permitted use of the Institute's 00015 // name or trademark for any purpose. 00016 // 00017 // DISCLAIMER: 00018 // THIS SOFTWARE AND/OR RELATED MATERIALS ARE PROVIDED "AS-IS" WITHOUT 00019 // WARRANTY OF ANY KIND INCLUDING ANY WARRANTIES OF PERFORMANCE OR 00020 // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE OR PURPOSE (AS SET 00021 // FORTH IN UCC 23212-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE 00022 // LICENSED PRODUCT, HOWEVER USED. IN NO EVENT SHALL CALTECH/JPL BE 00023 // LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING BUT NOT LIMITED TO 00024 // INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING ECONOMIC 00025 // DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF 00026 // WHETHER CALTECH/JPL SHALL BE ADVISED, HAVE REASON TO KNOW, OR IN 00027 // FACT SHALL KNOW OF THE POSSIBILITY. THE USER BEARS ALL RISK 00028 // RELATING TO QUALITY AND PERFORMANCE OF THE SOFTWARE AND/OR RELATED 00029 // MATERIALS. 00030 // 00031 // Change history: 00032 // 8/21/98: added pop() to portVector 00033 // 7/28/98: added <iostream.h> to includes 00034 00035 #include "circuitADT.h" 00036 #include "error.h" 00037 #include <iostream.h> 00038 00039 nportSet::nportSet() 00040 { 00041 this->head = 0; 00042 numnodes = 0; 00043 } 00044 00045 nportSet::nportSet(const nportSet & s) 00046 { 00047 // Initialize the new list before doing anything else. 00048 head = 0; 00049 numnodes = 0; 00050 00051 nportSetNode *ptr; 00052 00053 ptr = s.head; 00054 00055 while(ptr != 0) 00056 { 00057 add(ptr->dev); 00058 ptr = ptr->next; 00059 } 00060 } 00061 00062 nportSet::~nportSet() 00063 { 00064 while(numnodes > 0) 00065 remove(head); 00066 } 00067 00068 nportSetNode *nportSet::find(unsigned long id) 00069 { 00070 nportSetNode *node = this->head; 00071 while(node != 0) 00072 { 00073 if(node->key == id) return node; 00074 node = node->next; 00075 } 00076 return 0; 00077 } 00078 00079 void nportSet::remove(nportSetNode *node) 00080 { 00081 if(node != 0) 00082 { 00083 if(node->prev != 0) 00084 node->prev->next = node->next; 00085 else 00086 this->head = node->next; 00087 00088 if(node->next != 0) 00089 node->next->prev = node->prev; 00090 00091 delete node; 00092 00093 numnodes--; 00094 } 00095 } 00096 00097 int nportSet::len() const 00098 { 00099 return numnodes; 00100 } 00101 00102 void nportSet::add(nport *n) 00103 { 00104 // Only add device if it is *not* already in the list. 00105 if(find(n->id) == 0) 00106 { 00107 nportSetNode *node = new nportSetNode; 00108 node->dev = n; 00109 node->key = n->id; 00110 node->next = this->head; 00111 node->prev = 0; 00112 this->head = node; 00113 if(node->next != 0) 00114 node->next->prev = node; 00115 numnodes++; 00116 } 00117 } 00118 00119 nport *nportSet::pop(unsigned long id) 00120 { 00121 nportSetNode *tmp; 00122 nport *n; 00123 00124 tmp = find(id); 00125 00126 // Found it! 00127 if(tmp != 0) 00128 { 00129 n = tmp->dev; 00130 remove(tmp); 00131 return n; 00132 } 00133 00134 // Didn't find it. 00135 return 0; 00136 } 00137 00138 nport *nportSet::pop() 00139 { 00140 nport *tmpdev; 00141 nportSetNode *tmpnode; 00142 00143 // Don't do anything if the list is empty. 00144 if(numnodes == 0) 00145 return 0; 00146 00147 // Pop the list's head off. 00148 tmpnode = head; 00149 head = tmpnode->next; 00150 if(head != 0) 00151 head->prev = 0; 00152 numnodes--; 00153 00154 // Don't lose the head's device, store it in tmpdev 00155 tmpdev = tmpnode->dev; 00156 00157 // Delete the old head node. 00158 delete tmpnode; 00159 00160 return tmpdev; 00161 } 00162 00163 nportSet& nportSet::operator=(const nportSet & s) 00164 { 00165 // Beware of self assignment s = s 00166 if(this != &s) 00167 { 00168 // Delete the current nportSet 00169 while(numnodes > 0) 00170 remove(head); 00171 00172 nportSetNode *ptr; 00173 00174 ptr = s.head; 00175 00176 while(ptr != 0) 00177 { 00178 add(ptr->dev); 00179 ptr = ptr->next; 00180 } 00181 } 00182 return *this; 00183 } 00184 00185 void nportSet::show() 00186 { 00187 nportSetNode *ptr = head; 00188 00189 while(ptr != 0) 00190 { 00191 cout << ptr->key << " "; 00192 ptr = ptr->next; 00193 } 00194 cout << endl; 00195 } 00196 00197 tmpList::tmpList() 00198 { 00199 this->head = 0; 00200 numnodes = 0; 00201 } 00202 00203 tmpList::tmpList(const tmpList & l) 00204 { 00205 // Initialize the new list before doing anything else. 00206 this->head = 0; 00207 numnodes = 0; 00208 00209 tmpListNode *ptr; 00210 00211 ptr = l.head; 00212 00213 while(ptr != 0) 00214 { 00215 add(ptr->dev); 00216 ptr = ptr->next; 00217 } 00218 } 00219 00220 tmpList::~tmpList() 00221 { 00222 while(numnodes > 0) 00223 remove(head); 00224 } 00225 00226 tmpListNode *tmpList::find(port p) 00227 { 00228 tmpListNode *node = this->head; 00229 while(node != 0) 00230 { 00231 if(node->dev->get_port(p) != 0) return node; 00232 node = node->next; 00233 } 00234 return 0; 00235 } 00236 00237 void tmpList::remove(tmpListNode *node) 00238 { 00239 if(node != 0) 00240 { 00241 if(node->prev != 0) 00242 node->prev->next = node->next; 00243 else 00244 this->head = node->next; 00245 00246 if(node->next != 0) 00247 node->next->prev = node->prev; 00248 00249 delete node; 00250 00251 numnodes--; 00252 } 00253 } 00254 00255 int tmpList::len() const 00256 { 00257 return numnodes; 00258 } 00259 00260 void tmpList::add(connection *c) 00261 { 00262 tmpListNode *node = new tmpListNode; 00263 node->dev = c; 00264 node->next = this->head; 00265 node->prev = 0; 00266 this->head = node; 00267 if(node->next != 0) 00268 node->next->prev = node; 00269 numnodes++; 00270 } 00271 00272 connection *tmpList::pop(port p) 00273 { 00274 tmpListNode *tmp; 00275 connection *c; 00276 00277 tmp = find(p); 00278 00279 // Found it! 00280 if(tmp != 0) 00281 { 00282 c = tmp->dev; 00283 remove(tmp); 00284 return c; 00285 } 00286 00287 // Didn't find it. 00288 return 0; 00289 } 00290 00291 connection *tmpList::pop() 00292 { 00293 connection *tmpdev; 00294 tmpListNode *tmpnode; 00295 00296 // Don't do anything if the list is empty. 00297 if(numnodes == 0) 00298 return 0; 00299 00300 // Pop the list's head off. 00301 tmpnode = head; 00302 head = tmpnode->next; 00303 if(head != 0) 00304 head->prev = 0; 00305 numnodes--; 00306 00307 // Don't lose the head's device, store it in tmpdev 00308 tmpdev = tmpnode->dev; 00309 00310 // Delete the old head node. 00311 delete tmpnode; 00312 00313 return tmpdev; 00314 } 00315 00316 tmpList& tmpList::operator=(const tmpList & l) 00317 { 00318 // Beware of self assignment l = l 00319 if(this != &l) 00320 { 00321 // Delete the current tmpList 00322 while(numnodes > 0) 00323 remove(head); 00324 00325 tmpListNode *ptr; 00326 00327 ptr = l.head; 00328 00329 while(ptr != 0) 00330 { 00331 add(ptr->dev); 00332 ptr = ptr->next; 00333 } 00334 } 00335 return *this; 00336 } 00337 00338 void tmpList::show() 00339 { 00340 tmpListNode *ptr = head; 00341 00342 while(ptr != 0) 00343 { 00344 cout << ptr->dev->id << " "; 00345 ptr = ptr->next; 00346 } 00347 cout << endl; 00348 } 00349 00350 portStack::portStack() 00351 { 00352 this->head = 0; 00353 } 00354 00355 portStack::portStack(const portStack & s) 00356 { 00357 // Initialize new stack before doing anything else. 00358 head = 0; 00359 00360 portStackNode *current; 00361 00362 current = s.head; 00363 00364 while(current != 0) 00365 { 00366 push(current->cons); 00367 current = current->next; 00368 } 00369 } 00370 00371 portStack::~portStack() 00372 { 00373 while(!isEmpty()) 00374 pop(); 00375 } 00376 00377 int portStack::isEmpty() 00378 { 00379 if(head == 0) 00380 return TRUE; 00381 else 00382 return FALSE; 00383 } 00384 00385 int portStack::isPresent(port p) 00386 { 00387 portStackNode *current; 00388 00389 current = head; 00390 00391 while(current != 0) 00392 { 00393 if((current->cons.get(1) == p) || (current->cons.get(2) == p)) 00394 return TRUE; 00395 current = current->next; 00396 } 00397 00398 return FALSE; 00399 } 00400 00401 void portStack::push(portArray c) 00402 { 00403 if(c.len() == 2) 00404 { 00405 portStackNode *node = new portStackNode; 00406 node->cons = c; 00407 node->next = this->head; 00408 this->head = node; 00409 } 00410 else 00411 error::warning("portArray must be of length 2 for portStack::push"); 00412 } 00413 00414 portArray portStack::pop() 00415 { 00416 portArray tmpcons; 00417 portStackNode *tmpnode; 00418 00419 // Don't do anything if the stack is empty. 00420 if(isEmpty()) 00421 error::fatal("Attempt to call portStack::pop for an empty stack."); 00422 00423 // Pop the stack's head off. 00424 tmpnode = head; 00425 head = tmpnode->next; 00426 00427 // Don't lose the head's portArray, store it in tmparray 00428 tmpcons = tmpnode->cons; 00429 00430 // Delete the old head node. 00431 delete tmpnode; 00432 00433 return tmpcons; 00434 } 00435 00436 portStack& portStack::operator=(const portStack & s) 00437 { 00438 // Beware of self assignment s = s 00439 if(this != &s) 00440 { 00441 // Delete the current portStack 00442 while(!isEmpty()) 00443 pop(); 00444 00445 portStackNode *current; 00446 00447 current = s.head; 00448 00449 while(current != 0) 00450 { 00451 push(current->cons); 00452 current = current->next; 00453 } 00454 } 00455 return *this; 00456 } 00457 00458 void portStack::show() 00459 { 00460 portStackNode *current; 00461 00462 current = head; 00463 00464 while(current != 0) 00465 { 00466 cout << current->cons.get(1).id << ":"; 00467 cout << current->cons.get(1).index << ", "; 00468 cout << current->cons.get(2).id << ":"; 00469 cout << current->cons.get(2).index << endl; 00470 current = current->next; 00471 } 00472 } 00473 00474 portVector::portVector() 00475 { 00476 this->head = 0; 00477 numnodes = 0; 00478 } 00479 00480 portVector::portVector(const portVector & v) 00481 { 00482 // Initialize the new vector before doing anything else. 00483 this->head = 0; 00484 numnodes = 0; 00485 00486 // Copy the old vector into the new vector. 00487 for(int i=1; i<=v.len(); i++) 00488 add(v.get(i)); 00489 } 00490 00491 portVector::~portVector() 00492 { 00493 portVectorNode *tmp; 00494 00495 while(head !=0) 00496 { 00497 tmp = head; 00498 head = head->next; 00499 delete tmp; 00500 } 00501 } 00502 00503 int portVector::len() const 00504 { 00505 return numnodes; 00506 } 00507 00508 int portVector::isPresent(port p) 00509 { 00510 portVectorNode *current; 00511 00512 current = head; 00513 00514 while(current != 0) 00515 { 00516 if(current->label == p) 00517 return TRUE; 00518 current = current->next; 00519 } 00520 00521 return FALSE; 00522 } 00523 00524 int portVector::add(port p) 00525 { 00526 numnodes++; 00527 portVectorNode *node = new portVectorNode; 00528 node->label = p; 00529 node->key = numnodes; 00530 node->next = this->head; 00531 node->prev = 0; 00532 this->head = node; 00533 if(node->next != 0) 00534 node->next->prev = node; 00535 00536 return numnodes; 00537 } 00538 00539 port portVector::pop() 00540 { 00541 if (head == 0) 00542 // an empty vector 00543 error::fatal("Attempt to remove a port from an empty list."); 00544 00545 portVectorNode *node = head; // the node to be deleted 00546 port result = node->label; 00547 head = head->next; 00548 --numnodes; 00549 if (head) head->prev = 0; // only if a node remains 00550 delete node; 00551 return result; 00552 } 00553 00554 port portVector::get(int index) const 00555 { 00556 if((index<1) || (index>numnodes)) 00557 { 00558 error::warning("Index out of bounds for portVector get(int)."); 00559 return port(); 00560 } 00561 00562 portVectorNode *tmp; 00563 00564 tmp = head; 00565 00566 while(tmp != 0) 00567 { 00568 if(tmp->key == index) 00569 return tmp->label; 00570 else 00571 tmp = tmp->next; 00572 } 00573 00574 // Didn't find it. 00575 error::warning("portVector get(int) failed."); 00576 return port(); 00577 } 00578 00579 portVector& portVector::operator=(const portVector & v) 00580 { 00581 // Beware of self assignment v = v 00582 if(this != &v) 00583 { 00584 portVectorNode *tmp; 00585 00586 // Delete the current nportSet 00587 while(head !=0) 00588 { 00589 tmp = head; 00590 head = head->next; 00591 delete tmp; 00592 } 00593 00594 numnodes = 0; 00595 00596 for(int i=1; i<=v.len(); i++) 00597 add(v.get(i)); 00598 } 00599 return *this; 00600 }
Please direct comments and corrections to
supermix@submm.caltech.edu
Go to the supermix home page
Generated by
1.2.7