casacore
Loading...
Searching...
No Matches
BitFlagsEngine.h
Go to the documentation of this file.
1// # BitFlagsEngine.h: Templated virtual column engine to map bit flags to a Bool
2// # Copyright (C) 2009
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef TABLES_BITFLAGSENGINE_H
27#define TABLES_BITFLAGSENGINE_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/DataMan/BaseMappedArrayEngine.h>
32
33namespace casacore { // # NAMESPACE CASACORE - BEGIN
34
35// <summary> Non-templated Helper class to handle the mask. </summary>
36// <use visibility=local>
37class BFEngineMask {
38 public:
39 // Form the mask as given.
40 explicit BFEngineMask(uInt mask = 0xffffffff);
42 // Form the mask from the given keywords defining the bits.
43 BFEngineMask(const Array<String>& keys, uInt defaultMask);
44
45 // Make the mask from the given keywords defining the bits.
46 void makeMask(const TableColumn& column);
47
48 // Form the read mask from the specification.
49 // If keywords are given, the mask is formed from them.
50 void fromRecord(const RecordInterface& spec, const TableColumn& column, const String& prefix);
51
52 // Store the info in a Record.
53 void toRecord(RecordInterface& spec, const String& prefix) const;
54
55 // Get the mask.
56 uInt getMask() const { return itsMask; }
57
58 // Get the mask keywords.
59 const Array<String>& getKeys() const { return itsMaskKeys; }
60
61 private:
64};
65
66// <summary>
67// Templated virtual column engine to map bit flags to a Bool.
68// </summary>
69
70// <use visibility=export>
71
72// <reviewed reviewer="Gareth Hunt" date="94Nov17" tests="">
73// </reviewed>
74
75// <prerequisite>
76// # Classes you should understand before using this one.
77// <li> VirtualColumnEngine
78// <li> VirtualArrayColumn
79// </prerequisite>
80
81// <synopsis>
82// BitFlagsEngine is a virtual column engine which maps an integer column
83// containing flag bits to a Bool column. It can be used in a MeasurementSet
84// to have multiple flag categories, yet use all existing software that
85// deals with the Bool FLAG column.
86//
87// The engine support read as well as write access.
88// For both cases a mask can be defined telling which bits have to be taken
89// into account. For example, when writing to the Bool FLAG column, the data
90// in the bitflags column twill be or-ed with the bits as defined in the
91// writemask. Similary when reading FLAG, only the bits of the readmask are
92// taken into account.
93//
94// The masks can be defined in two ways:
95// <ul>
96// <li> The mask can be given directly as an integer value.
97// The default write mask is 1 (thus only bit 0), while the default
98// read mask is all bits.
99// <li> Symbolic names for mask bits can be defined as keywords in the
100// flagbits column. They define the bit value, not the bit number.
101// It makes it possible to combine bits in a keyword.
102// The keywords are stored in a subrecord of keyword FLAGSETS.
103// Example of keyword and their values could be:
104// <br>RFI=1, CAL=2, CLIP=4, OTHER=8, RFICAL=3
105// <br>Note that in this example RFICAL is defined such that it
106// contains RFI and CAL.
107// </ul>
108// A mask can be set at construction time, but it can be changed at runtime
109// using the <src>setProperties</src> function.
110// The masks are kept in special keywords (which are different from the
111// keywords defining the flag bits), so it is possible to change a mask
112// by changing those keywords before opening a table. However, that is
113// not recommended.
114//
115// BitFlagsEngine is known to the table system for data types uChar, Short,
116// and Int.
117// </synopsis>
118
119// <motivation>
120// The FLAG_CATEGORY defined the Measurement does not work because adding
121// an extra flag means resizing the entire array which is slow.
122// This class makes it possible to use an integer column to store flags
123// and map it directly to a Bool column.
124// </motivation>
125
126// <example>
127// <srcblock>
128// // Create the table description and 2 columns with indirect arrays in it.
129// // The Int column will be stored, while the Bool will be used as virtual.
130// TableDesc tableDesc ("", TableDesc::Scratch);
131// tableDesc.addColumn (ArrayColumnDesc<Int> ("BitBlags"));
132// tableDesc.addColumn (ArrayColumnDesc<Bool> ("FLAG"));
133//
134// // Create a new table using the table description.
135// SetupNewTable newtab (tableDesc, "tab.data", Table::New);
136//
137// // Create the engine and bind the FLAG column to it.
138// BitFlagsEngine<Int> flagsEngine("FLAG", "BitFlags");
139// newtab.bindColumn ("FLAG", flagsEngine);
140// // Create the table.
141// Table table (newtab);
142//
143// // Store a 3-D array (with dim. 2,3,4) into each row of the column.
144// // The shape of each array in the column is implicitly set by the put
145// // function. This will also set the shape of the underlying Int array.
146// ArrayColumn data (table, "virtualArray");
147// Array<Bool> someArray(IPosition(4,2,3,4));
148// someArray = True;
149// for (rownr_t i=0, i<10; i++) { // table will have 10 rows
150// table.addRow();
151// data.put (i, someArray)
152// }
153// </srcblock>
154// The underlying integer array will be stored according to the writemask
155// which defaults to 1.
156// </example>
157
158// <templating arg=StoredType>
159// <li> only suited for built-in integer data types
160// </templating>
161
162template <typename StoredType>
163class BitFlagsEngine : public BaseMappedArrayEngine<Bool, StoredType> {
164 // # Make members of parent class known.
165 public:
166 using BaseMappedArrayEngine<Bool, StoredType>::virtualName;
167
168 protected:
169 using BaseMappedArrayEngine<Bool, StoredType>::storedName;
170 using BaseMappedArrayEngine<Bool, StoredType>::table;
171 using BaseMappedArrayEngine<Bool, StoredType>::column;
172 using BaseMappedArrayEngine<Bool, StoredType>::setNames;
173
174 public:
175 // Construct an engine to map integer arrays in a column to Bool arrays.
176 // StoredColumnName is the name of the column where the integer
177 // data will be put and must have data type StoredType.
178 // The virtual column using this engine must have data type Bool.
179 // <br>A mask can be given that specifies which bits to use in the mapping
180 // from StoredType to Bool. Similarly a mask can be given defining which
181 // bits to set when mapping from Bool to StoredType.
182 BitFlagsEngine(const String& virtualColumnName, const String& storedColumnName,
183 StoredType readMask = StoredType(0xffffffff), StoredType writeMask = 1);
184
185 // Construct an engine to map integer arrays in a column to Bool arrays.
186 // StoredColumnName is the name of the column where the scaled
187 // data will be put and must have data type StoredType.
188 // The virtual column using this engine must have data type Bool.
189 // <br>A mask can be given that specifies which bits to use in the mapping
190 // from StoredType to Bool. Similarly a mask can be given defining which
191 // bits to set when mapping from Bool to StoredType.
192 // The masks are given using the values of keywords in the stored column.
193 // Each keyword should be an integer defining one or more bits and can be
194 // seen as a symbolic name. The keyword values are or-ed to form the mask.
195 // The keywords are stored in a subrecord of keyword FLAGSETS.
196 BitFlagsEngine(const String& virtualColumnName, const String& storedColumnName,
197 const Array<String>& readMaskKeys, const Array<String>& writeMaskKeys);
198
199 // Construct from a record specification as created by dataManagerSpec().
200 BitFlagsEngine(const Record& spec);
201
202 // Destructor is mandatory.
204
205 // Assignment is not needed and therefore forbidden
207
208 // Return the type name of the engine (i.e. its class name).
209 virtual String dataManagerType() const;
210
211 // Get the name given to the engine (is the virtual column name).
212 virtual String dataManagerName() const;
213
214 // Record a record containing data manager specifications.
215 virtual Record dataManagerSpec() const;
216
217 // Get data manager properties that can be modified.
218 // These are ReadMask, WriteMask, ReadMaskKeys, and WriteMaskKeys.
219 // It is a subset of the data manager specification.
220 virtual Record getProperties() const;
221
222 // Modify data manager properties.
223 // These are ReadMask, WriteMask, ReadMaskKeys, and/or WriteMaskKeys.
224 // Mask keys should be given as an array of strings giving the keyword
225 // names defining mask bits (similar to the constructor). Mask keys are
226 // only used if not empty.
227 virtual void setProperties(const Record& spec);
228
229 // Return the name of the class.
230 // This includes the names of the template arguments.
232
233 // Register the class name and the static makeObject "constructor".
234 // This will make the engine known to the table system.
235 // The automatically invoked registration function in DataManReg.cc
236 // contains BitFlagsEngine<Int>.
237 // Any other instantiation of this class must be registered "manually"
238 // (or added to DataManReg.cc).
239 static void registerClass();
240
241 private:
242 // Copy constructor is only used by clone().
243 // (so it is made private).
245
246 // Clone the engine object.
248
249 // Initialize the object for a new table.
250 // It defines the keywords containing the engine parameters.
251 void create64(rownr_t initialNrrow);
252
253 // Preparing consists of setting the writable switch and
254 // adding the initial number of rows in case of create.
255 // Furthermore it reads the keywords containing the engine parameters.
256 void prepare();
257
258 // Get an array in the given row.
259 // This will scale and offset from the underlying array.
261
262 // Put an array in the given row.
263 // This will scale and offset to the underlying array.
264 void putArray(rownr_t rownr, const Array<Bool>& array);
265
266 // Get a section of the array in the given row.
267 // This will scale and offset from the underlying array.
268 void getSlice(rownr_t rownr, const Slicer& slicer, Array<Bool>& array);
269
270 // Put into a section of the array in the given row.
271 // This will scale and offset to the underlying array.
272 void putSlice(rownr_t rownr, const Slicer& slicer, const Array<Bool>& array);
273
274 // Get an entire column.
275 // This will scale and offset from the underlying array.
277
278 // Put an entire column.
279 // This will scale and offset to the underlying array.
281
282 // Get some array values in the column.
283 // This will scale and offset from the underlying array.
284 virtual void getArrayColumnCells(const RefRows& rownrs, Array<Bool>& data);
285
286 // Put some array values in the column.
287 // This will scale and offset to the underlying array.
288 virtual void putArrayColumnCells(const RefRows& rownrs, const Array<Bool>& data);
289
290 // Get a section of all arrays in the column.
291 // This will scale and offset from the underlying array.
292 void getColumnSlice(const Slicer& slicer, Array<Bool>& array);
293
294 // Put a section of all arrays in the column.
295 // This will scale and offset to the underlying array.
296 void putColumnSlice(const Slicer& slicer, const Array<Bool>& array);
297
298 // Get a section of some arrays in the column.
299 // This will scale and offset from the underlying array.
300 virtual void getColumnSliceCells(const RefRows& rownrs, const Slicer& slicer, Array<Bool>& data);
301
302 // Put into a section of some arrays in the column.
303 // This will scale and offset to the underlying array.
304 virtual void putColumnSliceCells(const RefRows& rownrs, const Slicer& slicer,
305 const Array<Bool>& data);
306
307 // Map bit flags array to Bool array.
308 // This is meant when reading an array from the stored column.
310
311 // Map Bool array to bit flags array.
312 // This is meant when writing an array into the stored column.
314
315 // Functor to and an array and mask and convert to Bool.
316 struct FlagsToBool {
317 explicit FlagsToBool(StoredType readMask) : itsMask(readMask) {}
318 Bool operator()(StoredType value) const { return (value & itsMask) != 0; }
319
320 private:
321 StoredType itsMask;
322 };
323 // Functor to convert Bools to flags using a mask.
324 // By default only bit 0 is set.
325 // Flag bits not affected are kept.
326 struct BoolToFlags {
327 explicit BoolToFlags(StoredType writeMask) : itsMask(writeMask) {}
328 StoredType operator()(Bool flag, StoredType value) const {
329 return (flag ? value & itsMask : value);
330 }
331
332 private:
333 StoredType itsMask;
334 };
335
336 public:
337 // Define the "constructor" to construct this engine when a
338 // table is read back.
339 // This "constructor" has to be registered by the user of the engine.
340 // If the engine is commonly used, its registration can be added
341 // to the registerAllCtor function in DataManReg.cc.
342 // That function gets automatically invoked by the table system.
343 static DataManager* makeObject(const String& dataManagerType, const Record& spec);
344
345 private:
348 StoredType itsReadMask;
349 StoredType itsWriteMask;
350 Bool itsIsNew; // # True = new table
351};
352
353} // namespace casacore
354
355#ifndef CASACORE_NO_AUTO_TEMPLATES
356#include <casacore/tables/DataMan/BitFlagsEngine.tcc>
357#endif // # CASACORE_NO_AUTO_TEMPLATES
358#endif
BFEngineMask(const Array< String > &keys, uInt defaultMask)
Form the mask from the given keywords defining the bits.
BFEngineMask(uInt mask=0xffffffff)
Form the mask as given.
void toRecord(RecordInterface &spec, const String &prefix) const
Store the info in a Record.
uInt getMask() const
Get the mask.
const Array< String > & getKeys() const
Get the mask keywords.
Array< String > itsMaskKeys
void makeMask(const TableColumn &column)
Make the mask from the given keywords defining the bits.
void fromRecord(const RecordInterface &spec, const TableColumn &column, const String &prefix)
Form the read mask from the specification.
void setNames(const String &virtualName, const String &storedName)
BaseMappedArrayEngine(const String &virtualColumnName, const String &storedColumnName)
void mapOnGet(Array< Bool > &array, const Array< StoredType > &stored)
Map bit flags array to Bool array.
virtual void getArrayColumnCells(const RefRows &rownrs, Array< Bool > &data)
Get some array values in the column.
BitFlagsEngine(const BitFlagsEngine< StoredType > &)
Copy constructor is only used by clone().
void putArrayColumn(const Array< Bool > &array)
Put an entire column.
void putArray(rownr_t rownr, const Array< Bool > &array)
Put an array in the given row.
BitFlagsEngine(const String &virtualColumnName, const String &storedColumnName, StoredType readMask=StoredType(0xffffffff), StoredType writeMask=1)
Construct an engine to map integer arrays in a column to Bool arrays.
virtual Record getProperties() const
Get data manager properties that can be modified.
void putColumnSlice(const Slicer &slicer, const Array< Bool > &array)
Put a section of all arrays in the column.
void getColumnSlice(const Slicer &slicer, Array< Bool > &array)
Get a section of all arrays in the column.
void mapOnPut(const Array< Bool > &array, Array< StoredType > &stored)
Map Bool array to bit flags array.
static void registerClass()
Register the class name and the static makeObject "constructor".
virtual String dataManagerName() const
Get the name given to the engine (is the virtual column name).
void create64(rownr_t initialNrrow)
Initialize the object for a new table.
void getSlice(rownr_t rownr, const Slicer &slicer, Array< Bool > &array)
Get a section of the array in the given row.
void getArray(rownr_t rownr, Array< Bool > &array)
Get an array in the given row.
virtual void putArrayColumnCells(const RefRows &rownrs, const Array< Bool > &data)
Put some array values in the column.
void putSlice(rownr_t rownr, const Slicer &slicer, const Array< Bool > &array)
Put into a section of the array in the given row.
~BitFlagsEngine()
Destructor is mandatory.
virtual void setProperties(const Record &spec)
Modify data manager properties.
virtual void putColumnSliceCells(const RefRows &rownrs, const Slicer &slicer, const Array< Bool > &data)
Put into a section of some arrays in the column.
BitFlagsEngine< StoredType > & operator=(const BitFlagsEngine< StoredType > &)=delete
Assignment is not needed and therefore forbidden.
BitFlagsEngine(const Record &spec)
Construct from a record specification as created by dataManagerSpec().
virtual Record dataManagerSpec() const
Record a record containing data manager specifications.
DataManager * clone() const
Clone the engine object.
virtual void getColumnSliceCells(const RefRows &rownrs, const Slicer &slicer, Array< Bool > &data)
Get a section of some arrays in the column.
static String className()
Return the name of the class.
void getArrayColumn(Array< Bool > &array)
Get an entire column.
static DataManager * makeObject(const String &dataManagerType, const Record &spec)
Define the "constructor" to construct this engine when a table is read back.
virtual String dataManagerType() const
Return the type name of the engine (i.e.
BitFlagsEngine(const String &virtualColumnName, const String &storedColumnName, const Array< String > &readMaskKeys, const Array< String > &writeMaskKeys)
Construct an engine to map integer arrays in a column to Bool arrays.
void prepare()
Preparing consists of setting the writable switch and adding the initial number of rows in case of cr...
Abstract base class for a data manager.
Table & table() const
Get the table this object is associated with.
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
T * array
The actual storage.
Definition Block.h:689
unsigned int uInt
Definition aipstype.h:49
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
RecordInterface()
The default constructor creates an empty record with a variable structure.
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360
uInt64 rownr_t
Define the type of a row number in a table.
Definition aipsxtype.h:44
StoredType operator()(Bool flag, StoredType value) const
Bool operator()(StoredType value) const