• Main Page
  • Modules
  • Classes
  • Files
  • File List
  • File Members

/Users/garethloy/Musimathics/Musimat1.2/include/List.h

Go to the documentation of this file.
00001 /* $Revision: 1.5 $ $Date: 2006/09/08 18:56:52 $ $Author: dgl $ $Name:  $ $Id: List.h,v 1.5 2006/09/08 18:56:52 dgl Exp $ */
00002 #ifndef LIST_H
00003 #define LIST_H
00004 
00005 // The Musimat Tutorial � 2006 Gareth Loy
00006 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" � 2006 Gareth Loy 
00007 // and published exclusively by The MIT Press.
00008 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00009 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00010 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00011 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00012 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00013 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00014 // The Musimat website:                 http://www.musimat.com/
00015 // This program is released under the terms of the GNU General Public License
00016 // available here:                      http://www.gnu.org/licenses/gpl.txt
00017 
00020 
00021 #include <assert.h>
00022 #include <memory.h>
00023 #include <iostream>
00024 
00029 
00030 template <class Type>
00031 class List
00032 {
00035         friend ostream& operator<<(ostream& os,Const List& r) {
00036                 If (r.m_len > 0)
00037                         os << "(";
00038                 for (Integer i = 0; i < r.m_len; i++) {
00039                         os << r.m_val[i];
00040                         If (i != r.m_len-1)
00041                                 os << ", ";
00042                         Else
00043                                 os << ")";
00044                 }
00045                 os << endl;
00046                 Return os;
00047          }
00048 
00052          friend Void adjustSize(Integer len, List& r) {
00053                 If (r.m_val == 0) {
00054                         r.m_val = new Type [ r.m_len = len+1 ];
00055                 } Else If (r.m_len <= len) {
00056                         r.m_len = len+1;
00057                         r.m_val = (Type*) realloc( r.m_val, r.m_len * sizeof(Type));
00058                 }
00059          }
00060 
00063          friend istream& operator>>(istream& is, List& r) {
00064                  char ch;
00065                  Type t;
00066                  Integer len = 0;
00067                  is >> ch;
00068                  If (ch != '{' And ch != '(')
00069                          Return is;
00070                  While (is >> t >> ch And ch == ',') {
00071                         adjustSize(len, r);
00072                         r.m_val[len++] = t;
00073                  }
00074                 adjustSize(len, r);
00075                 r.m_val[len++] = t;
00076                 assert( ch == '}' || ch == ')');
00077                 Return is;
00078          }
00079 
00080 public:
00081 
00084         List() : m_val(0), m_len(0) { }
00085 
00090         List( List Const& L ) {
00091                 m_val = new Type [ m_len = L.m_len ];
00092                 memcpy(m_val, L.m_val, sizeof(Type) * m_len);
00093         }
00094 
00099         List(Type i_) { 
00100                 m_val = new Type [ m_len = 1 ];
00101                 m_val[0] = i_; 
00102         }
00103 
00109         List(Type i1_, Type i2_)  { 
00110                 m_val = new Type [ m_len = 2 ];
00111                 m_val[0] = i1_; 
00112                 m_val[1] = i2_; 
00113         }
00114 
00116         List(Type i1_, Type i2_, Type i3_)  {
00117                 m_val = new Type [ m_len = 3 ];
00118                 m_val[0] = i1_; 
00119                 m_val[1] = i2_; 
00120                 m_val[2] = i3_; 
00121         }
00122 
00124         List(Type i1_, Type i2_, Type i3_, Type i4_)  {
00125                 m_val = new Type [ m_len = 4 ];
00126                 m_val[0] = i1_; 
00127                 m_val[1] = i2_; 
00128                 m_val[2] = i3_; 
00129                 m_val[3] = i4_; 
00130         }
00131 
00133         List(Type i1_, Type i2_, Type i3_, Type i4_, Type i5_)  {
00134                 m_val = new Type [ m_len = 5 ];
00135                 m_val[0] = i1_; 
00136                 m_val[1] = i2_; 
00137                 m_val[2] = i3_; 
00138                 m_val[3] = i4_; 
00139                 m_val[4] = i5_; 
00140         }
00141 
00143         List(Type i1_, Type i2_, Type i3_, Type i4_, Type i5_, Type i6_)  {
00144                 m_val = new Type [ m_len = 6 ];
00145                 m_val[0] = i1_; m_val[1] = i2_;  m_val[2] = i3_; m_val[3] = i4_; m_val[4] = i5_; m_val[5] = i6_; 
00146         }
00147 
00149         List(Type i1_, Type i2_, Type i3_, Type i4_, Type i5_, Type i6_, Type i7_)  {
00150                 m_val = new Type [ m_len = 7 ];
00151                 m_val[0] = i1_; m_val[1] = i2_;  m_val[2] = i3_; m_val[3] = i4_; m_val[4] = i5_; m_val[5] = i6_;  m_val[6] = i7_; 
00152         }
00153 
00155         List(Type i1_, Type i2_, Type i3_, Type i4_, Type i5_, Type i6_, Type i7_, Type i8_)  {
00156                 m_val = new Type [ m_len = 8 ];
00157                 m_val[0] = i1_; m_val[1] = i2_;  m_val[2] = i3_; m_val[3] = i4_; m_val[4] = i5_; m_val[5] = i6_;  m_val[6] = i7_;  m_val[7] = i8_; 
00158         }
00159 
00161         List(Type i1_, Type i2_, Type i3_, Type i4_, Type i5_, Type i6_, Type i7_, Type i8_, Type i9_)  {
00162                 m_val = new Type [ m_len = 9 ];
00163                 m_val[0] = i1_; m_val[1] = i2_;  m_val[2] = i3_; m_val[3] = i4_; m_val[4] = i5_; m_val[5] = i6_;  m_val[6] = i7_;  m_val[7] = i8_; 
00164                 m_val[8] = i9_;
00165         }
00166 
00168         List(Type i1_, Type i2_, Type i3_, Type i4_, Type i5_, Type i6_, Type i7_, Type i8_, Type i9_, Type i10_)  {
00169                 m_val = new Type [ m_len = 10 ];
00170                 m_val[0] = i1_; m_val[1] = i2_;  m_val[2] = i3_; m_val[3] = i4_; m_val[4] = i5_; m_val[5] = i6_;  m_val[6] = i7_;  m_val[7] = i8_; 
00171                 m_val[8] = i9_; m_val[9] = i10_;
00172         }
00173 
00175         List(Type i1_, Type i2_, Type i3_, Type i4_, Type i5_, Type i6_, Type i7_, Type i8_, Type i9_, Type i10_, Type i11_)  {
00176                 m_val = new Type [ m_len = 11 ];
00177                 m_val[0] = i1_; m_val[1] = i2_;  m_val[2] = i3_; m_val[3] = i4_; m_val[4] = i5_; m_val[5] = i6_;  m_val[6] = i7_;  m_val[7] = i8_; 
00178                 m_val[8] = i9_; m_val[9] = i10_; m_val[10] = i11_;
00179         }
00180 
00182         List(Type i1_, Type i2_, Type i3_, Type i4_, Type i5_, Type i6_, Type i7_, Type i8_, Type i9_, Type i10_, Type i11_, Type i12_)  {
00183                 m_val = new Type [ m_len = 12 ];
00184                 m_val[0] = i1_; m_val[1] = i2_;  m_val[2] = i3_; m_val[3] = i4_; m_val[4] = i5_; m_val[5] = i6_;  m_val[6] = i7_;  m_val[7] = i8_; 
00185                 m_val[8] = i9_; m_val[9] = i10_; m_val[10] = i11_; m_val[11] = i12_;
00186         }
00187 
00189         List(Type i1_, Type i2_, Type i3_, Type i4_, Type i5_, Type i6_, Type i7_, Type i8_, Type i9_, Type i10_, Type i11_, Type i12_, Type i13_)  {
00190                 m_val = new Type [ m_len = 13 ];
00191                 m_val[0] = i1_; m_val[1] = i2_;  m_val[2] = i3_; m_val[3] = i4_; m_val[4] = i5_; m_val[5] = i6_;  m_val[6] = i7_;  m_val[7] = i8_; 
00192                 m_val[8] = i9_; m_val[9] = i10_; m_val[10] = i11_; m_val[11] = i12_; m_val[12] = i13_;
00193         }
00194 
00196         List(Type i1_, Type i2_, Type i3_, Type i4_, Type i5_, Type i6_, Type i7_, Type i8_, Type i9_, Type i10_, Type i11_, Type i12_, Type i13_, Type i14_ )  {
00197                 m_val = new Type [ m_len = 14 ];
00198                 m_val[0] = i1_; m_val[1] = i2_;  m_val[2] = i3_; m_val[3] = i4_; m_val[4] = i5_; m_val[5] = i6_;  m_val[6] = i7_;  m_val[7] = i8_; 
00199                 m_val[8] = i9_; m_val[9] = i10_; m_val[10] = i11_; m_val[11] = i12_; m_val[12] = i13_; m_val[13] = i14_;
00200         }
00201 
00203         List(Type i1_, Type i2_, Type i3_, Type i4_, Type i5_, Type i6_, Type i7_, Type i8_, Type i9_, Type i10_, Type i11_, Type i12_, Type i13_, Type i14_, Type i15_ )  {
00204                 m_val = new Type [ m_len = 15 ];
00205                 m_val[0] = i1_; m_val[1] = i2_;  m_val[2] = i3_; m_val[3] = i4_; m_val[4] = i5_; m_val[5] = i6_;  m_val[6] = i7_;  m_val[7] = i8_; 
00206                 m_val[8] = i9_; m_val[9] = i10_; m_val[10] = i11_; m_val[11] = i12_; m_val[12] = i13_; m_val[13] = i14_; m_val[14] = i15_;
00207         }
00208 
00210         List(Type i1_, Type i2_, Type i3_, Type i4_, Type i5_, Type i6_, Type i7_, Type i8_, Type i9_, Type i10_, Type i11_, Type i12_, Type i13_, Type i14_, Type i15_,
00211                 Type i16_, Type i17_, Type i18_, Type i19_, Type i20_, Type i21_, Type i22_ )  {
00212                 m_val = new Type [ m_len = 22 ];
00213                 m_val[0] = i1_; m_val[1] = i2_;  m_val[2] = i3_; m_val[3] = i4_; m_val[4] = i5_; m_val[5] = i6_;  m_val[6] = i7_;  m_val[7] = i8_; 
00214                 m_val[8] = i9_; m_val[9] = i10_; m_val[10] = i11_; m_val[11] = i12_; m_val[12] = i13_; m_val[13] = i14_; m_val[14] = i15_;
00215                 m_val[15] = i16_; m_val[16] = i17_; m_val[17] = i18_; m_val[18] = i19_; m_val[19] = i20_; m_val[20] = i21_; m_val[21] = i22_;
00216         }
00217 
00219         ~List() { 
00220                 If ( m_val )
00221                         delete [] m_val; 
00222                 m_val = 0;
00223                 m_len = 0;
00224         }
00225 
00228         Bool operator==( Const List& L ) {
00229                 If ( L.m_len != m_len )
00230                         Return(False);
00231                 For( Integer i = 0; i < length(); i++ ) {
00232                         If ( L.m_val[i] != m_val[i] )
00233                                 Return( False );
00234                 }
00235                 Return( True );
00236         }
00237 
00239         Bool operator!=( Const List& L ) {
00240                 If ( L.m_len != m_len )
00241                         Return(True);
00242                 Integer cnt = 0;
00243                 For( Integer i = 0; i < length(); i++ ) {
00244                         If ( L.m_val[i] == m_val[i] )
00245                                 cnt++;
00246                 }
00247                 Return( cnt != m_len );
00248         }
00249 
00253         List& operator+=( Const List& L ) {
00254                 m_val = (Type*) realloc( m_val, (m_len + L.m_len) * sizeof(Type));
00255                 memcpy(&m_val[m_len], L.m_val, sizeof(Type) * L.m_len);
00256                 m_len += L.m_len;
00257                 Return( *this );
00258         }
00259 
00263         List operator+( Const List &L ) {
00264                 List r = *this;
00265                 r += L;
00266                 Return r;
00267         }
00268 
00272         List& operator=( Const List& L ) {
00273                 If (this == &L)
00274                         Return *this;
00275                 If (m_val != 0)
00276                         delete [] m_val;
00277                 m_val = new Type [ m_len = L.m_len ];
00278                 memcpy(m_val, L.m_val, sizeof(Type) * m_len);
00279                 Return( *this );
00280         }
00281 
00286         Type& operator[]( Integer elem ) {
00287                 assert( elem >= 0);
00288                 checkSize(elem);
00289                 Return m_val[elem];
00290         }
00291 
00295         List& operator+=( Const Type x ) {
00296                 For ( Integer i = 0; i < m_len; i++ )
00297                         m_val[i] += x;
00298                 Return( *this );
00299         }
00300 
00304         List& operator-=( Const Type x ) {
00305                 For ( Integer i = 0; i < m_len; i++ )
00306                         m_val[i] -= x;
00307                 Return( *this );
00308         }
00309 
00313         List& operator+( Const Type x ) {
00314                 List* t = new List (*this);
00315                 *t = *this; // copy this
00316                 For ( Integer i = 0; i < m_len; i++ )
00317                         t->m_val[i] += x;
00318                 Return( *t );
00319         }
00320 
00324         List& operator-( Const Type x ) {
00325                 List* t = new List [ m_len ];
00326                 *t = *this; // copy this
00327                 For ( Integer i = 0; i < m_len; i++ )
00328                         t->m_val[i] -= x;
00329                 Return( *t );
00330         }
00331 
00335         List& operator*( Const Type x ) {
00336                 List* t = new List (*this);
00337                 *t = *this; // copy this
00338                 For ( Integer i = 0; i < m_len; i++ )
00339                         t->m_val[i] *= x;
00340                 Return( *t );
00341         }
00342 
00346         List& operator/( Const Type x ) {
00347                 List* t = new List [ m_len ];
00348                 *t = *this; // copy this
00349                 For ( Integer i = 0; i < m_len; i++ )
00350                         t->m_val[i] /= x;
00351                 Return( *t );
00352         }
00353 
00357         //List& operator+=( Const Real x ) {
00358         //      For ( Integer i = 0; i < m_len; i++ )
00359         //              m_val[i] += Type(x);
00360         //      Return( *this );
00361         //}
00362 
00366         //List& operator-=( Const Real x ) {
00367         //      For ( Integer i = 0; i < m_len; i++ )
00368         //              m_val[i] -= Type(x);
00369         //      Return( *this );
00370         //}
00371 
00375         //List& operator+( Const Real x ) {
00376         //      List* t = new List (*this);
00377         //      *t = *this; // copy this
00378         //      For ( Integer i = 0; i < m_len; i++ )
00379         //              t->m_val[i] += Type(x);
00380         //      Return( *t );
00381         //}
00382 
00386         //List& operator-( Const Real x ) {
00387         //      List* t = new List [ m_len ];
00388         //      *t = *this; // copy this
00389         //      For ( Integer i = 0; i < m_len; i++ )
00390         //              t->m_val[i] -= Type(x);
00391         //      Return( *t );
00392         //}
00393 
00397         //List& operator*( Const Real x ) {
00398         //      List* t = new List (*this);
00399         //      *t = *this; // copy this
00400         //      For ( Integer i = 0; i < m_len; i++ )
00401         //              t->m_val[i] *= Type(x);
00402         //      Return( *t );
00403         //}
00404 
00408         //List& operator/( Const Real x ) {
00409         //      List* t = new List;
00410         //      *t = *this;
00411         //      For ( Integer i = 0; i < m_len; i++ )
00412         //              t->m_val[i] /= Type(x);
00413         //      Return( *t );
00414         //}
00415 
00418         Void print(String s = 0) {
00419                 If (s)
00420                         cout << s;
00421                 cout << "{ ";
00422                 cout.precision(2);
00423                 For (Integer i = 0; i < m_len-1; i++)
00424                         cout << m_val[i] << ", ";
00425                 cout << m_val[m_len-1];
00426                 cout << " }" << endl;
00427         }
00428 
00430         Void SetListSize(Integer n) {
00431         If(m_val)
00432                         delete[] m_val;
00433         If (n > 0) {
00434             m_val = new Type[n];
00435             memset( m_val, 0, sizeof(Type) * n / sizeof(char) );
00436         }
00437         Else 
00438                         m_val = NULL;
00439         m_len = n;
00440     }
00441 
00447         Void checkSize(Integer len) {
00448                 If (m_val == 0)
00449                         m_val = new Type [ m_len = len+1 ];
00450                 Else If (m_len <= len) {
00451                         m_len = len+1;
00452                         m_val = (Type*) realloc( m_val, m_len * sizeof(Type));
00453                 }
00454         }
00455 
00458         Integer length() { 
00459                 Return m_len; 
00460         }
00461 
00464         Type max() {
00465                 assert(m_len > 0);
00466                 Type max = m_val[0];
00467                 For ( Integer i = 1; i < m_len; i++ )
00468                         max = m_val[i] < max ? max : m_val[i];
00469                 Return max;
00470         }
00471 
00475         Integer maxPos() {
00476                 assert(m_len > 0);
00477                 Type max = m_val[0];
00478                 Integer mp = 0;
00479                 For ( Integer i = 1; i < m_len; i++ ) {
00480                         If ( m_val[i] > max ) {
00481                                 max = m_val[i];
00482                                 mp = i;
00483                         }
00484                 }
00485                 Return mp;
00486         }
00487 
00490         Type min() {
00491                 assert(m_len > 0);
00492                 Type min = m_val[0];
00493                 For ( Integer i = 1; i < m_len; i++ )
00494                         min = m_val[i] < min ? m_val[i] : min;
00495                 Return min;
00496         }
00497 
00501         Integer minPos() {
00502                 assert(m_len > 0);
00503                 Type min = m_val[0];
00504                 Integer mp = 0;
00505                 For ( Integer i = 1; i < m_len; i++ ) {
00506                         If ( m_val[i] < min ) {
00507                                 min = m_val[i];
00508                                 mp = i;
00509                         }
00510                 }
00511                 Return mp;
00512         }
00513 
00517         Void rotate( Integer n, Integer i = 0 ) {
00518                 n = Mod( n, length() );                 // constrain rotation to length of list
00519                 Type x = m_val[ i ];                    // store f[ i ] for use after recursion
00520                 If ( i < length() - 1 )                 // reached the end?
00521                         rotate( n, i+1 );                       // no, set up next element via recursion
00522                 // We continue from here when the recursion unwinds
00523                 Integer pos = PosMod( i+n, length() );  // index list modulo its length
00524                 m_val[ pos ] = x;                               // assign value of x saved above
00525         }
00526 
00531         Void transpose( Type t, Type lim ) {
00532                 For ( Integer i = 0; i < length(); i = i + 1 ) {
00533                         Type x = m_val[ i ] + t;
00534                         m_val[ i ] = PosMod(x, lim);
00535                 }
00536         }
00537 
00540         Void invert( Type lim ) {
00541                 For ( Integer i = 0; i < length(); i = i + 1 ) {
00542                         m_val[ i ] = ( lim - m_val[ i ] ) % lim;
00543                 }
00544         }
00545 
00547         Void retrograde( ) {
00548                 Integer n = length( );
00549                 Type* t = new Type [ m_len ];
00550                 memcpy(t, m_val, sizeof(Type) * m_len);
00551                 For ( Integer i = 0; i < n; i = i + 1 )
00552                         m_val[ i ] = t[ n - i - 1 ];
00553         }
00554 
00555 private:
00556         Type* m_val;            
00557         Integer m_len;          
00558 };
00559 
00560 
00561 
00563 template<class Type> inline Integer Length( Type& L )  { Return( L.length() ); }
00564 
00565 
00571 
00573 template<class Type> Type inline Join( Type& L1, Type& L2 ) { Return ( L1 + L2 ); }
00574 
00576 template<class Type> Type inline Join( Type& L1, Type& L2, Type& L3) { Return L1 + L2 + L3; }
00577 
00579 template<class Type> Type inline Join( Type& L1, Type& L2, Type& L3, Type& L4) { Return L1 + L2 + L3 + L4; }
00580 
00582 template<class Type> Type inline Join( Type& L1, Type& L2, Type& L3, Type& L4, Type& L5) { Return L1 + L2 + L3 + L4 + L5; }
00583 
00585 template<class Type> Type inline Join( Type& L1, Type& L2, Type& L3, Type& L4, Type& L5, Type& L6) { Return L1 + L2 + L3 + L4 + L5 + L6; }
00586 
00588 template<class Type> Type inline Join( Type& L1, Type& L2, Type& L3, Type& L4, Type& L5, Type& L6, Type& L7) { Return L1 + L2 + L3 + L4 + L5 + L6 + L7; }
00590 
00592 template<class TargetListType, class ElementType, class ListType> TargetListType Map( ListType L, ElementType (*fn)(ElementType) )
00593 {
00594         For( Integer i = 0; i < Length( L ); i++ )
00595                 L[i] = fn( L[i] );
00596         Return( L );
00597 }
00598 
00599 #endif // LIST_H

Generated on Fri Nov 26 2010 16:18:25 for MusimatLib by  doxygen 1.7.2