2#ifndef AFW_TABLE_Catalog_h_INCLUDED
3#define AFW_TABLE_Catalog_h_INCLUDED
8#include "boost/iterator/iterator_adaptor.hpp"
9#include "boost/iterator/transform_iterator.hpp"
38template <
typename BaseT>
39class CatalogIterator :
public boost::iterator_adaptor<CatalogIterator<BaseT>, BaseT,
40 typename BaseT::value_type::element_type> {
44 template <
typename OtherBaseT>
50 template <
typename RecordT>
55 template <
typename RecordT>
57 *this->
base() = other;
62 friend class boost::iterator_core_access;
63 typename BaseT::value_type::element_type& dereference()
const {
return **this->
base(); }
97template <
typename RecordT>
103 using Table =
typename Record::Table;
125 : _table(
table), _internal() {
127 auto schema=Table::makeMinimalSchema();
128 _table=Table::make(schema);
145 template <
typename InputIterator>
147 : _table(
table), _internal() {
164 template <
typename OtherRecordT>
170 if (&other !=
this) {
171 _table = other._table;
172 _internal = other._internal;
188 (boost::format(
"Mask array with %d elements applied to catalog with %d elements") %
189 mask.size() %
size())
193 ndarray::Array<bool const, 1>::Iterator maskIter = mask.begin();
195 for (; maskIter != mask.end(); ++maskIter, ++catIter) {
196 if (*maskIter) result.push_back(catIter);
258 }
else if (step < 0) {
266 if (((step > 0) && (start >= stop)) || ((step < 0) && ((
std::ptrdiff_t)start <= stopd))) {
284 N = (stop - start) / step + (((stop - start) % step) ? 1 : 0);
291 for (
size_type i = start; i < stop; i += step) {
385 "Cannot get a column view from a CatalogT<RecordT const> (as column views are always "
386 "non-const views).");
388 return ColumnView::make(_table,
begin(),
end());
409 bool empty()
const {
return _internal.empty(); }
433 if (n <= _internal.size())
return;
434 _table->preallocate(n - _internal.size());
435 _internal.reserve(n);
443 _table->preallocate(n - old);
445 _internal[i] = _table->makeRecord();
473 template <
typename InputIterator>
474 void assign(InputIterator
first, InputIterator last,
bool deep =
false) {
474 void assign(InputIterator
first, InputIterator last,
bool deep =
false) {
…}
482 _internal.push_back(p);
491 _internal.push_back(r);
516 template <
typename InputIterator>
518 _maybeReserve(pos,
first, last, deep,
521 while (
first != last) {
527 while (
first != last) {
529 assert(pos !=
end());
537 template <
typename InputIterator>
541 "SchemaMapper's output schema does not match catalog's schema");
543 _maybeReserve(pos,
first, last,
true,
545 while (
first != last) {
546 pos =
insert(pos, _table->copyRecord(*
first, mapper));
555 return iterator(_internal.insert(pos.base(), p));
560 return iterator(_internal.insert(pos.base(), p));
573 _table.swap(other._table);
574 _internal.swap(other._internal);
581 template <
typename T>
589 template <
typename Compare>
593 template <
typename T>
601 template <
typename Compare>
621 template <
typename T>
624 template <
typename T>
642 template <
typename T>
645 template <
typename T>
648 template <
typename T>
651 template <
typename T>
654 template <
typename T>
657 template <
typename T>
680 template <
typename InputIterator>
681 void _maybeReserve(
iterator& pos, InputIterator
first, InputIterator last,
bool deep,
683 if (deep) _table->preallocate(last -
first);
686 template <
typename InputIterator>
687 void _maybeReserve(
iterator pos, InputIterator
first, InputIterator last,
bool deep,
696template <
typename RecordT,
typename T>
698 bool operator()(RecordT
const& a, RecordT
const& b)
const {
return a.get(
key) < b.get(
key); }
703template <
typename RecordT,
typename Adaptee>
712template <
typename RecordT,
typename T>
723template <
typename RecordT>
724template <
typename Compare>
728 if (
empty())
return true;
732 for (; i != this->
end(); ++i) {
733 if (f(i, last))
return false;
739template <
typename RecordT>
740template <
typename Compare>
746template <
typename RecordT>
753template <
typename RecordT>
760template <
typename RecordT>
766 using SearchIter = boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
iterator>;
771 if (i.base() ==
end() || *i != value) {
773 if (i.base() ==
end()) {
776 LOGL_DEBUG(
"lsst.afw.table.Catalog",
"Catalog is not sorted by the key. Finding a record may be slow.");
781template <
typename RecordT>
784 Key<T> const& key)
const {
787 using SearchIter = boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
const_iterator>;
792 if (i.base() ==
end() || *i != value) {
794 if (i.base() ==
end()) {
797 LOGL_DEBUG(
"lsst.afw.table.Catalog",
"Catalog is not sorted by the key. Finding a record may be slow.");
802template <
typename RecordT>
808 using SearchIter = boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
iterator>;
813template <
typename RecordT>
819 using SearchIter = boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
const_iterator>;
824template <
typename RecordT>
830 using SearchIter = boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
iterator>;
835template <
typename RecordT>
841 using SearchIter = boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
const_iterator>;
846template <
typename RecordT>
852 using SearchIter = boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
iterator>;
858template <
typename RecordT>
864 using SearchIter = boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
const_iterator>;
892template <
typename RecordT,
typename Catalog,
typename T>
894 typename Catalog::const_iterator iter = catalog.find(value, key);
895 if (iter == catalog.end()) {
901template <
typename Catalog,
typename T>
903 return catalog.lower_bound(value, key) - catalog.begin();
906template <
typename Catalog,
typename T>
908 return catalog.upper_bound(value, key) - catalog.begin();
911template <
typename Catalog,
typename T>
914 catalog.equal_range(value, key);
39class CatalogIterator :
public boost::iterator_adaptor<CatalogIterator<BaseT>, BaseT, {
…};
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
LSST DM logging module built on log4cxx.
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Lifetime-management for memory that goes into FITS memory files.
Iterator class for CatalogT.
CatalogIterator(BaseT const &base)
CatalogIterator & operator=(std::shared_ptr< RecordT > const &other) const
CatalogIterator(CatalogIterator< OtherBaseT > const &other)
void clear()
Remove all records from the catalog.
CatalogT & operator=(CatalogT &&other)
size_type capacity() const
Return the capacity of the catalog.
CatalogT(CatalogT const &other)
Shallow copy constructor.
const_iterator lower_bound(typename Field< T >::Value const &value, Key< T > const &key) const
std::shared_ptr< RecordT > const get(size_type i) const
Return a pointer to the record at index i.
ColumnView getColumnView() const
Return a ColumnView of this catalog's records.
const_iterator find(typename Field< T >::Value const &value, Key< T > const &key) const
void pop_back()
Remove the last record in the catalog.
void push_back(std::shared_ptr< RecordT > const &p)
Add the given record to the end of the catalog without copying.
reference at(size_type i) const
Return the record at index i (throws std::out_of_range).
static CatalogT readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU, int flags=0)
Read a FITS binary table from a regular file.
iterator erase(iterator first, iterator last)
Erase the records in the range [first, last).
void set(size_type i, std::shared_ptr< RecordT > const &p)
Set the record at index i to a pointer.
iterator insert(iterator pos, Record const &r)
Insert a copy of the given record at the given position.
typename Record::ColumnView ColumnView
const_iterator end() const
const_iterator cend() const
bool empty() const
Return true if the catalog has no records.
static CatalogT readFits(fits::MemFileManager &manager, int hdu=fits::DEFAULT_HDU, int flags=0)
Read a FITS binary table from a RAM file.
void assign(InputIterator first, InputIterator last, bool deep=false)
Replace the contents of the table with an iterator range.
bool isContiguous() const
Return true if all records are contiguous.
iterator find(typename Field< T >::Value const &value, Key< T > const &key)
Return an iterator to the record with the given value.
bool isSorted(Compare cmp) const
Return true if the catalog is in ascending order according to the given predicate.
iterator upper_bound(typename Field< T >::Value const &value, Key< T > const &key)
CatalogT< RecordT > subset(std::ptrdiff_t startd, std::ptrdiff_t stopd, std::ptrdiff_t step) const
Returns a shallow copy of a subset of this Catalog.
CatalogT(Schema const &schema)
Construct a catalog from a schema, creating a table with Table::make(schema).
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
reference back() const
Return the last record.
iterator insert(iterator pos, std::shared_ptr< RecordT > const &p)
Insert the given record at the given position without copying.
std::pair< iterator, iterator > equal_range(typename Field< T >::Value const &value, Key< T > const &key)
CatalogIterator< typename Internal::iterator > iterator
std::pair< const_iterator, const_iterator > equal_range(typename Field< T >::Value const &value, Key< T > const &key) const
CatalogT(CatalogT< OtherRecordT > const &other)
Shallow copy constructor from a container containing a related record type.
iterator erase(iterator pos)
Erase the record pointed to by pos, and return an iterator the next record.
CatalogT(CatalogT &&other)
CatalogIterator< typename Internal::const_iterator > const_iterator
void reserve(size_type n)
Increase the capacity of the catalog to the given size.
reference operator[](size_type i) const
Return the record at index i.
iterator lower_bound(typename Field< T >::Value const &value, Key< T > const &key)
Performed binary searches on sorted fields.
typename Record::Table Table
typename Internal::size_type size_type
CatalogT< RecordT > subset(ndarray::Array< bool const, 1 > const &mask) const
Return the subset of a catalog corresponding to the True values of the given mask array.
CatalogT(std::shared_ptr< Table > const &table=std::shared_ptr< Table >())
Construct a catalog from a table (or nothing).
reference front() const
Return the first record.
const_iterator begin() const
void sort(Key< T > const &key)
Sort the catalog in-place by the field with the given key.
void resize(size_type n)
Change the size of the catalog, removing or adding empty records as needed.
size_type max_size() const
Return the maximum number of elements allowed in a catalog.
CatalogT copy() const
Deep-copy the catalog using a cloned table.
void writeFits(fits::MemFileManager &manager, std::string const &mode="w", int flags=0) const
Write a FITS binary table to a RAM file.
void writeFits(fits::Fits &fitsfile, int flags=0) const
Write a FITS binary table to an open file object.
void insert(iterator pos, InputIterator first, InputIterator last, bool deep=false)
Insert an iterator range into the table.
Internal const & getInternal() const
CatalogT & operator=(CatalogT const &other)
Shallow assigment.
Internal & getInternal()
Return a reference to the internal vector-of-shared_ptr.
static CatalogT readFits(fits::Fits &fitsfile, int flags=0)
Read a FITS binary table from a file object already at the correct extension.
std::shared_ptr< Table > getTable() const
Return the table associated with the catalog.
const_iterator upper_bound(typename Field< T >::Value const &value, Key< T > const &key) const
Schema getSchema() const
Return the schema associated with the catalog's table.
std::shared_ptr< PeakRecord > pointer
void writeFits(std::string const &filename, std::string const &mode="w", int flags=0) const
Write a FITS binary table to a regular file.
void sort(Compare cmp)
Sort the catalog in-place by the field with the given predicate.
CatalogT(std::shared_ptr< Table > const &table, InputIterator first, InputIterator last, bool deep=false)
Construct a catalog from a table and an iterator range.
bool isSorted(Key< T > const &key) const
Return true if the catalog is in ascending order according to the given key.
void insert(SchemaMapper const &mapper, iterator pos, InputIterator first, InputIterator last)
Insert a range of records into the catalog by copying them with a SchemaMapper.
void swap(CatalogT &other) noexcept
Shallow swap of two catalogs.
void push_back(Record const &r)
Add a copy of the given record to the end of the catalog.
const_iterator cbegin() const
typename Internal::difference_type difference_type
A class used as a handle to a particular field in a table.
Defines the fields and offsets for a table.
A mapping between the keys of two Schemas, used to copy data between them.
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
static ContainerT apply(afw::fits::Fits &fits, int ioFlags, std::shared_ptr< InputArchive > archive=std::shared_ptr< InputArchive >())
Create a new Catalog by reading a FITS binary table.
static void apply(OutputT &output, std::string const &mode, ContainerT const &container, int flags)
Driver for writing FITS files.
Reports invalid arguments.
Reports attempts to exceed implementation-defined length limits for some classes.
Reports errors in the logical structure of the program.
const int DEFAULT_HDU
Specify that the default HDU should be read.
int _Catalog_upper_bound(Catalog const &catalog, T const &value, Key< T > const &key)
std::shared_ptr< RecordT > _Catalog_find(Catalog const &catalog, T const &value, Key< T > const &key)
std::pair< int, int > _Catalog_equal_range(Catalog const &catalog, T const &value, Key< T > const &key)
int _Catalog_lower_bound(Catalog const &catalog, T const &value, Key< T > const &key)
T Value
the type returned by BaseRecord::get
bool operator()(std::shared_ptr< RecordT > const &a, std::shared_ptr< RecordT > const &b) const
bool operator()(RecordT const &a, RecordT const &b) const