00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef CH_PARMPARSE_H
00042 #define CH_PARMPARSE_H
00043
00044 #include <iostream>
00045 #include <iomanip>
00046 #include <cstdlib>
00047 #include <string>
00048 #include <vector>
00049
00050 #include <cassert>
00051 #include "MayDay.H"
00052 #include "Misc.H"
00053 #include "Vector.H"
00054 using std::cout;
00055 using std::cerr;
00056 using std::endl;
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 class PP_UseCount
00076 {
00077 public:
00078
00079
00080
00081 PP_UseCount ();
00082
00083
00084
00085 PP_UseCount (const PP_UseCount& rhs);
00086
00087
00088
00089
00090
00091 PP_UseCount& operator= (const PP_UseCount& rhs);
00092
00093
00094
00095 ~PP_UseCount ();
00096
00097
00098
00099 bool unique () const;
00100
00101
00102
00103 int linkCount () const;
00104
00105 private:
00106
00107
00108
00109 unsigned int* cnt;
00110
00111
00112
00113
00114 void decrement ();
00115 };
00116
00117
00118
00119
00120
00121 inline
00122 PP_UseCount::PP_UseCount ()
00123 :
00124 cnt(new unsigned int(1))
00125 {}
00126
00127 inline
00128 PP_UseCount::PP_UseCount (const PP_UseCount& rhs)
00129 :
00130 cnt(rhs.cnt)
00131 {
00132 ++*cnt;
00133 }
00134
00135 inline
00136 bool
00137 PP_UseCount::unique () const
00138 {
00139 return *cnt == 1;
00140 }
00141
00142 inline
00143 void
00144 PP_UseCount::decrement ()
00145 {
00146 if (unique())
00147 {
00148 delete cnt;
00149 cnt = 0;
00150 }
00151 else
00152 --*cnt;
00153 }
00154
00155 inline
00156 PP_UseCount&
00157 PP_UseCount::operator= (const PP_UseCount& rhs)
00158 {
00159 ++*rhs.cnt;
00160 decrement();
00161 cnt = rhs.cnt;
00162 return *this;
00163 }
00164
00165 inline
00166 PP_UseCount::~PP_UseCount ()
00167 {
00168 decrement();
00169 }
00170
00171 inline
00172 int
00173 PP_UseCount::linkCount () const
00174 {
00175 return *cnt;
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 template <class T>
00201 class PP_CpPtr
00202 {
00203 public:
00204
00205
00206
00207 PP_CpPtr ();
00208
00209
00210
00211 PP_CpPtr (T* rhs);
00212
00213
00214
00215 ~PP_CpPtr ();
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 PP_CpPtr (const PP_CpPtr<T>& rhs);
00226
00227
00228
00229
00230 PP_CpPtr<T>& operator= (T* rhs);
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 PP_CpPtr<T>& operator= (const PP_CpPtr<T>& rhs);
00241
00242
00243
00244
00245
00246
00247 T& operator* () const;
00248
00249
00250
00251 bool isNull () const;
00252
00253
00254
00255 T* release ();
00256
00257
00258
00259 bool operator== (const PP_CpPtr<T>& rhs) const;
00260
00261
00262
00263 bool operator!= (const PP_CpPtr<T>& rhs) const;
00264
00265 protected:
00266 T* ptr;
00267 };
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 template<class T>
00286 class PP_CpClassPtr :
00287 public PP_CpPtr<T>
00288 {
00289 public:
00290
00291
00292
00293 PP_CpClassPtr ();
00294
00295
00296
00297 PP_CpClassPtr (T* rhs);
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 PP_CpClassPtr (const PP_CpClassPtr<T>& rhs);
00308
00309
00310
00311
00312 PP_CpClassPtr<T>& operator= (T* rhs);
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 PP_CpClassPtr<T>& operator= (const PP_CpClassPtr<T>& rhs);
00323
00324
00325
00326 T* operator-> () const;
00327 };
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 template<class T>
00347 class PP_LnPtr
00348 {
00349 public:
00350
00351
00352
00353 PP_LnPtr ();
00354
00355
00356
00357 PP_LnPtr (T* rhs);
00358
00359
00360
00361
00362
00363
00364 PP_LnPtr<T>& operator= (const PP_LnPtr<T>& rhs);
00365
00366
00367
00368
00369
00370 PP_LnPtr<T>& operator= (T* rhs)
00371 {
00372
00373
00374
00375 if (unique())
00376 delete ptr;
00377 ptr = rhs;
00378 ucnt = PP_UseCount();
00379 return *this;
00380 }
00381
00382
00383
00384
00385 ~PP_LnPtr ();
00386
00387
00388
00389 bool unique () const;
00390
00391
00392
00393 int linkCount () const;
00394
00395
00396
00397
00398
00399
00400 T& operator* () const;
00401
00402
00403
00404 bool isNull () const;
00405
00406
00407
00408 bool operator== (const PP_LnPtr<T>& rhs) const;
00409
00410
00411
00412 bool operator!= (const PP_LnPtr<T>& rhs) const;
00413
00414 protected:
00415 T* ptr;
00416
00417 private:
00418 PP_UseCount ucnt;
00419 };
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436 template<class T>
00437 class PP_LnClassPtr
00438 : public PP_LnPtr<T>
00439 {
00440 public:
00441
00442
00443
00444 PP_LnClassPtr ();
00445
00446
00447
00448 PP_LnClassPtr (T* rhs);
00449
00450
00451
00452
00453
00454
00455 PP_LnClassPtr<T>& operator= (const PP_LnClassPtr<T>& rhs);
00456
00457
00458
00459
00460
00461 PP_LnClassPtr<T>& operator= (T* rhs);
00462
00463
00464
00465 T* operator->() const;
00466 };
00467
00468
00469
00470
00471
00472 template <class T>
00473 inline
00474 PP_CpPtr<T>::PP_CpPtr ()
00475 : ptr(0)
00476 {}
00477
00478 template <class T>
00479 inline
00480 PP_CpPtr<T>::PP_CpPtr(T* rhs)
00481 : ptr(rhs)
00482 {}
00483
00484 template <class T>
00485 inline
00486 PP_CpPtr<T>::~PP_CpPtr()
00487 {
00488 delete ptr;
00489 }
00490
00491 template <class T>
00492 inline
00493 bool
00494 PP_CpPtr<T>::isNull () const
00495 {
00496 return ptr == 0;
00497 }
00498
00499 template <class T>
00500 inline
00501 PP_CpPtr<T>::PP_CpPtr (const PP_CpPtr<T>& rhs)
00502 {
00503 ptr = rhs.isNull() ? 0 : new T(*rhs.ptr);
00504 }
00505
00506 template <class T>
00507 inline
00508 PP_CpPtr<T>&
00509 PP_CpPtr<T>::operator= (const PP_CpPtr<T>& rhs)
00510 {
00511 if (!(ptr == rhs.ptr))
00512 {
00513 delete ptr;
00514 ptr = rhs.isNull() ? 0 : new T(*rhs.ptr);
00515 }
00516 return *this;
00517 }
00518
00519 template <class T>
00520 inline
00521 PP_CpPtr<T>&
00522 PP_CpPtr<T>::operator= (T* rhs)
00523 {
00524 delete ptr;
00525 ptr = rhs;
00526 return *this;
00527 }
00528
00529 template <class T>
00530 inline
00531 T&
00532 PP_CpPtr<T>::operator* () const
00533 {
00534 assert(ptr != 0);
00535 return *ptr;
00536 }
00537
00538 template <class T>
00539 inline
00540 T*
00541 PP_CpPtr<T>::release ()
00542 {
00543 T* old = ptr;
00544 ptr = 0;
00545 return old;
00546 }
00547
00548 template <class T>
00549 inline
00550 bool
00551 PP_CpPtr<T>::operator== (const PP_CpPtr<T>& rhs) const
00552 {
00553 return ptr == rhs.ptr;
00554 }
00555
00556 template <class T>
00557 inline
00558 bool
00559 PP_CpPtr<T>::operator!= (const PP_CpPtr<T>& rhs) const
00560 {
00561 return ptr != rhs.ptr;
00562 }
00563
00564 template <class T>
00565 inline
00566 PP_CpClassPtr<T>::PP_CpClassPtr ()
00567 : PP_CpPtr<T>()
00568 {}
00569
00570 template <class T>
00571 inline
00572 PP_CpClassPtr<T>::PP_CpClassPtr (T* rhs)
00573 : PP_CpPtr<T>(rhs)
00574 {}
00575
00576 template <class T>
00577 inline
00578 PP_CpClassPtr<T>::PP_CpClassPtr (const PP_CpClassPtr<T>& rhs)
00579 : PP_CpPtr<T>(rhs) {}
00580
00581 template <class T>
00582 inline
00583 PP_CpClassPtr<T>&
00584 PP_CpClassPtr<T>::operator= (T* rhs)
00585 {
00586 PP_CpPtr<T>::operator= (rhs);
00587 return *this;
00588 }
00589
00590 template <class T>
00591 inline
00592 PP_CpClassPtr<T>&
00593 PP_CpClassPtr<T>::operator= (const PP_CpClassPtr<T>& rhs)
00594 {
00595 PP_CpPtr<T>::operator= (rhs);
00596 return *this;
00597 }
00598
00599 template <class T>
00600 inline
00601 T*
00602 PP_CpClassPtr<T>::operator-> () const
00603 {
00604 return ptr;
00605 }
00606
00607 template <class T>
00608 inline
00609 PP_LnPtr<T>::PP_LnPtr ()
00610 : ptr(0)
00611 {}
00612
00613 template <class T>
00614 inline
00615 PP_LnPtr<T>::PP_LnPtr(T* rhs)
00616 : ptr(rhs)
00617 {}
00618
00619 template <class T>
00620 inline
00621 bool
00622 PP_LnPtr<T>::unique () const
00623 {
00624 return ucnt.unique();
00625 }
00626
00627 template <class T>
00628 inline
00629 PP_LnPtr<T>::~PP_LnPtr ()
00630 {
00631 if (ucnt.unique())
00632 delete ptr;
00633 }
00634
00635 template <class T>
00636 inline
00637 PP_LnPtr<T>&
00638 PP_LnPtr<T>::operator= (const PP_LnPtr<T>& rhs)
00639 {
00640 if (ptr != rhs.ptr)
00641 {
00642 if (unique())
00643 delete ptr;
00644 ptr = rhs.ptr;
00645 ucnt = rhs.ucnt;
00646 }
00647 return *this;
00648 }
00649
00650 template <class T>
00651 inline
00652 int
00653 PP_LnPtr<T>::linkCount () const
00654 {
00655 return ucnt.linkCount();
00656 }
00657
00658 template <class T>
00659 inline
00660 T&
00661 PP_LnPtr<T>::operator* () const
00662 {
00663 assert(ptr != 0);
00664 return *ptr;
00665 }
00666
00667 template <class T>
00668 inline
00669 bool
00670 PP_LnPtr<T>::isNull () const
00671 {
00672 return ptr == 0;
00673 }
00674
00675 template <class T>
00676 inline
00677 bool
00678 PP_LnPtr<T>::operator== (const PP_LnPtr<T>& rhs) const
00679 {
00680 return ptr == rhs.ptr;
00681 }
00682
00683 template <class T>
00684 inline
00685 bool
00686 PP_LnPtr<T>::operator!= (const PP_LnPtr<T>& rhs) const
00687 {
00688 return ptr != rhs.ptr;
00689 }
00690
00691 template <class T>
00692 inline
00693 PP_LnClassPtr<T>::PP_LnClassPtr ()
00694 {}
00695
00696 template <class T>
00697 inline
00698 PP_LnClassPtr<T>::PP_LnClassPtr (T* rhs)
00699 : PP_LnPtr<T>(rhs)
00700 {}
00701
00702 template <class T>
00703 inline
00704 PP_LnClassPtr<T>&
00705 PP_LnClassPtr<T>::operator= (const PP_LnClassPtr<T>& rhs)
00706 {
00707 PP_LnPtr<T>::operator=(rhs);
00708 return *this;
00709 }
00710
00711 template <class T>
00712 inline
00713 PP_LnClassPtr<T>&
00714 PP_LnClassPtr<T>::operator= (T* rhs)
00715 {
00716 PP_LnPtr<T>::operator=(rhs);
00717 return *this;
00718 }
00719
00720 template <class T>
00721 inline
00722 T*
00723 PP_LnClassPtr<T>::operator->() const
00724 {
00725 return ptr;
00726 }
00727
00728
00729
00730
00731
00732
00733
00734 class PP_StringRep
00735 {
00736 friend class PP_String;
00737 char* s;
00738 int bufferlength;
00739 public:
00740 PP_StringRep (int _len = 0);
00741 ~PP_StringRep ();
00742
00743
00744
00745 void resize (int n);
00746 };
00747
00748
00749
00750
00751
00752 inline
00753 PP_StringRep::PP_StringRep (int _len)
00754 {
00755 bufferlength = _len;
00756 s = new char [bufferlength];
00757 }
00758
00759 inline
00760 PP_StringRep::~PP_StringRep ()
00761 {
00762 delete [] s;
00763 s = 0;
00764 }
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793 class PP_String
00794 {
00795 public:
00796
00797
00798
00799 PP_String ();
00800
00801
00802
00803
00804 PP_String (char c);
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814 PP_String (int len);
00815
00816
00817
00818
00819 PP_String (const char* s);
00820
00821
00822
00823 PP_String (const PP_String& rhs);
00824
00825
00826
00827 PP_String& operator= (const PP_String& rhs);
00828
00829
00830
00831
00832 PP_String& operator+= (const PP_String& right);
00833
00834
00835
00836
00837
00838 PP_String& operator+= (const char* right);
00839
00840
00841
00842
00843
00844 PP_String& operator+= (char c);
00845
00846
00847
00848
00849 PP_String& toUpper ();
00850
00851
00852
00853
00854 PP_String& toLower ();
00855
00856
00857
00858
00859
00860
00861
00862 std::istream& getline (std::istream& strm);
00863
00864
00865
00866
00867 int length () const;
00868
00869
00870
00871
00872 bool isNull () const;
00873
00874
00875
00876
00877 char& operator [] (int k);
00878
00879
00880
00881
00882 char operator[] (int k) const;
00883
00884
00885
00886
00887 const char* c_str () const;
00888
00889
00890
00891
00892
00893
00894 double toDouble () const;
00895
00896
00897
00898
00899
00900
00901 int toInteger () const;
00902
00903
00904
00905
00906
00907
00908 long toLong () const;
00909
00910
00911
00912 friend std::ostream& operator<< (std::ostream& os,
00913 const PP_String& str);
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927 friend std::istream& operator>> (std::istream& is,
00928 PP_String& str);
00929
00930 protected:
00931 void copyModify ();
00932
00933 private:
00934 PP_LnClassPtr<PP_StringRep> p;
00935 int len;
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946 friend inline bool operator< (const PP_String& left,
00947 const PP_String& right);
00948
00949
00950
00951 friend inline bool operator<= (const PP_String& left,
00952 const PP_String& right);
00953
00954
00955
00956 friend inline bool operator!= (const PP_String& left,
00957 const PP_String& right);
00958
00959
00960
00961 friend inline bool operator== (const PP_String& left,
00962 const PP_String& right);
00963
00964
00965
00966 friend inline bool operator>= (const PP_String& left,
00967 const PP_String& right);
00968
00969
00970
00971 friend inline bool operator> (const PP_String& left,
00972 const PP_String& right);
00973
00974
00975
00976 friend inline bool operator< (const PP_String& left,
00977 const char* right);
00978
00979
00980
00981 friend inline bool operator<= (const PP_String& left,
00982 const char* right);
00983
00984
00985
00986 friend inline bool operator!= (const PP_String& left,
00987 const char* right);
00988
00989
00990
00991 friend inline bool operator== (const PP_String& left,
00992 const char* right);
00993
00994
00995
00996 friend inline bool operator>= (const PP_String& left,
00997 const char* right);
00998
00999
01000
01001 friend inline bool operator> (const PP_String& left,
01002 const char* right);
01003
01004
01005
01006 friend inline bool operator< (const char* left,
01007 const PP_String& right);
01008
01009
01010
01011 friend inline bool operator<= (const char* left,
01012 const PP_String& right);
01013
01014
01015
01016 friend inline bool operator!= (const char* left,
01017 const PP_String& right);
01018
01019
01020
01021 friend inline bool operator== (const char* left,
01022 const PP_String& right);
01023
01024
01025
01026 friend inline bool operator>= (const char* left,
01027 const PP_String& right);
01028
01029
01030
01031 friend inline bool operator> (const char* left,
01032 const PP_String& right);
01033 };
01034
01035
01036
01037
01038
01039 inline
01040 bool
01041 PP_String::isNull () const
01042 {
01043 return len == 0;
01044 }
01045
01046 inline
01047 int
01048 PP_String::length () const
01049 {
01050 return len;
01051 }
01052
01053 inline
01054 double
01055 PP_String::toDouble () const
01056 {
01057 return len == 0 ? 0 : std::atof(p->s);
01058 }
01059
01060 inline
01061 int
01062 PP_String::toInteger () const
01063 {
01064 return len == 0 ? 0 : atoi(p->s);
01065 }
01066
01067 inline
01068 long
01069 PP_String::toLong () const
01070 {
01071 return len == 0 ? 0 : atol(p->s);
01072 }
01073
01074 inline
01075 const char*
01076 PP_String::c_str () const
01077 {
01078 return p->s;
01079 }
01080
01081 inline
01082 char
01083 PP_String::operator[] (int index) const
01084 {
01085 assert(index >=0 && index < len);
01086 return p->s[index];
01087 }
01088
01089 inline
01090 PP_String
01091 operator+ (const PP_String& left,
01092 const PP_String& right)
01093 {
01094 PP_String result(left);
01095 return result += right;
01096 }
01097
01098 inline
01099 bool
01100 operator< (const PP_String& left,
01101 const PP_String& right)
01102 {
01103 return ::strcmp(left.c_str(), right.c_str()) < 0;
01104 }
01105
01106 inline
01107 bool
01108 operator<= (const PP_String& left,
01109 const PP_String& right)
01110 {
01111 return ::strcmp(left.c_str(), right.c_str()) <= 0;
01112 }
01113
01114 inline
01115 bool
01116 operator!= (const PP_String& left,
01117 const PP_String& right)
01118 {
01119 return ::strcmp(left.c_str(), right.c_str()) != 0;
01120 }
01121
01122 inline
01123 bool
01124 operator== (const PP_String& left,
01125 const PP_String& right)
01126 {
01127 return ::strcmp(left.c_str(), right.c_str()) == 0;
01128 }
01129
01130 inline
01131 bool
01132 operator>= (const PP_String& left,
01133 const PP_String& right)
01134 {
01135 return ::strcmp(left.c_str(), right.c_str()) >= 0;
01136 }
01137
01138 inline
01139 bool
01140 operator> (const PP_String& left,
01141 const PP_String& right)
01142 {
01143 return ::strcmp(left.c_str(), right.c_str()) > 0;
01144 }
01145
01146 inline
01147 bool
01148 operator< (const PP_String& left,
01149 const char* right)
01150 {
01151 return ::strcmp(left.c_str(), right) < 0;
01152 }
01153
01154 inline
01155 bool
01156 operator<= (const PP_String& left,
01157 const char* right)
01158 {
01159 return ::strcmp(left.c_str(), right) <= 0;
01160 }
01161
01162 inline
01163 bool
01164 operator!= (const PP_String& left,
01165 const char* right)
01166 {
01167 return ::strcmp(left.c_str(), right) != 0;
01168 }
01169
01170 inline
01171 bool
01172 operator== (const PP_String& left,
01173 const char* right)
01174 {
01175 return ::strcmp(left.c_str(), right) == 0;
01176 }
01177
01178 inline
01179 bool
01180 operator>= (const PP_String& left,
01181 const char* right)
01182 {
01183 return ::strcmp(left.c_str(), right) >= 0;
01184 }
01185
01186 inline
01187 bool
01188 operator> (const PP_String& left,
01189 const char* right)
01190 {
01191 return ::strcmp(left.c_str(), right) > 0;
01192 }
01193
01194 inline
01195 bool
01196 operator< (const char* left,
01197 const PP_String& right)
01198 {
01199 return ::strcmp(left, right.c_str()) < 0;
01200 }
01201
01202 inline
01203 bool
01204 operator<= (const char* left,
01205 const PP_String& right)
01206 {
01207 return ::strcmp(left, right.c_str()) <= 0;
01208 }
01209
01210 inline
01211 bool
01212 operator!= (const char* left,
01213 const PP_String& right)
01214 {
01215 return ::strcmp(left, right.c_str()) != 0;
01216 }
01217
01218 inline
01219 bool
01220 operator== (const char* left,
01221 const PP_String& right)
01222 {
01223 return ::strcmp(left, right.c_str()) == 0;
01224 }
01225
01226 inline
01227 bool
01228 operator>= (const char* left,
01229 const PP_String& right)
01230 {
01231 return ::strcmp(left, right.c_str()) >= 0;
01232 }
01233
01234 inline
01235 bool
01236 operator> (const char* left,
01237 const PP_String& right)
01238 {
01239 return ::strcmp(left, right.c_str()) > 0;
01240 }
01241
01242
01243
01244
01245 template <class T> class PP_ListLink;
01246 template <class T> class PP_ListIterator;
01247 template <class T> class PP_List;
01248
01249 template <class T>
01250 class PP_ListLink
01251 {
01252 private:
01253 friend class PP_List<T>;
01254 friend class PP_ListIterator<T>;
01255
01256 PP_ListLink (const T& _val,
01257 PP_ListLink<T>* _pre,
01258 PP_ListLink<T>* _suc)
01259 :
01260 val(_val),
01261 pre(_pre),
01262 suc(_suc) {}
01263
01264 private:
01265 T val;
01266 PP_ListLink<T>* pre;
01267 PP_ListLink<T>* suc;
01268 };
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285 template <class T>
01286 class PP_ListIterator
01287 {
01288 public:
01289
01290
01291
01292 PP_ListIterator (const PP_List<T>& aList);
01293
01294
01295
01296 PP_ListIterator (const PP_ListIterator<T>& rhs);
01297
01298
01299
01300
01301 void rewind ();
01302
01303
01304
01305
01306 const T& operator() () const;
01307
01308
01309
01310
01311 const T& operator* () const;
01312
01313
01314
01315
01316
01317
01318
01319 operator void* ();
01320
01321
01322
01323
01324 bool operator! () const;
01325
01326
01327
01328
01329 const T& value () const;
01330
01331
01332
01333
01334
01335
01336 PP_ListIterator<T>& operator++ ();
01337
01338
01339
01340
01341
01342
01343 PP_ListIterator<T>& operator-- ();
01344
01345
01346
01347
01348
01349
01350
01351 PP_ListIterator<T> operator-- (int);
01352
01353
01354
01355
01356
01357
01358 PP_ListIterator<T> operator++ (int);
01359
01360
01361
01362
01363 bool operator== (const PP_ListIterator<T>&) const;
01364
01365
01366
01367 bool operator!= (const PP_ListIterator<T>&) const;
01368
01369 protected:
01370
01371
01372
01373 PP_ListIterator (const PP_List<T>& _list,
01374 PP_ListLink<T>* _p);
01375
01376
01377
01378 const PP_List<T>& list;
01379
01380
01381
01382 PP_ListLink<T>* p;
01383
01384 private:
01385 friend class PP_List<T>;
01386
01387
01388
01389 PP_ListIterator ();
01390 PP_ListIterator<T>& operator= (const PP_ListIterator<T>&);
01391 };
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416
01417
01418
01419
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429 template <class T>
01430 class PP_List
01431 {
01432 public:
01433
01434
01435
01436 PP_List ();
01437
01438
01439
01440 PP_List (const PP_List<T>& rhs);
01441
01442
01443
01444 PP_List<T>& operator= (const PP_List<T>& rhs);
01445
01446
01447
01448 ~PP_List();
01449
01450
01451
01452 void prepend (const T& value);
01453
01454
01455
01456 void append (const T& value);
01457
01458
01459
01460 void add (const T& value);
01461
01462
01463
01464 void join (const PP_List<T>& src);
01465
01466
01467
01468
01469
01470
01471
01472 void catenate (PP_List<T>& src);
01473
01474
01475
01476 void clear ();
01477
01478
01479
01480 T& firstElement () const;
01481
01482
01483
01484 T& lastElement () const;
01485
01486
01487
01488
01489
01490 bool includes (const T& value) const;
01491
01492
01493
01494
01495
01496
01497 bool operator== (const PP_List<T>& rhs) const;
01498
01499
01500
01501 bool operator!= (const PP_List<T>& rhs) const;
01502
01503
01504
01505 bool isEmpty () const;
01506
01507
01508
01509 bool isNotEmpty () const;
01510
01511
01512
01513 int length () const;
01514
01515
01516
01517 void removeFirst ();
01518
01519
01520
01521 void removeLast ();
01522
01523
01524
01525 const T& operator[] (const PP_ListIterator<T>& li) const;
01526
01527
01528
01529 T& operator[] (const PP_ListIterator<T>& li);
01530
01531
01532
01533 void remove (const T& value);
01534
01535
01536
01537
01538 void remove (const PP_List<T>& lst);
01539
01540
01541
01542 void remove (PP_ListIterator<T>& lit);
01543
01544
01545
01546 void replace (PP_ListIterator<T>& li,
01547 const T& val);
01548
01549
01550
01551
01552 void addAfter (PP_ListIterator<T>& lit,
01553 const T& val);
01554
01555
01556
01557
01558 void addBefore (PP_ListIterator<T>& lit,
01559 const T& val);
01560
01561
01562
01563 PP_ListIterator<T> first () const;
01564
01565
01566
01567 PP_ListIterator<T> last () const;
01568
01569 protected:
01570
01571
01572
01573 void remove (PP_ListLink<T> *ln);
01574
01575
01576
01577 PP_ListLink<T>* addBefore (PP_ListLink<T>* ln,
01578 const T& val);
01579
01580
01581
01582 PP_ListLink<T>* addAfter (PP_ListLink<T>* ln,
01583 const T& val);
01584
01585
01586
01587 PP_ListLink<T>* head;
01588
01589
01590
01591 PP_ListLink<T>* tail;
01592
01593
01594
01595 friend class PP_ListIterator<T>;
01596 };
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606 template <class T>
01607 inline
01608 PP_ListIterator<T>::PP_ListIterator (const PP_List<T>& _list,
01609 PP_ListLink<T>* _p)
01610 :
01611 list(_list),
01612 p(_p)
01613 {}
01614
01615 template <class T>
01616 inline
01617 PP_ListIterator<T>::PP_ListIterator (const PP_List<T>& aList)
01618 :
01619 list(aList)
01620 {
01621 p = list.head;
01622 }
01623
01624 template <class T>
01625 inline
01626 PP_ListIterator<T>::PP_ListIterator (const PP_ListIterator<T>& li)
01627 :
01628 list(li.list),
01629 p(li.p)
01630 {}
01631
01632 template <class T>
01633 inline
01634 void
01635 PP_ListIterator<T>::rewind ()
01636 {
01637 p = list.head;
01638 }
01639
01640 template <class T>
01641 inline
01642 const T&
01643 PP_ListIterator<T>::operator() () const
01644 {
01645 assert(p != 0);
01646 return p->val;
01647 }
01648
01649 template <class T>
01650 inline
01651 const T&
01652 PP_ListIterator<T>::operator* () const
01653 {
01654 assert(p != 0);
01655 return p->val;
01656 }
01657
01658 template <class T>
01659 inline
01660 PP_ListIterator<T>::operator void* ()
01661 {
01662 return p != 0 ? this : 0;
01663 }
01664
01665 template <class T>
01666 inline
01667 bool
01668 PP_ListIterator<T>::operator! () const
01669 {
01670 return p == 0 ? true : false;
01671 }
01672
01673 template <class T>
01674 inline
01675 const T&
01676 PP_ListIterator<T>::value () const
01677 {
01678 assert(p != 0);
01679 return p->val;
01680 }
01681
01682 template <class T>
01683 inline
01684 PP_ListIterator<T>&
01685 PP_ListIterator<T>::operator++ ()
01686 {
01687 if (p)
01688 p = p->suc;
01689 return *this;
01690 }
01691
01692 template <class T>
01693 inline
01694 PP_ListIterator<T>&
01695 PP_ListIterator<T>::operator-- ()
01696 {
01697 if (p)
01698 p = p->pre;
01699 return *this;
01700 }
01701
01702 template <class T>
01703 inline
01704 PP_ListIterator<T>
01705 PP_ListIterator<T>::operator++ (int)
01706 {
01707 const PP_ListIterator<T> li = *this;
01708 ++(*this);
01709 return li;
01710 }
01711
01712 template <class T>
01713 inline
01714 PP_ListIterator<T>
01715 PP_ListIterator<T>::operator-- (int)
01716 {
01717 const PP_ListIterator<T> li = *this;
01718 --(*this);
01719 return li;
01720 }
01721
01722 template <class T>
01723 inline
01724 bool
01725 PP_ListIterator<T>::operator== (const PP_ListIterator<T>& _li) const
01726 {
01727 return (&list == &_li.list && p == _li.p) ? true : false;
01728 }
01729
01730 template <class T>
01731 inline
01732 bool
01733 PP_ListIterator<T>::operator!= (const PP_ListIterator<T>& _li) const
01734 {
01735 return ! PP_ListIterator<T>::operator==(_li);
01736 }
01737
01738
01739
01740
01741
01742 template <class T>
01743 inline
01744 PP_List<T>::PP_List ()
01745 :
01746 head(0),
01747 tail(0)
01748 {}
01749
01750 template <class T>
01751 inline
01752 PP_List<T>::~PP_List ()
01753 {
01754 clear();
01755 }
01756
01757 template <class T>
01758 inline
01759 void
01760 PP_List<T>::prepend (const T& value)
01761 {
01762 addBefore(head, value);
01763 }
01764
01765 template <class T>
01766 inline
01767 void
01768 PP_List<T>::append (const T& value)
01769 {
01770 addAfter(tail, value);
01771 }
01772
01773 template <class T>
01774 inline
01775 T&
01776 PP_List<T>::firstElement () const
01777 {
01778 assert(head != 0);
01779 return head->val;
01780 }
01781
01782 template <class T>
01783 inline
01784 T&
01785 PP_List<T>::lastElement () const
01786 {
01787 assert(tail != 0);
01788 return tail->val;
01789 }
01790
01791 template <class T>
01792 inline
01793 bool
01794 PP_List<T>::isEmpty () const
01795 {
01796 return head == 0 && tail == 0;
01797 }
01798
01799 template <class T>
01800 inline
01801 bool
01802 PP_List<T>::isNotEmpty () const
01803 {
01804 return !isEmpty();
01805 }
01806
01807 template <class T>
01808 inline
01809 void
01810 PP_List<T>::removeFirst ()
01811 {
01812 remove(head);
01813 }
01814
01815 template <class T>
01816 inline
01817 void
01818 PP_List<T>::removeLast ()
01819 {
01820 remove(tail);
01821 }
01822
01823 template <class T>
01824 inline
01825 const T&
01826 PP_List<T>::operator[] (const PP_ListIterator<T>& li) const
01827 {
01828 assert(li.p != 0);
01829 return li.p->val;
01830 }
01831
01832 template <class T>
01833 inline
01834 T&
01835 PP_List<T>::operator[] (const PP_ListIterator<T>& li)
01836 {
01837 assert(li.p != 0);
01838 return li.p->val;
01839 }
01840
01841 template <class T>
01842 inline
01843 void
01844 PP_List<T>::replace (PP_ListIterator<T>& li,
01845 const T& _val)
01846 {
01847 assert(li.p != 0);
01848 li.p->val = _val;
01849 }
01850
01851 template <class T>
01852 inline
01853 void
01854 PP_List<T>::addAfter (PP_ListIterator<T>& lit,
01855 const T& val)
01856 {
01857 addAfter(lit.p, val);
01858 }
01859
01860 template <class T>
01861 inline
01862 void
01863 PP_List<T>::addBefore (PP_ListIterator<T>& lit,
01864 const T& val)
01865 {
01866 addBefore(lit.p, val);
01867 }
01868
01869 template <class T>
01870 inline
01871 PP_ListIterator<T>
01872 PP_List<T>::first () const
01873 {
01874 return PP_ListIterator<T>(*this,head);
01875 }
01876
01877 template <class T>
01878 inline
01879 PP_ListIterator<T>
01880 PP_List<T>::last () const
01881 {
01882 return PP_ListIterator<T>(*this,tail);
01883 }
01884
01885
01886
01887
01888
01889 template <class T>
01890 inline
01891 PP_List<T>::PP_List (const PP_List<T>& source)
01892 :
01893 head(0),
01894 tail(0)
01895 {
01896 if (source.isEmpty())
01897 tail = head = 0;
01898 else
01899 for (PP_ListIterator<T> li(source); li; ++li)
01900 append(li());
01901 }
01902
01903
01904
01905
01906
01907 template <class T>
01908 inline void
01909 PP_List<T>::add (const T& value)
01910 {
01911 append(value);
01912 }
01913
01914 template <class T>
01915 inline
01916 int
01917 PP_List<T>::length () const
01918 {
01919 int len = 0;
01920 for (PP_ListIterator<T> li(*this); li; ++li)
01921 len++;
01922 return len;
01923 }
01924
01925 template <class T>
01926 inline
01927 PP_List<T>&
01928 PP_List<T>::operator= (const PP_List<T>& source)
01929 {
01930 if (!(this == &source))
01931 {
01932 clear();
01933 for (PP_ListIterator<T> li(source); li; ++li)
01934 append(li());
01935 }
01936 return *this;
01937 }
01938
01939 template <class T>
01940 inline PP_ListLink<T> *
01941 PP_List<T>::addBefore (PP_ListLink<T>* ln,
01942 const T& val)
01943 {
01944 assert(ln != 0 || head == 0);
01945
01946 PP_ListLink<T>* newlink;
01947
01948 if (ln == head)
01949 {
01950 head = newlink = new PP_ListLink<T>(val, 0, head);
01951
01952 if (tail == 0)
01953 tail = head;
01954 else
01955 head->suc->pre = newlink;
01956 }
01957 else
01958 {
01959 newlink = new PP_ListLink<T>(val, ln->pre, ln);
01960
01961 ln->pre->suc = newlink;
01962 ln->pre = newlink;
01963 }
01964
01965 return newlink;
01966 }
01967
01968 template <class T>
01969 inline
01970 PP_ListLink<T>*
01971 PP_List<T>::addAfter (PP_ListLink<T>* ln,
01972 const T& val)
01973 {
01974 assert(ln != 0 || tail == 0);
01975
01976 PP_ListLink<T>* newlink;
01977
01978
01979
01980
01981
01982
01983
01984 if (ln == tail)
01985 {
01986 tail = newlink = new PP_ListLink<T>(val,tail,0);
01987
01988 if (head == 0)
01989 head = tail;
01990 else
01991 tail->pre->suc = newlink;
01992 }
01993 else
01994 {
01995 newlink = new PP_ListLink<T>(val, ln, ln->suc);
01996
01997 ln->suc->pre = newlink;
01998 ln->suc = newlink;
01999 }
02000
02001 return newlink;
02002 }
02003
02004 template <class T>
02005 inline void
02006 PP_List<T>::join (const PP_List<T>& list2)
02007 {
02008 for (PP_ListIterator<T> li2(list2); li2; ++li2)
02009 append(li2());
02010 }
02011
02012 template <class T>
02013 inline void
02014 PP_List<T>::catenate (PP_List<T>& list2)
02015 {
02016 if (list2.isEmpty())
02017
02018
02019
02020 ;
02021 else if (isEmpty())
02022 {
02023 head = list2.head;
02024 tail = list2.tail;
02025 list2.head = 0;
02026 list2.tail = 0;
02027 }
02028 else
02029 {
02030 tail->suc = list2.head;
02031 list2.head->pre = tail;
02032 tail = list2.tail;
02033 list2.head = 0;
02034 list2.tail = 0;
02035 }
02036 }
02037
02038 template <class T>
02039 inline void
02040 PP_List<T>::clear ()
02041 {
02042 PP_ListLink<T>* next = 0;
02043
02044 for (PP_ListLink<T>* p = head; p != 0; p = next)
02045 {
02046 next = p->suc;
02047 p->suc = 0;
02048 delete p;
02049 }
02050 tail = head = 0;
02051 }
02052
02053 template <class T>
02054 inline bool
02055 PP_List<T>::includes (const T& v) const
02056 {
02057 bool rc = false;
02058 for (PP_ListIterator<T> li(*this); li && !rc; ++li)
02059 if (v == li())
02060 rc = true;
02061 return rc;
02062 }
02063
02064 template<class T>
02065 inline bool
02066 PP_List<T>::operator== (const PP_List<T>& rhs) const
02067 {
02068 if (length() == rhs.length())
02069 {
02070 for (PP_ListIterator<T> li(*this), ri(rhs); li; ++li, ++ri)
02071 if (li() != ri())
02072 return false;
02073 return true;
02074 }
02075
02076 return false;
02077 }
02078
02079 template<class T>
02080 inline bool
02081 PP_List<T>::operator!= (const PP_List<T>& rhs) const
02082 {
02083 return !operator==(rhs);
02084 }
02085
02086 template <class T>
02087 inline void
02088 PP_List<T>::remove (PP_ListIterator<T>& li)
02089 {
02090 PP_ListLink<T> *np = li.p->suc;
02091 remove(li.p);
02092 li.p = np;
02093 }
02094
02095 template <class T>
02096 inline void
02097 PP_List<T>::remove (const T& _v)
02098 {
02099 for (PP_ListIterator<T> litr(*this); litr; ++litr)
02100 if (litr() == _v)
02101 remove(litr);
02102 }
02103
02104 template <class T>
02105 inline void
02106 PP_List<T>::remove (const PP_List<T>& _lv)
02107 {
02108 for (PP_ListIterator<T> litr(_lv); litr; ++litr)
02109 remove(litr());
02110 }
02111
02112 template <class T>
02113 inline void
02114 PP_List<T>::remove (PP_ListLink<T>* ln)
02115 {
02116 assert(head !=0 && tail != 0);
02117
02118 if (head == ln && tail == ln)
02119 head = tail = 0;
02120 else if (head == ln)
02121 {
02122 assert(ln->pre == 0);
02123 head = ln->suc;
02124 head->pre = 0;
02125 }
02126 else if (tail == ln)
02127 {
02128 assert(ln->suc == 0);
02129 tail = ln->pre;
02130 tail->suc = 0;
02131 }
02132 else
02133 {
02134 assert(ln->suc != 0 && ln->pre != 0);
02135 ln->suc->pre = ln->pre;
02136 ln->pre->suc = ln->suc;
02137 }
02138 delete ln;
02139 ln = 0;
02140 }
02141
02142
02143
02144
02145
02146 template <class T> class PP_Array;
02147
02148
02149
02150
02151
02152
02153
02154
02155
02156
02157
02158
02159
02160
02161
02162
02163
02164
02165
02166
02167
02168
02169
02170
02171
02172
02173
02174
02175
02176
02177
02178
02179
02180
02181
02182
02183
02184
02185
02186
02187
02188
02189
02190 template <class T>
02191 class PP_Array
02192 {
02193 public:
02194
02195
02196
02197 PP_Array ();
02198
02199
02200
02201
02202 PP_Array (long len);
02203
02204
02205
02206
02207 PP_Array (long len,
02208 const T& initialvalue);
02209
02210
02211
02212
02213 PP_Array (const T* vec,
02214 long len);
02215
02216
02217
02218 PP_Array (const PP_Array<T>& rhs);
02219
02220
02221
02222
02223 PP_Array<T>& operator= (const PP_Array<T>& rhs);
02224
02225
02226
02227 ~PP_Array ();
02228
02229
02230
02231
02232 void clear ();
02233
02234
02235
02236 bool ready () const;
02237
02238
02239
02240
02241 void reserve (long _truesize);
02242
02243
02244
02245
02246
02247
02248
02249 void shrinkWrap ();
02250
02251
02252
02253
02254
02255
02256
02257
02258
02259 void resize (long newlen);
02260
02261
02262
02263
02264
02265
02266
02267
02268
02269 void resize (long newlen,
02270 const T& initialvalue);
02271
02272
02273
02274 long length () const;
02275
02276
02277
02278
02279 long trueSize () const;
02280
02281
02282
02283
02284
02285 T& operator[] (long K);
02286
02287
02288
02289 const T& operator[] (long K) const;
02290
02291
02292
02293 T& get (long i);
02294
02295
02296
02297 const T& get (long i) const;
02298
02299
02300
02301
02302
02303 T* dataPtr ();
02304
02305
02306
02307 const T* dataPtr () const;
02308
02309
02310
02311 void set (long i,
02312 const T& elem);
02313
02314
02315
02316 void swap (long i,
02317 long j);
02318
02319
02320
02321 bool operator== (const PP_Array<T>& rhs) const;
02322
02323
02324
02325 bool operator!= (const PP_Array<T>& rhs) const;
02326
02327 protected:
02328
02329
02330
02331 long truesize;
02332
02333
02334
02335 long nelem;
02336
02337
02338
02339 T* vp;
02340
02341 private:
02342
02343
02344
02345 PP_Array<T>& operator= (int);
02346 };
02347
02348
02349
02350
02351
02352 template <class T>
02353 inline
02354 PP_Array<T>::PP_Array ()
02355 {
02356 nelem = 0;
02357 vp = new T[1];
02358 truesize = 1;
02359 }
02360
02361 template <class T>
02362 inline
02363 PP_Array<T>::PP_Array (long len)
02364 {
02365 assert(len >= 0);
02366 nelem = len;
02367 vp = new T[len];
02368 truesize = nelem;
02369 }
02370
02371 template <class T>
02372 inline
02373 void
02374 PP_Array<T>::clear ()
02375 {
02376 delete [] vp;
02377 vp = 0;
02378 nelem = 0;
02379 truesize = 0;
02380 }
02381
02382 template <class T>
02383 inline
02384 PP_Array<T>::~PP_Array ()
02385 {
02386 clear();
02387 }
02388
02389 template <class T>
02390 inline
02391 bool
02392 PP_Array<T>::ready () const
02393 {
02394 return vp != 0 && nelem != 0;
02395 }
02396
02397 template <class T>
02398 inline
02399 long
02400 PP_Array<T>::length () const
02401 {
02402 return nelem;
02403 }
02404
02405 template <class T>
02406 inline
02407 long
02408 PP_Array<T>::trueSize () const
02409 {
02410 return truesize;
02411 }
02412
02413 template <class T>
02414 inline
02415 T&
02416 PP_Array<T>::operator[] (long i)
02417 {
02418 assert(vp != 0);
02419 assert(i >= 0 && i < nelem);
02420 return vp[i];
02421 }
02422
02423 template <class T>
02424 inline
02425 const T&
02426 PP_Array<T>::operator[] (long i) const
02427 {
02428 assert(vp != 0);
02429 assert(i >= 0 && i < nelem);
02430 return vp[i];
02431 }
02432
02433 template <class T>
02434 inline
02435 T&
02436 PP_Array<T>::get (long i)
02437 {
02438 assert(vp != 0);
02439 assert(i >= 0 && i < nelem);
02440 return vp[i];
02441 }
02442
02443 template <class T>
02444 inline
02445 const T&
02446 PP_Array<T>::get (long i) const
02447 {
02448 assert(vp != 0);
02449 assert(i >= 0 && i < nelem);
02450 return vp[i];
02451 }
02452
02453 template <class T>
02454 inline
02455 void
02456 PP_Array<T>::set (long i,
02457 const T& elem)
02458 {
02459 assert(vp != 0);
02460 assert(i >= 0 && i < nelem);
02461 vp[i] = elem;
02462 }
02463
02464 template <class T>
02465 inline
02466 T*
02467 PP_Array<T>::dataPtr ()
02468 {
02469 return vp;
02470 }
02471
02472 template <class T>
02473 inline
02474 const T*
02475 PP_Array<T>::dataPtr () const
02476 {
02477 return vp;
02478 }
02479
02480 template <class T>
02481 inline
02482 void
02483 PP_Array<T>::swap (long i,
02484 long j)
02485 {
02486 assert(i >= 0 && i < nelem);
02487 assert(j >= 0 && j < nelem);
02488 T tmp = vp[i];
02489 vp[i] = vp[j];
02490 vp[j] = tmp;
02491 }
02492
02493 template <class T>
02494 inline
02495 bool
02496 PP_Array<T>::operator!= (const PP_Array<T>& rhs) const
02497 {
02498 return !(operator==(rhs));
02499 }
02500
02501
02502
02503
02504
02505 template <class T>
02506 PP_Array<T>::PP_Array (long len,
02507 const T& initialValue)
02508 {
02509 assert(len >= 0);
02510 nelem = len;
02511 vp = new T[len];
02512 truesize = nelem;
02513 for(long i = 0; i < nelem; ++i)
02514 vp[i] = initialValue;
02515 }
02516
02517 template <class T>
02518 PP_Array<T>::PP_Array (const T* vec,
02519 long len)
02520 {
02521 assert(len >= 0);
02522 nelem = len;
02523 vp = new T[len];
02524 truesize = nelem;
02525 for(long i = 0; i < nelem; ++i)
02526 vp[i] = vec[i];
02527 }
02528
02529 template <class T>
02530 PP_Array<T>::PP_Array (const PP_Array<T>& a)
02531 {
02532 nelem = a.nelem;
02533 vp = new T[nelem];
02534 truesize = nelem;
02535 for (long i = 0; i < nelem; i++)
02536 vp[i] = a.vp[i];
02537 }
02538
02539 template <class T>
02540 PP_Array<T>&
02541 PP_Array<T>::operator= (const PP_Array<T>& sa)
02542 {
02543 if (this != &sa)
02544 {
02545 clear();
02546 vp = new T[sa.nelem];
02547 nelem = sa.nelem;
02548 truesize = nelem;
02549 for(long i = 0; i < nelem; i++)
02550 vp[i] = sa.vp[i];
02551 }
02552 return *this;
02553 }
02554
02555 template <class T>
02556 inline
02557 void
02558 PP_Array<T>::resize (long newlen)
02559 {
02560 if (newlen == nelem)
02561 return;
02562 if (newlen <= truesize)
02563 {
02564 nelem = newlen;
02565 return;
02566 }
02567 T* newvp = new T[newlen];
02568 long len = Min(newlen,nelem);
02569 for (long i = 0; i < len; i++)
02570 newvp[i] = vp[i];
02571 delete [] vp;
02572 vp = newvp;
02573 nelem = newlen;
02574 truesize = newlen;
02575 }
02576
02577 template <class T>
02578 inline
02579 void PP_Array<T>::resize (long newlen,
02580 const T& initialValue)
02581 {
02582 if (newlen == nelem)
02583 return;
02584 if (newlen <= truesize)
02585 {
02586 for(long i = nelem; i < newlen; ++i)
02587 vp[i] = initialValue;
02588 nelem = newlen;
02589 return;
02590 }
02591 T* newvp = new T[newlen];
02592 long len = Min(newlen,nelem);
02593 long i;
02594 for (i = 0; i < len; i++)
02595 newvp[i] = vp[i];
02596 for(i = len; i < newlen; ++i)
02597 newvp[i] = initialValue;
02598 delete [] vp;
02599 vp = newvp;
02600 nelem = newlen;
02601 truesize = newlen;
02602 }
02603
02604 template <class T>
02605 void
02606 PP_Array<T>::reserve (long _truesize)
02607 {
02608 if (_truesize > truesize)
02609 {
02610 T* newvp = new T[_truesize];
02611 for (long i = 0; i < nelem; i++)
02612 newvp[i] = vp[i];
02613 delete [] vp;
02614 vp = newvp;
02615 truesize = _truesize;
02616 }
02617 }
02618
02619 template <class T>
02620 void
02621 PP_Array<T>::shrinkWrap ()
02622 {
02623 if (nelem != truesize)
02624 {
02625 T* newvp = new T[nelem];
02626 for (long i = 0; i < nelem; i++)
02627 newvp[i] = vp[i];
02628 delete [] vp;
02629 vp = newvp;
02630 truesize = nelem;
02631 }
02632 }
02633
02634 template <class T>
02635 bool
02636 PP_Array<T>::operator== (const PP_Array<T>& rhs) const
02637 {
02638 if (length() != rhs.length())
02639 return false;
02640
02641 for (long i = 0; i < length(); ++i)
02642 if (!((*this)[i] == rhs[i]))
02643 return false;
02644
02645 return true;
02646 }
02647
02648
02649
02650
02651
02652
02653
02654
02655
02656
02657
02658
02659
02660
02661
02662
02663
02664
02665
02666
02667
02668
02669
02670
02671
02672
02673
02674
02675
02676
02677
02678
02679
02680
02681
02682
02683
02684
02685
02686
02687
02688
02689
02690
02691
02692
02693
02694
02695
02696
02697
02698
02699
02700
02701
02702
02703
02704
02705
02706
02707
02708
02709
02710
02711
02712
02713
02714
02715
02716
02717
02718
02719
02720
02721
02722
02723
02724
02725
02726
02727
02728
02729
02730
02731
02732
02733
02734
02735
02736
02737
02738
02739
02740
02741
02742
02743
02744
02745
02746
02747
02748
02749
02750
02751
02752
02753
02754
02755
02756
02757
02758
02759
02760
02761
02762
02763
02764
02765
02766
02767
02768
02769
02770
02771
02772
02773
02774
02775
02776
02777
02778
02779
02780
02781
02782
02783
02784
02785
02786
02787
02788
02789
02790
02791
02792
02793
02794
02795
02796
02797
02798
02799
02800 class PP_entry;
02801
02802
02803
02804
02805
02806
02807
02808
02809
02810
02811
02812
02813
02814
02815
02816
02817
02818
02819
02820
02821
02822
02823
02824
02825
02826
02827
02828
02829
02830
02831
02832
02833
02834
02835
02836
02837
02838
02839
02840
02841
02842
02843
02844
02845
02846
02847
02848
02849
02850
02851
02852
02853
02854
02855
02856
02857
02858
02859
02860
02861
02862
02863
02864
02865
02866
02867
02868
02869
02870
02871
02872
02873
02874
02875 class ParmParse
02876 {
02877 friend class PP_entry;
02878 public:
02879
02881
02882
02883
02884
02885
02886
02887
02888
02889
02890 ParmParse (int argc,
02891 char** argv,
02892 const char* prefix = 0,
02893 const char* parfile = 0);
02894
02895
02896
02897
02898
02899
02900 ParmParse (const char* prefix = 0);
02901
02902
02903
02904
02905 ~ParmParse();
02906
02908
02910
02913 bool contains (const char* name);
02914
02916
02919 bool contains (const std::string& name);
02920
02921
02922
02923
02924
02925 int countval (const char* name,
02926 int n = -1);
02927
02928
02929
02930
02931 int countname (const char* name);
02932
02933
02934
02935
02936 int countname (const std::string& name);
02937
02938
02939
02940 void dumpTable (std::ostream& os);
02941
02943
02945
02946
02947
02948
02949
02950
02951
02952
02953
02954 void get (const char* name,
02955 int& ref,
02956 int ival=0);
02957
02958
02959
02960
02961
02962
02963
02964
02965
02966
02967 int query (const char* name,
02968 int& ref,
02969 int ival=0);
02970
02971
02972
02973
02974
02975
02976
02977
02978
02979 void get (const char* name,
02980 float& ref,
02981 int ival=0);
02982
02983
02984
02985
02986
02987
02988
02989
02990
02991
02992 int query (const char* name,
02993 float& ref,
02994 int ival=0);
02995
02996
02997
02998
02999
03000
03001
03002
03003
03004
03005 void get (const char* name,
03006 double& ref,
03007 int ival=0);
03008
03009
03010
03011
03012
03013
03014
03015
03016
03017
03018 int query (const char* name,
03019 double& ref,
03020 int ival=0);
03021
03022
03023
03024
03025
03026
03027
03028
03029
03030 void get (const char* name,
03031 std::string& ref,
03032 int ival=0);
03033
03034
03035
03036
03037
03038
03039
03040
03041
03042
03043 int query (const char* name,
03044 std::string& ref,
03045 int ival=0);
03046
03048
03049
03050
03051
03052
03053
03054
03055
03056
03057
03058
03059
03060
03061 void getarr (const char* name,
03062 Vector<int>& ref,
03063 int start_ix,
03064 int num_val);
03065
03066 void getarr (const char* name,
03067 std::vector<int>& ref,
03068 int start_ix,
03069 int num_val);
03070
03071
03072
03073
03074
03075
03076
03077
03078
03079
03080
03081
03082
03083
03084 int queryarr (const char* name,
03085 Vector<int>& ref,
03086 int start_ix,
03087 int num_val);
03088
03089
03090 int queryarr (const char* name,
03091 std::vector<int>& ref,
03092 int start_ix,
03093 int num_val);
03094
03095
03096
03097
03098
03099
03100
03101
03102
03103
03104
03105
03106
03107 void getarr (const char* name,
03108 Vector<float>& ref,
03109 int start_ix,
03110 int num_val);
03111
03112
03113 void getarr (const char* name,
03114 std::vector<float>& ref,
03115 int start_ix,
03116 int num_val);
03117
03118
03119
03120
03121
03122
03123
03124
03125
03126
03127
03128
03129
03130
03131
03132 int queryarr (const char* name,
03133 Vector<float>& ref,
03134 int start_ix,
03135 int num_val);
03136
03137
03138 int queryarr (const char* name,
03139 std::vector<float>& ref,
03140 int start_ix,
03141 int num_val);
03142
03143
03144
03145
03146
03147
03148
03149
03150
03151
03152
03153
03154
03155
03156 void getarr (const char* name,
03157 Vector<double>& ref,
03158 int start_ix,
03159 int num_val);
03160
03161 void getarr (const char* name,
03162 std::vector<double>& ref,
03163 int start_ix,
03164 int num_val);
03165
03166
03167
03168
03169
03170
03171
03172
03173
03174
03175
03176
03177
03178
03179 int queryarr (const char* name,
03180 Vector<double>& ref,
03181 int start_ix,
03182 int num_val);
03183
03184 int queryarr (const char* name,
03185 std::vector<double>& ref,
03186 int start_ix,
03187 int num_val);
03188
03189
03190
03191
03192
03193
03194
03195
03196
03197
03198
03199
03200
03201 void getarr (const char* name,
03202 Vector<std::string>& ref,
03203 int start_ix,
03204 int num_val);
03205
03206 void getarr (const char* name,
03207 std::vector<std::string>& ref,
03208 int start_ix,
03209 int num_val);
03210
03211
03212
03213
03214
03215
03216
03217
03218
03219
03220
03221
03222
03223
03224 int queryarr (const char* name,
03225 Vector<std::string>& ref,
03226 int start_ix,
03227 int num_val);
03228
03229
03230 int queryarr (const char* name,
03231 std::vector<std::string>& ref,
03232 int start_ix,
03233 int num_val);
03234
03235
03236
03237 enum PPType
03238 {
03239 ppDefn,
03240 ppOption,
03241 ppInt,
03242 ppFloat,
03243 ppDouble,
03244 ppString,
03245 ppEQ_sign,
03246 ppEOF
03247 };
03248 protected:
03249
03250
03251
03252
03253 static PP_List<PP_entry*> table;
03254
03255
03256
03257 static int xargc;
03258 static char** xargv;
03259
03260
03261
03262 static int num_obj;
03263
03264
03265
03266 void bldTable (const char* str,
03267 int lenstr,
03268 PP_List<PP_entry*>& tab);
03269
03270
03271
03272 void addDefn (PP_String& def,
03273 PP_List<PP_String>& val,
03274 PP_List<PP_entry*>& tab);
03275
03276
03277
03278 void read_file (const char* fname,
03279 PP_List<PP_entry*>& tab);
03280
03281
03282
03283 PPType getToken (const char*,
03284 int&,
03285 int,
03286 char*);
03287
03288
03289
03290 void rmTable ();
03291
03292
03293
03294 PP_String thePrefix;
03295
03296
03297
03298 void ppinit (const char* parfile);
03299
03300
03301
03302 const PP_entry* ppindex (int n,
03303 const char* name) const;
03304
03305
03306
03307
03308
03309
03310
03311
03312 void getval (const char* name,
03313 const PPType type,
03314 void* ptr,
03315 int ival,
03316 int k=-1);
03317
03318
03319
03320 void getarr (const char* name,
03321 const PPType type,
03322 void* ptr,
03323 int start_ix,
03324 int num_val,
03325 int k=-1);
03326 int queryval (const char* name,
03327 const PPType type,
03328 void* ptr,
03329 int ival,
03330 int k=-1);
03331 int queryarr (const char* name,
03332 const PPType type,
03333 void* ptr,
03334 int start_ix,
03335 int num_val,
03336 int k=-1);
03337
03338 bool isInteger (const PP_String& str,
03339 int& val);
03340 int isDouble (const PP_String& str,
03341 double& val);
03342 };
03343
03344 class PP_entry
03345 {
03346 private:
03347 friend class ParmParse;
03348 PP_entry() {}
03349 PP_entry (PP_String& name,
03350 ParmParse::PPType typ,
03351 PP_List<PP_String>& vals);
03352 ~PP_entry() {}
03353
03354 PP_String defname;
03355 ParmParse::PPType deftype;
03356 PP_Array<PP_String> val;
03357
03358 void dump (std::ostream& os) const;
03359 };
03360
03361
03362
03363
03364
03365 inline
03366 int
03367 ParmParse::countval (const char* name,
03368 int n)
03369 {
03370
03371
03372
03373 const PP_entry* def = ppindex(n,name);
03374 return def == 0 ? 0 : def->val.length();
03375 }
03376
03377
03378 inline
03379 void
03380 ParmParse::get (const char* name,
03381 int& ptr,
03382 int ival)
03383 {
03384 getval(name,ppInt,&ptr,ival,-1);
03385 }
03386
03387
03388 inline
03389 int
03390 ParmParse::query (const char* name,
03391 int& ptr,
03392 int ival)
03393 {
03394 return queryval(name,ppInt,&ptr,ival,-1);
03395 }
03396
03397
03398 inline
03399 void
03400 ParmParse::get (const char* name,
03401 float& ptr,
03402 int ival)
03403 {
03404 getval(name,ppFloat,&ptr,ival,-1);
03405 }
03406
03407
03408 inline
03409 int
03410 ParmParse::query (const char* name,
03411 float& ptr,
03412 int ival)
03413 {
03414 return queryval(name,ppFloat,&ptr,ival,-1);
03415 }
03416
03417
03418 inline
03419 void
03420 ParmParse::get (const char* name,
03421 double& ptr,
03422 int ival)
03423 {
03424 getval(name,ppDouble,&ptr,ival,-1);
03425 }
03426
03427
03428 inline
03429 int
03430 ParmParse::query (const char* name,
03431 double& ptr,
03432 int ival)
03433 {
03434 return queryval(name,ppDouble,&ptr,ival,-1);
03435 }
03436
03437
03438 inline
03439 void
03440 ParmParse::get (const char* name,
03441 std::string& ptr,
03442 int ival)
03443 {
03444 PP_String pp_string;
03445 getval(name,ppString,&pp_string,ival,-1);
03446 ptr = pp_string.c_str();
03447 }
03448
03449
03450 inline
03451 int
03452 ParmParse::query (const char* name,
03453 std::string& ptr,
03454 int ival)
03455 {
03456 PP_String pp_string;
03457 int status = queryval(name,ppString,&pp_string,ival,-1);
03458 if (status != 0)
03459 ptr = pp_string.c_str();
03460 return status;
03461 }
03462
03463
03464 inline
03465 void
03466 ParmParse::getarr (const char* name,
03467 std::vector<int>& ptr,
03468 int start_ix,
03469 int num_val)
03470 {
03471 if (ptr.size() < num_val)
03472 ptr.resize(num_val);
03473 int* c_array = new int[num_val];
03474 getarr(name,ppInt,c_array,start_ix,num_val,-1);
03475 for (int i = 0; i < num_val; ++i)
03476 {
03477 ptr[i] = c_array[i];
03478 }
03479 delete[] c_array;
03480 }
03481
03482
03483 inline
03484 void
03485 ParmParse::getarr (const char* name,
03486 Vector<int>& ptr,
03487 int start_ix,
03488 int num_val)
03489 {
03490 if (ptr.size() < num_val)
03491 ptr.resize(num_val);
03492 int* c_array = new int[num_val];
03493 getarr(name,ppInt,c_array,start_ix,num_val,-1);
03494 for (int i = 0; i < num_val; ++i)
03495 {
03496 ptr[i] = c_array[i];
03497 }
03498 delete[] c_array;
03499 }
03500
03501
03502 inline
03503 int
03504 ParmParse::queryarr (const char* name,
03505 std::vector<int>& ptr,
03506 int start_ix,
03507 int num_val)
03508 {
03509 if (ptr.size() < num_val)
03510 ptr.resize(num_val);
03511 int* c_array = new int[num_val];
03512 int status = queryarr(name,ppInt,c_array,start_ix,num_val,-1);
03513 if (status != 0 )
03514 {
03515 for (int i = 0; i < num_val; ++i)
03516 {
03517 ptr[i] = c_array[i];
03518 }
03519 }
03520 return status;
03521 }
03522
03523 inline
03524 int
03525 ParmParse::queryarr (const char* name,
03526 Vector<int>& ptr,
03527 int start_ix,
03528 int num_val)
03529 {
03530 if (ptr.size() < num_val)
03531 ptr.resize(num_val);
03532 int* c_array = new int[num_val];
03533 int status = queryarr(name,ppInt,c_array,start_ix,num_val,-1);
03534 if (status != 0 )
03535 {
03536 for (int i = 0; i < num_val; ++i)
03537 {
03538 ptr[i] = c_array[i];
03539 }
03540 }
03541 return status;
03542 }
03543
03544
03545 inline
03546 void
03547 ParmParse::getarr (const char* name,
03548 std::vector<float>& ptr,
03549 int start_ix,
03550 int num_val)
03551 {
03552 if (ptr.size() < num_val)
03553 ptr.resize(num_val);
03554 float* c_array = new float[num_val];
03555 getarr(name,ppFloat,c_array,start_ix,num_val,-1);
03556 for (int i = 0; i < num_val; ++i)
03557 {
03558 ptr[i] = c_array[i];
03559 }
03560 delete[] c_array;
03561 }
03562
03563 inline
03564 void
03565 ParmParse::getarr (const char* name,
03566 Vector<float>& ptr,
03567 int start_ix,
03568 int num_val)
03569 {
03570 if (ptr.size() < num_val)
03571 ptr.resize(num_val);
03572 float* c_array = new float[num_val];
03573 getarr(name,ppFloat,c_array,start_ix,num_val,-1);
03574 for (int i = 0; i < num_val; ++i)
03575 {
03576 ptr[i] = c_array[i];
03577 }
03578 delete[] c_array;
03579 }
03580
03581
03582 inline
03583 int
03584 ParmParse::queryarr (const char* name,
03585 std::vector<float>& ptr,
03586 int start_ix,
03587 int num_val)
03588 {
03589 if (ptr.size() < num_val)
03590 ptr.resize(num_val);
03591 float* c_array = new float[num_val];
03592 int status = queryarr(name,ppFloat,c_array,start_ix,num_val,-1);
03593 if (status != 0)
03594 {
03595 for (int i = 0; i < num_val; ++i)
03596 {
03597 ptr[i] = c_array[i];
03598 }
03599 }
03600 delete[] c_array;
03601 return status;
03602 }
03603
03604
03605 inline
03606 int
03607 ParmParse::queryarr (const char* name,
03608 Vector<float>& ptr,
03609 int start_ix,
03610 int num_val)
03611 {
03612 if (ptr.size() < num_val)
03613 ptr.resize(num_val);
03614 float* c_array = new float[num_val];
03615 int status = queryarr(name,ppFloat,c_array,start_ix,num_val,-1);
03616 if (status != 0)
03617 {
03618 for (int i = 0; i < num_val; ++i)
03619 {
03620 ptr[i] = c_array[i];
03621 }
03622 }
03623 delete[] c_array;
03624 return status;
03625 }
03626
03627
03628 inline
03629 void
03630 ParmParse::getarr (const char* name,
03631 std::vector<double>& ptr,
03632 int start_ix,
03633 int num_val)
03634 {
03635 if (ptr.size() < num_val)
03636 ptr.resize(num_val);
03637 double* c_array = new double[num_val];
03638 int status = queryarr(name,ppDouble,c_array,start_ix,num_val,-1);
03639 if (status == 0)
03640 {
03641 cerr << "ParmParse::getarr(): "
03642 << name
03643 << " not found in table" << endl;
03644 dumpTable(cerr);
03645 MayDay::Abort();
03646 }
03647 for (int i = 0; i < num_val; ++i)
03648 {
03649 ptr[i] = c_array[i];
03650 }
03651 delete[] c_array;
03652 }
03653
03654
03655 inline
03656 void
03657 ParmParse::getarr (const char* name,
03658 Vector<double>& ptr,
03659 int start_ix,
03660 int num_val)
03661 {
03662 if (ptr.size() < num_val)
03663 ptr.resize(num_val);
03664 double* c_array = new double[num_val];
03665 int status = queryarr(name,ppDouble,c_array,start_ix,num_val,-1);
03666 if (status == 0)
03667 {
03668 cerr << "ParmParse::getarr(): "
03669 << name
03670 << " not found in table" << endl;
03671 dumpTable(cerr);
03672 MayDay::Abort();
03673 }
03674 for (int i = 0; i < num_val; ++i)
03675 {
03676 ptr[i] = c_array[i];
03677 }
03678 delete[] c_array;
03679 }
03680
03681
03682 inline
03683 int
03684 ParmParse::queryarr (const char* name,
03685 std::vector<double>& ptr,
03686 int start_ix,
03687 int num_val)
03688 {
03689 if (ptr.size() < num_val)
03690 ptr.resize(num_val);
03691 double* c_array = new double[num_val];
03692 int status = queryarr(name,ppDouble,c_array,start_ix,num_val,-1);
03693 if (status != 0)
03694 {
03695 for (int i = 0; i < num_val; ++i)
03696 {
03697 ptr[i] = c_array[i];
03698 }
03699 }
03700 delete[] c_array;
03701 return status;
03702 }
03703
03704
03705
03706
03707 inline
03708 int
03709 ParmParse::queryarr (const char* name,
03710 Vector<double>& ptr,
03711 int start_ix,
03712 int num_val)
03713 {
03714 if (ptr.size() < num_val)
03715 ptr.resize(num_val);
03716 double* c_array = new double[num_val];
03717 int status = queryarr(name,ppDouble,c_array,start_ix,num_val,-1);
03718 if (status != 0)
03719 {
03720 for (int i = 0; i < num_val; ++i)
03721 {
03722 ptr[i] = c_array[i];
03723 }
03724 }
03725 delete[] c_array;
03726 return status;
03727 }
03728
03729
03730 inline
03731 void
03732 ParmParse::getarr (const char* name,
03733 std::vector<std::string>& ptr,
03734 int start_ix,
03735 int num_val)
03736 {
03737 if (ptr.size() < num_val)
03738 ptr.resize(num_val);
03739 PP_String* c_array = new PP_String[num_val];
03740 getarr(name,ppString,c_array,start_ix,num_val,-1);
03741 for (int i = 0; i < num_val; ++i)
03742 {
03743 ptr[i] = c_array[i].c_str();
03744 }
03745 delete[] c_array;
03746 }
03747
03748
03749
03750 inline
03751 void
03752 ParmParse::getarr (const char* name,
03753 Vector<std::string>& ptr,
03754 int start_ix,
03755 int num_val)
03756 {
03757 if (ptr.size() < num_val)
03758 ptr.resize(num_val);
03759 PP_String* c_array = new PP_String[num_val];
03760 getarr(name,ppString,c_array,start_ix,num_val,-1);
03761 for (int i = 0; i < num_val; ++i)
03762 {
03763 ptr[i] = c_array[i].c_str();
03764 }
03765 delete[] c_array;
03766 }
03767
03768
03769 inline
03770 int
03771 ParmParse::queryarr (const char* name,
03772 std::vector<std::string>& ptr,
03773 int start_ix,
03774 int num_val)
03775 {
03776 if (ptr.size() < num_val)
03777 ptr.resize(num_val);
03778 PP_String* c_array = new PP_String[num_val];
03779 int status = queryarr(name,ppString,c_array,start_ix,num_val,-1);
03780 if (status != 0)
03781 {
03782 for (int i = 0; i < num_val; ++i)
03783 {
03784 ptr[i] = c_array[i].c_str();
03785 }
03786 }
03787 delete[] c_array;
03788 return status;
03789 }
03790
03791
03792 inline
03793 int
03794 ParmParse::queryarr (const char* name,
03795 Vector<std::string>& ptr,
03796 int start_ix,
03797 int num_val)
03798 {
03799 if (ptr.size() < num_val)
03800 ptr.resize(num_val);
03801 PP_String* c_array = new PP_String[num_val];
03802 int status = queryarr(name,ppString,c_array,start_ix,num_val,-1);
03803 if (status != 0)
03804 {
03805 for (int i = 0; i < num_val; ++i)
03806 {
03807 ptr[i] = c_array[i].c_str();
03808 }
03809 }
03810 delete[] c_array;
03811 return status;
03812 }
03813
03814
03815 inline
03816 bool
03817 ParmParse::isInteger (const PP_String& str,
03818 int& val)
03819 {
03820
03821
03822
03823 char* endp = 0;
03824 val = (int) strtol(str.c_str(), &endp, 10);
03825 return *endp == 0;
03826 }
03827
03828
03829 inline
03830 int
03831 ParmParse::isDouble (const PP_String& str,
03832 double& val)
03833 {
03834 char* endp = 0;
03835 val = std::strtod(str.c_str(), &endp);
03836 return *endp == 0;
03837 }
03838
03839
03840 inline
03841 int
03842 ParmParse::countname (const std::string& name)
03843 {
03844 return countname(name.c_str());
03845 }
03846
03847 #endif