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"
11 #include "lsst/base.h"
12 #include "lsst/pex/exceptions.h"
36 template <
typename BaseT>
37 class CatalogIterator :
public boost::iterator_adaptor<CatalogIterator<BaseT>, BaseT,
38 typename BaseT::value_type::element_type> {
42 template <
typename OtherBaseT>
48 template <
typename RecordT>
49 operator std::shared_ptr<RecordT>()
const {
53 template <
typename RecordT>
55 *this->base() = other;
61 typename BaseT::value_type::element_type& dereference()
const {
return **this->base(); }
95 template <
typename RecordT>
97 typedef std::vector<std::shared_ptr<RecordT>> Internal;
101 typedef typename Record::Table
Table;
113 std::shared_ptr<Table>
getTable()
const {
return _table; }
124 explicit CatalogT(std::shared_ptr<Table>
const& table = std::shared_ptr<Table>())
125 : _table(table), _internal() {}
140 template <
typename InputIterator>
141 CatalogT(std::shared_ptr<Table>
const& table, InputIterator
first, InputIterator last,
bool deep =
false)
142 : _table(table), _internal() {
155 template <
typename OtherRecordT>
157 : _table(other.
getTable()), _internal(other.
begin().base(), other.
end().base()) {}
161 if (&other !=
this) {
162 _table = other._table;
163 _internal = other._internal;
176 pex::exceptions::LengthError,
177 (boost::format(
"Mask array with %d elements applied to catalog with %d elements") %
182 ndarray::Array<bool const, 1>::Iterator maskIter = mask.begin();
184 for (; maskIter != mask.end(); ++maskIter, ++catIter) {
185 if (*maskIter) result.
push_back(catIter);
219 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
"Step cannot be zero");
247 }
else if (step < 0) {
255 if (((step > 0) && (start >= stop)) || ((step < 0) && ((std::ptrdiff_t)start <= stopd))) {
273 N = (stop - start) / step + (((stop - start) % step) ? 1 : 0);
275 N = (size_t)((stopd - (std::ptrdiff_t)start) / step +
276 (((stopd - (std::ptrdiff_t)start) % step) ? 1 : 0));
284 for (std::ptrdiff_t i = (std::ptrdiff_t)start; i > stopd; i +=
step) {
299 void writeFits(std::string
const& filename, std::string
const& mode =
"w",
int flags = 0)
const {
336 return io::FitsReader::apply<CatalogT>(filename, hdu, flags);
349 return io::FitsReader::apply<CatalogT>(manager, hdu, flags);
360 return io::FitsReader::apply<CatalogT>(fitsfile, flags);
369 if (std::is_const<RecordT>::value) {
371 pex::exceptions::LogicError,
372 "Cannot get a column view from a CatalogT<RecordT const> (as column views are always "
373 "non-const views).");
375 return ColumnView::make(_table,
begin(),
end());
396 bool empty()
const {
return _internal.empty(); }
420 if (n <= _internal.size())
return;
421 _table->preallocate(n - _internal.size());
437 std::shared_ptr<RecordT>
const get(
size_type i)
const {
return _internal[i]; }
440 void set(
size_type i, std::shared_ptr<RecordT>
const& p) { _internal[i] = p; }
447 template <
typename InputIterator>
448 void assign(InputIterator
first, InputIterator last,
bool deep =
false) {
455 std::shared_ptr<RecordT> p = _table->copyRecord(r);
456 _internal.push_back(p);
460 void push_back(std::shared_ptr<RecordT>
const& p) { _internal.push_back(p); }
464 std::shared_ptr<RecordT> r = _table->makeRecord();
465 _internal.push_back(r);
490 template <
typename InputIterator>
492 _maybeReserve(pos, first, last, deep,
493 (
typename std::iterator_traits<InputIterator>::iterator_category*)0);
495 while (first != last) {
496 pos =
insert(pos, *first);
501 while (first != last) {
503 assert(pos !=
end());
511 template <
typename InputIterator>
514 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
515 "SchemaMapper's output schema does not match catalog's schema");
517 _maybeReserve(pos, first, last,
true,
518 (
typename std::iterator_traits<InputIterator>::iterator_category*)0);
519 while (first != last) {
520 pos =
insert(pos, _table->copyRecord(*first, mapper));
528 std::shared_ptr<RecordT> p = _table->copyRecord(r);
529 return iterator(_internal.insert(pos.base(), p));
534 return iterator(_internal.insert(pos.base(), p));
542 return iterator(_internal.erase(first.base(), last.base()));
547 _table.swap(other._table);
548 _internal.swap(other._internal);
555 template <
typename T>
563 template <
typename Compare>
567 template <
typename T>
575 template <
typename Compare>
576 void sort(Compare cmp);
593 template <
typename T>
596 template <
typename T>
614 template <
typename T>
617 template <
typename T>
620 template <
typename T>
623 template <
typename T>
626 template <
typename T>
629 template <
typename T>
652 template <
typename InputIterator>
653 void _maybeReserve(
iterator& pos, InputIterator
first, InputIterator last,
bool deep,
654 std::random_access_iterator_tag*) {
655 if (deep) _table->preallocate(last - first);
658 template <
typename InputIterator>
659 void _maybeReserve(
iterator pos, InputIterator first, InputIterator last,
bool deep,
660 std::input_iterator_tag*) {}
662 std::shared_ptr<Table> _table;
668 template <
typename RecordT,
typename T>
670 bool operator()(RecordT
const& a, RecordT
const& b)
const {
return a.get(
key) < b.get(
key); }
675 template <
typename RecordT,
typename Adaptee>
677 bool operator()(std::shared_ptr<RecordT>
const& a, std::shared_ptr<RecordT>
const& b)
const {
684 template <
typename RecordT,
typename T>
695 template <
typename RecordT>
696 template <
typename Compare>
700 if (empty())
return true;
704 for (; i != this->
end(); ++i) {
705 if (f(i, last))
return false;
711 template <
typename RecordT>
712 template <
typename Compare>
715 std::stable_sort(_internal.begin(), _internal.end(), f);
718 template <
typename RecordT>
719 template <
typename T>
725 template <
typename RecordT>
726 template <
typename T>
732 template <
typename RecordT>
733 template <
typename T>
738 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
iterator> SearchIter;
740 SearchIter i = std::lower_bound(SearchIter(begin(), f), SearchIter(
end(), f), value);
741 if (i.base() ==
end() || *i != value)
return end();
745 template <
typename RecordT>
746 template <
typename T>
748 Key<T> const& key)
const {
751 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
const_iterator> SearchIter;
753 SearchIter i = std::lower_bound(SearchIter(begin(), f), SearchIter(
end(), f), value);
754 if (i.base() ==
end() || *i != value)
return end();
758 template <
typename RecordT>
759 template <
typename T>
764 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
iterator> SearchIter;
765 SearchIter i = std::lower_bound(SearchIter(begin(), f), SearchIter(
end(), f), value);
769 template <
typename RecordT>
770 template <
typename T>
775 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
const_iterator> SearchIter;
776 SearchIter i = std::lower_bound(SearchIter(begin(), f), SearchIter(
end(), f), value);
780 template <
typename RecordT>
781 template <
typename T>
786 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
iterator> SearchIter;
787 SearchIter i = std::upper_bound(SearchIter(begin(), f), SearchIter(
end(), f), value);
791 template <
typename RecordT>
792 template <
typename T>
797 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
const_iterator> SearchIter;
798 SearchIter i = std::upper_bound(SearchIter(begin(), f), SearchIter(
end(), f), value);
802 template <
typename RecordT>
803 template <
typename T>
808 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
iterator> SearchIter;
809 std::pair<SearchIter, SearchIter> i =
810 std::equal_range(SearchIter(begin(), f), SearchIter(
end(), f), value);
811 return std::make_pair(i.first.base(), i.second.base());
814 template <
typename RecordT>
815 template <
typename T>
820 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT, T>,
const_iterator> SearchIter;
821 std::pair<SearchIter, SearchIter> i =
822 std::equal_range(SearchIter(begin(), f), SearchIter(
end(), f), value);
823 return std::make_pair(i.first.base(), i.second.base());
848 template <
typename RecordT,
typename Catalog,
typename T>
850 typename Catalog::const_iterator iter = catalog.find(value, key);
851 if (iter == catalog.end()) {
852 return std::shared_ptr<RecordT>();
857 template <
typename Catalog,
typename T>
859 return catalog.lower_bound(value, key) - catalog.begin();
862 template <
typename Catalog,
typename T>
864 return catalog.upper_bound(value, key) - catalog.begin();
867 template <
typename Catalog,
typename T>
869 std::pair<typename Catalog::const_iterator, typename Catalog::const_iterator> p =
870 catalog.equal_range(value, key);
871 return std::pair<int, int>(p.first - catalog.begin(), p.second - catalog.begin());
879 #endif // !AFW_TABLE_Catalog_h_INCLUDED
CatalogT(CatalogT< OtherRecordT > const &other)
Shallow copy constructor from a container containing a related record type.
void push_back(std::shared_ptr< RecordT > const &p)
Add the given record to the end of the catalog without copying.
const_iterator end() const
Iterator access.
const_iterator begin() const
Iterator access.
Defines the fields and offsets for a table.
std::shared_ptr< RecordT > _Catalog_find(Catalog const &catalog, T const &value, Key< T > const &key)
const_iterator cend() const
Iterator access.
iterator lower_bound(typename Field< T >::Value const &value, Key< T > const &key)
Performed binary searches on sorted fields.
bool operator()(RecordT const &a, RecordT const &b) const
iterator insert(iterator pos, std::shared_ptr< RecordT > const &p)
Insert the given record at the given position without copying.
static CatalogT readFits(std::string const &filename, int hdu=INT_MIN, int flags=0)
Read a FITS binary table from a regular file.
Internal::difference_type difference_type
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
bool isSorted(Key< T > const &key) const
Return true if the catalog is in ascending order according to the given key.
static CatalogT readFits(fits::MemFileManager &manager, int hdu=INT_MIN, int flags=0)
Read a FITS binary table from a RAM file.
const_iterator cbegin() const
Iterator access.
CatalogT & operator=(CatalogT const &other)
Shallow assigment.
CatalogIterator(CatalogIterator< OtherBaseT > const &other)
A custom container class for records, based on std::vector.
A mapping between the keys of two Schemas, used to copy data between them.
void sort(Key< T > const &key)
Sort the catalog in-place by the field with the given key.
Internal & getInternal()
Return a reference to the internal vector-of-shared_ptr.
void pop_back()
Remove the last record in the catalog.
int _Catalog_lower_bound(Catalog const &catalog, T const &value, Key< T > const &key)
void insert(iterator pos, InputIterator first, InputIterator last, bool deep=false)
Insert an iterator range into the table.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
reference at(size_type i) const
Return the record at index i (throws std::out_of_range).
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(std::shared_ptr< Table > const &table, InputIterator first, InputIterator last, bool deep=false)
Construct a catalog from a table and an iterator range.
std::pair< int, int > _Catalog_equal_range(Catalog const &catalog, T const &value, Key< T > const &key)
size_type size() const
Return the number of elements in the catalog.
CatalogT(CatalogT const &other)
Shallow copy constructor.
CatalogIterator & operator=(std::shared_ptr< RecordT > const &other) const
int _Catalog_upper_bound(Catalog const &catalog, T const &value, Key< T > const &key)
bool empty() const
Return true if the catalog has no records.
void push_back(Record const &r)
Add a copy of the given record to the end of the catalog.
Internal const & getInternal() const
Return a reference to the internal vector-of-shared_ptr.
void clear()
Remove all records from the catalog.
std::shared_ptr< Table > getTable() const
Return the table associated with the catalog.
Internal::size_type size_type
Lifetime-management for memory that goes into FITS memory files.
iterator end()
Iterator access.
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(Schema const &schema)
Construct a catalog from a schema, creating a table with Table::make(schema).
iterator erase(iterator pos)
Erase the record pointed to by pos, and return an iterator the next record.
CatalogT copy() const
Deep-copy the catalog using a cloned table.
Iterator class for CatalogT.
bool operator()(std::shared_ptr< RecordT > const &a, std::shared_ptr< RecordT > const &b) const
Record::ColumnView ColumnView
size_type max_size() const
Return the maximum number of elements allowed in a catalog.
iterator erase(iterator first, iterator last)
Erase the records in the range [first, last).
CatalogIterator(BaseT const &base)
bool isContiguous() const
Return true if all records are contiguous.
CatalogT(std::shared_ptr< Table > const &table=std::shared_ptr< Table >())
Construct a catalog from a table (or nothing).
CatalogIterator< typename Internal::const_iterator > const_iterator
void swap(CatalogT &other)
Shallow swap of two catalogs.
void reserve(size_type n)
Increase the capacity of the catalog to the given size.
iterator insert(iterator pos, Record const &r)
Insert a copy of the given record at the given position.
void assign(InputIterator first, InputIterator last, bool deep=false)
Replace the contents of the table with an iterator range.
A class used as a handle to a particular field in a table.
T Value
the type returned by BaseRecord::get
size_type capacity() const
Return the capacity of the catalog.
static void apply(OutputT &output, std::string const &mode, ContainerT const &container, int flags)
Driver for writing FITS files.
ColumnView getColumnView() const
Return a ColumnView of this catalog's records.
reference back() const
Return the last record.
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.
reference operator[](size_type i) const
Return the record at index i.
reference front() const
Return the first record.
iterator upper_bound(typename Field< T >::Value const &value, Key< T > const &key)
Performed binary searches on sorted fields.
iterator find(typename Field< T >::Value const &value, Key< T > const &key)
Return an iterator to the record with the given value.
void writeFits(fits::Fits &fitsfile, int flags=0) const
Write a FITS binary table to an open file object.
void writeFits(std::string const &filename, std::string const &mode="w", int flags=0) const
Write a FITS binary table to a regular file.
iterator begin()
Iterator access.
std::pair< iterator, iterator > equal_range(typename Field< T >::Value const &value, Key< T > const &key)
Performed binary searches on sorted fields.
static CatalogT readFits(fits::Fits &fitsfile, int flags=0)
Read a FITS binary table from a file object already at the correct extension.
Schema getSchema() const
Return the schema associated with the catalog's table.
afw::table::Schema schema
CatalogIterator< typename Internal::iterator > iterator
void set(size_type i, std::shared_ptr< RecordT > const &p)
Set the record at index i to a pointer.
void writeFits(fits::MemFileManager &manager, std::string const &mode="w", int flags=0) const
Write a FITS binary table to a RAM file.
std::shared_ptr< RecordT > pointer
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
friend class boost::iterator_core_access