lsst.afw  16.0-10-gd1b3a1c94
AliasMap.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2014 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #include "lsst/pex/exceptions.h"
27 
28 namespace lsst {
29 namespace afw {
30 namespace table {
31 
32 void AliasMap::_apply(std::string& name) const {
33  // Loop in order to keep replacing as long as we keep finding matches,
34  // but we count how many replacements we've made to avoid an infinite loop
35  // due to a cycle between aliases. That's not the most efficient way to
36  // find cycles, but since a cycle represents a bug in some other code that
37  // should be rare, we don't really care.
38  for (std::size_t count = 0; count <= _internal.size(); ++count) {
39  Iterator i = _internal.lower_bound(name);
40  if (i != _internal.end() && i->first.size() == name.size() && i->first == name) {
41  // We have a complete match: alias matches the full name we were given
42  name = i->second;
43  } else if (i != _internal.begin()) {
44  // Might still have a partial match: in this case, the iterator
45  // lower_bound returns will be one past the best match, since
46  // smaller strings are considered "less than" longer strings
47  // that they share the same initial charaters with.
48  --i;
49  if (i->first.size() < name.size() && name.compare(0, i->first.size(), i->first) == 0) {
50  name.replace(0, i->first.size(), i->second);
51  } else {
52  return; // no match; exit
53  }
54  } else {
55  return; // no match; exit
56  }
57  }
58  throw LSST_EXCEPT(pex::exceptions::RuntimeError,
59  (boost::format("Cycle detected in schema aliases involving name '%s'") % name).str());
60 }
61 
63  std::string result(name);
64  _apply(result);
65  return result;
66 }
67 
69  Iterator i = _internal.lower_bound(name);
70  if (i == _internal.end()) {
72  (boost::format("Alias '%s' not found") % name).str());
73  }
74  return i->second;
75 }
76 
77 void AliasMap::set(std::string const& alias, std::string const& target) {
78  _internal[alias] = target;
79  if (_table) {
80  _table->handleAliasChange(alias);
81  }
82 }
83 
84 bool AliasMap::erase(std::string const& alias) {
85  bool result = _internal.erase(alias);
86  if (_table) {
87  _table->handleAliasChange(alias);
88  }
89  return result;
90 }
91 
92 bool AliasMap::operator==(AliasMap const& other) const { return _internal == other._internal; }
93 
94 bool AliasMap::contains(AliasMap const& other) const {
95  return std::includes(begin(), end(), other.begin(), other.end());
96 }
97 } // namespace table
98 } // namespace afw
99 } // namespace lsst
std::string get(std::string const &alias) const
Return the target of the given alias.
Definition: AliasMap.cc:68
bool contains(AliasMap const &other) const
Return true if all aliases in this are also in other (with the same targets).
Definition: AliasMap.cc:94
Key< Flag > const & target
Iterator end() const
Return a iterator to one past the end of the map.
Definition: AliasMap.h:63
void set(std::string const &alias, std::string const &target)
Add an alias to the schema or replace an existing one.
Definition: AliasMap.cc:77
Mapping class that holds aliases for a Schema.
Definition: AliasMap.h:36
T end(T... args)
ItemVariant const * other
Definition: Schema.cc:55
T lower_bound(T... args)
STL class.
A base class for image defects.
T replace(T... args)
std::string apply(std::string const &name) const
Apply any aliases that match the given field name and return a de-aliased name.
Definition: AliasMap.cc:62
T erase(T... args)
bool operator==(AliasMap const &other) const
Equality comparison.
Definition: AliasMap.cc:92
bool erase(std::string const &alias)
Remove an alias from the schema if it is present.
Definition: AliasMap.cc:84
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:194
T count(T... args)
T size(T... args)
#define LSST_EXCEPT(type,...)
T begin(T... args)
Iterator begin() const
Return a iterator to the beginning of the map.
Definition: AliasMap.h:60
std::map< std::string, std::string >::const_iterator Iterator
An iterator over alias->target pairs.
Definition: AliasMap.h:57
virtual void handleAliasChange(std::string const &alias)
Definition: BaseTable.h:176
T compare(T... args)
T includes(T... args)