This file is indexed.

/usr/include/casacore/lattices/Lattices/SubLattice.tcc is in casacore-dev 2.2.0-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//# SubLattice.cc: A subset of a Lattice
//# Copyright (C) 1997,1998,1999,2000,2001,2003
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//#        Internet email: aips2-request@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA
//#
//# $Id: SubLattice.tcc 21563 2015-02-16 07:05:15Z gervandiepen $

#ifndef LATTICES_SUBLATTICE_TCC
#define LATTICES_SUBLATTICE_TCC

#include <casacore/lattices/Lattices/SubLattice.h>
#include <casacore/lattices/Lattices/LatticeIterInterface.h>
#include <casacore/lattices/LEL/LatticeExpr.h>
#include <casacore/lattices/LRegions/LCRegion.h>
#include <casacore/casa/Arrays/IPosition.h>
#include <casacore/casa/Utilities/Assert.h>
#include <casacore/casa/Exceptions/Error.h>


namespace casacore { //# NAMESPACE CASACORE - BEGIN

template<class T>
SubLattice<T>::SubLattice()
: itsLatticePtr   (0),
  itsMaskLatPtr   (0),
  itsWritable     (False),
  itsHasLattPMask (False),
  itsPixelMask    (0),
  itsOwnPixelMask (0)
{}

template<class T>
SubLattice<T>::SubLattice (const Lattice<T>& lattice,
			   AxesSpecifier axesSpec)
{
  setPtr (lattice.clone(), 0, False);
  setRegion();
  setAxesMap (axesSpec);
}

template<class T>
SubLattice<T>::SubLattice (Lattice<T>& lattice,
			   Bool writableIfPossible,
			   AxesSpecifier axesSpec)
{
  setPtr (lattice.clone(), 0, writableIfPossible);
  setRegion();
  setAxesMap (axesSpec);
}

template<class T>
SubLattice<T>::SubLattice (const MaskedLattice<T>& lattice,
			   AxesSpecifier axesSpec)
{
  setPtr (0, lattice.cloneML(), False);
  setRegion();
  setAxesMap (axesSpec);
}

template<class T>
SubLattice<T>::SubLattice (MaskedLattice<T>& lattice,
			   Bool writableIfPossible,
			   AxesSpecifier axesSpec)
{
  setPtr (0, lattice.cloneML(), writableIfPossible);
  setRegion();
  setAxesMap (axesSpec);
}

template<class T>
SubLattice<T>::SubLattice (const Lattice<T>& lattice,
			   const LatticeRegion& region,
			   AxesSpecifier axesSpec)
{
  setPtr (lattice.clone(), 0, False);
  setRegion (region);
  setAxesMap (axesSpec);
}

template<class T>
SubLattice<T>::SubLattice (Lattice<T>& lattice,
			   const LatticeRegion& region,
			   Bool writableIfPossible,
			   AxesSpecifier axesSpec)
{
  setPtr (lattice.clone(), 0, writableIfPossible);
  setRegion (region);
  setAxesMap (axesSpec);
}

template<class T>
SubLattice<T>::SubLattice (const MaskedLattice<T>& lattice,
			   const LatticeRegion& region,
			   AxesSpecifier axesSpec)
{
  setPtr (0, lattice.cloneML(), False);
  setRegion (region);
  setAxesMap (axesSpec);
}

template<class T>
SubLattice<T>::SubLattice (MaskedLattice<T>& lattice,
			   const LatticeRegion& region,
			   Bool writableIfPossible,
			   AxesSpecifier axesSpec)
{
  setPtr (0, lattice.cloneML(), writableIfPossible);
  setRegion (region);
  setAxesMap (axesSpec);
}

template<class T>
SubLattice<T>::SubLattice (const Lattice<T>& lattice,
			   const Slicer& slicer,
			   AxesSpecifier axesSpec)
{
  setPtr (lattice.clone(), 0, False);
  setRegion (slicer);
  setAxesMap (axesSpec);
}

template<class T>
SubLattice<T>::SubLattice (Lattice<T>& lattice,
			   const Slicer& slicer,
			   Bool writableIfPossible,
			   AxesSpecifier axesSpec)
{
  setPtr (lattice.clone(), 0, writableIfPossible);
  setRegion (slicer);
  setAxesMap (axesSpec);
}

template<class T>
SubLattice<T>::SubLattice (const MaskedLattice<T>& lattice,
			   const Slicer& slicer,
			   AxesSpecifier axesSpec)
{
  setPtr (0, lattice.cloneML(), False);
  setRegion (slicer);
  setAxesMap (axesSpec);
}

template<class T>
SubLattice<T>::SubLattice (MaskedLattice<T>& lattice,
			   const Slicer& slicer,
			   Bool writableIfPossible,
			   AxesSpecifier axesSpec)
{
  setPtr (0, lattice.cloneML(), writableIfPossible);
  setRegion (slicer);
  setAxesMap (axesSpec);
}

template<class T>
SubLattice<T>::SubLattice (const SubLattice<T>& other)
: MaskedLattice<T>(),
  itsLatticePtr   (0),
  itsMaskLatPtr   (0),
  itsPixelMask    (0),
  itsOwnPixelMask (0)
{
  operator= (other);
}

template<class T>
SubLattice<T>::~SubLattice()
{
  // Note that itsMaskLatPtr (if filled in) always points to the same
  // object as itsLatticePtr, so it does not need to be deleted.
  delete itsLatticePtr;
  delete itsPixelMask;
  delete itsOwnPixelMask;
}

template<class T>
SubLattice<T>& SubLattice<T>::operator= (const SubLattice<T>& other)
{
  if (this != &other) {
    itsRegion = other.itsRegion;
    delete itsLatticePtr;
    itsLatticePtr = other.itsLatticePtr;
    itsMaskLatPtr = other.itsMaskLatPtr;
    if (itsMaskLatPtr != 0) {
      itsMaskLatPtr = itsMaskLatPtr->cloneML();
      itsLatticePtr = itsMaskLatPtr;
    } else if (itsLatticePtr != 0) {
      itsLatticePtr = itsLatticePtr->clone();
    }
    itsWritable = other.itsWritable;
    delete itsPixelMask;
    itsPixelMask = 0;
    delete itsOwnPixelMask;
    itsOwnPixelMask = 0;
    if (other.itsOwnPixelMask != 0) {
      itsOwnPixelMask = other.itsOwnPixelMask->clone();
    }
    itsHasLattPMask = other.itsHasLattPMask;
    itsAxesMap = other.itsAxesMap;
  }
  return *this;
}

template<class T>
MaskedLattice<T>* SubLattice<T>::cloneML() const
{
  return new SubLattice<T> (*this);
}

template<class T>
void SubLattice<T>::setPtr (Lattice<T>* latticePtr,
			    MaskedLattice<T>* maskLatPtr,
			    Bool writableIfPossible)
{
  itsHasLattPMask = False;
  itsPixelMask    = 0;
  itsOwnPixelMask = 0;
  if (maskLatPtr == 0) {
    itsLatticePtr = latticePtr;
    itsMaskLatPtr = 0;
  } else {
    itsLatticePtr = maskLatPtr;
    if (! maskLatPtr->isMasked()) {
      itsMaskLatPtr = 0;
    } else {
      itsMaskLatPtr = maskLatPtr;
      itsHasLattPMask = itsMaskLatPtr->hasPixelMask();
    }
  }
  itsWritable = False;
  if (writableIfPossible  &&  itsLatticePtr->isWritable()) {
    itsWritable = True;
  }
}

template<class T>
void SubLattice<T>::setRegion (const LatticeRegion& region)
{
    ThrowIf(
        ! (itsLatticePtr->shape().isEqual(region.region().latticeShape())),
        "shape of lattice " + itsLatticePtr->shape().toString()
        + " mismatches lattice shape in region " + region.region().latticeShape().toString()
    );
  itsRegion = region;
}
template<class T>
void SubLattice<T>::setRegion (const Slicer& slicer)
{
  setRegion (LatticeRegion (slicer, itsLatticePtr->shape()));
}
template<class T>
void SubLattice<T>::setRegion()
{
  IPosition shape = itsLatticePtr->shape();
  setRegion (LatticeRegion (Slicer(IPosition(shape.nelements(),0), shape),
			    shape));
}

template<class T>
void SubLattice<T>::setAxesMap (const AxesSpecifier& axesSpec)
{
  itsAxesMap = axesSpec.apply (itsRegion.slicer().length());
  if (itsAxesMap.isReordered()) {
    throw AipsError ("SubLattice does not support axes reordering");
  }
  itsAxesSpec = axesSpec;
}

template<class T>
Bool SubLattice<T>::isMasked() const
{
  return  (itsMaskLatPtr != 0  ||  itsRegion.hasMask()
	   ||  itsOwnPixelMask != 0);
}

template<class T>
Bool SubLattice<T>::isPersistent() const
{
  return itsLatticePtr->isPersistent()
     &&  !isMasked()
     &&  !itsAxesMap.isRemoved()
     &&  shape().isEqual (itsLatticePtr->shape());
}

template<class T>
Bool SubLattice<T>::isPaged() const
{
  return itsLatticePtr->isPaged();
}

template<class T>
Bool SubLattice<T>::canReferenceArray() const
{
  return itsLatticePtr->canReferenceArray();
}

template<class T>
Bool SubLattice<T>::isWritable() const
{
  return itsWritable;
}

template<class T>
Bool SubLattice<T>::lock (FileLocker::LockType type, uInt nattempts)
{
  return itsLatticePtr->lock (type, nattempts);
}
template<class T>
void SubLattice<T>::unlock()
{
  itsLatticePtr->unlock();
}
template<class T>
Bool SubLattice<T>::hasLock (FileLocker::LockType type) const
{
  return itsLatticePtr->hasLock (type);
}
template<class T>
void SubLattice<T>::resync()
{
  itsLatticePtr->resync();
}

template<class T>
void SubLattice<T>::flush()
{
  itsLatticePtr->flush();
}

template<class T>
void SubLattice<T>::tempClose()
{
  itsLatticePtr->tempClose();
}

template<class T>
void SubLattice<T>::reopen()
{
  itsLatticePtr->reopen();
}

template<class T>
Bool SubLattice<T>::hasPixelMask() const
{
  return itsHasLattPMask  ||  itsOwnPixelMask != 0;
}

template<class T>
const Lattice<Bool>& SubLattice<T>::pixelMask() const
{
  return ((const SubLattice<T>*)this)->pixelMask();
}
template<class T>
Lattice<Bool>& SubLattice<T>::pixelMask()
{
  if (itsPixelMask == 0) {
    if (!hasPixelMask()) {
      throw (AipsError ("SubLattice::pixelMask - no pixelmask available"));
    }
    if (itsHasLattPMask) {
      // Construct the pixelmask (as a subset of the parent pixelmask).
      Lattice<Bool>& fullMask = itsMaskLatPtr->pixelMask();
      itsPixelMask = new SubLattice<Bool> (fullMask, itsRegion, itsWritable,
					   itsAxesSpec);
      // If there is an own pixelmask, and them.
      if (itsOwnPixelMask != 0) {
	Lattice<Bool>* pmask = itsPixelMask;
	itsPixelMask = new LatticeExpr<Bool> (*pmask && *itsOwnPixelMask);
	delete pmask;
      }
    } else {
      itsPixelMask = itsOwnPixelMask->clone();
    }
  }
  return *itsPixelMask;
}

template<class T>
void SubLattice<T>::setPixelMask (const Lattice<Bool>& pixelMask,
				  Bool mayExist)
{
  if (!mayExist  &&  itsHasLattPMask) {
    throw (AipsError ("SubLattice::setPixelMask - "
		      "underlying lattice has a pixelmask already"));
  }
  if (!(shape().isEqual(pixelMask.shape()))) {
    throw (AipsError ("SubLattice::setPixelMask - "
		      "shape of pixel mask mismatches sublattice"));
  }
  delete itsPixelMask;
  itsPixelMask = 0;
  delete itsOwnPixelMask;
  itsOwnPixelMask = 0;
  itsOwnPixelMask = pixelMask.clone();
}

template<class T>
const LatticeRegion* SubLattice<T>::getRegionPtr() const
{
  return &itsRegion;
}

template<class T>
IPosition SubLattice<T>::shape() const
{
  return itsAxesMap.shapeToNew (itsRegion.slicer().length());
}

template<class T>
String SubLattice<T>::name (Bool stripPath) const
{
  return itsLatticePtr->name(stripPath);
}

template<class T>
Bool SubLattice<T>::doGetSlice (Array<T>& buffer,
				const Slicer& section)
{
  if (! itsAxesMap.isRemoved()) {
    return itsLatticePtr->getSlice (buffer, itsRegion.convert (section));
  }
  // Axes have been removed, so we have to reform the array buffer
  // to be able to get data from the original lattice.
  // Get the section shape in the original lattice.
  Slicer latSect = itsRegion.convert (itsAxesMap.slicerToOld (section));
  Array<T> tmp;
  Bool reformed = False;
  if (buffer.shape().isEqual (section.length())) {
    // Use (in principle) the same buffer storage if its shape is correct.
    // This is needed for LatticeIterator to work correctly, because it
    // expects to get the data in the buffer it provides
    // (unless ref=True is returned).
    Array<T> tmp2 = buffer.reform (latSect.length());
    tmp.reference (tmp2);
    reformed = True;
  }
  Bool ref = itsLatticePtr->getSlice (tmp, latSect);
  // Reform (i.e. remove axes) if the buffer did not have the correct shape
  // or if the lattice data are referenced.
  if (!reformed  ||  ref) {
    Array<T> tmp2 = tmp.reform (section.length());
    buffer.reference (tmp2);
  }
  return ref;
}

template<class T>
void SubLattice<T>::doPutSlice (const Array<T>& sourceBuffer,
				const IPosition& where, 
				const IPosition& stride)
{
  if (!itsWritable) {
      throw (AipsError ("SubLattice::putSlice - non-writable lattice"));
  }
  if (! itsAxesMap.isRemoved()) {
    itsLatticePtr->putSlice (sourceBuffer, itsRegion.convert (where),
			     stride * itsRegion.slicer().stride());
  } else {
    Array<T> tmp = sourceBuffer.reform
                            (itsAxesMap.shapeToOld (sourceBuffer.shape()));
    itsLatticePtr->putSlice (tmp, 
			     itsRegion.convert (itsAxesMap.posToOld (where)),
			     itsAxesMap.shapeToOld (stride) *
                                                itsRegion.slicer().stride());
  }
}


template<class T>
uInt SubLattice<T>::advisedMaxPixels() const
{
  return itsLatticePtr->advisedMaxPixels();
}

template<class T>
IPosition SubLattice<T>::doNiceCursorShape (uInt maxPixels) const
{
  IPosition cursorShape (itsLatticePtr->niceCursorShape (maxPixels));
  const IPosition& shape = itsRegion.slicer().length();
  for (uInt i=0; i<shape.nelements(); i++) {
    if (cursorShape(i) > shape(i)) {
      cursorShape(i) = shape(i);
    }
  }
  return itsAxesMap.shapeToNew (cursorShape);
}
template<class T>
T SubLattice<T>::getAt (const IPosition& where) const
{
	return itsLatticePtr->getAt (positionInParent(where));
	/*
  if (! itsAxesMap.isRemoved()) {
    return itsLatticePtr->getAt (itsRegion.convert (where));
  }
  return itsLatticePtr->getAt (itsRegion.convert(itsAxesMap.posToOld (where)));
  */
}

template<class T>
void SubLattice<T>::putAt (const T& value, const IPosition& where)
{
	ThrowIf(! itsWritable, "SubLattice::putAt - non-writable lattice");
	itsLatticePtr->putAt (value, positionInParent(where));
  /*
  if (! itsAxesMap.isRemoved()) {
    itsLatticePtr->putAt (value, itsRegion.convert (where));
  } else {
    itsLatticePtr->putAt (value, itsRegion.convert (itsAxesMap.posToOld
						                 (where)));
  }
  */
}

template<class T>
Bool SubLattice<T>::doGetMaskSlice (Array<Bool>& buffer,
				    const Slicer& section)
{
  // If the lattice has no mask, we can return the region and/or pixel mask.
  if (itsMaskLatPtr == 0) {
    if (itsOwnPixelMask == 0) {
      // Note that if the region has no mask, it will return all True.
      return getRegionDataSlice (buffer, section);
    }
    if (! itsRegion.hasMask()) {
      return itsOwnPixelMask->getSlice (buffer, section);
    }
    // Return AND of region and pixel mask.
    Bool ref = getRegionDataSlice (buffer, section);
    andMask (buffer, ref, itsOwnPixelMask->getSlice (section));
    return False;
  }
  // The lattice has a mask.
  // If there are no other masks, we can return the lattice's mask.
  if (! itsRegion.hasMask()) {
    if (itsOwnPixelMask == 0) {
      return getMaskDataSlice (buffer, section);
    }
    // Return AND of lattice and pixel mask.
    Bool ref = getMaskDataSlice (buffer, section);
    andMask (buffer, ref, itsOwnPixelMask->getSlice (section));
    return False;
  }
  // Lattice and region have a mask, so they have to be ANDed.
  Bool ref = getMaskDataSlice (buffer, section);
  Array<Bool> tmpbuf;
  getRegionDataSlice (tmpbuf, section);
  andMask (buffer, ref, tmpbuf);
  if (itsOwnPixelMask != 0) {
    andMask (buffer, False, itsOwnPixelMask->getSlice (section));
  }
  return False;
}

template<class T>
void SubLattice<T>::andMask (Array<Bool>& buffer, Bool ref,
			     const Array<Bool>& tmpbuf) const
{
  // Make a copy if the array is referenced.
  if (ref) {
    Array<Bool> mask;
    mask = buffer;
    buffer.reference (mask);
  }
  // And the masks.
  Bool deleteBuf, deleteTmp;
  const Bool* tmpptr = tmpbuf.getStorage (deleteTmp);
  Bool* bufptr = buffer.getStorage (deleteBuf);
  uInt n = buffer.nelements();
  for (uInt i=0; i<n; i++) {
    if (!tmpptr[i]) {
      bufptr[i] = tmpptr[i];
    }
  }
  tmpbuf.freeStorage (tmpptr, deleteTmp);
  buffer.putStorage (bufptr, deleteBuf);
}


template<class T>
Bool SubLattice<T>::getRegionDataSlice (Array<Bool>& buffer,
					const Slicer& section)
{
  if (! itsAxesMap.isRemoved()) {
    return itsRegion.getSlice (buffer, section);
  }
  Bool ref = itsRegion.getSlice (buffer, itsAxesMap.slicerToOld (section));
  Array<Bool> tmp = buffer.reform (section.length());
  buffer.reference (tmp);
  return ref;
}

template<class T>
Bool SubLattice<T>::getMaskDataSlice (Array<Bool>& buffer,
				      const Slicer& section)
{
  if (! itsAxesMap.isRemoved()) {
    return itsMaskLatPtr->doGetMaskSlice (buffer, itsRegion.convert (section));
  }
  Bool ref = itsMaskLatPtr->doGetMaskSlice (buffer, itsRegion.convert
                                          (itsAxesMap.slicerToOld (section)));
  Array<Bool> tmp = buffer.reform (section.length());
  buffer.reference (tmp);
  return ref;
}


template <class T>
Bool SubLattice<T>::ok() const
{
  return itsLatticePtr->ok();
}

template<class T>
LatticeIterInterface<T>* SubLattice<T>::makeIter (const LatticeNavigator& nav,
						  Bool useRef) const
{
  return new LatticeIterInterface<T> (*this, nav, useRef);
  // Make a clone of the navigator to be able to apply our region.
///  LatticeNavigator* navPtr = navigator.clone();
///  const Slicer& section = itsRegionPtr->box();
///  navPtr->subSection (section.start(), section.end(), section.stride());
///  delete navPtr;
///  return iterPtr;
}

} //# NAMESPACE CASACORE - END


#endif