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")
46  << base_exception::str1_t(sanitize(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")
76  << base_exception::str1_t(sanitize(name))
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_ */
Store values mapped against name strings and value IDs.
Definition: named_value_map.hpp:22
Main namespace of vdj_pipe library.
Definition: sequence_file.hpp:14
Definition: exception.hpp:23
Definition: id_iterator.hpp:16