sv.sh
util/test.wav
+/.cproject
+/.project
+/cpidjinn.cbp
+/cpidjinn.layout
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] );
}
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++){
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;
}
// 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)
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<br>
- * <0: item1 < item2<br>
- * >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.
*
*/
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;
} 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;
}
}
#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
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);
return rc;
}
public:
- virtual int compareItems(const T* item1, const T* item2) const = 0;
inline int count() const {
return m_count;
}
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;
}
public:
virtual void* cloneItem(const void* source) {
- int length = strlen(reinterpret_cast<const char*>(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<const char*>(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<const char*>(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<const char*>(item1),
+ reinterpret_cast<const char*>(item2));
+ return rc;
+ }
};
#endif // ARRAYLIST_H
}
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() {