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 namespace casa { 00035 00036 //# Forward Declarations 00037 class TBTable; 00038 00039 // <summary> 00040 // Holds a potentially multi-dimensional array. 00041 // <summary> 00042 // 00043 // <synopsis> 00044 // A TBArray holds a array object as used by the table browser. The data 00045 // is represented by String values, while the array structure is represented by 00046 // vectors of void*s. For all but the "last" dimension, the void*s point to 00047 // other vector<void*>s, and on the last dimension the void*s point to Strings. 00048 // NOTE: this class is mostly obsolete now that the browser uses the TBData 00049 // structure. 00050 // </synopsis> 00051 00052 class TBArray { 00053 public: 00054 // Constructor for a data array (in other words, an array found in the data 00055 // of a table). Takes as input the row and column of the array in table, 00056 // the type of table, and the String holding the values of the array which 00057 // need to be parsed. 00058 TBArray(int row, int col, String type, String array); 00059 00060 // Constructor for a table keyword array (in other words, an array found in 00061 // the table keywords). Takes as input the index of the keyword in the 00062 // table keywords list, the type of the array, and the String holding the 00063 // values of the array which need to be parsed. 00064 TBArray(int keywordIndex, String type, String array); 00065 00066 // Constructor for a field keyword array (in other words, an array found in 00067 // the field keywords). Takes as input the name of the field, the index of 00068 // the keyword in the field keywords list, the type of the array, and the 00069 // String holding the values of the array which need to be parsed. 00070 TBArray(String col, int index, String type, String array); 00071 00072 // Constructor for a non-specific array. Takes as input the type of the 00073 // array and the String holding the values of the array which need to be 00074 // parsed. 00075 TBArray(String type, String array); 00076 00077 ~TBArray(); 00078 00079 00080 // Returns true if the array is valid, false otherwise. 00081 bool isValid(); 00082 00083 // Returns the dimensions of the array in list format. 00084 std::vector<int> getDimensions(); 00085 00086 // Returns the dimensionality of the array. For example, a 4x4 array 00087 // would return 2 while a 4x4x4 array would return 3. 00088 unsigned int dim(); 00089 00090 // If the dimensions are thought of as a list (e.g., 2x4x2), then this 00091 // method returns the ith dimension in the list. 00092 int dimensionAt(unsigned int i); 00093 00094 // Returns true if the array is one-dimensional, false otherwise. 00095 bool isOneDimensional(); 00096 00097 // Returns the type of the array. 00098 String getType(); 00099 00100 // Returns the data representation. In all but the last dimension, the 00101 // void*s point to vector<void*>s; in the last dimension the void*s point 00102 // to Strings. 00103 std::vector<void*>* getData(); 00104 00105 // Returns the row of the table where the array is located. This is only 00106 // valid for data arrays. 00107 int getRow(); 00108 00109 // Returns the column of the table where the array is located. This is 00110 // only valid for data arrays. 00111 int getCol(); 00112 00113 00114 // Returns true if the given array is in the same location as this array, 00115 // false otherwise. 00116 // For data arrays: true if the row and col values are equal; 00117 // for table keyword arrays: true if they refer to the same keyword index; 00118 // for field keyword arrays: true if the fields are the same and the 00119 // keyword indices are the same. 00120 bool sameLocationAs(TBArray* array); 00121 00122 // Returns the name of this array, assuming that it belongs to the given 00123 // table. For data arrays: "[table name][[row],[col]]"; 00124 // for table keyword arrays: "[table name] [keyword name]"; 00125 // for field keyword arrays: "[table name] [field name, keyword name]". 00126 String getName(TBTable* table); 00127 00128 // Returns the data at the given coordinates, or blank if the coordinates 00129 // are invalid. 00130 String dataAt(std::vector<int> d); 00131 00132 // Sets the data at the given coordinates to the given value. This call 00133 // does NOT write through to the underlying table; it only updates the data 00134 // representation. 00135 void setDataAt(std::vector<int> d, String newVal); 00136 00137 // Returns true if the given coordinates are valid for this array, false 00138 // otherwise. 00139 bool dimensionIsValid(std::vector<int> d); 00140 00141 // Returns a "flattened" String representation of this array. Each cell 00142 // is appended to the String separated by a space. 00143 String toFlattenedString(); 00144 00145 // Returns true if this array contains the given value, false otherwise. 00146 bool contains(String value); 00147 00148 // Returns true if this array contains any value that is between the two 00149 // given values, false otherwise. 00150 bool containsBetween(String value1, String value2); 00151 00152 // Returns true if this array contains any value that is less than the 00153 // given value, false otherwise. 00154 bool containsLessThan(String value); 00155 00156 // Returns true if this array contains any value that is greater than the 00157 // given value, false otherwise. 00158 bool containsGreaterThan(String value); 00159 00160 private: 00161 // Holds the dimensions of this array. 00162 std::vector<int> dimensions; 00163 00164 // Data representation. 00165 std::vector<void*> data; 00166 00167 // Indicates whether the array is valid or not. 00168 bool valid; 00169 00170 // The type of the array. 00171 String type; 00172 00173 // The row of the array for data arrays, or the keyword index for other 00174 // arrays. 00175 int row; 00176 00177 // The column of the array for data arrays, invalid for other arrays. 00178 int col; 00179 00180 // Indicates whether this array is one-dimensional or not. 00181 bool oneDim; 00182 00183 // Indicates whether this is a data array or not. 00184 bool isData; 00185 00186 // Indicates whether this is a field keyword array or not. 00187 bool isColKeyword; 00188 00189 // Holds the field name for a field keyword array, empty otherwise. 00190 String field; 00191 00192 00193 // Parses the given String into the array. 00194 void parseArray(String* table); 00195 00196 // Helper for parseArray(). Parses a single row into the given vector. 00197 String parseRow(String& str, std::vector<void*>* r, std::vector<int> d, int x); 00198 00199 // Helper for parseArray(). Parses a table with dimension > 1. 00200 void parseMultidimensionalTable(String str); 00201 00202 // Helper for parseArray(). Creates placeholder objects (such as empty 00203 // Strings and vectors) into the given row. 00204 void insertPlaceholders(std::vector<void*>* r, std::vector<int> d, int x); 00205 00206 // Helper method for toFlattenedString(); 00207 String toFlattenedString(std::vector<void*>* row, int d); 00208 00209 // Helper method for contains(). 00210 bool contains(std::vector<void*>* data, int n, String v); 00211 00212 // Helper method for containsBetween(). 00213 bool containsBetween(std::vector<void*>* data, int n, double v1, double v2); 00214 00215 // Helper method for containsLessThan(). 00216 bool containsLessThan(std::vector<void*>* data, int n, double v); 00217 00218 // Helper method for containsGreaterThan(). 00219 bool containsGreaterThan(std::vector<void*>* data, int n, double v); 00220 00221 // Deletes the data in the given row. 00222 void deleteData(std::vector<void*>* data, int n); 00223 }; 00224 00225 } 00226 00227 #endif /* TBARRAY_H_ */