Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

marray.C

Go to the documentation of this file.
00001 //----------------------------------------------------------------------
00002 //               The Motion Strategy Library (MSL)
00003 //----------------------------------------------------------------------
00004 //
00005 // Copyright (c) 1998-2000 Iowa State University and Steve LaValle.  
00006 // All Rights Reserved.
00007 // 
00008 // Permission to use, copy, and distribute this software and its 
00009 // documentation is hereby granted free of charge, provided that 
00010 // (1) it is not a component of a commercial product, and 
00011 // (2) this notice appears in all copies of the software and
00012 //     related documentation. 
00013 // 
00014 // Iowa State University and the author make no representations
00015 // about the suitability or fitness of this software for any purpose.  
00016 // It is provided "as is" without express or implied warranty.
00017 //----------------------------------------------------------------------
00018 
00019 #include "marray.h"
00020 
00021 
00022 template<class E> MultiArray<E>::MultiArray(const vector<int> &dims, 
00023                                             const E &x) {
00024   int i,offset;
00025 
00026   MaxSize = 10000000; // Maximum allowable size
00027   Dimensions = dims;
00028   Dimension = dims.size();
00029   
00030   Offsets = vector<int>(Dimension);
00031 
00032   offset = 1;
00033   for (i = 0; i < Dimension; i++) {
00034     Offsets[i] = offset;
00035     offset *= Dimensions[i];
00036   }
00037 
00038   Size = offset;
00039 
00040   if (Size <= MaxSize) {
00041     // Make the array to hold all of the data
00042     A = vector<E>(Size);
00043     for (i = 0; i < Size; i++)
00044       A[i] = x; // Write the value x to all elements
00045   }
00046   else {
00047     cout << "Size " << Size << " exceeds MaxSize limit " << MaxSize << "\n";
00048     exit(-1);
00049   }
00050 
00051 }
00052 
00053 
00054 template<class E> MultiArray<E>::MultiArray(const vector<int> &dims) {
00055   E x;
00056 
00057   MultiArray(dims,x);
00058 }
00059 
00060 
00061 template<class E> inline E& MultiArray<E>::operator[](const vector<int> 
00062                                                       &indices) {
00063   int i,index;
00064 
00065   index = indices[0];
00066 
00067   for (i = 1; i < Dimension; i++) {
00068     index += indices[i]*Offsets[i];
00069   }
00070 
00071   return A[index];
00072 }
00073 
00074 
00075 template<class E> inline bool MultiArray<E>::Increment(vector<int> &indices) {
00076   int i;
00077   bool carry,done;
00078 
00079   carry = false;
00080   done = false;
00081 
00082   if (indices[0] < Dimensions[0] - 1)
00083     indices[0]++;
00084   else { // Carry
00085     indices[0] = 0;
00086     carry = true;
00087     i = 1;
00088     while ((carry)&&(i < Dimension)) {
00089       if (indices[i] < Dimensions[i] - 1) {
00090         indices[i]++;
00091         carry = false;
00092       }
00093       else {
00094         indices[i] = 0;
00095         if (i == Dimension - 1)
00096           done = true;
00097       }
00098       i++;
00099     }
00100   }
00101 
00102   // This will report true if the end of array was reached
00103   return done;
00104 }
00105 
00106 
00107 
00108 
00109 
Motion Strategy Library


Web page maintained by Steve LaValle
Partial support provided by NSF CAREER Award IRI-970228 (LaValle), Honda Research, and Iowa State University.
Contributors: Anna Atramentov, Peng Cheng, James Kuffner, Steve LaValle, and Libo Yang.