From f82b860f94f9bf107c21acacf9df5aab6cf8c3e7 Mon Sep 17 00:00:00 2001 From: derek Date: Sun, 16 Oct 2011 01:48:00 -0400 Subject: [PATCH] Removed data duplication in last update to Sam*Dictionaries --- CMakeLists.txt | 2 +- docs/Doxyfile | 2 +- src/api/CMakeLists.txt | 2 +- src/api/SamReadGroupDictionary.cpp | 30 +++++++++++++++++++----------- src/api/SamReadGroupDictionary.h | 4 ++-- src/api/SamSequenceDictionary.cpp | 30 +++++++++++++++++++----------- src/api/SamSequenceDictionary.h | 4 ++-- 7 files changed, 45 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bcbdb3f..bc61435 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,7 @@ ensure_out_of_source_build (" # set BamTools version information set (BamTools_VERSION_MAJOR 2) set (BamTools_VERSION_MINOR 0) -set (BamTools_VERSION_BUILD 3) +set (BamTools_VERSION_BUILD 4) # set our library and executable destination dirs set (EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin") diff --git a/docs/Doxyfile b/docs/Doxyfile index 6e882dd..b50929b 100644 --- a/docs/Doxyfile +++ b/docs/Doxyfile @@ -31,7 +31,7 @@ PROJECT_NAME = BamTools # This could be handy for archiving the generated documentation or # if some version control system is used. -PROJECT_NUMBER = 2.0.3 +PROJECT_NUMBER = 2.0.4 # The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) # base path where the generated documentation will be put. diff --git a/src/api/CMakeLists.txt b/src/api/CMakeLists.txt index 3e2ddd5..8278f66 100644 --- a/src/api/CMakeLists.txt +++ b/src/api/CMakeLists.txt @@ -49,7 +49,7 @@ set( BamToolsAPISources # create main BamTools API shared library add_library( BamTools SHARED ${BamToolsAPISources} ) -set_target_properties( BamTools PROPERTIES SOVERSION "2.0.3" ) +set_target_properties( BamTools PROPERTIES SOVERSION "2.0.4" ) set_target_properties( BamTools PROPERTIES OUTPUT_NAME "bamtools" ) # create main BamTools API static library diff --git a/src/api/SamReadGroupDictionary.cpp b/src/api/SamReadGroupDictionary.cpp index 7e36a15..007221a 100644 --- a/src/api/SamReadGroupDictionary.cpp +++ b/src/api/SamReadGroupDictionary.cpp @@ -2,7 +2,7 @@ // SamReadGroupDictionary.cpp (c) 2010 Derek Barnett // Marth Lab, Department of Biology, Boston College // --------------------------------------------------------------------------- -// Last modified: 14 October 2011 (DB) +// Last modified: 16 October 2011 (DB) // --------------------------------------------------------------------------- // Provides methods for operating on a collection of SamReadGroup entries. // *************************************************************************** @@ -47,7 +47,7 @@ SamReadGroupDictionary::~SamReadGroupDictionary(void) { } void SamReadGroupDictionary::Add(const SamReadGroup& readGroup) { if ( IsEmpty() || !Contains(readGroup) ) { m_data.push_back(readGroup); - m_lookupData[readGroup.ID] = readGroup; + m_lookupData[readGroup.ID] = m_data.size() - 1; } } @@ -219,15 +219,20 @@ void SamReadGroupDictionary::Remove(const SamReadGroup& readGroup) { */ void SamReadGroupDictionary::Remove(const std::string& readGroupId) { - SamReadGroupIterator rgIter = Begin(); - SamReadGroupIterator rgEnd = End(); - for ( size_t i = 0 ; rgIter != rgEnd; ++rgIter, ++i ) { - SamReadGroup& rg = (*rgIter); - if( rg.ID == readGroupId ) { - m_data.erase( Begin() + i ); - } + // 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); } @@ -280,10 +285,13 @@ int SamReadGroupDictionary::Size(void) const { \return a modifiable reference to the SamReadGroup associated with the ID */ SamReadGroup& SamReadGroupDictionary::operator[](const std::string& readGroupId) { + if ( !Contains(readGroupId) ) { SamReadGroup rg(readGroupId); m_data.push_back(rg); - m_lookupData[readGroupId] = rg; + m_lookupData[readGroupId] = m_data.size() - 1; } - return m_lookupData[readGroupId]; + + const size_t index = m_lookupData[readGroupId]; + return m_data.at(index); } diff --git a/src/api/SamReadGroupDictionary.h b/src/api/SamReadGroupDictionary.h index c66c6d5..a4aeda9 100644 --- a/src/api/SamReadGroupDictionary.h +++ b/src/api/SamReadGroupDictionary.h @@ -2,7 +2,7 @@ // SamReadGroupDictionary.h (c) 2010 Derek Barnett // Marth Lab, Department of Biology, Boston College // --------------------------------------------------------------------------- -// Last modified: 14 October 2011 (DB) +// Last modified: 16 October 2011 (DB) // --------------------------------------------------------------------------- // Provides methods for operating on a collection of SamReadGroup entries. // *************************************************************************** @@ -77,7 +77,7 @@ class API_EXPORT SamReadGroupDictionary { // data members private: SamReadGroupContainer m_data; - std::map m_lookupData; + std::map m_lookupData; }; } // namespace BamTools diff --git a/src/api/SamSequenceDictionary.cpp b/src/api/SamSequenceDictionary.cpp index 25694f3..5d2ab64 100644 --- a/src/api/SamSequenceDictionary.cpp +++ b/src/api/SamSequenceDictionary.cpp @@ -2,7 +2,7 @@ // SamSequenceDictionary.cpp (c) 2010 Derek Barnett // Marth Lab, Department of Biology, Boston College // --------------------------------------------------------------------------- -// Last modified: 14 October 2011 (DB) +// Last modified: 16 October 2011 (DB) // --------------------------------------------------------------------------- // Provides methods for operating on a collection of SamSequence entries. // ************************************************************************* @@ -47,7 +47,7 @@ SamSequenceDictionary::~SamSequenceDictionary(void) { } void SamSequenceDictionary::Add(const SamSequence& sequence) { if ( IsEmpty() || !Contains(sequence) ) { m_data.push_back(sequence); - m_lookupData[sequence.Name] = sequence; + m_lookupData[sequence.Name] = m_data.size() - 1; } } @@ -223,15 +223,20 @@ void SamSequenceDictionary::Remove(const SamSequence& sequence) { */ void SamSequenceDictionary::Remove(const std::string& sequenceName) { - SamSequenceIterator seqIter = Begin(); - SamSequenceIterator seqEnd = End(); - for ( size_t i = 0 ; seqIter != seqEnd; ++seqIter, ++i ) { - SamSequence& seq = (*seqIter); - if( seq.Name == sequenceName ) { - m_data.erase( Begin() + i ); - } + // skip if empty dictionary or if name unknown + if ( IsEmpty() || !Contains(sequenceName) ) + return; + + // update 'lookup index' for every entry after @sequenceName + const size_t indexToRemove = m_lookupData[sequenceName]; + const size_t numEntries = m_data.size(); + for ( size_t i = indexToRemove+1; i < numEntries; ++i ) { + const SamSequence& sq = m_data.at(i); + --m_lookupData[sq.Name]; } + // erase entry from containers + m_data.erase( Begin() + indexToRemove ); m_lookupData.erase(sequenceName); } @@ -284,10 +289,13 @@ int SamSequenceDictionary::Size(void) const { \return a modifiable reference to the SamSequence associated with the name */ SamSequence& SamSequenceDictionary::operator[](const std::string& sequenceName) { + if ( !Contains(sequenceName) ) { SamSequence seq(sequenceName, 0); m_data.push_back(seq); - m_lookupData[sequenceName] = seq; + m_lookupData[sequenceName] = m_data.size() - 1; } - return m_lookupData[sequenceName]; + + const size_t index = m_lookupData[sequenceName]; + return m_data.at(index); } diff --git a/src/api/SamSequenceDictionary.h b/src/api/SamSequenceDictionary.h index cf7c2f3..d267dbd 100644 --- a/src/api/SamSequenceDictionary.h +++ b/src/api/SamSequenceDictionary.h @@ -2,7 +2,7 @@ // SamSequenceDictionary.h (c) 2010 Derek Barnett // Marth Lab, Department of Biology, Boston College // --------------------------------------------------------------------------- -// Last modified: 14 October 2011 +// Last modified: 16 October 2011 // --------------------------------------------------------------------------- // Provides methods for operating on a collection of SamSequence entries. // *************************************************************************** @@ -77,7 +77,7 @@ class API_EXPORT SamSequenceDictionary { // data members private: SamSequenceContainer m_data; - std::map m_lookupData; + std::map m_lookupData; }; } // namespace BamTools -- 2.39.2