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"
18 namespace lsst {
namespace afw {
namespace table {
34 template <
typename BaseT>
36 :
public boost::iterator_adaptor<CatalogIterator<BaseT>,BaseT,typename BaseT::value_type::element_type>
42 template <
typename OtherBaseT>
49 template <
typename RecordT>
50 operator PTR(RecordT) ()
const {
return *this->base(); }
52 template <
typename RecordT>
54 *this->base() = other;
60 typename BaseT::value_type::element_type &
dereference()
const {
return **this->base(); }
94 template <
typename RecordT>
100 typedef typename Record::Table
Table;
138 template <
typename InputIterator>
139 CatalogT(
PTR(
Table)
const & table, InputIterator first, InputIterator last,
bool deep=
false) :
154 template <
typename OtherRecordT>
161 if (&other !=
this) {
176 pex::exceptions::LengthError,
177 (
boost::format(
"Mask array with %d elements applied to catalog with %d elements")
178 % mask.size() %
size()).str()
182 ndarray::Array<bool const,1>::Iterator maskIter = mask.begin();
184 for (; maskIter != mask.end(); ++maskIter, ++catIter) {
185 if (*maskIter) result.
push_back(catIter);
220 pex::exceptions::InvalidParameterError,
221 "Step cannot be zero"
250 }
else if (step < 0) {
258 if (((step > 0) && (start >= stop)) ||
259 ((step < 0) && ((std::ptrdiff_t)start <= stopd))) {
277 N = (stop - start) / step + (((stop - start) % step) ? 1 : 0);
279 N = (size_t)((stopd - (std::ptrdiff_t)start) / step +
280 (((stopd - (std::ptrdiff_t)start) % step) ? 1 : 0));
284 for (
size_type i=start; i<stop; i+=step) {
288 for (std::ptrdiff_t i=(std::ptrdiff_t)start; i>stopd; i+=step) {
303 void writeFits(std::string
const & filename, std::string
const & mode=
"w",
int flags=0)
const {
340 return io::FitsReader::apply<CatalogT>(filename, hdu, flags);
353 return io::FitsReader::apply<CatalogT>(manager, hdu, flags);
364 return io::FitsReader::apply<CatalogT>(fitsfile, flags);
373 if (std::is_const<RecordT>::value) {
375 pex::exceptions::LogicError,
376 "Cannot get a column view from a CatalogT<RecordT const> (as column views are always "
452 template <
typename InputIterator>
453 void assign(InputIterator first, InputIterator last,
bool deep=
false) {
497 template <
typename InputIterator>
498 void insert(
iterator pos, InputIterator first, InputIterator last,
bool deep=
false) {
500 pos, first, last, deep, (
typename std::iterator_traits<InputIterator>::iterator_category*)0
503 while (first != last) {
504 pos =
insert(pos, *first);
509 while (first != last) {
511 assert(pos !=
end());
519 template <
typename InputIterator>
523 pex::exceptions::InvalidParameterError,
524 "SchemaMapper's output schema does not match catalog's schema"
528 pos, first, last,
true, (
typename std::iterator_traits<InputIterator>::iterator_category*)0
530 while (first != last) {
566 template <
typename T>
574 template <
typename Compare>
578 template <
typename T>
586 template <
typename Compare>
587 void sort(Compare cmp);
604 template <
typename T>
607 template <
typename T>
625 template <
typename T>
628 template <
typename T>
631 template <
typename T>
634 template <
typename T>
637 template <
typename T>
638 std::pair<iterator,iterator>
641 template <
typename T>
642 std::pair<const_iterator,const_iterator>
665 template <
typename InputIterator>
667 iterator & pos, InputIterator first, InputIterator last,
bool deep,
668 std::random_access_iterator_tag *
670 if (deep)
_table->preallocate(last - first);
673 template <
typename InputIterator>
675 iterator pos, InputIterator first, InputIterator last,
bool deep,
676 std::input_iterator_tag *
685 template <
typename RecordT,
typename T>
688 bool operator()(RecordT
const & a, RecordT
const &
b)
const {
return a.get(key) < b.get(key); }
693 template <
typename RecordT,
typename Adaptee>
697 return adaptee(*a, *b);
703 template <
typename RecordT,
typename T>
715 template <
typename RecordT>
716 template <
typename Compare>
720 if (
empty())
return true;
723 for (; i != this->
end(); ++i) {
724 if (f(i, last))
return false;
730 template <
typename RecordT>
731 template <
typename Compare>
734 std::stable_sort(_internal.begin(), _internal.end(), f);
737 template <
typename RecordT>
738 template <
typename T>
744 template <
typename RecordT>
745 template <
typename T>
751 template <
typename RecordT>
752 template <
typename T>
757 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
iterator> SearchIter;
759 SearchIter i = std::lower_bound(SearchIter(
begin(), f), SearchIter(
end(), f), value);
760 if (i.base() ==
end() || *i != value)
return end();
764 template <
typename RecordT>
765 template <
typename T>
770 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
const_iterator> SearchIter;
772 SearchIter i = std::lower_bound(SearchIter(
begin(), f), SearchIter(
end(), f), value);
773 if (i.base() ==
end() || *i != value)
return end();
777 template <
typename RecordT>
778 template <
typename T>
783 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
iterator> SearchIter;
784 SearchIter i = std::lower_bound(SearchIter(
begin(), f), SearchIter(
end(), f), value);
788 template <
typename RecordT>
789 template <
typename T>
794 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
const_iterator> SearchIter;
795 SearchIter i = std::lower_bound(SearchIter(
begin(), f), SearchIter(
end(), f), value);
799 template <
typename RecordT>
800 template <
typename T>
805 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
iterator> SearchIter;
806 SearchIter i = std::upper_bound(SearchIter(
begin(), f), SearchIter(
end(), f), value);
810 template <
typename RecordT>
811 template <
typename T>
816 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
const_iterator> SearchIter;
817 SearchIter i = std::upper_bound(SearchIter(
begin(), f), SearchIter(
end(), f), value);
821 template <
typename RecordT>
822 template <
typename T>
827 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
iterator> SearchIter;
828 std::pair<SearchIter,SearchIter> i
829 = std::equal_range(SearchIter(
begin(), f), SearchIter(
end(), f), value);
830 return std::make_pair(i.first.base(), i.second.base());
834 template <
typename RecordT>
835 template <
typename T>
840 typedef boost::transform_iterator<detail::KeyExtractionFunctor<RecordT,T>,
const_iterator> SearchIter;
841 std::pair<SearchIter,SearchIter> i
842 = std::equal_range(SearchIter(
begin(), f), SearchIter(
end(), f), value);
843 return std::make_pair(i.first.base(), i.second.base());
868 template <
typename RecordT,
typename Catalog,
typename T>
870 typename Catalog::const_iterator
iter = catalog.find(value, key);
871 if (iter == catalog.end()) {
872 return PTR(RecordT)();
877 template <
typename Catalog,
typename T>
879 return catalog.lower_bound(value, key) - catalog.begin();
882 template <
typename Catalog,
typename T>
884 return catalog.upper_bound(value, key) - catalog.begin();
887 template <
typename Catalog,
typename T>
889 std::pair<typename Catalog::const_iterator,typename Catalog::const_iterator> p
890 = catalog.equal_range(value, key);
891 return std::pair<int,int>(p.first - catalog.begin(), p.second - catalog.begin());
899 #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
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.
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,...)
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
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).
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.
Include files required for standard LSST Exception handling.
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