vdj_pipe
pipeline for processing DNA sequence data
named_value_map.hpp
Go to the documentation of this file.
1 
7 #ifndef NAMED_VALUE_MAP_HPP_
8 #define NAMED_VALUE_MAP_HPP_
9 #include <string>
10 #include "boost/foreach.hpp"
11 #include "boost/assert.hpp"
16 #include "vdj_pipe/exception.hpp"
17 
18 namespace vdj_pipe{ namespace detail{
19 
22 template<class Id, class Val> class Named_value_map {
25 public:
26  typedef Id id_type;
27  typedef Val value_type;
29  typedef iterator const_iterator;
30 
32  : vm_(id_type(1)),
33  nm_(id_type(1))
34  {}
35 
36  iterator begin() const {return iterator(vm_.min_id());}
37  iterator end() const {return ++iterator(vm_.max_id());}
38  std::size_t size() const {return nm_.size();}
39 
40  id_type insert_new_name(std::string const& name) {
41  if( name.empty() ) return id_type(0);
42  const std::pair<id_type, bool> p = nm_.insert(name);
43  if( ! p.second ) BOOST_THROW_EXCEPTION(
45  << base_exception::msg_t("duplicate value name")
47  );
48  vm_.insert(p.first);
49  return p.first;
50  }
51 
52  id_type insert_name(std::string const& name) {
53  if( name.empty() ) return id_type(0);
54  const std::pair<id_type, bool> p = nm_.insert(name);
55  if( p.second ) vm_.insert(p.first);
56  return p.first;
57  }
58 
59  std::string const& name(const id_type id) const {
60  BOOST_ASSERT(id && "invalid ID");
61  return nm_[id];
62  }
63 
64  id_type const* find_id(std::string const& name) const {
65  return nm_.find(name);
66  }
67 
68  id_type value_id(std::string const& name) const {
69  if( name.empty() ) return id_type(0);
70  if( id_type const* vid = nm_.find(name) ) {
71  return *vid;
72  }
73  BOOST_THROW_EXCEPTION(
75  << base_exception::msg_t("value name does not exist")
77  );
78  }
79 
80  value_type const& operator[](const id_type vid) const {
81  BOOST_ASSERT(vid && "invalid ID");
82  return vm_[vid];
83  }
84 
85  value_type& operator[](const id_type vid) {
86  BOOST_ASSERT(vid && "invalid ID");
87  return vm_[vid];
88  }
89 
90  void clear_values() {
91  BOOST_FOREACH(const id_type vid, *this) {
92  vm_[vid] = value_type();
93  }
94  }
95 
96 private:
97  val_map vm_;
98  name_map nm_;
99 };
100 
101 }//namespace detail
102 }//namespace vdj_pipe
103 #endif /* NAMED_VALUE_MAP_HPP_ */
Named_value_map()
Definition: named_value_map.hpp:31
detail::Id_bimap< Id, std::string > name_map
Definition: named_value_map.hpp:24
std::pair< id_type, bool > insert(obj_type const &obj)
Definition: id_bimap.hpp:160
iterator end() const
Definition: named_value_map.hpp:37
val_map vm_
Definition: named_value_map.hpp:97
name_map nm_
Definition: named_value_map.hpp:98
Id_iterator< id_type > iterator
Definition: named_value_map.hpp:28
Store values mapped against name strings and value IDs.
Definition: named_value_map.hpp:22
detail::Id_map< Id, Val > val_map
Definition: named_value_map.hpp:23
id_type const * find(ObjCompat const &obj) const
Definition: id_bimap.hpp:151
Main namespace of vdj_pipe library.
Definition: keywords_variable.hpp:11
id_type min_id() const
Definition: id_map.hpp:41
id_type const * find_id(std::string const &name) const
Definition: named_value_map.hpp:64
Id id_type
Definition: named_value_map.hpp:26
boost::error_info< struct errinfo_str1_, std::string > str1_t
Definition: exception.hpp:25
id_type insert_new_name(std::string const &name)
Definition: named_value_map.hpp:40
iterator const_iterator
Definition: named_value_map.hpp:29
id_type insert_name(std::string const &name)
Definition: named_value_map.hpp:52
Definition: exception.hpp:23
Definition: id_iterator.hpp:16
boost::error_info< struct errinfo_message_, std::string > msg_t
Definition: exception.hpp:24
value_type & operator[](const id_type vid)
Definition: named_value_map.hpp:85
id_type value_id(std::string const &name) const
Definition: named_value_map.hpp:68
std::string sanitize(const char c)
Definition: sanitize_string.cpp:53
std::size_t size() const
Definition: named_value_map.hpp:38
id_type max_id() const
Definition: id_map.hpp:42
std::size_t size() const
Definition: id_bimap.hpp:115
iterator begin() const
Definition: named_value_map.hpp:36
id_type insert(value_type const &obj)
Definition: id_map.hpp:83
void clear_values()
Definition: named_value_map.hpp:90
Val value_type
Definition: named_value_map.hpp:27
value_type const & operator[](const id_type vid) const
Definition: named_value_map.hpp:80
std::string const & name(const id_type id) const
Definition: named_value_map.hpp:59