// SamReadGroupDictionary.cpp (c) 2010 Derek Barnett
// Marth Lab, Department of Biology, Boston College
// ---------------------------------------------------------------------------
-// Last modified: 10 October 2011 (DB)
+// Last modified: 16 October 2011 (DB)
// ---------------------------------------------------------------------------
// Provides methods for operating on a collection of SamReadGroup entries.
// ***************************************************************************
#include "api/SamReadGroupDictionary.h"
using namespace BamTools;
-#include <algorithm>
#include <iostream>
using namespace std;
*/
SamReadGroupDictionary::SamReadGroupDictionary(const SamReadGroupDictionary& other)
: m_data(other.m_data)
+ , m_lookupData(other.m_lookupData)
{ }
/*! \fn SamReadGroupDictionary::~SamReadGroupDictionary(void)
\param[in] readGroup entry to be added
*/
void SamReadGroupDictionary::Add(const SamReadGroup& readGroup) {
- if ( IsEmpty() || !Contains(readGroup) )
+ if ( IsEmpty() || !Contains(readGroup) ) {
m_data.push_back(readGroup);
+ m_lookupData[readGroup.ID] = m_data.size() - 1;
+ }
}
/*! \fn void SamReadGroupDictionary::Add(const std::string& readGroupId)
*/
void SamReadGroupDictionary::Clear(void) {
m_data.clear();
+ m_lookupData.clear();
}
/*! \fn SamReadGroupConstIterator SamReadGroupDictionary::ConstBegin(void) const
\return \c true if dictionary contains a read group with this ID
*/
bool SamReadGroupDictionary::Contains(const std::string& readGroupId) const {
- return ( IndexOf(readGroupId) != (int)m_data.size() );
+ return ( m_lookupData.find(readGroupId) != m_lookupData.end() );
}
/*! \fn bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const
\return \c true if dictionary contains read group (matching on ID).
*/
bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const {
- return Contains( readGroup.ID );
+ return Contains(readGroup.ID);
}
/*! \fn SamReadGroupIterator SamReadGroupDictionary::End(void)
return m_data.end();
}
-/*! \fn int SamReadGroupDictionary::IndexOf(const std::string& readGroupId) const
- \internal
- \return index of read group if found. Otherwise, returns vector::size() (invalid index).
-*/
-int SamReadGroupDictionary::IndexOf(const std::string& readGroupId) const {
- SamReadGroupConstIterator begin = ConstBegin();
- SamReadGroupConstIterator iter = begin;
- SamReadGroupConstIterator end = ConstEnd();
- for ( ; iter != end; ++iter ) {
- const SamReadGroup& current = (*iter);
- if ( current.ID == readGroupId )
- break;
- }
- return distance( begin, iter );
-}
-
/*! \fn bool SamReadGroupDictionary::IsEmpty(void) const
\brief Returns \c true if dictionary contains no read groups
\sa Size()
\param[in] readGroup read group to remove (matches on ID)
*/
void SamReadGroupDictionary::Remove(const SamReadGroup& readGroup) {
- Remove( readGroup.ID );
+ Remove(readGroup.ID);
}
/*! \fn void SamReadGroupDictionary::Remove(const std::string& readGroupId)
\sa Remove()
*/
void SamReadGroupDictionary::Remove(const std::string& readGroupId) {
- if ( Contains(readGroupId) )
- m_data.erase( m_data.begin() + IndexOf(readGroupId) );
+
+ // skip if empty dictionary or if ID unknown
+ if ( IsEmpty() || !Contains(readGroupId) )
+ return;
+
+ // update 'lookup index' for every entry after @readGroupId
+ const size_t indexToRemove = m_lookupData[readGroupId];
+ const size_t numEntries = m_data.size();
+ for ( size_t i = indexToRemove+1; i < numEntries; ++i ) {
+ const SamReadGroup& rg = m_data.at(i);
+ --m_lookupData[rg.ID];
+ }
+
+ // erase entry from containers
+ m_data.erase( Begin() + indexToRemove );
+ m_lookupData.erase(readGroupId);
}
/*! \fn void SamReadGroupDictionary::Remove(const std::vector<SamReadGroup>& readGroups)
*/
SamReadGroup& SamReadGroupDictionary::operator[](const std::string& readGroupId) {
- // look up read group ID
- int index = IndexOf(readGroupId);
-
- // if found, return read group at index
- if ( index != (int)m_data.size() )
- return m_data[index];
-
- // otherwise, append new read group and return reference
- else {
+ if ( !Contains(readGroupId) ) {
SamReadGroup rg(readGroupId);
m_data.push_back(rg);
- return m_data.back();
+ m_lookupData[readGroupId] = m_data.size() - 1;
}
+
+ const size_t index = m_lookupData[readGroupId];
+ return m_data.at(index);
}