From: hama Date: Sat, 13 Aug 2016 20:14:37 +0000 (+0200) Subject: compareItems(), new() X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=6f1eb1bb6769db8d31e6e44d756443fa88477e06;p=cpidjinn compareItems(), new() * compareItems() moved to ItemFactory * malloc() replaced by new operator --- diff --git a/.gitignore b/.gitignore index 1638e53..0f0e3d7 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,7 @@ sv.gdb sv.sh util/test.wav +/.cproject +/.project +/cpidjinn.cbp +/cpidjinn.layout diff --git a/util/arraylist.cpp b/util/arraylist.cpp index 83140a1..3652de1 100644 --- a/util/arraylist.cpp +++ b/util/arraylist.cpp @@ -32,11 +32,10 @@ BaseArrayList::BaseArrayList(ItemFactory& factory, int capacity, int blocksize, m_blocksize(blocksize <= 0 ? 16 : blocksize), m_maxBlocksize(maxBlocksize), m_count(0), - m_buffer((void**)malloc((m_capacity + 1) * sizeof m_buffer[0])), + m_buffer(new void*[m_capacity]), m_sorted(sorted), m_factory(factory) { - m_buffer[m_capacity] = (void**) MAGIC; TRACE2("buffer: %llx capacity: %d\n", (long long int) m_buffer, m_capacity); memset(m_buffer, 0, m_capacity * sizeof m_buffer[0] ); } @@ -51,11 +50,10 @@ BaseArrayList::BaseArrayList ( const BaseArrayList& source ) : m_blocksize(source.m_blocksize), m_maxBlocksize(source.m_maxBlocksize), m_count(source.m_count), - m_buffer((void**) malloc((source.m_capacity + 1) * sizeof m_buffer[0])), + m_buffer(new void*[m_capacity]), m_sorted(source.m_sorted), m_factory(source.m_factory) { - m_buffer[m_capacity] = (void**) MAGIC; TRACE2("buffer (copy): %llx capacity: %d\n", (long long int) m_buffer, m_capacity); ensuresSize(m_count = source.m_count); for (int ix = 0; ix < m_count; ix++){ @@ -70,8 +68,7 @@ BaseArrayList::BaseArrayList ( const BaseArrayList& source ) : BaseArrayList::~BaseArrayList() { clear(); TRACE1("buffer del: %llx\n", (long long int) m_buffer); - assert((long long int) m_buffer[m_capacity] == (long long int) MAGIC); - free(m_buffer); + delete[] m_buffer; m_buffer = NULL; } @@ -147,7 +144,7 @@ bool BaseArrayList::binarySearch(const void* item, int& index) const { // binary search over the sorted vector: while (lbound <= ubound) { int half = (ubound + lbound) / 2; - compareRc = compareItems(item, (const void*) m_buffer[half]); + compareRc = m_factory.compareItems(item, (const void*) m_buffer[half]); if (compareRc < 0) ubound = half - 1; else if (compareRc > 0) @@ -176,20 +173,6 @@ BaseArrayList& BaseArrayList::clear(){ return *this; } -/** - * Compares two items. - * - * @param item1 the first item to compare - * @param item2 the 2nd item to compare - * @return 0: the items are equal
- * <0: item1 < item2
- * >0: item2 > item2 - */ -int BaseArrayList::compareItems(const void* item1, const void* item2) const{ - int rc = item1 == item2 ? 0 : (int) ((char*) item1 - (char*) item2); - return rc; -} - /** * Ensures that the list have at least the given size. * @@ -198,19 +181,16 @@ int BaseArrayList::compareItems(const void* item1, const void* item2) const{ */ BaseArrayList& BaseArrayList::ensuresSize(int capacity){ if (capacity > m_capacity){ - assert((long long int) m_buffer[m_capacity] == (long long int) MAGIC); if ((m_blocksize *= 2) > m_maxBlocksize) m_blocksize = m_maxBlocksize; if (capacity - m_capacity < m_blocksize) capacity = m_capacity + m_blocksize; - void** buffer = (void**) malloc(((m_capacity = capacity) + 1) * sizeof (m_buffer[0])); - buffer[m_capacity] = (void**) MAGIC; + void** buffer = new void*[m_capacity = capacity]; TRACE1("buffer (ensure): %llx\n", (long long int) buffer); assert(m_count <= m_capacity && m_count >= 0); memcpy(buffer, m_buffer, sizeof(m_buffer[0]) * m_count); TRACE1("buffer del: %llx\n", (long long int) m_buffer); - free(m_buffer); - + delete[] m_buffer; m_buffer = buffer; } return *this; @@ -230,7 +210,7 @@ int BaseArrayList::indexOf(const void* item) const{ } else { rc = -1; for (int ix = 0; rc == -1 && ix < m_count; ix++){ - if (compareItems(item, (const void*) m_buffer[ix]) == 0) + if (m_factory.compareItems(item, (const void*) m_buffer[ix]) == 0) rc = ix; } } diff --git a/util/arraylist.hpp b/util/arraylist.hpp index c3b0a73..773cc5e 100644 --- a/util/arraylist.hpp +++ b/util/arraylist.hpp @@ -11,11 +11,11 @@ #ifndef ARRAYLIST_H #define ARRAYLIST_H #include "trace.hpp" -#define MAGIC 0xdeadbeafaffedecaLL class ItemFactory { public: virtual void* cloneItem(const void* source) = 0; + virtual int compareItems(const void* item1, const void* item2) const; virtual void destroyItem(const void* item) = 0; }; class BaseArrayList @@ -30,7 +30,6 @@ public: BaseArrayList& add(const void* item, int index = 0x7ffffff); bool binarySearch(const void* item, int& index) const; BaseArrayList& clear(); - virtual int compareItems(const void* item1, const void* item2) const; BaseArrayList& ensuresSize(int capacity); int indexOf(const void* item) const; BaseArrayList& remove(const void* item); @@ -81,7 +80,6 @@ private: return rc; } public: - virtual int compareItems(const T* item1, const T* item2) const = 0; inline int count() const { return m_count; } @@ -122,11 +120,6 @@ public: CStringList ( const CStringList& source ) : ArrayList(source){ } -public: - virtual int compareItems(const char* item1, const char* item2) const{ - int rc = strcmp(item1, item2); - return rc; - } public: DynBuffer& join(DynBuffer& buffer, const char* separator = NULL); void dump(const char* title) const; @@ -142,22 +135,20 @@ public: } public: virtual void* cloneItem(const void* source) { - int length = strlen(reinterpret_cast(source)); - char* rc = (char*) malloc(length + 1 + 8); - memcpy(rc, source, length + 1); - *(long long int *) (rc + length + 1) = MAGIC; - //char* rc = strdup(source); + char* rc = strdup(reinterpret_cast(source)); TRACEF(("strdup [%llx] -> [%llx]: %s\n", (long long int) source, (long long int) rc, (char*) rc)); return rc; } virtual void destroyItem(const void* item) { TRACE2("free [%llx]: %s\n", (long long int) item, (char*) item); // reserved with strdup() - int length = strlen(reinterpret_cast(item)); - if (*(long long int*) ((int8_t*) item + length + 1) != ((long long int) MAGIC)) - printf("\n*********** destroyItem(): assert %llx\n**********\n", - * (long long int*) ((int8_t*)item + length + 1)); ::free((void*) item); } +public: + virtual int compareItems(const void* item1, const void* item2) const{ + int rc = strcmp(reinterpret_cast(item1), + reinterpret_cast(item2)); + return rc; + } }; #endif // ARRAYLIST_H diff --git a/util/cuarraylist.cpp b/util/cuarraylist.cpp index e11c8c1..718f1fe 100644 --- a/util/cuarraylist.cpp +++ b/util/cuarraylist.cpp @@ -181,18 +181,14 @@ class TestArrayList : public UnitTest { } void testCompareItems() { - int capacity = 16; - int blocksize = 16; - int maxBlocksize = 1024*1024; - bool sorted = true; - CStringList list ( capacity, blocksize, maxBlocksize, ! sorted ); + CStringFactory& factory = *CStringFactory::instance(); // same length: - checkT ( list.compareItems ( "abc", "abd" ) < 0 ); - checkT ( list.compareItems ( "abc", "abc" ) == 0 ); - checkT ( list.compareItems ( "abc", "abb" ) > 0 ); + checkT ( factory.compareItems ( "abc", "abd" ) < 0 ); + checkT ( factory.compareItems ( "abc", "abc" ) == 0 ); + checkT ( factory.compareItems ( "abc", "abb" ) > 0 ); // different length: - checkT ( list.compareItems ( "abc", "abc " ) < 0 ); - checkT ( list.compareItems ( "abc ", "abc" ) > 0 ); + checkT ( factory.compareItems ( "abc", "abc " ) < 0 ); + checkT ( factory.compareItems ( "abc ", "abc" ) > 0 ); } void testCount() {