From b3258766c8d3bf1849f93d5a72f6c58eca7d242d Mon Sep 17 00:00:00 2001 From: hama Date: Sat, 24 Jan 2015 00:41:46 +0100 Subject: [PATCH] status() for ReByteBuffer, ReSeqArray and ReHashList --- base/ReByteBuffer.cpp | 30 ++++++++++++++++++++++++++---- base/ReByteBuffer.hpp | 4 +++- base/ReHashList.cpp | 15 +++++++++++++++ base/ReHashList.hpp | 1 + base/ReSeqArray.cpp | 29 ++++++++++++++++++++++++++--- base/ReSeqArray.hpp | 3 +++ cunit/cuReHashList.cpp | 1 + cunit/cuReTraverser.cpp | 7 +++++++ cunit/testall.cpp | 2 +- os/ReDirTools.cpp | 2 +- 10 files changed, 84 insertions(+), 10 deletions(-) diff --git a/base/ReByteBuffer.cpp b/base/ReByteBuffer.cpp index 514f2bd..25bd8e5 100644 --- a/base/ReByteBuffer.cpp +++ b/base/ReByteBuffer.cpp @@ -26,7 +26,8 @@ ReByteBuffer::ReByteBuffer(size_t delta) : // m_primaryBuffer m_buffer(m_primaryBuffer), m_length(0), - m_capacity(sizeof m_primaryBuffer - 1) + m_capacity(sizeof m_primaryBuffer - 1), + m_reallocation(0) { m_buffer[0] = '\0'; } @@ -39,7 +40,8 @@ ReByteBuffer::ReByteBuffer(const char* source) : // m_primaryBuffer m_buffer(m_primaryBuffer), m_length(0), - m_capacity(sizeof m_primaryBuffer) + m_capacity(sizeof m_primaryBuffer), + m_reallocation(0) { m_buffer[0] = '\0'; append(source); @@ -55,7 +57,8 @@ ReByteBuffer::ReByteBuffer(const Byte* source, size_t sourceLength) : // m_primaryBuffer m_buffer(m_primaryBuffer), m_length(0), - m_capacity(sizeof m_primaryBuffer) + m_capacity(sizeof m_primaryBuffer), + m_reallocation(0) { m_buffer[0] = '\0'; append(source, sourceLength); @@ -80,7 +83,8 @@ ReByteBuffer::ReByteBuffer(const ReByteBuffer& source) : // m_primaryBuffer m_buffer(m_primaryBuffer), m_length(0), - m_capacity(sizeof m_primaryBuffer) + m_capacity(sizeof m_primaryBuffer), + m_reallocation(0) { append(source.buffer(), source.length()); } @@ -297,6 +301,7 @@ void ReByteBuffer::ensureSize(size_t size){ delete[] m_buffer; m_buffer = buffer; m_capacity = size; + m_reallocation++; } } /** @@ -594,3 +599,20 @@ bool ReByteBuffer::startsWith(const Byte* head, size_t headLength, const bool ig rc = indexOf(head, headLength, 0, headLength, ignoreCase) == 0; return rc; } + +/** + * Appends the status data of the instance onto the buffer. + * + * @param buffer OUT: the space for the data report + * @param prefix NULL or a prefix of the output + * @return buffer.str() (for chaining) + */ +const char* ReByteBuffer::status(ReByteBuffer& buffer, const char* prefix){ + if (prefix != NULL) + buffer.append(prefix); + buffer.append("capacity:").appendInt(m_capacity) + .append(" length: ").appendInt(m_length) + .append(" realloc: ").appendInt(m_reallocation).append("\n"); + return buffer.str(); +} + diff --git a/base/ReByteBuffer.hpp b/base/ReByteBuffer.hpp index 32545ce..c8d5301 100644 --- a/base/ReByteBuffer.hpp +++ b/base/ReByteBuffer.hpp @@ -166,7 +166,7 @@ public: bool splice(size_t ix, size_t replacedLength, const Byte* source, size_t length); bool startsWith(const Byte* head, size_t headLength = -1, const bool ignoreCase = false) const; - + const char* status(ReByteBuffer& buffer, const char* prefix = NULL); /** @brief Returns the buffer content as C string. * This is exactly the same result as from getBuffer(). * @return The internal used buffer. @@ -185,6 +185,8 @@ protected: size_t m_length; //@ The size of m_buffer. size_t m_capacity; + //@ number of reallocation in ensureSize() + int m_reallocation; }; #endif /* REBYTEBUFFER_H_ */ diff --git a/base/ReHashList.cpp b/base/ReHashList.cpp index d264baf..a4eb368 100644 --- a/base/ReHashList.cpp +++ b/base/ReHashList.cpp @@ -191,5 +191,20 @@ void ReHashList::setCapacity(int maxKeys, int keySpace, int contentSpace){ m_values.setCapacity(maxKeys, contentSpace); } +/** + * Appends the status data of the instance onto the buffer. + * + * @param buffer OUT: the space for the data report + * @param prefix NULL or a prefix of the output + * @return buffer.str() (for chaining) + */ +const char* ReHashList::status(ReByteBuffer& buffer, const char* prefix){ + if (prefix != NULL) + buffer.append(prefix).append("\n", 1); + m_keys.status(buffer, "keys: "); + m_values.status(buffer, "values: "); + return buffer.str(); +} + diff --git a/base/ReHashList.hpp b/base/ReHashList.hpp index 3c0a249..2a7eea7 100644 --- a/base/ReHashList.hpp +++ b/base/ReHashList.hpp @@ -43,6 +43,7 @@ public: void put(const char* key, const char* value); void put(const ReByteBuffer& key, const ReByteBuffer& value); void setCapacity(int maxKeys, int keySpace, int contentSpace); + const char* status(ReByteBuffer& buffer, const char* prefix = NULL); protected: int find(const Byte* key, size_t length) const; diff --git a/base/ReSeqArray.cpp b/base/ReSeqArray.cpp index 36079d1..4efc809 100644 --- a/base/ReSeqArray.cpp +++ b/base/ReSeqArray.cpp @@ -73,7 +73,8 @@ ReSeqArray::ReSeqArray(int deltaList, int deltaBuffer) : m_offsetOfTag(sizeof(Index) + 1), m_offsetOfLength(sizeof(Index)), m_sorted(false), - m_ignoreCase(false) + m_ignoreCase(false), + m_age(0) { } /** @brief Destructor. @@ -96,7 +97,8 @@ ReSeqArray::ReSeqArray(const ReSeqArray& source) m_offsetOfTag(source.m_offsetOfTag), m_offsetOfLength(source.m_offsetOfLength), m_sorted(source.m_sorted), - m_ignoreCase(false) + m_ignoreCase(false), + m_age(0) { } /** @brief Assignment operator. @@ -139,6 +141,7 @@ ReSeqArray::Index ReSeqArray::add(Index index, const Byte* source, setSequence(&seq, m_content.length(), sourceLength, tag); m_content.append(source, sourceLength); Index rc = index; + m_age++; if (m_sorted && index == (Index) -1){ binarySearch(source, sourceLength, rc); m_list.insert(rc * m_entrySize, (Byte*) &seq, m_entrySize); @@ -171,7 +174,7 @@ bool ReSeqArray::binarySearch(const Byte* toFind, int length, Index& index, if (length < 0) length = strlen(toFind); bool rc = false; - int lbound = 0; + int lbound = 0; int theCount = count(); int ubound = theCount - 1; int compareRc = 0; @@ -210,6 +213,7 @@ bool ReSeqArray::binarySearch(const Byte* toFind, int length, Index& index, void ReSeqArray::clear(){ m_content.setLength(0); m_list.setLength(0); + m_age++; } /** @@ -412,6 +416,7 @@ size_t ReSeqArray::getLengthAndTag(const Sequence* seq, Tag& tag) const{ */ void ReSeqArray::remove(Index index){ if (index <= count()){ + m_age++; Sequence* seq = getInfo(index); size_t currentLength = getLength(seq); // Is this the last entry in m_content? @@ -437,6 +442,7 @@ void ReSeqArray::set(Index index, const Byte* source, if (index >= count()) add(index, source, sourceLength, tag); else { + m_age++; if (sourceLength == (size_t) -1) sourceLength = strlen(source) + 1; Sequence* seq = getInfo(index); @@ -683,6 +689,7 @@ void ReSeqArray::setSizes(int sizeOfTag, int sizeOfLength, size_t constantSize){ void ReSeqArray::sort(){ // Build the heap in array so that largest value is at the root: int theCount = (int) count(); + m_age++; for (int start = (theCount - 2) / 2; start >= 0; start--) { // (shift down the node at index 'start' to the proper place such // that all nodes below the start index are in heap order) @@ -701,6 +708,22 @@ void ReSeqArray::sort(){ shiftDown(0, end); } } +/** + * Appends the status data of the instance onto the buffer. + * + * @param buffer OUT: the space for the data report + * @param prefix NULL or a prefix of the output + * @return buffer.str() (for chaining) + */ +const char* ReSeqArray::status(ReByteBuffer& buffer, const char* prefix){ + if (prefix != NULL) + buffer.append(prefix); + buffer.append("age: ").appendInt(m_age).append("\n"); + m_list.status(buffer, "list: "); + m_content.status(buffer, "content: "); + return buffer.str(); +} + /** * Correct the order in the heap. * diff --git a/base/ReSeqArray.hpp b/base/ReSeqArray.hpp index 9507583..b23817a 100644 --- a/base/ReSeqArray.hpp +++ b/base/ReSeqArray.hpp @@ -72,6 +72,7 @@ public: void setSorted(bool onNotOff); void setIgnoreCase(bool onNotOff); void sort(); + const char* status(ReByteBuffer& buffer, const char* prefix = NULL); protected: /** @brief Returns a pointer of the content buffer. * @return A pointer of the first byte of the content buffer. @@ -116,6 +117,8 @@ protected: bool m_sorted; //@ true: the comparison will be case insensitive bool m_ignoreCase; + //@ number of modifications (insert/replace/remove) + int m_age; }; #endif /* RESEQLIST_H_ */ diff --git a/cunit/cuReHashList.cpp b/cunit/cuReHashList.cpp index 6d29295..a8ee236 100644 --- a/cunit/cuReHashList.cpp +++ b/cunit/cuReHashList.cpp @@ -58,6 +58,7 @@ private: maxKeys, key.setLength(0).appendMilliSec(duration1).str(), countGet, value.setLength(0).appendMilliSec(duration2).str(), collisions); + log(false, hash.status(value.setLength(0))); } void testBasic(){ ReHashList hash; diff --git a/cunit/cuReTraverser.cpp b/cunit/cuReTraverser.cpp index 4c9c0c2..2297529 100644 --- a/cunit/cuReTraverser.cpp +++ b/cunit/cuReTraverser.cpp @@ -58,6 +58,8 @@ private: } void run(){ initTree(); + + testToolStatistic(); testBasic(); testDirOptions(); checkSetFilterFromProgramArgs(); @@ -223,6 +225,11 @@ private: checkT(buffer.startsWith(" 0.000087 MB 5 6\t")); expected.set(m_base.str(), m_base.length()); } + void testToolStatistic(){ + ReDirTools tools; + const char* argv[] = { "dt", "stat", "-P;*;-*/cache/*", m_base.str(), "2" }; + tools.main(5, (char**) argv); + } }; extern void testReTraverser(void); diff --git a/cunit/testall.cpp b/cunit/testall.cpp index 2d9cb08..45e9f01 100644 --- a/cunit/testall.cpp +++ b/cunit/testall.cpp @@ -65,8 +65,8 @@ void testMath(){ void testAll(){ try { - testOs(); testBase(); + testOs(); testMath(); testString(); } catch (ReException e){ diff --git a/os/ReDirTools.cpp b/os/ReDirTools.cpp index b088e7c..7fe7660 100644 --- a/os/ReDirTools.cpp +++ b/os/ReDirTools.cpp @@ -150,7 +150,7 @@ void ReDirOptions::addStandardFilterOptions(){ m_programArgs.addInt("mindepth", i18n("the depth of the subdirectory is greater or equal \n" "0: search is done in all subdirectories"), - 'd', "min-depth", 512); + 'd', "min-depth", 0); m_programArgs.addString("output", i18n("the name of the output file.\n" "The output will written to this file instead of stdout"), -- 2.39.5