10 #include "boost/assert.hpp" 15 namespace vdj_pipe{
namespace detail{
19 template<
class Id,
class Obj,
template<
class,
class>
class Stor = std::vector >
21 typedef Stor<Obj, std::allocator<Obj> > vector_t;
26 typedef Obj value_type;
27 typedef typename vector_t::iterator iterator;
28 typedef typename vector_t::const_iterator const_iterator;
31 explicit Id_map(
const id_type id0)
37 std::size_t size()
const {
return vid_.size() - erased_.size(); }
38 const_iterator begin()
const {
return vid_.begin();}
39 const_iterator end()
const {
return vid_.end();}
40 bool empty()
const {
return !(vid_.size() - erased_.size());}
41 id_type min_id()
const {
return id0_;}
42 id_type max_id()
const {
return pos2id(vid_.size()-1);}
43 void reserve(
const id_type
id) {vid_.reserve(vpos(
id) + 1);}
45 value_type
const& operator[](
const id_type
id)
const {
46 BOOST_ASSERT( check_range(
id) &&
"invalid ID" );
47 return vid_[vpos(
id)];
50 value_type& operator[](
const id_type
id) {
51 BOOST_ASSERT( check_range(
id) &&
"invalid ID" );
52 return vid_[vpos(
id)];
55 value_type
const& at(
const id_type
id)
const {
56 if( ! check_range(
id) ) BOOST_THROW_EXCEPTION(
58 << Err::msg_t(
"invalid ID")
61 return vid_[vpos(
id)];
64 value_type& at(
const id_type
id) {
65 if( ! check_range(
id) ) BOOST_THROW_EXCEPTION(
67 << Err::msg_t(
"invalid ID")
70 return vid_[vpos(
id)];
73 value_type
const* find(
const id_type
id)
const {
74 if( check_range(
id) )
return & this->operator[](
id);
78 value_type* find(
const id_type
id) {
79 if( check_range(
id) )
return & this->operator[](
id);
83 id_type insert(value_type
const& obj) {
84 if( erased_.empty() ) {
85 const id_type
id = pos2id(vid_.size());
89 const id_type
id = erased_.back();
91 const std::size_t n = vpos(
id);
92 BOOST_ASSERT(vid_.size() > n);
102 const std::size_t n = vpos(
id);
103 if( (! erased_.erase(
id)) && (n >= vid_.size()) ) {
109 void erase(
const id_type
id) {
110 BOOST_ASSERT( check_range(
id) );
111 const std::size_t pos = vpos(
id);
113 vid_[pos] = value_type();
126 std::size_t vpos(
const id_type
id)
const {
127 BOOST_ASSERT(
id >= id0_ &&
"invalid ID");
128 return id() - id0_();
131 id_type pos2id(
const std::size_t n)
const {
return id_type(n + id0_());}
133 bool check_range(
const id_type
id)
const {
134 return id >= id0_ && vpos(
id) < vid_.size();
Main namespace of vdj_pipe library.
Definition: sequence_file.hpp:14
Definition: id_map.hpp:20
value_type & insert(const id_type id)
Definition: id_map.hpp:101
Definition: exception.hpp:23