Flow123d  last_with_con_2.0.0-4-g42e6930
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 }
32 
33 
34 
36 : Scalar(other), data_(other.data_)
37 { }
38 
39 
40 
41 Selection::Selection(const string &name, const string &desc)
42 : data_(std::make_shared<SelectionData>(name))
43 {
44  data_->description_=desc;
45 }
46 
47 
48 
49 Selection &Selection::add_value(const int value, const std::string &key, const std::string &description) {
50  ASSERT(!is_finished())(key)(type_name()).error("Declaration of new key in finished Selection.");
51 
52  data_->add_value(value, key, description);
53  return *this;
54 }
55 
57  this->add_attribute_(key, value);
58  return *this;
59 }
60 
61 
62 const Selection & Selection::close() const {
63  data_->closed_=true;
65 }
66 
67 
68 
69 
70 
72 {
73  std::size_t seed=0;
74  boost::hash_combine(seed, "Selection");
75  boost::hash_combine(seed, type_name());
76  boost::hash_combine(seed, data_->description_);
77  for( Key &key : data_->keys_) {
78  boost::hash_combine(seed, key.key_);
79  boost::hash_combine(seed, key.description_);
80  boost::hash_combine(seed, key.value);
81  }
82  return seed;
83 }
84 
85 
86 
87 bool Selection::is_finished() const {
88  return is_closed();
89 }
90 
91 
92 bool Selection::is_closed() const {
93  return data_->closed_;
94 }
95 
96 string Selection::type_name() const {
97  return data_->type_name_;
98 }
99 
100 
101 
102 bool Selection::operator==(const TypeBase &other) const {
103  return typeid(*this) == typeid(other) && (type_name() == static_cast<const Selection *>(&other)->type_name());
104 }
105 
106 
107 
108 int Selection::name_to_int(const string &key) const {
109  finished_check();
110  KeyHash key_h = key_hash(key);
111  SelectionData::key_to_index_const_iter it = data_->key_to_index_.find(key_h);
112  if (it != data_->key_to_index_.end())
113  return (data_->keys_[it->second].value);
114  else
115  throw ExcSelectionKeyNotFound() << EI_KeyName(key) << EI_Selection(*this);
116 }
117 
118 
119 string Selection::int_to_name(const int &val) const {
120  finished_check();
121  auto it = data_->value_to_index_.find(val);
122  if (it != data_->value_to_index_.end())
123  return data_->keys_[it->second].key_;
124  else
125  throw ExcSelectionValueNotFound() << EI_Value(val) << EI_Selection(*this);
126 }
127 
128 
130 {
131  for (auto it = sel.begin(); it != sel.end(); ++it)
132  {
133  int value = it->value;
134  while (data_->value_to_index_.find(value) != data_->value_to_index_.end()) value++;
135  add_value(value, it->key_, it->description_);
136  }
137 
138  return *this;
139 }
140 
141 
142 
143 string Selection::key_list() const {
144  ostringstream os;
145  for(unsigned int i=0; i<size(); i++) os << "'" <<data_->keys_[i].key_ << "' ";
146  return os.str();
147 }
148 
149 
150 
151 // Implements @p TypeBase::make_instance.
153  return std::make_pair( std::make_shared<Selection>(*this), ParameterMap() );
154 }
155 
156 
157 
158 void Selection::SelectionData::add_value(const int value, const std::string &key, const std::string &description) {
159  KeyHash key_h = TypeBase::key_hash(key);
160  ASSERT(key_to_index_.find(key_h) == key_to_index_.end())(key)(type_name_).error("Key already exists in Selection.");
161  value_to_index_const_iter it = value_to_index_.find(value);
162  const std::string &existing_key = keys_[it->second].key_;
163  ASSERT(it == value_to_index_.end())(value)(key)(existing_key)(type_name_)
164  .error("Value of new key conflicts with value of existing key in Selection");
165 
166  unsigned int new_idx = key_to_index_.size();
167  key_to_index_.insert(std::make_pair(key_h, new_idx));
168  value_to_index_.insert(std::make_pair(value, new_idx));
169 
170  Key tmp_key = { new_idx, key, description, value };
171  keys_.push_back(tmp_key);
172 }
173 
174 
175 
176 
177 
178 } // closing namespace Type
179 } // close namespace Input
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:79
unsigned int size() const
Returns number of values in 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:77
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:97
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.
Base of all scalar types.
Definition: type_base.hh:424
TypeHash content_hash() const override
Implements TypeBase::content_hash.
static KeyHash key_hash(const string &str)
Hash function.
Definition: type_base.hh:243
void add_value(const int value, const std::string &key, const std::string &description)
Inster new value to the Selection.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
void add_attribute_(std::string name, json_string val)
Add attribute of given name to attribute map.
Definition: type_base.cc:106
Selection()
Default constructor. Empty selection.
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:240
Structure for description of one key in selection.
std::map< std::string, TypeHash > ParameterMap
Defines map of used parameters.
Definition: type_base.hh:95
string type_name() const override
Implements Type::TypeBase::type_name.
Selection & add_value(const int value, const std::string &key, const std::string &description="")
Adds one new value with name given by key to the Selection.
bool operator==(const TypeBase &other) const override
Implements TypeBase::operator== compare also Selection names.
keys_const_iterator end() const
Container-like access to the keys of the Selection.
bool is_closed() const override
Implements TypeBase::is_closed.
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:85
void finished_check() const
Assertion for finished Selection (methods are called in correct order).
keys_const_iterator begin() const
Container-like access to the keys of the Selection.
string key_list() const
Create string from values of keys.
boost::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::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:82
bool is_finished() const override
Implements TypeBase::is_finished.
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Template for classes storing finite set of named values.
std::string type_name_(double)
Definition: field_values.cc:22