From 9c692916410d9cc5f1d27a5fac8a997f202411bb Mon Sep 17 00:00:00 2001 From: hama Date: Thu, 15 Jan 2015 00:14:29 +0100 Subject: [PATCH] Refactoring ReHashList (using sorted ReSeqArray) --- base/ReHashList.cpp | 9 ++------- base/ReSeqArray.cpp | 15 +++++++++------ base/ReSeqArray.hpp | 8 ++++---- cunit/cuReSeqArray.cpp | 14 ++++++++------ cunit/testall.cpp | 5 ++--- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/base/ReHashList.cpp b/base/ReHashList.cpp index 1b32acc..2d60e73 100644 --- a/base/ReHashList.cpp +++ b/base/ReHashList.cpp @@ -45,7 +45,7 @@ ReHashList::ReHashList(bool ignoreCase, int keyTagSize, int contentLengthSize, m_keys.setIgnoreCase(ignoreCase); m_keys.setSorted(true); m_keys.setSizes(keyTagSize, keyLengthSize); - m_keys.setSizes(0, contentLengthSize); + m_values.setSizes(0, contentLengthSize); } /** Destructor */ @@ -142,10 +142,6 @@ bool ReHashList::next(ReArrayPosition& position, ReByteBuffer* key, */ void ReHashList::put(const Byte* key, size_t keyLength, const Byte* value, size_t valueLength){ - if (keyLength == (size_t) -1) - keyLength = strlen(key); - if (valueLength == (size_t) -1) - valueLength = strlen(value); ReSeqArray::Index index; ReSeqArray::Tag tag; if (m_keys.binarySearch(key, keyLength, index, &tag)){ @@ -153,8 +149,7 @@ void ReHashList::put(const Byte* key, size_t keyLength, m_values.set((ReSeqArray::Index) tag, value, valueLength, 0); } else { // insert a new value. - tag = (ReSeqArray::Tag) m_values.count(); - m_values.add(-1, value, valueLength); + tag = (ReSeqArray::Tag) m_values.add(-1, value, valueLength); m_keys.add(index, key, keyLength, tag); } } diff --git a/base/ReSeqArray.cpp b/base/ReSeqArray.cpp index e266dcd..cca2b36 100644 --- a/base/ReSeqArray.cpp +++ b/base/ReSeqArray.cpp @@ -132,7 +132,7 @@ ReSeqArray::Index ReSeqArray::add(Index index, const Byte* source, m_list.insert(rc * m_entrySize, (Byte*) &seq, m_entrySize); } else { if (index >= count()){ - rc = m_content.length(); + rc = count(); m_list.append((Byte*) &seq, m_entrySize); }else{ m_list.insert(index * m_entrySize, (Byte*) &seq, m_entrySize); @@ -161,7 +161,7 @@ bool ReSeqArray::binarySearch(const Byte* toFind, int length, Index& index, bool rc = false; int lbound = 0; int theCount = count(); - int ubound = theCount; + int ubound = theCount - 1; int compareRc = 0; // binary search over the sorted vector: while(lbound <= ubound){ @@ -183,6 +183,8 @@ bool ReSeqArray::binarySearch(const Byte* toFind, int length, Index& index, else { rc = true; index = half; + if (tag != NULL) + getLengthAndTag(seq, *tag); break; } } @@ -561,11 +563,12 @@ void ReSeqArray::setSorted(bool onNotOff){ * @param sizeOfLength 0: no length stored (constant length, stored in * m_commonSize). * Otherwise: length in byte: 1, 2, 3, 4 or 5 - * @param constantSize 0 or the size of the sequence if the length is not + * @param constantSize INDIVIDUAL_SIZE: the length will be stored in each element + * 0 or the size of the sequence if the length is not * indiviually stored.
* If > 0 sizeOfLengthmust be 0! */ -void ReSeqArray::setSizes(int sizeOfTag, int sizeOfLength, int constantSize){ +void ReSeqArray::setSizes(int sizeOfTag, int sizeOfLength, size_t constantSize){ switch(m_sizeOfLength = sizeOfLength){ case 0: case 1: @@ -596,10 +599,10 @@ void ReSeqArray::setSizes(int sizeOfTag, int sizeOfLength, int constantSize){ } m_offsetOfLength = sizeof(Index); m_offsetOfTag = sizeof(Index) + m_sizeOfLength; - if (sizeOfLength > 0 && constantSize > 0){ + if (sizeOfLength > 0 && constantSize != INDIVIDUAL_SIZE){ globalLogger()->sayF(LOG_ERROR | CAT_LIB, LC_SET_SIZES_1, i18n("collision of sizeOfLength $1 and constantSize $2")) - .arg(sizeOfLength).arg(constantSize).end(); + .arg(sizeOfLength).arg((int) constantSize).end(); constantSize = 0; } m_commonSize = constantSize; diff --git a/base/ReSeqArray.hpp b/base/ReSeqArray.hpp index 3f06bb2..5cb59fc 100644 --- a/base/ReSeqArray.hpp +++ b/base/ReSeqArray.hpp @@ -35,9 +35,10 @@ public: ReSeqArray(const ReSeqArray& source); ReSeqArray& operator = (const ReSeqArray& source); public: - Index add(Index index, const Byte* source, size_t sourceLength, Tag tag = 0); + Index add(Index index, const Byte* source, size_t sourceLength, + Tag tag = 0); bool binarySearch(const Byte* toFind, int length, Index& index, - Tag* tag = NULL) const; + Tag* tag = NULL) const; void clear(); int compare(Index index1, Index index2); /** @brief Returns the count of defined entries in the list. @@ -64,7 +65,7 @@ public: } void remove(Index index); void set(Index index, const Byte* source, size_t sourceLength, Tag tag); - void setSizes(int sizeOfTag, int sizeOfLength, int constantLength = 0); + void setSizes(int sizeOfTag, int sizeOfLength, size_t constantLength = INDIVIDUAL_SIZE); void setSorted(bool onNotOff); void setIgnoreCase(bool onNotOff); void sort(); @@ -76,7 +77,6 @@ protected: return m_content.buffer(); } friend class ReHashList; - friend class ReStringList; /** @brief Returns the info of an entry of the list. * @param index The index of the wanted entry. * @return The pointer of the entry. diff --git a/cunit/cuReSeqArray.cpp b/cunit/cuReSeqArray.cpp index bdfca22..1a787d0 100644 --- a/cunit/cuReSeqArray.cpp +++ b/cunit/cuReSeqArray.cpp @@ -71,6 +71,8 @@ private: list.add(ix, value.str(), -1, (ReSeqArray::Tag) ix*10); } for (int ix = 0; ix <= count; ix++){ + if (count == 2 && ix == 2) + ix = 2; value.setLength(2); value.buffer()[0] = 'a' + ix; value.buffer()[1] = 'a'; @@ -97,11 +99,11 @@ private: list.add(-1, "abc", -1, 300); list.add(-1, "abc", -1, 350); list.add(-1, "cde", -1, 400); - log(false, "unsorted:"); - list.dump(stdout); + //log(false, "unsorted:"); + //list.dump(stdout); list.setSorted(true); - log(false, "sorted:"); - list.dump(stdout); + //log(false, "sorted:"); + //list.dump(stdout); checkElement(list, 0U, "AbC", 350); checkElement(list, 1U, "AbC", 300); checkElement(list, 2U, "AbCd", 100); @@ -124,7 +126,7 @@ private: ReByteBuffer value, expectedValue; list.add(-1, "bcd", -1, 200); list.add(0, "abc", -1, 100); - list.dump(stdout); + // list.dump(stdout); checkEqu(0U, list.find("AbC", -1)); checkEqu(1U, list.find("BCD", -1)); } @@ -170,7 +172,7 @@ private: if (expectedTag != tag) checkEqu(expectedTag, tag); } - list.dump(stdout); + //list.dump(stdout); expectedValue.setLength(0); for (size_t ix = 0; ix < maxIx; ix++){ expectedTag = -1; diff --git a/cunit/testall.cpp b/cunit/testall.cpp index d181b88..2e8906a 100644 --- a/cunit/testall.cpp +++ b/cunit/testall.cpp @@ -12,9 +12,8 @@ void testBase(){ extern void testReHashList(void); - //testReHashList(); - extern void testReSeqArray(); - testReSeqArray(); + testReHashList(); + extern void testReTestUnit(); // testReTestUnit(); extern void testReByteBuffer(); -- 2.39.5