Skip to content

Commit

Permalink
Merge pull request #498 from gsomix/CSet
Browse files Browse the repository at this point in the history
Fixes in CSet
  • Loading branch information
karlnapf committed May 5, 2012
2 parents 3ceca27 + 1786947 commit 64d63bd
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
3 changes: 2 additions & 1 deletion examples/undocumented/libshogun/library_hashset.cpp
Expand Up @@ -16,8 +16,9 @@ int main(int argc, char** argv)
set->remove(0.1);
set->add(0.4);

delete set;
exit_shogun();

delete set;
return 0;
}

2 changes: 1 addition & 1 deletion src/shogun/base/init.cpp
Expand Up @@ -54,7 +54,7 @@ namespace shogun
sg_math = new shogun::CMath();
#ifdef TRACE_MEMORY_ALLOCS
if (!sg_mallocs)
sg_mallocs = new shogun::CSet<MemoryBlock>(631, 1024);
sg_mallocs = new shogun::CSet<MemoryBlock>(631, 1024, false);

SG_REF(sg_mallocs);
#endif
Expand Down
68 changes: 55 additions & 13 deletions src/shogun/lib/Set.h
Expand Up @@ -55,40 +55,67 @@ template<class T> class CSet: public CSGObject
num_elements=0;
hash_array=NULL;
array=NULL;
use_sg_mallocs=false;
}

/** Custom constructor */
CSet(int32_t size, int32_t reserved=1024)
CSet(int32_t size, int32_t reserved=1024, bool tracable=true)
{
hash_size=size;
free_index=0;
num_elements=0;
use_sg_mallocs=tracable;

if(use_sg_mallocs)
{
hash_array=SG_CALLOC(HashSetNode<T>*, size);
}
else
{
hash_array=(HashSetNode<T>**) calloc(size, sizeof(HashSetNode<T>*));
}

hash_array=SG_MALLOC(HashSetNode<T>*, size);
for (int32_t i=0; i<size; i++)
{
hash_array[i]=NULL;
}

array=new DynArray<HashSetNode<T>*>(reserved);
array=new DynArray<HashSetNode<T>*>(reserved, tracable);
}

/** Default destructor */
virtual ~CSet()
{
if (hash_array!=NULL)
if (array!=NULL)
{
for (int32_t i=0; i<hash_size; i++)
for(int32_t i=0; i<array->get_num_elements(); i++)
{
delete hash_array[i];
if(array->get_element(i)!=NULL)
{
if(use_sg_mallocs)
{
SG_FREE(array->get_element(i));
}
else
{
free(array->get_element(i));
}
}
}
SG_FREE(hash_array);
delete array;
}
if (array!=NULL)

if (hash_array!=NULL)
{
delete array;
}
if(use_sg_mallocs)
{
SG_FREE(hash_array);
}
else
{
free(hash_array);
}
}
}

/** @return object name */
Expand Down Expand Up @@ -187,7 +214,7 @@ template<class T> class CSet: public CSGObject
*/
T* get_element_ptr(int32_t index)
{
return &(array->get_element_ptr(index)->element);
return &(array->get_element(index)->element);
}

/** operator overload for set read only access
Expand Down Expand Up @@ -260,10 +287,23 @@ template<class T> class CSet: public CSGObject
int32_t new_index;
HashSetNode<T>* new_node;

if(array==NULL)
{
return;
}

if ((free_index>=array->get_num_elements()) || (array->get_element(free_index)==NULL))
{
// init new node
new_node=new HashSetNode<T>;
if(use_sg_mallocs)
{
new_node=SG_MALLOC(HashSetNode<T>, 1);
}
else
{
new_node=(HashSetNode<T>*) calloc(1, sizeof(HashSetNode<T>));
}

array->append_element(new_node);

new_index=free_index;
Expand Down Expand Up @@ -332,6 +372,8 @@ template<class T> class CSet: public CSGObject


protected:
/** */
bool use_sg_mallocs;
/** hashtable size */
int32_t hash_size;

Expand Down
2 changes: 1 addition & 1 deletion src/shogun/lib/memory.h
Expand Up @@ -74,7 +74,7 @@ class MemoryBlock
/** copy constructor
* @param b b
*/
MemoryBlock(const MemoryBlock &b);
MemoryBlock(const MemoryBlock &b);

/** equality
* @param b b
Expand Down

0 comments on commit 64d63bd

Please sign in to comment.