1#include "boost/preprocessor/seq/for_each.hpp"
2#include "boost/preprocessor/tuple/to_seq.hpp"
23struct KeyPairCompareEqual {
26 return std::visit([
this](
auto const & pair) ->
bool {
return this->_target == pair.first; }, v);
29 explicit KeyPairCompareEqual(Key<T>
const &
target) : _target(
target) {}
32 Key<T>
const &_target;
37struct MapMinimalSchema {
39 void operator()(SchemaItem<U>
const &item)
const {
43 SchemaItem<U> inputItem =
_mapper->getInputSchema().find(item.key);
44 outputKey =
_mapper->addMapping(item.key);
45 }
catch (pex::exceptions::NotFoundError &) {
46 outputKey =
_mapper->addOutputField(item.field);
49 outputKey =
_mapper->addOutputField(item.field);
51 assert(outputKey == item.key);
54 explicit MapMinimalSchema(SchemaMapper *
mapper,
bool doMap) :
_mapper(
mapper), _doMap(doMap) {}
64 void operator()(SchemaItem<T>
const &item)
const {
65 Field<T>
field(
prefix + item.field.getName(), item.field.getDoc(), item.field.getUnits(), item.field);
69 explicit AddMapped(SchemaMapper *mapper_) :
mapper(mapper_) {}
78 void operator()(SchemaItem<T>
const &item)
const {
79 Field<T>
field(
prefix + item.field.getName(), item.field.getDoc(), item.field.getUnits(), item.field);
83 explicit AddUnmapped(SchemaMapper *mapper_) :
mapper(mapper_) {}
89struct RemoveMinimalSchema {
91 void operator()(SchemaItem<T>
const &item)
const {
93 mapper->addMapping(item.key);
97 RemoveMinimalSchema(SchemaMapper *mapper_, Schema
const &minimal_) :
mapper(mapper_),
minimal(minimal_) {}
131 typename Impl::KeyPairMap::iterator i =
132 std::find_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
133 Field<T> inputField = _impl->_input.find(inputKey).field;
134 if (i != _impl->_map.end()) {
135 Key<T> const &outputKey = std::get<std::pair<Key<T>,
Key<T>>>(*i).second;
136 _impl->_output.replaceField(outputKey, inputField);
139 Key<T> outputKey = _impl->_output.addField(inputField, doReplace);
147 typename Impl::KeyPairMap::iterator i =
148 std::find_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
149 if (i != _impl->_map.end()) {
150 Key<T> const &outputKey = std::get<std::pair<Key<T>,
Key<T>>>(*i).second;
151 _impl->_output.replaceField(outputKey,
field);
154 Key<T> outputKey = _impl->_output.addField(
field, doReplace);
162 typename Impl::KeyPairMap::iterator i =
163 std::find_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
164 if (i != _impl->_map.end()) {
165 Key<T> const &outputKey = std::get<std::pair<Key<T>,
Key<T>>>(*i).second;
168 _impl->_output.replaceField(outputKey,
field);
171 Field<T> inputField = _impl->_input.find(inputKey).field;
173 Key<T> outputKey = _impl->_output.addField(outputField, doReplace);
182 "Must add minimal schema to mapper before adding any other fields");
184 MapMinimalSchema f(
this, doMap);
196 std::swap(_impl->_input, _impl->_output);
197 std::for_each(_impl->_map.begin(), _impl->_map.end(), SwapKeyPair());
202 return std::count_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
207 typename Impl::KeyPairMap::iterator i =
208 std::find_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
209 if (i == _impl->_map.end()) {
212 return std::get<std::pair<Key<T>,
Key<T>>>(*i).second;
221 (
boost::format(
"prefix vector size (%d) must be the same as input vector size (%d)") %
227 result.emplace_back(inputs[i]);
232 AddMapped functor(&
result[j]);
233 if (!prefixes.
empty()) functor.prefix = prefixes[i];
234 inputs[i].forEach(functor);
236 AddUnmapped functor(&
result[j]);
237 if (!prefixes.
empty()) functor.prefix = prefixes[i];
238 inputs[i].forEach(functor);
247#define INSTANTIATE_LAYOUTMAPPER(r, data, elem) \
248 template Key<elem> SchemaMapper::addOutputField(Field<elem> const &, bool); \
249 template Key<elem> SchemaMapper::addMapping(Key<elem> const &, bool); \
250 template Key<elem> SchemaMapper::addMapping(Key<elem> const &, Field<elem> const &, bool); \
251 template Key<elem> SchemaMapper::addMapping(Key<elem> const &, std::string const &, bool); \
252 template bool SchemaMapper::isMapped(Key<elem> const &) const; \
253 template Key<elem> SchemaMapper::getMapping(Key<elem> const &) const;
Key< Flag > const & target
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
#define INSTANTIATE_LAYOUTMAPPER(r, data, elem)
A class used as a handle to a particular field in a table.
Defines the fields and offsets for a table.
void forEach(F &func) const
Apply a functor to each SchemaItem in the Schema.
void setAliasMap(std::shared_ptr< AliasMap > aliases)
Set the alias map.
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
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).
SchemaMapper()
Construct an empty mapper; useless unless you assign a fully-constructed one to it.
SchemaMapper & operator=(SchemaMapper const &other)
Assignment (copy-on-write).
Key< T > addMapping(Key< T > const &inputKey, bool doReplace=false)
Add a new field to the output Schema that is a copy of a field in the input Schema.
static SchemaMapper removeMinimalSchema(Schema const &input, Schema const &minimal)
Create a mapper by removing fields from the front of a schema.
static std::vector< SchemaMapper > join(std::vector< Schema > const &inputs, std::vector< std::string > const &prefixes=std::vector< std::string >())
Combine a sequence of schemas into one, creating a SchemaMapper for each.
Schema & editOutputSchema()
Return a reference to the output schema that allows it to be modified in place.
Key< T > getMapping(Key< T > const &inputKey) const
Return the output Key corresponding to the given input Key, or raise NotFoundError.
bool isMapped(Key< T > const &inputKey) const
Return true if the given input Key is mapped to an output Key.
void addMinimalSchema(Schema const &minimal, bool doMap=true)
Add the given minimal schema to the output schema.
void invert()
Swap the input and output schemas in-place.
A private implementation class to hide the messy details of SchemaMapper.
decltype(makeKeyPairVariantType(FieldTypes{})) KeyPairVariant
A Variant type that can hold any one of the allowed pairx types.
Reports attempts to exceed implementation-defined length limits for some classes.
Reports errors in the logical structure of the program.
Reports attempts to access elements using an invalid key.
BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_SCALAR, _, BOOST_PP_TUPLE_TO_SEQ(AFW_TABLE_SCALAR_FIELD_TYPE_N, AFW_TABLE_SCALAR_FIELD_TYPE_TUPLE)) BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_ARRAY
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
A base class for image defects.
A description of a field in a table.
Field< T > copyRenamed(std::string const &newName) const
Return a new Field with a new name and other properties the same as this.
#define AFW_TABLE_FIELD_TYPE_N
#define AFW_TABLE_FIELD_TYPE_TUPLE