LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
FrameDict.h
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2017 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 #ifndef ASTSHIM_FRAMEDICT_H
23 #define ASTSHIM_FRAMEDICT_H
24 
25 #include <cassert>
26 #include <memory>
27 #include <set>
28 #include <sstream>
29 #include <stdexcept>
30 #include <unordered_map>
31 
32 #include "astshim/base.h"
33 #include "astshim/detail/utils.h"
34 #include "astshim/Frame.h"
35 #include "astshim/FrameSet.h"
36 
37 namespace ast {
38 
67 class FrameDict : public FrameSet {
68  friend class Object;
69 
70 public:
81  explicit FrameDict(Frame const &frame, std::string const &options = "") : FrameSet(frame, options) {
82  _domainIndexDict = _makeNewDict(*this);
83  }
84 
97  explicit FrameDict(Frame const &baseFrame, Mapping const &mapping, Frame const &currentFrame,
98  std::string const &options = "")
99  : FrameSet(baseFrame, mapping, currentFrame) {
100  _domainIndexDict = _makeNewDict(*this);
101  }
102 
110  explicit FrameDict(FrameSet const &frameSet) : FrameSet(frameSet), _domainIndexDict() {
111  _domainIndexDict = _makeNewDict(*this);
112  }
113 
114  virtual ~FrameDict() {}
115 
117  FrameDict(FrameDict const &) = default;
118  FrameDict(FrameDict &&) = default;
119  FrameDict &operator=(FrameDict const &) = delete;
120  FrameDict &operator=(FrameDict &&) = default;
121 
123  std::shared_ptr<FrameDict> copy() const { return std::static_pointer_cast<FrameDict>(copyPolymorphic()); }
124 
131  void addFrame(int iframe, Mapping const &map, Frame const &frame) override;
132 
137  void addFrame(std::string const &domain, Mapping const &map, Frame const &frame);
138 
143 
144  using FrameSet::getFrame;
145 
151  std::shared_ptr<Frame> getFrame(std::string const &domain, bool copy = true) const {
152  return getFrame(getIndex(domain), copy);
153  }
154 
155  using FrameSet::getMapping;
156 
163  return getMapping(from, getIndex(to));
164  }
165 
172  return getMapping(getIndex(from), to);
173  }
174 
181  return getMapping(getIndex(from), getIndex(to));
182  }
183 
189  int getIndex(std::string const &domain) const {
190  auto domainUpper = detail::stringToUpper(domain);
191  auto it = _domainIndexDict.find(domainUpper);
192  if (it == _domainIndexDict.end()) {
193  throw std::out_of_range("No frame found with domain " + domain);
194  }
195  return it->second;
196  }
197 
201  bool hasDomain(std::string const &domain) const { return _domainIndexDict.count(domain) > 0; }
202 
204 
210  void mirrorVariants(std::string const &domain) { mirrorVariants(getIndex(domain)); }
211 
212  using FrameSet::remapFrame;
213 
219  void remapFrame(std::string const &domain, Mapping &map) { remapFrame(getIndex(domain), map); }
220 
222  void removeFrame(int iframe) override;
223 
229  void removeFrame(std::string const &domain);
230 
231  using FrameSet::setBase;
232 
238  void setBase(std::string const &domain) { setBase(getIndex(domain)); }
239 
240  using FrameSet::setCurrent;
241 
247  void setCurrent(std::string const &domain) { setCurrent(getIndex(domain)); }
248 
254  void setDomain(std::string const &domain) override;
255 
256 protected:
257  virtual std::shared_ptr<Object> copyPolymorphic() const override {
258  return copyImpl<FrameDict, AstFrameSet>();
259  }
260 
274  return std::static_pointer_cast<FrameSet>(copyImpl<FrameSet, AstFrameSet>());
275  }
276 
278  explicit FrameDict(AstFrameSet *rawptr);
279 
280 private:
281  /*
282  Build a new domain:index dict from a FrameSet
283  */
284  static std::unordered_map<std::string, int> _makeNewDict(FrameSet const &frameSet);
285 
286  /*
287  Update the internal data using the supplied frameSet, which is swapped with the old data.
288 
289  Build a new domain::index dict from frameSet. If successful, replace the current dict
290  and swap frameSet with the old frame set data.
291 
292  If any exception is thrown this FrameDict (and the frameSet argument) is unchanged.
293  */
294  void _update(FrameSet &frameSet) {
295  _domainIndexDict = _makeNewDict(frameSet);
296  this->swapRawPointers(frameSet);
297  }
298 
299  std::unordered_map<std::string, int> _domainIndexDict; // Dict of frame domain:index
300 };
301 
302 } // namespace ast
303 
304 #endif
table::Key< table::Array< std::uint8_t > > frameSet
table::Key< int > from
table::Key< int > to
A FrameSet whose frames can be referenced by domain name.
Definition: FrameDict.h:67
void addFrame(int iframe, Mapping const &map, Frame const &frame) override
Add a new Frame and an associated Mapping to this FrameSet so as to define a new coordinate system,...
Definition: FrameDict.cc:32
void mirrorVariants(std::string const &domain)
Variant of mirrorVariants(int) with the frame specified by domain.
Definition: FrameDict.h:210
bool hasDomain(std::string const &domain) const
Return True if a frame in this FrameDict has the specified domain.
Definition: FrameDict.h:201
std::shared_ptr< Frame > getFrame(int iframe, bool copy=true) const
Obtain a deep copy of the specified Frame.
Definition: FrameSet.h:270
std::shared_ptr< FrameSet > getFrameSet() const
Return a deep copy as a FrameSet.
Definition: FrameDict.h:273
virtual ~FrameDict()
Definition: FrameDict.h:114
void setCurrent(std::string const &domain)
Variant of setCurrent(int) with the frame specified by domain.
Definition: FrameDict.h:247
std::shared_ptr< FrameDict > copy() const
Return a deep copy of this object.
Definition: FrameDict.h:123
FrameDict & operator=(FrameDict const &)=delete
std::set< std::string > getAllDomains() const
Get the domain names for all contained Frames (excluding frames with empty or defaulted domain names)...
Definition: FrameDict.cc:45
std::shared_ptr< Mapping > getMapping(std::string const &from, int to) const
Variant of FrameSet::getMapping with the first frame specified by domain.
Definition: FrameDict.h:171
FrameDict(FrameDict &&)=default
FrameDict(Frame const &frame, std::string const &options="")
Construct a FrameDict from a single Frame.
Definition: FrameDict.h:81
std::shared_ptr< Mapping > getMapping(std::string const &from, std::string const &to) const
Variant of FrameSet::getMapping with the both frames specified by domain.
Definition: FrameDict.h:180
void removeFrame(int iframe) override
Remove a Frame from a FrameSet.
Definition: FrameDict.cc:53
std::shared_ptr< Mapping > getMapping(int from=BASE, int to=CURRENT) const
Obtain a Mapping that converts between two Frames in a FrameSet.
Definition: FrameSet.h:304
FrameDict(FrameDict const &)=default
Copy constructor: make a deep copy.
void remapFrame(std::string const &domain, Mapping &map)
Variant of remapFrame(int, Mapping&) with the frame specified by domain.
Definition: FrameDict.h:219
FrameDict(Frame const &baseFrame, Mapping const &mapping, Frame const &currentFrame, std::string const &options="")
Construct a FrameDict from two frames and a mapping that connects them.
Definition: FrameDict.h:97
virtual std::shared_ptr< Object > copyPolymorphic() const override
Return a deep copy of this object.
Definition: FrameDict.h:257
FrameDict(FrameSet const &frameSet)
Construct a FrameDict from a FrameSet.
Definition: FrameDict.h:110
FrameDict & operator=(FrameDict &&)=default
int getIndex(std::string const &domain) const
Get the index of a frame specified by domain.
Definition: FrameDict.h:189
std::shared_ptr< Mapping > getMapping(int from, std::string const &to) const
Variant of FrameSet::getMapping with the second frame specified by domain.
Definition: FrameDict.h:162
void setBase(std::string const &domain)
Variant of setBase(int) with the frame specified by domain.
Definition: FrameDict.h:238
std::shared_ptr< Frame > getFrame(std::string const &domain, bool copy=true) const
Variant of getFrame(int, bool) where the frame is specified by domain name.
Definition: FrameDict.h:151
void setDomain(std::string const &domain) override
Set the domain of the current frame (and update the internal dict).
Definition: FrameDict.cc:61
Frame is used to represent a coordinate system.
Definition: Frame.h:157
A FrameSet consists of a set of one or more Frames (which describe coordinate systems),...
Definition: FrameSet.h:99
std::shared_ptr< Frame > getFrame(int iframe, bool copy=true) const
Obtain a deep copy of the specified Frame.
Definition: FrameSet.h:270
void setBase(int ind)
Set Base: index of base Frame.
Definition: FrameSet.h:471
void setCurrent(int ind)
Set Current: index of current Frame, starting from 1.
Definition: FrameSet.h:476
void mirrorVariants(int iframe)
Indicates that all access to the Variant attribute of the current Frame should should be forwarded to...
Definition: FrameSet.h:367
std::shared_ptr< Mapping > getMapping(int from=BASE, int to=CURRENT) const
Obtain a Mapping that converts between two Frames in a FrameSet.
Definition: FrameSet.h:304
void remapFrame(int iframe, Mapping &map)
Modify the relationship (i.e.
Definition: FrameSet.h:410
An abstract base class for objects which transform one set of coordinates to another.
Definition: Mapping.h:59
Abstract base class for all AST objects.
Definition: Object.h:51
friend class FrameDict
Definition: Object.h:53
T count(T... args)
T end(T... args)
T find(T... args)
std::string stringToUpper(std::string const &str)
Return a copy of a string in which all characters are uppercase.
Definition: utils.h:108
AST wrapper classes and functions.