casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TBArray.h
Go to the documentation of this file.
00001 //# TBArray.h: Holds a potentially multi-dimensional array.
00002 //# Copyright (C) 2005
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: $
00027 #ifndef TBARRAY_H_
00028 #define TBARRAY_H_
00029 
00030 #include <casa/BasicSL/String.h>
00031 
00032 #include <vector>
00033 
00034 #include <casa/namespace.h>
00035 using namespace std;
00036 
00037 namespace casa {
00038 
00039 //# Forward Declarations
00040 class TBTable;
00041 
00042 // <summary>
00043 // Holds a potentially multi-dimensional array.
00044 // <summary>
00045 //
00046 // <synopsis>
00047 // A TBArray holds a array object as used by the table browser.  The data
00048 // is represented by String values, while the array structure is represented by
00049 // vectors of void*s.  For all but the "last" dimension, the void*s point to
00050 // other vector<void*>s, and on the last dimension the void*s point to Strings.
00051 // NOTE: this class is mostly obsolete now that the browser uses the TBData
00052 // structure.
00053 // </synopsis>
00054 
00055 class TBArray {
00056 public:
00057     // Constructor for a data array (in other words, an array found in the data
00058     // of a table).  Takes as input the row and column of the array in table,
00059     // the type of table, and the String holding the values of the array which
00060     // need to be parsed.
00061     TBArray(int row, int col, String type, String array);
00062 
00063     // Constructor for a table keyword array (in other words, an array found in
00064     // the table keywords).  Takes as input the index of the keyword in the
00065     // table keywords list, the type of the array, and the String holding the
00066     // values of the array which need to be parsed.
00067     TBArray(int keywordIndex, String type, String array);
00068 
00069     // Constructor for a field keyword array (in other words, an array found in
00070     // the field keywords).  Takes as input the name of the field, the index of
00071     // the keyword in the field keywords list, the type of the array, and the
00072     // String holding the values of the array which need to be parsed.
00073     TBArray(String col, int index, String type, String array);
00074     
00075     // Constructor for a non-specific array.  Takes as input the type of the
00076     // array and the String holding the values of the array which need to be
00077     // parsed.
00078     TBArray(String type, String array);
00079 
00080     ~TBArray();
00081 
00082     
00083     // Returns true if the array is valid, false otherwise.
00084     bool isValid();
00085     
00086     // Returns the dimensions of the array in list format.
00087     vector<int> getDimensions();
00088 
00089     // Returns the dimensionality of the array.  For example, a 4x4 array
00090     // would return 2 while a 4x4x4 array would return 3.
00091     unsigned int dim();
00092 
00093     // If the dimensions are thought of as a list (e.g., 2x4x2), then this
00094     // method returns the ith dimension in the list.
00095     int dimensionAt(unsigned int i);
00096 
00097     // Returns true if the array is one-dimensional, false otherwise.
00098     bool isOneDimensional();
00099 
00100     // Returns the type of the array.
00101     String getType();
00102 
00103     // Returns the data representation.  In all but the last dimension, the
00104     // void*s point to vector<void*>s; in the last dimension the void*s point
00105     // to Strings.
00106     vector<void*>* getData();
00107 
00108     // Returns the row of the table where the array is located.  This is only
00109     // valid for data arrays.
00110     int getRow();
00111 
00112     // Returns the column of the table where the array is located.  This is
00113     // only valid for data arrays.
00114     int getCol();
00115     
00116     
00117     // Returns true if the given array is in the same location as this array,
00118     // false otherwise.
00119     // For data arrays: true if the row and col values are equal;
00120     // for table keyword arrays: true if they refer to the same keyword index;
00121     // for field keyword arrays: true if the fields are the same and the
00122     // keyword indices are the same.
00123     bool sameLocationAs(TBArray* array);
00124 
00125     // Returns the name of this array, assuming that it belongs to the given
00126     // table. For data arrays: "[table name][[row],[col]]";
00127     // for table keyword arrays: "[table name] [keyword name]";
00128     // for field keyword arrays: "[table name] [field name, keyword name]".
00129     String getName(TBTable* table);
00130 
00131     // Returns the data at the given coordinates, or blank if the coordinates
00132     // are invalid.
00133     String dataAt(vector<int> d);
00134 
00135     // Sets the data at the given coordinates to the given value.  This call
00136     // does NOT write through to the underlying table; it only updates the data
00137     // representation.
00138     void setDataAt(vector<int> d, String newVal);
00139 
00140     // Returns true if the given coordinates are valid for this array, false
00141     // otherwise.
00142     bool dimensionIsValid(vector<int> d);
00143 
00144     // Returns a "flattened" String representation of this array.  Each cell
00145     // is appended to the String separated by a space.
00146     String toFlattenedString();
00147 
00148     // Returns true if this array contains the given value, false otherwise.
00149     bool contains(String value);
00150 
00151     // Returns true if this array contains any value that is between the two
00152     // given values, false otherwise.
00153     bool containsBetween(String value1, String value2);
00154 
00155     // Returns true if this array contains any value that is less than the
00156     // given value, false otherwise.
00157     bool containsLessThan(String value);
00158     
00159     // Returns true if this array contains any value that is greater than the
00160     // given value, false otherwise.
00161     bool containsGreaterThan(String value);
00162 
00163 private:
00164     // Holds the dimensions of this array.
00165     vector<int> dimensions;
00166 
00167     // Data representation.
00168     vector<void*> data;
00169 
00170     // Indicates whether the array is valid or not.
00171     bool valid;
00172 
00173     // The type of the array.
00174     String type;
00175 
00176     // The row of the array for data arrays, or the keyword index for other
00177     // arrays.
00178     int row;
00179 
00180     // The column of the array for data arrays, invalid for other arrays.
00181     int col;
00182 
00183     // Indicates whether this array is one-dimensional or not.
00184     bool oneDim;
00185 
00186     // Indicates whether this is a data array or not.
00187     bool isData;
00188 
00189     // Indicates whether this is a field keyword array or not.
00190     bool isColKeyword;
00191 
00192     // Holds the field name for a field keyword array, empty otherwise.
00193     String field;
00194 
00195     
00196     // Parses the given String into the array.
00197     void parseArray(String* table);
00198 
00199     // Helper for parseArray().  Parses a single row into the given vector.
00200     String parseRow(String& str, vector<void*>* r, vector<int> d, int x);
00201 
00202     // Helper for parseArray().  Parses a table with dimension > 1.
00203     void parseMultidimensionalTable(String str);
00204 
00205     // Helper for parseArray().  Creates placeholder objects (such as empty
00206     // Strings and vectors) into the given row.
00207     void insertPlaceholders(vector<void*>* r, vector<int> d, int x);
00208 
00209     // Helper method for toFlattenedString();
00210     String toFlattenedString(vector<void*>* row, int d);
00211 
00212     // Helper method for contains().
00213     bool contains(vector<void*>* data, int n, String v);
00214 
00215     // Helper method for containsBetween().
00216     bool containsBetween(vector<void*>* data, int n, double v1, double v2);
00217 
00218     // Helper method for containsLessThan().
00219     bool containsLessThan(vector<void*>* data, int n, double v);
00220 
00221     // Helper method for containsGreaterThan().
00222     bool containsGreaterThan(vector<void*>* data, int n, double v);
00223     
00224     // Deletes the data in the given row.
00225     void deleteData(vector<void*>* data, int n);
00226 };
00227 
00228 }
00229 
00230 #endif /* TBARRAY_H_ */