Browse Source

Cleaned up Rehash a bit; also not completely happy with this

release/0.19
Jessica James 8 years ago
parent
commit
66fdd1aa7b
  1. 137
      Jupiter/Rehash.cpp
  2. 64
      Jupiter/Rehash.h

137
Jupiter/Rehash.cpp

@ -1,5 +1,5 @@
/**
* Copyright (C) 2014-2015 Jessica James.
* Copyright (C) 2014-2016 Jessica James.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@ -28,126 +28,117 @@
class RehashFunction : public Jupiter::Rehashable
{
public:
int(*function)(void);
int OnRehash() { return function(); };
bool OnBadRehash(bool removed) { return removed; };
RehashFunction(int(*func)(void)) { RehashFunction::function = func; };
Jupiter::OnRehashFunctionType m_function;
int OnRehash()
{
return m_function();
}
bool OnBadRehash(bool in_removed)
{
return in_removed;
}
RehashFunction(Jupiter::OnRehashFunctionType in_function)
{
m_function = in_function;
}
};
/** List of Rehashable objects */
Jupiter::DLList<Jupiter::Rehashable> rehashables;
Jupiter::DLList<Jupiter::Rehashable> o_rehashables;
/** List of RehashFunction objects */
Jupiter::DLList<RehashFunction> rehashFunctions;
Jupiter::DLList<RehashFunction> o_rehash_functions;
Jupiter::Rehashable::Rehashable()
{
rehashables.add(this);
o_rehashables.add(this);
}
Jupiter::Rehashable::Rehashable(const Jupiter::Rehashable &)
{
rehashables.add(this);
o_rehashables.add(this);
}
Jupiter::Rehashable::~Rehashable()
{
if (rehashables.size() != 0)
for (Jupiter::DLList<Jupiter::Rehashable>::Node *node = o_rehashables.getHead(); node != nullptr; node = node->next)
{
for (Jupiter::DLList<Jupiter::Rehashable>::Node *n = rehashables.getNode(0); n != nullptr; n = n->next)
if (node->data == this)
{
if (n->data == this)
{
rehashables.remove(n);
break;
}
o_rehashables.remove(node);
break;
}
}
}
unsigned int Jupiter::rehash()
size_t Jupiter::rehash()
{
if (rehashables.size() == 0) return 0;
unsigned int total = 0;
int r;
Jupiter::DLList<Jupiter::Rehashable>::Node *n = rehashables.getNode(0);
Jupiter::DLList<Jupiter::Rehashable>::Node *d;
while (n != nullptr)
size_t total_errors = 0;
int rehash_result;
Jupiter::DLList<Jupiter::Rehashable>::Node *node = o_rehashables.getHead();
Jupiter::DLList<Jupiter::Rehashable>::Node *dead_node;
while (node != nullptr)
{
r = n->data->OnRehash();
if (r != 0)
rehash_result = node->data->OnRehash();
if (rehash_result != 0)
{
total++;
if (r < 0)
++total_errors;
if (rehash_result < 0)
{
d = n;
n = n->next;
if (d->data->OnBadRehash(true))
delete rehashables.remove(d);
else rehashables.remove(d);
dead_node = node;
node = node->next;
if (dead_node->data->OnBadRehash(true))
delete o_rehashables.remove(dead_node);
else
o_rehashables.remove(dead_node);
continue;
}
rehashables.remove(n)->OnBadRehash(false);
o_rehashables.remove(node)->OnBadRehash(false);
}
n = n->next;
node = node->next;
}
return total;
return total_errors;
}
unsigned int Jupiter::getRehashableCount()
size_t Jupiter::getRehashableCount()
{
return rehashables.size();
return o_rehashables.size();
}
void Jupiter::addOnRehash(int(*function)(void))
void Jupiter::addOnRehash(OnRehashFunctionType in_function)
{
rehashFunctions.add(new RehashFunction(function));
o_rehash_functions.add(new RehashFunction(in_function));
}
bool Jupiter::removeOnRehash(int(*function)(void))
bool Jupiter::removeOnRehash(OnRehashFunctionType in_function)
{
if (rehashFunctions.size() == 0) return false;
for (Jupiter::DLList<RehashFunction>::Node *n = rehashFunctions.getNode(0); n != nullptr; n = n->next)
for (Jupiter::DLList<RehashFunction>::Node *node = o_rehash_functions.getHead(); node != nullptr; node = node->next)
{
if (n->data->function == function)
if (node->data->m_function == in_function)
{
delete rehashFunctions.remove(n);
delete o_rehash_functions.remove(node);
return true;
}
}
return false;
}
unsigned int Jupiter::removeAllOnRehash()
{
unsigned int r = rehashFunctions.size();
while (rehashFunctions.size() != 0) delete rehashFunctions.remove(size_t{ 0 });
return r;
}
// C forward interfaces
extern "C" unsigned int Jupiter_rehash()
{
return Jupiter::rehash();
return false;
}
extern "C" unsigned int Jupiter_getRehashableCount()
size_t Jupiter::removeAllOnRehash()
{
return Jupiter::getRehashableCount();
}
size_t result = o_rehash_functions.size();
extern "C" void Jupiter_addOnRehash(int(*function)(void))
{
return Jupiter::addOnRehash(function);
}
while (o_rehash_functions.size() != 0)
delete o_rehash_functions.remove(size_t{ 0 });
extern "C" bool Jupiter_removeOnRehash(int(*function)(void))
{
return Jupiter::removeOnRehash(function);
return result;
}
extern "C" unsigned int Jupiter_removeAllOnRehash()
{
return Jupiter::removeAllOnRehash();
}

64
Jupiter/Rehash.h

@ -1,5 +1,5 @@
/**
* Copyright (C) 2014-2015 Jessica James.
* Copyright (C) 2014-2016 Jessica James.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@ -26,11 +26,8 @@
#include "Jupiter.h"
#if defined __cplusplus
namespace Jupiter
{
class JUPITER_API Rehashable
{
public:
@ -72,21 +69,23 @@ namespace Jupiter
*
* @return Number of objects which returned an error.
*/
JUPITER_API unsigned int rehash();
JUPITER_API size_t rehash();
/**
* @brief Fetches the number of rehashable objects.
*
* @return Number of rehashable objects.
*/
JUPITER_API unsigned int getRehashableCount();
JUPITER_API size_t getRehashableCount();
typedef int(*OnRehashFunctionType)(void);
/**
* @brief Adds a function to be called during the rehash process.
*
* @param function Function to call (@see Jupiter::Rehashable::OnRehash()).
*/
JUPITER_API void addOnRehash(int(*function)(void));
JUPITER_API void addOnRehash(OnRehashFunctionType in_function);
/**
* @brief Removes a function from the rehash list.
@ -94,7 +93,7 @@ namespace Jupiter
* @param function Function to remove.
* @return True if a function was removed, false otherwise.
*/
JUPITER_API bool removeOnRehash(int(*function)(void));
JUPITER_API bool removeOnRehash(OnRehashFunctionType in_function);
/**
* @brief Removes all functions from the rehash list.
@ -102,55 +101,8 @@ namespace Jupiter
*
* @return Number of functions removed.
*/
JUPITER_API unsigned int removeAllOnRehash();
JUPITER_API size_t removeAllOnRehash();
} // Jupiter namespace
extern "C"
{
#else
#include <stdbool.h> // bool type
#endif // __cplusplus
/**
* @brief Calls OnRehash() on every rehashable object.
*
* @return Number of objects which returned an error.
*/
JUPITER_API unsigned int Jupiter_rehash();
/**
* @brief Fetches the number of rehashable objects.
*
* @return Number of rehashable objects.
*/
JUPITER_API unsigned int Jupiter_getRehashableCount();
/**
* @brief Adds a function to be called during the rehash process.
*
* @param function Function to call (@see Jupiter::Rehashable::OnRehash()).
*/
JUPITER_API void Jupiter_addOnRehash(int(*function)(void));
/**
* @brief Removes a function from the rehash list.
*
* @param function Function to remove.
* @return True if a function was removed, false otherwise.
*/
JUPITER_API bool Jupiter_removeOnRehash(int(*function)(void));
/**
* @brief Removes all functions from the rehash list.
* Note: This should be called during application clean-up.
*
* @return Number of functions removed.
*/
JUPITER_API unsigned int Jupiter_removeAllOnRehash();
#if defined __cplusplus
} // extern "C"
#endif // __cplusplus
#endif // _REHASH_H_HEADER
Loading…
Cancel
Save