00001 template <class Element>
00002 Container<Element>::Container() {
00003 nptrs=0;
00004 locked=false;
00005 current=first=last=NULL;
00006 for(int i=0;i<maxcounters;i++){
00007 current1[i]=first1[i]=NULL;
00008 }
00009 }
00010 template <class Element>
00011 Container<Element>::~Container() {
00012 nptrs=0;
00013 current=first=last=NULL;
00014 for(int i=0;i<maxcounters;i++){
00015 current1[i]=first1[i]=NULL;
00016 }
00017 }
00018 template <class Element>
00019 bool Container<Element>::checkPool(char *msg, Pool<Element> *pool) {
00020 if(nptrs>0) {
00021 current=first;
00022 do{
00023 if(pool->hook->next==current){cout<<"checkPool:hook->next=current="<<current<<endl;cout.flush();exit(1);}
00024 current=current->next;
00025 }while(current!=first);
00026 }
00027 return pool->check(msg);
00028 }
00029 template <class Element>
00030 bool Container<Element>::append0()
00031 {
00032 if(last==NULL) {
00033 last=GridContainer::pool->get();
00034 if(last==NULL) {
00035 cout<<"APPEND: CAN'T ALLOCATE FROM POOL\n";
00036 cout.flush();
00037 return false;
00038 }
00039 last->next=last->prev=last;
00040 last->element=NULL;
00041 current=first=last;
00042 #ifdef OMP
00043 for(int i=0;i<maxcounters;i++)current1[i]=first1[i]=last;
00044 #endif
00045 } else {
00046 Ptr<Element> *lastnext=last->next;
00047 last->next=GridContainer::pool->get();
00048 if(last->next==NULL) {
00049 cout<<"APPEND: CAN'T ALLOCATE FROM POOL\n";cout.flush();
00050 return false;
00051 }
00052 lastnext->prev=last->next;
00053 last->next->prev=last;
00054 last=last->next;
00055 last->next=lastnext;
00056 last->element=NULL;
00057 if(first!=lastnext){
00058 cout<<"ERROR IN append0: first="<<first<<" != last->next="<<lastnext<<endl;
00059 first=lastnext;
00060 }
00061 }
00062 nptrs++;
00063 return true;
00064 }
00065 template <class Element>
00066 bool Container<Element>::append()
00067 {
00068 #ifdef OMP
00069 WAIT; locked=true;
00070 #endif
00071 if(!append0())return false;
00072 #ifdef OMP
00073 for(int i=0;i<maxcounters;i++) {
00074 if(first1[i]==NULL)first1[i]=first;
00075 if(current1[i]==NULL)current1[i]=current;
00076 }
00077 locked=false;
00078 #endif
00079 return true;
00080 }
00081 template <class Element>
00082 bool Container<Element>::append(int icounter)
00083 {
00084 bool returned=true;
00085 #ifdef OMP
00086 WAIT; locked=true;
00087 if(first1[icounter]!=NULL)first=first1[icounter];
00088 if(current1[icounter]!=NULL)current=current1[icounter];
00089 #endif
00090 returned=append0();
00091 #ifdef OMP
00092 for(int i=0;i<maxcounters;i++) {
00093 if(first1[i]==NULL)first1[i]=first;
00094 if(current1[i]==NULL)current1[i]=current;
00095 }
00096 locked=false;
00097 #endif
00098 return returned;
00099 }
00100 template <class Element>
00101 bool Container<Element>::append(Element *element)
00102 {
00103 bool returned=true;
00104 #ifdef OMP
00105 WAIT; locked=true;
00106 #endif
00107 returned=append0();
00108 last->element=element;
00109 #ifdef OMP
00110 for(int i=0;i<maxcounters;i++) {
00111 if(first1[i]==NULL)first1[i]=first;
00112 if(current1[i]==NULL)current1[i]=current;
00113 }
00114 locked=false;
00115 #endif
00116 return returned;
00117 }
00118 template <class Element>
00119 bool Container<Element>::append(Element *element,int icounter)
00120 {
00121 bool returned=true;
00122 #ifdef OMP
00123 WAIT; locked=true;
00124 if(first1[icounter]!=NULL)first=first1[icounter];
00125 if(current1[icounter]!=NULL)current=current1[icounter];
00126 #endif
00127 returned=append0();
00128 last->element=element;
00129 #ifdef OMP
00130 first1[icounter]=first;
00131 current1[icounter]=current;
00132 for(int i=0;i<maxcounters;i++) {
00133 if(first1[i]==NULL)first1[i]=first;
00134 if(current1[i]==NULL)current1[i]=current;
00135 }
00136 locked=false;
00137 #endif
00138 return returned;
00139 }
00140 template <class Element>
00141 Ptr<Element>* Container<Element>::append1(Element *element,int icounter)
00142 {
00143 bool returned=true;
00144 #ifdef OMP
00145 WAIT; locked=true;
00146
00147
00148 #endif
00149 returned=append0();
00150 last->element=element;
00151 #ifdef OMP
00152
00153
00154
00155
00156
00157
00158 locked=false;
00159 #endif
00160 return last;
00161 }
00162 template <class Element>
00163 bool Container<Element>::link0(Ptr<Element> *&ptr)
00164 {
00165 if(ptr==NULL) return false;
00166
00167 if(nptrs==0) {
00168 last=ptr;
00169 last->next=last->prev=last;
00170 current=first=last;
00171 #ifdef OMP
00172 for(int i=0;i<maxcounters;i++)current1[i]=first1[i]=last;
00173 #endif
00174
00175 } else {
00176
00177 last->next->prev=ptr;
00178 ptr->next=last->next;
00179 ptr->prev=last;
00180 last->next=ptr;
00181 last=ptr;
00182 }
00183 nptrs++;
00184
00185
00186
00187
00188
00189
00190
00191 return true;
00192 }
00193 template <class Element>
00194 bool Container<Element>::link(Ptr<Element> *&ptr)
00195 {
00196 bool returned=true;
00197 #ifdef OMP
00198 WAIT; locked=true;
00199 #endif
00200 returned=link0(ptr);
00201 #ifdef OMP
00202 for(int i=0;i<maxcounters;i++) {
00203 if(first1[i]==NULL)first1[i]=first;
00204 if(current1[i]==NULL)current1[i]=current;
00205 }
00206 locked=false;
00207 #endif
00208 return returned;
00209 }
00210 template <class Element>
00211 bool Container<Element>::unlink0(Ptr<Element> *&ptr) {
00212 if(last==NULL||ptr==NULL||nptrs<=0) {
00213 cout<<"WARNING: Failed to unlink from the list of "<<nptrs<<": last="<<last<<", ptr="<<ptr<<endl;cout.flush();
00214
00215
00216
00217
00218
00219
00220
00221 return false;
00222
00223 }
00224 if(nptrs==1){
00225 if(ptr==first) {
00226 first=last=current=NULL;
00227 #ifdef OMP
00228 for(int i=0;i<maxcounters;i++) first1[i]=current1[i]=NULL;
00229 #endif
00230 } else {
00231 cerr<<"WARNING: CAN'T UNLINK POINTER: ptr="<<ptr<<", first="<<first<<", first->next="<<first->next<<", last="<<last<<", last->prev="<<last->prev<<", nptrs="<<nptrs<<endl;cerr.flush();
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 return false;
00242
00243 }
00244 } else {
00245 if(ptr==current) current=current->next;
00246 if(ptr==first)first=first->next;
00247 #ifdef OMP
00248 for(int i=0;i<maxcounters;i++){
00249 if(ptr==current1[i])current1[i]=current1[i]->next;
00250 if(ptr==first1[i])first1[i]=first1[i]->next;
00251 }
00252 #endif
00253 if(ptr==last)last=last->prev;
00254 ptr->next->prev=ptr->prev;
00255 ptr->prev->next=ptr->next;
00256 }
00257 nptrs--;
00258 return true;
00259 }
00260 template <class Element>
00261 bool Container<Element>::unlink(Ptr<Element> *&ptr) {
00262 bool returned=true;
00263 #ifdef OMP
00264 WAIT; locked=true;
00265 #endif
00266 returned=unlink0(ptr);
00267 #ifdef OMP
00268 for(int i=0;i<maxcounters;i++){
00269 if(current1[i]==NULL)current1[i]=current;
00270 if(first1[i]==NULL)first1[i]=first;}
00271 locked=false;
00272 #endif
00273 return returned;
00274 }
00275 template <class Element>
00276 bool Container<Element>::insert0()
00277 {
00278 if(first==NULL) {
00279 first=GridContainer::pool->get();
00280 if(first==NULL) {
00281 cout<<"FAILED TO INSERT INTO CONTAINER\n";cout.flush();
00282 return false;
00283 }
00284 first->next=first->prev=first;
00285 first->element=NULL;
00286 current=last=first;
00287 #ifdef OMP
00288 for(int i=0;i<maxcounters;i++)current1[i]=first1[i]=last;
00289 #endif
00290 } else {
00291
00292 Ptr<Element> *newptr=GridContainer::pool->get();
00293 if(newptr==NULL) {
00294 cout<<"FAILED TO INSERT INTO CONTAINER\n";cout.flush();
00295 return false;
00296 }
00297 current->next->prev=newptr;
00298 newptr->next=current->next;
00299 newptr->prev=current;
00300 current->next=newptr;
00301 current=current->next;
00302 current->element=NULL;
00303 last=current;
00304 }
00305 nptrs++;
00306 return true;
00307 }
00308 template <class Element>
00309 bool Container<Element>::insert()
00310 {
00311 bool returned=true;
00312 #ifdef OMP
00313 WAIT; locked=true;
00314 #endif
00315 returned=insert0();
00316 #ifdef OMP
00317 for(int i=0;i<maxcounters;i++) {
00318 if(first1[i]==NULL)first1[i]=first;
00319 if(current1[i]==NULL)current1[i]=current;
00320 }
00321 locked=false;
00322 #endif
00323 return returned;
00324 }
00325 template <class Element>
00326 bool Container<Element>::insert(int icounter)
00327 {
00328 bool returned=true;
00329 #ifdef OMP
00330 WAIT; locked=true;
00331 if(first1[icounter]!=NULL)first=first1[icounter];
00332 if(current1[icounter]!=NULL)current=current1[icounter];
00333 #endif
00334 returned=insert0();
00335 #ifdef OMP
00336 first1[icounter]=first;
00337 current1[icounter]=current;
00338 for(int i=0;i<maxcounters;i++) {
00339 if(first1[i]==NULL)first1[i]=first;
00340 if(current1[i]==NULL)current1[i]=current;
00341 }
00342 locked=false;
00343 #endif
00344 return returned;
00345 }
00346 template <class Element>
00347 bool Container<Element>::insert(Element *element)
00348 {
00349 bool returned=true;
00350 #ifdef OMP
00351 WAIT; locked=true;
00352 #endif
00353 returned=insert0();
00354 current->element=element;
00355 #ifdef OMP
00356 for(int i=0;i<maxcounters;i++) {
00357 if(first1[i]==NULL)first1[i]=first;
00358 if(current1[i]==NULL)current1[i]=current;
00359 }
00360 locked=false;
00361 #endif
00362 return returned;
00363 }
00364 template <class Element>
00365 bool Container<Element>::insert(Element *element,int icounter)
00366 {
00367 bool returned=true;
00368 #ifdef OMP
00369 WAIT; locked=true;
00370 if(first1[icounter]!=NULL)first=first1[icounter];
00371 if(current1[icounter]!=NULL)current=current1[icounter];
00372 #endif
00373 returned=insert0();
00374 current->element=element;
00375 #ifdef OMP
00376 first1[icounter]=first;
00377 current1[icounter]=current;
00378 for(int i=0;i<maxcounters;i++) {
00379 if(first1[i]==NULL)first1[i]=first;
00380 if(current1[i]==NULL)current1[i]=current;
00381 }
00382 locked=false;
00383 #endif
00384 return returned;
00385 }
00386 template <class Element>
00387 Ptr<Element>* Container<Element>::insert1(Element *element,int icounter)
00388 {
00389 bool returned=true;
00390 #ifdef OMP
00391 WAIT; locked=true;
00392 if(first1[icounter]!=NULL)first=first1[icounter];
00393 if(current1[icounter]!=NULL)current=current1[icounter];
00394 #endif
00395 returned=insert0();
00396 current->element=element;
00397 #ifdef OMP
00398 first1[icounter]=first;
00399 current1[icounter]=current;
00400 for(int i=0;i<maxcounters;i++) {
00401 if(first1[i]==NULL)first1[i]=first;
00402 if(current1[i]==NULL)current1[i]=current;
00403 }
00404 locked=false;
00405 #endif
00406 return current;
00407 }
00408 template <class Element>
00409 bool Container<Element>::remove0() {
00410 if(current==NULL||nptrs<=0) {
00411 cout<<"CAN'T REMOVE FROM AN EMPTY CONTAINER OF "<<nptrs<<" ELEMENTS\n";cout.flush();
00412 return false;
00413 }
00414 if(nptrs==1) {
00415 GridContainer::pool->put(current);
00416 current=first=last=NULL;
00417 #ifdef OMP
00418 for(int i=0;i<maxcounters;i++) first1[i]=current1[i]=NULL;
00419 #endif
00420 } else {
00421 if(current==last)last=last->prev;
00422 if(current==first)first=first->next;
00423 Ptr<Element> *old=current;
00424 current=current->prev;
00425 GridContainer::pool->put(old);
00426 }
00427 nptrs--;
00428 return true;
00429 }
00430 template <class Element>
00431 bool Container<Element>::remove() {
00432 bool returned=true;
00433 #ifdef OMP
00434 WAIT; locked=true;
00435 for(int i=0;i<maxcounters;i++){
00436 if(first1[i]==first)first1[i]=NULL;
00437 if(current1[i]==current)current1[i]=NULL;
00438 }
00439 #endif
00440 returned=remove0();
00441 #ifdef OMP
00442 for(int i=0;i<maxcounters;i++) {
00443 if(first1[i]==NULL)first1[i]=first;
00444 if(current1[i]==NULL)current1[i]=current;
00445 }
00446 locked=false;
00447 #endif
00448 return returned;
00449 }
00450 template <class Element>
00451 bool Container<Element>::remove(int icounter) {
00452 bool returned=true;
00453 #ifdef OMP
00454 WAIT; locked=true;
00455 if(first1[icounter]!=NULL)first=first1[icounter];
00456 if(current1[icounter]!=NULL)current=current1[icounter];
00457 for(int i=0;i<maxcounters;i++){
00458 if(first1[i]==first)first1[i]=NULL;
00459 if(current1[i]==current)current1[i]=NULL;
00460 }
00461 #endif
00462 returned=remove0();
00463 #ifdef OMP
00464 for(int i=0;i<maxcounters;i++){
00465 if(first1[i]==NULL)first1[i]=first;
00466 if(current1[i]==NULL)current1[i]=current;
00467 }
00468 locked=false;
00469 #endif
00470 return returned;
00471 }
00472 template <class Element>
00473 bool Container<Element>::remove0(Ptr<Element> *&ptr) {
00474 if(current==NULL||ptr==NULL||nptrs<=0) {
00475 cout<<"Failed to remove from the list: the list is empty\n";cout.flush();
00476 return false;
00477 }
00478
00479 if(nptrs==1){
00480 if(ptr==first) {
00481 GridContainer::pool->put(ptr);
00482 first=last=current=NULL;
00483 #ifdef OMP
00484 for(int i=0;i<maxcounters;i++)current1[i]=first1[i]=NULL;
00485 #endif
00486 } else {
00487 cout<<"CAN'T REMOVE ptr="<<ptr<<" FROM THE CONTAINER\n";cout.flush();
00488 return false;
00489 }
00490 } else {
00491 if(ptr==last)last=last->prev;
00492 if(ptr==current) current=current->next;
00493 if(ptr==first)first=first->next;
00494 #ifdef OMP
00495 for(int i=0;i<maxcounters;i++) {
00496 if(ptr==current1[i])current1[i]=current1[i]->next;
00497 if(ptr==first1[i])first1[i]=first1[i]->next;
00498 }
00499 #endif
00500 GridContainer::pool->put(ptr);
00501 }
00502
00503
00504
00505
00506 nptrs--;
00507
00508
00509 return true;
00510 }
00511 template <class Element>
00512 bool Container<Element>::remove(Ptr<Element> *ptr) {
00513 bool returned=true;
00514 #ifdef OMP
00515 WAIT; locked=true;
00516 #endif
00517 returned=remove0(ptr);
00518 #ifdef OMP
00519 locked=false;
00520 #endif
00521 return returned;
00522 }
00523 template <class Element>
00524 bool Container<Element>::remove(Ptr<Element> *ptr,int icounter) {
00525 bool returned=true;
00526 #ifdef OMP
00527 WAIT; locked=true;
00528 if(icounter>=0&&icounter<maxcounters){
00529 if(current1[icounter]!=NULL)current=current1[icounter];
00530 if(first1[icounter]!=NULL)first=first1[icounter];
00531 }
00532 for(int i=0;i<maxcounters;i++){
00533 if(first1[i]==first)first1[i]=NULL;
00534 if(current1[i]==current)current1[i]=NULL;
00535 }
00536 #endif
00537 returned=remove0(ptr);
00538 #ifdef OMP
00539 for(int i=0;i<maxcounters;i++){
00540 if(first1[i]==NULL)first1[i]=first;
00541 if(current1[i]==NULL)current1[i]=current;
00542 }
00543 locked=false;
00544 #endif
00545 return returned;
00546 }