1 #ifndef GENESIS_SEQUENCE_SEQUENCE_DICT_H_
2 #define GENESIS_SEQUENCE_SEQUENCE_DICT_H_
43 #include <unordered_map>
87 inline std::string
const&
label()
const
133 void add(
Sequence const& sequence,
bool also_look_up_first_word =
true )
135 add( sequence.
label(), sequence.
length(), also_look_up_first_word );
143 void add( std::string
const& name,
size_t length,
bool also_look_up_first_word =
true )
148 add( std::move( e ), also_look_up_first_word );
156 void add(
Entry const& entry,
bool also_look_up_first_word =
true )
158 add(
Entry{ entry }, also_look_up_first_word);
171 void add(
Entry&& entry,
bool also_look_up_first_word =
true )
175 if( indices_.count( entry.name ) > 0 ) {
176 throw std::runtime_error(
177 "Cannot add duplicate sequence name \"" + entry.name +
"\" to SequenceDict."
180 assert( indices_.count( entry.name ) == 0 );
185 auto const label2 =
utils::split( entry.name,
"\t " )[0];
186 if( also_look_up_first_word && indices_.count( label2 ) > 0 ) {
187 throw std::runtime_error(
188 "Cannot add duplicate sequence name \"" + label2 +
"\" to SequenceDict, "
189 "which is the shortened version of the original name \"" + entry.name +
"\"."
196 indices_[ entry.name ] = entries_.size();
197 if( also_look_up_first_word ) {
198 indices_[ label2 ] = entries_.size();
200 entries_.push_back( entry );
201 assert( indices_.count( entry.name ) == 1 && indices_[ entry.name ] == entries_.size() - 1 );
203 ! also_look_up_first_word ||
204 ( indices_.count( label2 ) == 1 && indices_[ label2 ] == entries_.size() - 1 )
220 return entries_.size();
225 return entries_[index];
230 return entries_.at(index);
236 assert( idx < entries_.size() );
237 return entries_[ idx ];
242 auto const it = indices_.find( name );
243 if( it == indices_.end() ) {
244 throw std::runtime_error(
245 "Sequence name \"" + name +
"\" not found in SequenceDict."
248 assert( entries_[it->second].name == name );
254 return indices_.count( name ) > 0;
259 auto const it = indices_.find( name );
260 if( it == indices_.end() ) {
261 return entries_.end();
263 assert( entries_[it->second].name == name );
264 return entries_.begin() + it->second;
273 return entries_.cbegin();
278 return entries_.cend();
288 std::vector<Entry> entries_;
289 std::unordered_map<std::string, size_t> indices_;
296 #endif // include guard