2 #ifndef AFW_TABLE_Catalog_h_INCLUDED
3 #define AFW_TABLE_Catalog_h_INCLUDED
7 #include "boost/iterator/iterator_adaptor.hpp"
8 #include "boost/iterator/transform_iterator.hpp"
17 namespace lsst {
namespace afw {
namespace table {
33 template <
typename BaseT>
35 :
public boost::iterator_adaptor<CatalogIterator<BaseT>,BaseT,typename BaseT::value_type::element_type>
41 template <
typename OtherBaseT>
48 template <
typename RecordT>
49 operator PTR(RecordT) ()
const {
return *this->base(); }
51 template <
typename RecordT>
53 *this->base() = other;
59 typename BaseT::value_type::element_type &
dereference()
const {
return **this->base(); }
93 template <
typename RecordT>
99 typedef typename Record::Table
Table;
137 template <
typename InputIterator>
138 CatalogT(
PTR(
Table)
const & table, InputIterator first, InputIterator last,
bool deep=
false) :
153 template <
typename OtherRecordT>
160 if (&other !=
this) {
175 pex::exceptions::LengthError,
176 (
boost::format(
"Mask array with %d elements applied to catalog with %d elements")
183 for (; maskIter != mask.
end(); ++maskIter, ++catIter) {
184 if (*maskIter) result.
push_back(catIter);
219 pex::exceptions::InvalidParameterError,
220 "Step cannot be zero"
249 }
else if (step < 0) {
257 if (((step > 0) && (start >= stop)) ||
258 ((step < 0) && ((std::ptrdiff_t)start <= stopd))) {
276 N = (stop - start) / step + (((stop - start) % step) ? 1 : 0);
278 N = (size_t)((stopd - (std::ptrdiff_t)start) / step +
279 (((stopd - (std::ptrdiff_t)start) % step) ? 1 : 0));
283 for (
size_type i=start; i<stop; i+=step) {
287 for (std::ptrdiff_t i=(std::ptrdiff_t)start; i>stopd; i+=step) {
302 void writeFits(std::string
const & filename, std::string
const & mode=
"w",
int flags=0)
const {
339 return io::FitsReader::apply<CatalogT>(filename, hdu, flags);
352 return io::FitsReader::apply<CatalogT>(manager, hdu, flags);
363 return io::FitsReader::apply<CatalogT>(fitsfile, flags);
372 if (boost::is_const<RecordT>::value) {
374 pex::exceptions::LogicError,
375 "Cannot get a column view from a CatalogT<RecordT const> (as column views are always "
451 template <
typename InputIterator>
452 void assign(InputIterator first, InputIterator last,
bool deep=
false) {
496 template <
typename InputIterator>
497 void insert(
iterator pos, InputIterator first, InputIterator last,
bool deep=
false) {
499 pos, first, last, deep, (
typename std::iterator_traits<InputIterator>::iterator_category*)0
502 while (first != last) {
503 pos =
insert(pos, *first);
508 while (first != last) {
510 assert(pos !=
end());
518 template <
typename InputIterator>
522 pex::exceptions::InvalidParameterError,
523 "SchemaMapper's output schema does not match catalog's schema"
527 pos, first, last,
true, (
typename std::iterator_traits<InputIterator>::iterator_category*)0
529 while (first != last) {
565 template <
typename T>
573 template <
typename Compare>
577 template <
typename T>
585 template <
typename Compare>
586 void sort(Compare cmp);
603 template <
typename T>
606 template <
typename T>
624 template <
typename T>
627 template <
typename T>
630 template <
typename T>
633 template <
typename T>
636 template <
typename T>
637 std::pair<iterator,iterator>
640 template <
typename T>
641 std::pair<const_iterator,const_iterator>
664 template <
typename InputIterator>
666 iterator & pos, InputIterator first, InputIterator last,
bool deep,
667 std::random_access_iterator_tag *
669 if (deep)
_table->preallocate(last - first);
672 template <
typename InputIterator>
674 iterator pos, InputIterator first, InputIterator last,
bool deep,
675 std::input_iterator_tag *
684 template <
typename RecordT,
typename T>
687 bool operator()(RecordT
const & a, RecordT
const &
b)
const {
return a.get(key) < b.get(key); }
692 template <
typename RecordT,
typename Adaptee>
696 return adaptee(*a, *b);
702 template <
typename RecordT,
typename T>
714 template <
typename RecordT>
715 template <
typename Compare>
719 if (
empty())
return true;
722 for (; i != this->
end(); ++i) {
723 if (f(i, last))
return false;
729 template <
typename RecordT>
730 template <
typename Compare>
733 std::stable_sort(_internal.begin(), _internal.end(), f);
736 template <
typename RecordT>
737 template <
typename T>
743 template <
typename RecordT>
744 template <
typename T>
750 template <
typename RecordT>
751 template <
typename T>
756 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
iterator> SearchIter;
758 SearchIter i = std::lower_bound(SearchIter(
begin(), f), SearchIter(
end(), f), value);
759 if (i.base() ==
end() || *i != value)
return end();
763 template <
typename RecordT>
764 template <
typename T>
769 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
const_iterator> SearchIter;
771 SearchIter i = std::lower_bound(SearchIter(
begin(), f), SearchIter(
end(), f), value);
772 if (i.base() ==
end() || *i != value)
return end();
776 template <
typename RecordT>
777 template <
typename T>
782 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
iterator> SearchIter;
783 SearchIter i = std::lower_bound(SearchIter(
begin(), f), SearchIter(
end(), f), value);
787 template <
typename RecordT>
788 template <
typename T>
793 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
const_iterator> SearchIter;
794 SearchIter i = std::lower_bound(SearchIter(
begin(), f), SearchIter(
end(), f), value);
798 template <
typename RecordT>
799 template <
typename T>
804 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
iterator> SearchIter;
805 SearchIter i = std::upper_bound(SearchIter(
begin(), f), SearchIter(
end(), f), value);
809 template <
typename RecordT>
810 template <
typename T>
815 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
const_iterator> SearchIter;
816 SearchIter i = std::upper_bound(SearchIter(
begin(), f), SearchIter(
end(), f), value);
820 template <
typename RecordT>
821 template <
typename T>
826 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
iterator> SearchIter;
827 std::pair<SearchIter,SearchIter> i
828 = std::equal_range(SearchIter(
begin(), f), SearchIter(
end(), f), value);
829 return std::make_pair(i.first.base(), i.second.base());
833 template <
typename RecordT>
834 template <
typename T>
839 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
const_iterator> SearchIter;
840 std::pair<SearchIter,SearchIter> i
841 = std::equal_range(SearchIter(
begin(), f), SearchIter(
end(), f), value);
842 return std::make_pair(i.first.base(), i.second.base());
867 template <
typename RecordT,
typename Catalog,
typename T>
869 typename Catalog::const_iterator
iter = catalog.find(value, key);
870 if (iter == catalog.end()) {
871 return PTR(RecordT)();
876 template <
typename Catalog,
typename T>
878 return catalog.lower_bound(value, key) - catalog.begin();
881 template <
typename Catalog,
typename T>
883 return catalog.upper_bound(value, key) - catalog.begin();
886 template <
typename Catalog,
typename T>
888 std::pair<typename Catalog::const_iterator,typename Catalog::const_iterator> p
889 = catalog.equal_range(value, key);
890 return std::pair<int,int>(p.first - catalog.begin(), p.second - catalog.begin());
898 #endif // !AFW_TABLE_Catalog_h_INCLUDED
CatalogT(CatalogT< OtherRecordT > const &other)
Shallow copy constructor from a container containing a related record type.
const_iterator end() const
const_iterator begin() const
Defines the fields and offsets for a table.
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, boost::shared_ptr< RecordT > const &p)
Insert the given record at the given position without copying.
Internal::difference_type difference_type
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
CatalogT(boost::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.
boost::shared_ptr< Table > _table
CatalogT & operator=(CatalogT const &other)
Shallow assigment.
CatalogIterator(CatalogIterator< OtherBaseT > const &other)
A custom container class for records, based on std::vector.
afw::table::Schema schema
Include files required for standard LSST Exception handling.
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.
bool operator()(boost::shared_ptr< RecordT > const &a, boost::shared_ptr< RecordT > const &b) const
void pop_back()
Remove the last record in the catalog.
std::pair< int, int > _Catalog_equal_range(Catalog const &catalog, T const &value, Key< T > const &key)
boost::shared_ptr< PeakRecord > pointer
int _Catalog_lower_bound(Catalog const &catalog, T const &value, Key< T > const &key)
std::vector< boost::shared_ptr< RecordT > > Internal
void insert(iterator pos, InputIterator first, InputIterator last, bool deep=false)
Insert an iterator range into the table.
void _maybeReserve(iterator pos, InputIterator first, InputIterator last, bool deep, std::input_iterator_tag *)
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. The arguments correspond to python's slice() synt...
size_type size() const
Return the number of elements in the catalog.
void push_back(boost::shared_ptr< RecordT > const &p)
Add the given record to the end of the catalog without copying.
CatalogT(CatalogT const &other)
Shallow copy constructor.
static CatalogT readFits(std::string const &filename, int hdu=0, int flags=0)
Read a FITS binary table from a regular file.
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.
Internal::size_type size_type
Lifetime-management for memory that goes into FITS memory files.
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).
BaseT::value_type::element_type & dereference() const
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.
size_type size() const
Return the size of the first dimension.
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)
void _maybeReserve(iterator &pos, InputIterator first, InputIterator last, bool deep, std::random_access_iterator_tag *)
boost::shared_ptr< RecordT > _Catalog_find(Catalog const &catalog, T const &value, Key< T > const &key)
bool isContiguous() const
Return true if all records are contiguous.
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.
#define LSST_EXCEPT(type,...)
A multidimensional strided array.
boost::shared_ptr< RecordT > const get(size_type i) const
Return a pointer to the record at index i.
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
Iterator end() const
Return an Iterator to one past the end of the array.
size_type capacity() const
Return the capacity of the catalog.
boost::shared_ptr< Table > getTable() const
Return the table associated with the catalog.
static void apply(OutputT &output, std::string const &mode, ContainerT const &container, int flags)
Driver for writing FITS files.
boost::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
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.
afw::table::Key< double > b
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.
void set(size_type i, boost::shared_ptr< RecordT > const &p)
Set the record at index i to a pointer.
iterator find(typename Field< T >::Value const &value, Key< T > const &key)
Return an iterator to the record with the given value.
CatalogT(boost::shared_ptr< Table > const &table=boost::shared_ptr< Table >())
Construct a catalog from a table (or nothing).
ExpressionTraits< Derived >::Iterator Iterator
Nested expression or element iterator.
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.
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.
static CatalogT readFits(fits::MemFileManager &manager, int hdu=0, int flags=0)
Read a FITS binary table from a RAM file.
CatalogIterator< typename Internal::iterator > iterator
void writeFits(fits::MemFileManager &manager, std::string const &mode="w", int flags=0) const
Write a FITS binary table to a RAM file.
std::pair< iterator, iterator > equal_range(typename Field< T >::Value const &value, Key< T > const &key)
Performed binary searches on sorted fields.
CatalogIterator & operator=(boost::shared_ptr< RecordT > const &other) const
friend class boost::iterator_core_access
Iterator begin() const
Return an Iterator to the beginning of the array.