Flow123d  release_3.0.0-1182-ga9abfa7
type_selection.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file type_selection.cc
15  * @brief
16  */
17 
18 #include "input/type_selection.hh"
19 #include "input/type_repository.hh"
20 #include <boost/functional/hash.hpp>
21 
22 namespace Input {
23 namespace Type {
24 
25 using std::string;
26 
28 : data_(std::make_shared<SelectionData>("EmptySelection"))
29 {
30  close();
31  finish();
32 }
33 
34 
35 
37 : Scalar(other), data_(other.data_)
38 { }
39 
40 
41 
42 Selection::Selection(const string &name, const string &desc)
43 : data_(std::make_shared<SelectionData>(name))
44 {
45  data_->description_=desc;
46 }
47 
48 
49 
50 Selection &Selection::add_value(const int value, const std::string &key,
51  const std::string &description, TypeBase::attribute_map attributes)
52 {
53  ASSERT(!is_closed())(key)(type_name()).error("Declaration of new key in closed Selection.");
54 
55  data_->add_value(value, key, description, attributes);
56  return *this;
57 }
58 
60  this->add_attribute_(key, value);
61  return *this;
62 }
63 
64 
65 const Selection & Selection::close() const {
66  data_->closed_=true;
68 }
69 
70 
72  ASSERT(finish_type != FinishStatus::none_).error();
73 
74  if (this->is_finished()) return data_->finish_status_;
75 
76  ASSERT(data_->closed_)(this->type_name()).error();
77 
78  data_->finish_status_ = finish_type;
79  return data_->finish_status_;
80 }
81 
82 
83 
85 {
86  std::size_t seed=0;
87  boost::hash_combine(seed, "Selection");
88  boost::hash_combine(seed, type_name());
89  boost::hash_combine(seed, data_->description_);
90  for( Key &key : data_->keys_) {
91  boost::hash_combine(seed, key.key_);
92  boost::hash_combine(seed, key.description_);
93  boost::hash_combine(seed, key.value);
94  }
95  return seed;
96 }
97 
98 
99 
101  return data_->finish_status_;
102 }
103 
104 
106  return data_->finish_status_ != FinishStatus::none_;
107 }
108 
109 
110 bool Selection::is_closed() const {
111  return data_->closed_;
112 }
113 
114 string Selection::type_name() const {
115  return data_->type_name_;
116 }
117 
118 
119 string Selection::class_name() const {
120  return "Selection";
121 }
122 
123 
124 bool Selection::operator==(const TypeBase &other) const {
125  return typeid(*this) == typeid(other) && (type_name() == static_cast<const Selection *>(&other)->type_name());
126 }
127 
128 
129 
130 int Selection::name_to_int(const string &key) const {
131  finished_check();
132  KeyHash key_h = key_hash(key);
133  SelectionData::key_to_index_const_iter it = data_->key_to_index_.find(key_h);
134  if (it != data_->key_to_index_.end())
135  return (data_->keys_[it->second].value);
136  else
137  throw ExcSelectionKeyNotFound() << EI_KeyName(key) << EI_Selection(*this);
138 }
139 
140 
141 string Selection::int_to_name(const int &val) const {
142  finished_check();
143  auto it = data_->value_to_index_.find(val);
144  if (it != data_->value_to_index_.end())
145  return data_->keys_[it->second].key_;
146  else
147  throw ExcSelectionValueNotFound() << EI_Value(val) << EI_Selection(*this);
148 }
149 
150 
152 {
153  for (auto it = sel.begin(); it != sel.end(); ++it)
154  {
155  int value = it->value;
156  while (data_->value_to_index_.find(value) != data_->value_to_index_.end()) value++;
157  add_value(value, it->key_, it->description_);
158  }
159 
160  return *this;
161 }
162 
163 
164 
165 string Selection::key_list() const {
166  ostringstream os;
167  for(unsigned int i=0; i<size(); i++) os << "'" <<data_->keys_[i].key_ << "' ";
168  return os.str();
169 }
170 
171 
172 
173 // Implements @p TypeBase::make_instance.
175  return std::make_pair( std::make_shared<Selection>(*this), ParameterMap() );
176 }
177 
178 
179 
181 : type_name_(name), closed_(false), finish_status_(FinishStatus::none_)
182 {}
183 
184 
185 
186 void Selection::SelectionData::add_value(const int value, const std::string &key,
187  const std::string &description, TypeBase::attribute_map attributes) {
188  KeyHash key_h = TypeBase::key_hash(key);
189  ASSERT(key_to_index_.find(key_h) == key_to_index_.end())(key)(type_name_).error("Key already exists in Selection.");
191  const std::string &existing_key = keys_[it->second].key_;
192  ASSERT(it == value_to_index_.end())(value)(key)(existing_key)(type_name_)
193  .error("Value of new key conflicts with value of existing key in Selection");
194 
195  unsigned int new_idx = key_to_index_.size();
196  key_to_index_.insert(std::make_pair(key_h, new_idx));
197  value_to_index_.insert(std::make_pair(value, new_idx));
198 
199  Key tmp_key = { new_idx, key, description, value, attributes };
200  keys_.push_back(tmp_key);
201 }
202 
203 
204 
205 
206 
207 } // closing namespace Type
208 } // close namespace Input
std::shared_ptr< T > add_type(const T &type)
Add type to TypeRepository if doesn&#39;t exist there or get existing type with same TypeHash.
std::map< int, unsigned int >::const_iterator value_to_index_const_iter
Container-like access to the map of valid value to index.
Base of classes for declaring structure of the input data.
Definition: type_base.hh:99
unsigned int size() const
Returns number of values in the Selection.
SelectionData(const string &name)
Constructor.
std::vector< Key > keys_
Vector of values of the Selection.
string int_to_name(const int &value) const
Returns value name for the given value.
string desc() const
Returns string with Type extensive documentation.
Definition: type_base.cc:100
std::pair< std::shared_ptr< TypeBase >, ParameterMap > MakeInstanceReturnType
Return type of make_instance methods, contains instance of generic type and map of used parameters...
Definition: type_base.hh:118
Abstract linear system class.
Definition: balance.hh:37
int name_to_int(const string &key) const
Converts given value name key to the value.
Selection & copy_values(const Selection &sel)
Copy all keys and values from sel.
Selection & add_attribute(std::string key, TypeBase::json_string value)
std::shared_ptr< SelectionData > data_
Handle to actual Selection data.
FinishStatus finish_status() const override
Implements TypeBase::finish_status.
Base of all scalar types.
Definition: type_base.hh:449
static KeyHash key_hash(const string &str)
Hash function.
Definition: type_base.hh:268
Mat< double, N, 1 > vec
Definition: armor.hh:211
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:346
void add_attribute_(std::string name, json_string val)
Add attribute of given name to attribute map.
Definition: type_base.cc:135
Selection()
Default constructor. Empty selection.
static constexpr bool value
Definition: json.hpp:87
static TypeRepository & get_instance()
Return singleton instance of class.
string KeyHash
The type of hash values used in associative array that translates key names to indices in Record and ...
Definition: type_base.hh:265
Structure for description of one key in selection.
std::map< std::string, TypeHash > ParameterMap
Defines map of used parameters.
Definition: type_base.hh:116
FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_) override
Finish declaration of the Selection type.
string type_name() const override
Implements Type::TypeBase::type_name.
bool is_finished() const override
Implements TypeBase::is_finished.
void add_value(const int value, const std::string &key, const std::string &description, TypeBase::attribute_map attributes)
Inster new value to the Selection.
Selection & add_value(const int value, const std::string &key, const std::string &description="", TypeBase::attribute_map attributes=TypeBase::attribute_map())
Adds one new value with name given by key to the Selection.
bool is_closed() const override
Implements TypeBase::is_closed.
string class_name() const override
Override Type::TypeBase::class_name.
keys_const_iterator end() const
Container-like access to the keys of the Selection.
TypeHash content_hash() const override
Implements TypeBase::content_hash.
std::map< int, unsigned int > value_to_index_
Map : valid value to index.
const Selection & close() const
Close the Selection, no more values can be added.
std::map< KeyHash, unsigned int >::const_iterator key_to_index_const_iter
Container-like access to the map of valid value name to index.
std::string json_string
String stored in JSON format.
Definition: type_base.hh:106
bool operator==(const TypeBase &other) const override
Implements TypeBase::operator== compare also Selection names.
void finished_check() const
Assertion for finished Selection (methods are called in correct order).
std::map< KeyHash, unsigned int > key_to_index_
Map : valid value name to index.
string type_name_
Name of the Selection.
keys_const_iterator begin() const
Container-like access to the keys of the Selection.
string key_list() const
Create string from values of keys.
std::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:102
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Template for classes storing finite set of named values.