VisBufferImpl2Internal.h

Go to the documentation of this file.
00001 /*
00002  * VisBufferImpl2Internal.h
00003  *
00004  *  Created on: Aug 22, 2013
00005  *      Author: jjacobs
00006  */
00007 
00008 #ifndef VISBUFFERIMPL2INTERNAL_H_
00009 #define VISBUFFERIMPL2INTERNAL_H_
00010 
00011 #include <msvis/MSVis/ViImplementation2.h>
00012 #include <cassert>
00013 
00014 namespace casa {
00015 
00016 namespace ms {
00017     class Vbi2MsRow;
00018 }
00019 
00020 namespace vi {
00021 
00023 //
00024 // Auxiliary Classes are contained in the "vb" namespace.
00025 //
00026 // These include VbCacheItemBase, VbCacheItem, VisBufferCache
00027 // and VisBufferState.
00028 
00029 
00030 
00031 
00032 // Possible array shapes of data coming from the main table cells.
00033 
00034 typedef enum {NoCheck, Nr, NfNr, NcNr, NcNfNr, NcNfNcatNr, I3Nr, N_ShapePatterns} ShapePattern;
00035 
00036 class VisBufferCache;
00037 
00038 class VbCacheItemBase {
00039 
00040     // Provides a common base class for all of the cached value classes.
00041     // This is required because the actualy value classes use a template
00042     // to capture the underlying value type.
00043 
00044     friend class VisBufferImpl2;
00045 
00046 public:
00047 
00048     VbCacheItemBase (bool isMutable)
00049     : isKey_p (False),
00050       isMutable_p (isMutable),
00051       vbComponent_p (VisBufferComponent2::Unknown),
00052       vb_p (0) {}
00053 
00054     virtual ~VbCacheItemBase () {}
00055 
00056     virtual void appendRows (Int nRowsToAdd, Bool truncate = False) = 0;
00057     virtual void clear (Bool clearStatusOnly = False) = 0;
00058     virtual void clearDirty () = 0;
00059     virtual void copyRowElement (Int sourceRow, Int destinationRow) = 0;
00060     virtual void fill () const = 0;
00061     VisBufferComponent2
00062     getComponent () const
00063     {
00064         return vbComponent_p;
00065     }
00066     virtual Bool isArray () const = 0;
00067     virtual Bool isDirty () const = 0;
00068     virtual Bool isPresent () const = 0;
00069     virtual Bool isShapeOk () const = 0;
00070     virtual void resize (Bool /*copyValues*/) {}
00071     virtual void resizeRows (Int /*newNRows*/) {}
00072     virtual void setDirty () = 0;
00073     virtual String shapeErrorMessage () const = 0;
00074 
00075 protected:
00076 
00077     virtual void copy (const VbCacheItemBase * other, Bool fetchIfNeeded) = 0;
00078 
00079     VisBufferImpl2 * getVb () const
00080     {
00081         return vb_p;
00082     }
00083 
00084     virtual void initialize (VisBufferCache * cache, VisBufferImpl2 * vb, VisBufferComponent2 component,
00085                              Bool isKey = True);
00086 
00087     Bool isKey () const { return isKey_p;}
00088     bool isMutable () const { return isMutable_p; }
00089 
00090     virtual void setAsPresent (Bool isPresent = True) const = 0;
00091     void setIsKey (Bool isKey)
00092     {
00093         isKey_p = isKey;
00094     }
00095 
00096 private:
00097 
00098     Bool isKey_p;
00099     const bool isMutable_p;
00100     VisBufferComponent2 vbComponent_p;
00101     VisBufferImpl2 * vb_p; // [use]
00102 
00103 };
00104 
00105 typedef std::vector<VbCacheItemBase *> CacheRegistry;
00106 
00107 template <typename T, Bool IsComputed = False>
00108 class VbCacheItem : public VbCacheItemBase {
00109 
00110     friend class VisBufferImpl2;
00111 
00112 public:
00113 
00114     typedef T DataType;
00115     typedef void (VisBufferImpl2::* Filler) (T &) const;
00116 
00117     VbCacheItem (bool isMutable = false)
00118     : VbCacheItemBase (isMutable), isPresent_p (False)
00119     {}
00120 
00121     virtual ~VbCacheItem () {}
00122 
00123     virtual void appendRows (Int, Bool)
00124     {
00125         // Noop for scalars
00126     }
00127 
00128     virtual void
00129     clear (Bool clearStatusOnly)
00130     {
00131         if (! clearStatusOnly) {
00132             clearValue (item_p);
00133         }
00134         setAsPresent (False);
00135         clearDirty ();
00136     }
00137 
00138     virtual void
00139     clearDirty ()
00140     {
00141         isDirty_p = False;
00142     }
00143 
00144     virtual void copyRowElement (Int /*sourceRow*/, Int /*destinationRow*/) {} // noop
00145 
00146 
00147     virtual void
00148     fill () const
00149     {
00150         const VisBufferImpl2 * vb = getVb();
00151 
00152         ThrowIf (! vb->isAttached (),
00153                  String::format ("Can't fill VisBuffer component %s: Not attached to VisibilityIterator",
00154                                  VisBufferComponents2::name (getComponent()).c_str()));
00155 
00156         ThrowIf (! IsComputed && ! vb->isFillable (),
00157                  String::format ("Cannot fill VisBuffer component %s: %s",
00158                                  VisBufferComponents2::name (getComponent()).c_str(),
00159                                  vb->getFillErrorMessage ().c_str()));
00160 
00161         (vb ->* filler_p) (item_p);
00162     }
00163 
00164     const T &
00165     get () const
00166     {
00167         if (! isPresent()){
00168             fill ();
00169             setAsPresent ();
00170             isDirty_p = False;
00171         }
00172 
00173         return item_p;
00174     }
00175 
00176     T &
00177     getRef (Bool fillIfAbsent = True)
00178     {
00179         if (! isPresent() && fillIfAbsent){
00180             fill ();
00181         }
00182         setAsPresent();
00183 
00184         // Caller is getting a modifiabled reference to the
00185         // datum (otherwise they would use "get"): assume
00186         // that it will be used to modify the datum and mark
00187         // it as dirty.
00188 
00189         isDirty_p = True;
00190 
00191         return item_p;
00192     }
00193 
00194     void
00195     initialize (VisBufferCache * cache, VisBufferImpl2 * vb, Filler filler,
00196                 VisBufferComponent2 component = VisBufferComponent2::Unknown,
00197                 Bool isKey = True)
00198     {
00199         VbCacheItemBase::initialize (cache, vb, component, isKey);
00200         filler_p = filler;
00201     }
00202 
00203     Bool isArray () const
00204     {
00205         return False;
00206     }
00207 
00208     Bool
00209     isDirty () const
00210     {
00211         return isDirty_p;
00212     }
00213 
00214     Bool
00215     isPresent () const
00216     {
00217         return isPresent_p;
00218     }
00219 
00220     virtual Bool
00221     isShapeOk () const
00222     {
00223         return True;
00224     }
00225 
00226     virtual void
00227     set (const T & newItem)
00228     {
00229         ThrowIf (! isMutable () && ! getVb()->isWritable (), "This VisBuffer is readonly");
00230 
00231         ThrowIf (isKey() && ! getVb()->isRekeyable (),
00232                  "This VisBuffer is does not allow row key values to be changed.");
00233 
00234         // Set operations to a rekeyable VB are allowed to change the shapes of the
00235         // values.  When T derives from Array, the assign method will use Array::assign
00236         // which resizes the destination value to match the source value.  For nonkeyable
00237         // VBs, the normal operator= method is used which for Arrays will throw an
00238         // exception when a shape incompatibility exists between the source and destination.
00239 
00240         if (isKey ()){
00241             assign (item_p, newItem);
00242         }
00243         else{
00244             item_p = newItem;
00245         }
00246 
00247         ThrowIf (! isShapeOk (), shapeErrorMessage() );
00248 
00249         setAsPresent();
00250         isDirty_p = True;
00251     }
00252 
00253 
00254     template <typename U>
00255     void
00256     set (const U & newItem)
00257     {
00258         ThrowIf (! isMutable () && ! getVb()->isWritable (), "This VisBuffer is readonly");
00259 
00260         ThrowIf (isKey () && ! getVb()->isRekeyable (),
00261                  "This VisBuffer is does not allow row key values to be changed.");
00262 
00263         item_p = newItem;
00264 
00265         ThrowIf (! isShapeOk (), shapeErrorMessage() );
00266 
00267         setAsPresent();
00268         isDirty_p = True;
00269     }
00270 
00271     template <typename U>
00272     void
00273     setSpecial (const U & newItem)
00274     {
00275         // For internal use for items which aren't really demand-fetched
00276 
00277         item_p = newItem;
00278         setAsPresent();
00279         isDirty_p = False;
00280     }
00281 
00282     virtual void
00283     setDirty ()
00284     {
00285         isDirty_p = True;
00286     }
00287 
00288     virtual String
00289     shapeErrorMessage () const
00290     {
00291         ThrowIf (True, "Scalar shapes should not have shape errors.");
00292 
00293         return String();
00294     }
00295 
00296 protected:
00297 
00298     void
00299     assign (T & lhs, const T & rhs)
00300     {
00301         lhs = rhs;
00302     }
00303 
00304     template <typename E>
00305     static void clearValue (Array <E> & value){
00306         value.resize();
00307     }
00308 
00309     static void clearValue (Int & value){
00310         value = 0;
00311     }
00312 
00313     static void clearValue (MDirection & value){
00314         value = MDirection ();
00315     }
00316 
00317 
00318 //    virtual void
00319 //    copy (const VbCacheItemBase * otherRaw, Bool markAsCached)
00320 //    {
00321 //        // Convert generic pointer to one pointint to this
00322 //        // cache item type.
00323 //
00324 //        const VbCacheItem * other = dynamic_cast <const VbCacheItem *> (otherRaw);
00325 //        Assert (other != 0);
00326 //
00327 //        // Capture the cached status of the other item
00328 //
00329 //        isPresent_p = other->isPresent_p;
00330 //
00331 //        // If the other item was cached then copy it over
00332 //        // otherwise clear out this item.
00333 //
00334 //        if (isPresent_p){
00335 //            item_p = other->item_p;
00336 //        }
00337 //        else {
00338 //            item_p = T ();
00339 //
00340 //            if (markAsCached){
00341 //                isPresent_p = True;
00342 //            }
00343 //        }
00344 //    }
00345 
00346     virtual void
00347     copy (const VbCacheItemBase * otherRaw, Bool fetchIfNeeded)
00348     {
00349         const VbCacheItem<T, IsComputed> * other =
00350             dynamic_cast <const VbCacheItem<T, IsComputed> *> (otherRaw);
00351         copyAux (other, fetchIfNeeded);
00352     }
00353 
00354     void
00355     copyAux (const VbCacheItem<T, IsComputed> * other, bool fetchIfNeeded)
00356     {
00357         if (other->isPresent()){
00358 
00359             item_p = other->item_p;
00360             setAsPresent ();
00361             isDirty_p = False;
00362         }
00363         else if (fetchIfNeeded){
00364             set (other->get());
00365         }
00366         else {
00367 
00368             setAsPresent (False);
00369             isDirty_p = False;
00370         }
00371     }
00372 
00373     T &
00374     getItem () const
00375     {
00376         return item_p;
00377     }
00378 
00379     void
00380     setAsPresent (Bool isPresent = True) const
00381     {
00382         isPresent_p = isPresent;
00383     }
00384 
00385 private:
00386 
00387     Filler       filler_p;
00388     mutable Bool isDirty_p;
00389     mutable Bool isPresent_p;
00390     mutable T    item_p;
00391 };
00392 
00393 template <typename T, Bool IsComputed = False>
00394 class VbCacheItemArray : public VbCacheItem<T, IsComputed> {
00395 public:
00396 
00397     typedef typename VbCacheItem<T>::Filler Filler;
00398     typedef typename T::IteratorSTL::value_type ElementType;
00399 
00400     VbCacheItemArray(bool isMutable = false)
00401     : VbCacheItem<T, IsComputed> (isMutable), capacity_p (0), shapePattern_p (NoCheck) {}
00402     virtual ~VbCacheItemArray () {}
00403 
00404     virtual void appendRows (Int nRows, Bool truncate)
00405     {
00406 
00407         // Only used when time averaging
00408 
00409         IPosition shape = this->getItem().shape();
00410         Int nDims = shape.size();
00411 
00412         if (nDims == 0 || shapePattern_p == NoCheck){
00413             // This item is empty or unfillable so leave it alone.
00414         }
00415         else if (truncate){
00416 
00417             // Make any excess rows disappear with a little hack to
00418             // avoid a copy.  This leaves the storage unchanged and merely
00419             // changes the associated bookkeeping values.
00420 
00421             AssertCc (nRows <= shape.last());
00422 
00423             shape.last() = nRows;
00424 
00425             this->getItem().adjustLastAxis (shape);
00426 
00427         }
00428         else{
00429 
00430             // The array needs to resized to hold nRows worth of data.  If the
00431             // shape of the existing array is the same as the existing one ignoring
00432             // the number of rows then we expect the array
00433 
00434             this->setAsPresent(); // This VB is being filled manually
00435             IPosition desiredShape = this->getVb()->getValidShape (shapePattern_p);
00436             IPosition currentShape = getShape();
00437 
00438             // Determine if the existing shape is the same as the desired shape
00439             // ignoring rows.  If is the same, then the existing data will need
00440             // to be copied in the event that the array needs to be resized
00441             // (i.e., reallocated).
00442 
00443             Bool shapeOk = True; // will ignore last dimension
00444             for (uInt i = 0; i < currentShape.nelements() - 1; i++){
00445                 shapeOk = shapeOk && desiredShape [i] == currentShape [i];
00446             }
00447 
00448             desiredShape.last() = nRows;
00449 
00450             if (shapeOk){
00451 
00452               // Only the number of rows differs from the current shape.  
00453               // This call will preserve any existing data.
00454 
00455               this->getItem().adjustLastAxis (desiredShape, 20);
00456             } 
00457             else {
00458 
00459               // Since the core shape is changing, the existing data is
00460               // not useful; this call will not preserve it.
00461 
00462               this->getItem().reformOrResize (desiredShape);
00463             }
00464         }
00465     }
00466 
00467     virtual void copyRowElement (Int sourceRow, Int destinationRow)
00468     {
00469         copyRowElementAux (this->getItem(), sourceRow, destinationRow);
00470     }
00471 
00472     virtual IPosition getShape() const
00473     {
00474         return this->getItem().shape();
00475     }
00476 
00477 
00478     void
00479     initialize (VisBufferCache * cache,
00480                 VisBufferImpl2 * vb,
00481                 Filler filler,
00482                 VisBufferComponent2 component,
00483                 ShapePattern shapePattern,
00484                 Bool isKey)
00485     {
00486         VbCacheItem<T, IsComputed>::initialize (cache, vb, filler, component, isKey);
00487         shapePattern_p = shapePattern;
00488     }
00489 
00490 
00491     virtual Bool
00492     isShapeOk () const
00493     {
00494         // Check to see if the shape of this data item is consistent
00495         // with the expected shape.
00496 
00497         Bool result = shapePattern_p == NoCheck ||
00498                       this->getItem().shape() == this->getVb()->getValidShape (shapePattern_p);
00499 
00500         return result;
00501     }
00502 
00503 
00504     Bool isArray () const
00505     {
00506         return True;
00507     }
00508 
00509     void
00510     resize (Bool copyValues)
00511     {
00512         if (shapePattern_p != NoCheck){
00513 
00514             IPosition desiredShape = this->getVb()->getValidShape (shapePattern_p);
00515 
00516             this->getItem().resize (desiredShape, copyValues);
00517             capacity_p = desiredShape.last();
00518 
00519             if (! copyValues){
00520                 this->getItem() = typename T::value_type();
00521             }
00522 
00523         }
00524     }
00525 
00526     void
00527     resizeRows (Int newNRows)
00528     {
00529         IPosition shape = this->getItem().shape();
00530 
00531         if (shapePattern_p != NoCheck){
00532 
00533             // Change the last dimension to be the new number of rows,
00534             // then resize, copying values.
00535 
00536             shape.last() = newNRows;
00537 
00538             this->getItem().resize (shape, True);
00539 
00540             this->setDirty();
00541         }
00542     }
00543 
00544     virtual void
00545     set (const T & newItem)
00546     {
00547         ThrowIf (! this->isMutable() && ! this->getVb()->isWritable (), "This VisBuffer is readonly");
00548 
00549         ThrowIf (this->isKey() && ! this->getVb()->isRekeyable (),
00550                  "This VisBuffer is does not allow row key values to be changed.");
00551 
00552         // Now check for a conformant shape.
00553 
00554         IPosition itemShape = newItem.shape();
00555         Bool parameterShapeOk = shapePattern_p == NoCheck ||
00556                                 itemShape == this->getVb()->getValidShape (shapePattern_p);
00557         ThrowIf (! parameterShapeOk,
00558                  "Invalid parameter shape:: " + shapeErrorMessage (& itemShape));
00559 
00560         VbCacheItem<T,IsComputed>::set (newItem);
00561     }
00562 
00563     template <typename U>
00564     void
00565     set (const U & newItem)
00566     {
00567         if (! this->isPresent()){ // Not present so give it a shape
00568             set (T (this->getVb()->getValidShape (shapePattern_p)));
00569         }
00570 
00571         VbCacheItem<T,IsComputed>::set (newItem);
00572     }
00573 
00574     virtual String
00575     shapeErrorMessage (const IPosition * badShape = 0) const
00576     {
00577 
00578         ThrowIf (shapePattern_p == NoCheck,
00579                  "No shape error message for NoCheck type array");
00580 
00581         ThrowIf (isShapeOk () && badShape == 0,
00582                  "Shape is OK so no error message.");
00583 
00584         String badShapeString = (badShape != 0) ? badShape->toString()
00585                                                 : this->getItem().shape().toString();
00586 
00587         ostringstream os;
00588 
00589         os << "VisBuffer::ShapeError: "
00590            << VisBufferComponents2::name (this->getComponent())
00591            << " should have shape "
00592            << this->getVb()->getValidShape(shapePattern_p).toString()
00593            << " but had shape "
00594            << badShapeString;
00595 
00596         return os.str();
00597     }
00598 
00599 protected:
00600 
00601     void
00602     assign (T & dst, const T & src)
00603     {
00604         dst.assign (src);
00605     }
00606 
00607     static void
00608     copyRowElementAux (Cube<typename T::value_type> & cube, Int sourceRow, Int destinationRow)
00609     {
00610         IPosition shape = cube.shape();
00611         Int nI = shape(1);
00612         Int nJ = shape(0);
00613 
00614         for (Int i = 0; i < nI; i++){
00615             for (Int j = 0; j < nJ; j++){
00616                 cube (j, i, destinationRow) = cube (j, i, sourceRow);
00617             }
00618         }
00619     }
00620 
00621     static void
00622     copyRowElementAux (Matrix<typename T::value_type> & matrix, Int sourceRow, Int destinationRow)
00623     {
00624         IPosition shape = matrix.shape();
00625         Int nJ = shape(0);
00626 
00627         for (Int j = 0; j < nJ; j++){
00628             matrix (j, destinationRow) = matrix (j, sourceRow);
00629         }
00630     }
00631 
00632     static void
00633     copyRowElementAux (Array<typename T::value_type> & array, Int sourceRow, Int destinationRow)
00634     {
00635         IPosition shape = array.shape();
00636         AssertCc (shape.nelements() == 4);
00637 
00638         Int nH = shape(2);
00639         Int nI = shape(1);
00640         Int nJ = shape(0);
00641 
00642         for (Int h = 0; h < nH; h++){
00643             for (Int i = 0; i < nI; i++){
00644                 for (Int j = 0; j < nJ; j++){
00645                     array (IPosition (4, j, i, h, destinationRow)) =
00646                         array (IPosition (4, j, i, h, sourceRow));
00647                 }
00648             }
00649         }
00650     }
00651 
00652     static void
00653     copyRowElementAux (Vector<typename T::value_type> & vector, Int sourceRow, Int destinationRow)
00654     {
00655         vector (destinationRow) = vector (sourceRow);
00656     }
00657 
00658 private:
00659 
00660     Int capacity_p;
00661     ShapePattern shapePattern_p;
00662 };
00663 
00664 class VisBufferCache {
00665 
00666     // Holds the cached values for a VisBuffer object.
00667 
00668 public:
00669 
00670     VisBufferCache (VisBufferImpl2 * vb);
00671 
00672     void appendComplete ();
00673     Int appendRow ();
00674     void initialize (VisBufferImpl2 * vb);
00675     void registerItem (VbCacheItemBase * item);
00676 
00677     // The values that are potentially cached.
00678 
00679     VbCacheItemArray <Vector<Int> > antenna1_p;
00680     VbCacheItemArray <Vector<Int> > antenna2_p;
00681     VbCacheItemArray <Vector<Int> > arrayId_p;
00682     VbCacheItemArray <Vector<SquareMatrix<Complex, 2> >, True> cjones_p;
00683     VbCacheItemArray <Cube<Complex> > correctedVisCube_p;
00684 //    VbCacheItemArray <Matrix<CStokesVector> > correctedVisibility_p;
00685     VbCacheItemArray <Vector<Int> > corrType_p;
00686     VbCacheItem <Int> dataDescriptionId_p;
00687     VbCacheItemArray <Vector<Int> > dataDescriptionIds_p;
00688     VbCacheItemArray <Vector<MDirection> > direction1_p; //where the first antenna/feed is pointed to
00689     VbCacheItemArray <Vector<MDirection> > direction2_p; //where the second antenna/feed is pointed to
00690     VbCacheItemArray <Vector<Double> > exposure_p;
00691     VbCacheItemArray <Vector<Int> > feed1_p;
00692     VbCacheItemArray <Vector<Float> > feed1Pa_p;
00693     VbCacheItemArray <Vector<Int> > feed2_p;
00694     VbCacheItemArray <Vector<Float> > feed2Pa_p;
00695     VbCacheItemArray <Vector<Int> > fieldId_p;
00696 //    VbCacheItemArray <Matrix<Bool> > flag_p;
00697     VbCacheItemArray <Array<Bool> > flagCategory_p;
00698     VbCacheItemArray <Cube<Bool> > flagCube_p;
00699     VbCacheItemArray <Vector<Bool> > flagRow_p;
00700     VbCacheItemArray <Cube<Float> > floatDataCube_p;
00701     VbCacheItemArray <Matrix<Float> > imagingWeight_p;
00702     VbCacheItemArray <Cube<Complex> > modelVisCube_p;
00703 //    VbCacheItemArray <Matrix<CStokesVector> > modelVisibility_p;
00704     VbCacheItem <Int> nAntennas_p;
00705     VbCacheItem <Int> nChannels_p;
00706     VbCacheItem <Int> nCorrelations_p;
00707     VbCacheItem <Int> nRows_p;
00708     VbCacheItemArray <Vector<Int> > observationId_p;
00709     VbCacheItem <MDirection> phaseCenter_p;
00710     VbCacheItem <Int> polFrame_p;
00711     VbCacheItem <Int> polarizationId_p;
00712     VbCacheItemArray <Vector<Int> > processorId_p;
00713     VbCacheItemArray <Vector<uInt> > rowIds_p;
00714     VbCacheItemArray <Vector<Int> > scan_p;
00715     VbCacheItemArray <Matrix<Float> > sigma_p;
00716     //VbCacheItemArray <Matrix<Float> > sigmaMat_p;
00717     VbCacheItemArray <Vector<Int> > spectralWindows_p;
00718     VbCacheItemArray <Vector<Int> > stateId_p;
00719     VbCacheItemArray <Vector<Double> > time_p;
00720     VbCacheItemArray <Vector<Double> > timeCentroid_p;
00721     VbCacheItemArray <Vector<Double> > timeInterval_p;
00722     VbCacheItemArray <Matrix<Double> > uvw_p;
00723     VbCacheItemArray <Cube<Complex> > visCube_p;
00724 //    VbCacheItemArray <Matrix<CStokesVector> > visibility_p;
00725     VbCacheItemArray <Matrix<Float> > weight_p;
00726     //VbCacheItemArray <Matrix<Float> > weightMat_p;
00727     VbCacheItemArray <Cube<Float> > weightSpectrum_p;
00728     VbCacheItemArray <Cube<Float> > sigmaSpectrum_p;
00729 
00730     CacheRegistry registry_p;
00731 
00732     template <typename T, typename U>
00733     static void
00734     sortCorrelationItem (vi::VbCacheItem<T> & dataItem, IPosition & blc, IPosition & trc,
00735                          IPosition & mat, U & tmp, Bool sort)
00736     {
00737 
00738         T & data = dataItem.getRef ();
00739         U p1, p2, p3;
00740 
00741         if (dataItem.isPresent() && data.nelements() > 0) {
00742 
00743             blc(0) = trc(0) = 1;
00744             p1.reference(data (blc, trc).reform(mat));
00745 
00746             blc(0) = trc(0) = 2;
00747             p2.reference(data (blc, trc).reform(mat));
00748 
00749             blc(0) = trc(0) = 3;
00750             p3.reference(data (blc, trc).reform(mat));
00751 
00752             if (sort){ // Sort correlations: (PP,QQ,PQ,QP) -> (PP,PQ,QP,QQ)
00753 
00754                 tmp = p1;
00755                 p1 = p2;
00756                 p2 = p3;
00757                 p3 = tmp;
00758             }
00759             else {      // Unsort correlations: (PP,PQ,QP,QQ) -> (PP,QQ,PQ,QP)
00760 
00761                 tmp = p3;
00762                 p3 = p2;
00763                 p2 = p1;
00764                 p1 = tmp;
00765             }
00766         }
00767     }
00768 };
00769 
00770 class VisBufferState {
00771 
00772 public:
00773 
00774     template<typename T>
00775     class FrequencyCache {
00776     public:
00777 
00778         typedef Vector<T> (ViImplementation2::* Updater) (Double, Int, Int, Int) const;
00779 
00780         FrequencyCache (Updater updater)
00781         : frame_p (-1),
00782           msId_p (-1),
00783           spectralWindowId_p (-1),
00784           time_p (-1),
00785           updater_p (updater)
00786         {}
00787 
00788         Int frame_p;
00789         Int msId_p;
00790         Int spectralWindowId_p;
00791         Double time_p;
00792         Updater updater_p;
00793         Vector<T> values_p;
00794 
00795         void
00796         flush ()
00797         {
00798             time_p = -1; // will be enough to cause a reload
00799         }
00800 
00801         void
00802         updateCacheIfNeeded (const ViImplementation2 * rovi,
00803                              Int rowInBuffer,
00804                              Int frame,
00805                              const VisBufferImpl2 * vb)
00806         {
00807             Int msId = vb->msId();
00808             Int spectralWindowId = vb->spectralWindows()(rowInBuffer);
00809             Double time = vb->time()(rowInBuffer);
00810 
00811             if (time == time_p && frame == frame_p && msId == msId_p &&
00812                 spectralWindowId == spectralWindowId_p){
00813                 return;
00814             }
00815 
00816             time_p = time;
00817             frame_p = frame;
00818             msId_p = msId;
00819             spectralWindowId_p = spectralWindowId;
00820 
00821             values_p.assign ((rovi ->* updater_p) (time_p, frame_p, spectralWindowId_p, msId_p));
00822         }
00823     };
00824 
00825     VisBufferState ()
00826     : appendCapacity_p (0),
00827       appendSize_p (0),
00828       areCorrelationsSorted_p (False),
00829       channelNumbers_p (& ViImplementation2::getChannels),
00830       dirtyComponents_p (),
00831       frequencies_p (& ViImplementation2::getFrequencies),
00832       isAttached_p (False),
00833       isFillable_p (False),
00834       isNewMs_p (False),
00835       isNewArrayId_p (False),
00836       isNewFieldId_p (False),
00837       isNewSpectralWindow_p (False),
00838       isRekeyable_p (False),
00839       isWritable_p (False),
00840       msId_p (-1),
00841       pointingTableLastRow_p (-1),
00842       validShapes_p (N_ShapePatterns),
00843       vi_p (0),
00844       visModelData_p (0),
00845       weightScaling_p ( )
00846     {}
00847 
00848     Int appendCapacity_p;
00849     Int appendSize_p;
00850     Bool areCorrelationsSorted_p; // Have correlations been sorted by sortCorr?
00851     FrequencyCache<Int> channelNumbers_p;
00852     Vector<Int> correlations_p;
00853     Vector<Stokes::StokesTypes> correlationsDefined_p;
00854     Vector<Stokes::StokesTypes> correlationsSelected_p;
00855     VisBufferComponents2 dirtyComponents_p;
00856     FrequencyCache<Double> frequencies_p;
00857     Bool isAttached_p;
00858     Bool isFillable_p;
00859     Bool isNewMs_p;
00860     Bool isNewArrayId_p;
00861     Bool isNewFieldId_p;
00862     Bool isNewSpectralWindow_p;
00863     Bool isRekeyable_p;
00864     Bool isWritable_p;
00865     Int msId_p;
00866     String msName_p;
00867     Bool newMs_p;
00868     mutable Int pointingTableLastRow_p;
00869     Subchunk subchunk_p;
00870     Vector<IPosition> validShapes_p;
00871     ViImplementation2 * vi_p; // [use]
00872     mutable VisModelDataI * visModelData_p;
00873     CountedPtr <WeightScaling> weightScaling_p;
00874 };
00875 
00876 
00877 }
00878 
00879 }
00880 
00881 
00882 #endif /* VISBUFFERIMPL2INTERNAL_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1