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 * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -28,126 +28,117 @@
class RehashFunction : public Jupiter::Rehashable class RehashFunction : public Jupiter::Rehashable
{ {
public: public:
int(*function)(void); Jupiter::OnRehashFunctionType m_function;
int OnRehash() { return function(); };
bool OnBadRehash(bool removed) { return removed; }; int OnRehash()
RehashFunction(int(*func)(void)) { RehashFunction::function = func; }; {
return m_function();
}
bool OnBadRehash(bool in_removed)
{
return in_removed;
}
RehashFunction(Jupiter::OnRehashFunctionType in_function)
{
m_function = in_function;
}
}; };
/** List of Rehashable objects */ /** List of Rehashable objects */
Jupiter::DLList<Jupiter::Rehashable> rehashables; Jupiter::DLList<Jupiter::Rehashable> o_rehashables;
/** List of RehashFunction objects */ /** List of RehashFunction objects */
Jupiter::DLList<RehashFunction> rehashFunctions; Jupiter::DLList<RehashFunction> o_rehash_functions;
Jupiter::Rehashable::Rehashable() Jupiter::Rehashable::Rehashable()
{ {
rehashables.add(this); o_rehashables.add(this);
} }
Jupiter::Rehashable::Rehashable(const Jupiter::Rehashable &) Jupiter::Rehashable::Rehashable(const Jupiter::Rehashable &)
{ {
rehashables.add(this); o_rehashables.add(this);
} }
Jupiter::Rehashable::~Rehashable() 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) o_rehashables.remove(node);
{ break;
rehashables.remove(n);
break;
}
} }
} }
} }
unsigned int Jupiter::rehash() size_t Jupiter::rehash()
{ {
if (rehashables.size() == 0) return 0; size_t total_errors = 0;
unsigned int total = 0; int rehash_result;
int r; Jupiter::DLList<Jupiter::Rehashable>::Node *node = o_rehashables.getHead();
Jupiter::DLList<Jupiter::Rehashable>::Node *n = rehashables.getNode(0); Jupiter::DLList<Jupiter::Rehashable>::Node *dead_node;
Jupiter::DLList<Jupiter::Rehashable>::Node *d;
while (n != nullptr) while (node != nullptr)
{ {
r = n->data->OnRehash(); rehash_result = node->data->OnRehash();
if (r != 0) if (rehash_result != 0)
{ {
total++; ++total_errors;
if (r < 0) if (rehash_result < 0)
{ {
d = n; dead_node = node;
n = n->next; node = node->next;
if (d->data->OnBadRehash(true))
delete rehashables.remove(d); if (dead_node->data->OnBadRehash(true))
else rehashables.remove(d); delete o_rehashables.remove(dead_node);
else
o_rehashables.remove(dead_node);
continue; 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 *node = o_rehash_functions.getHead(); node != nullptr; node = node->next)
for (Jupiter::DLList<RehashFunction>::Node *n = rehashFunctions.getNode(0); n != nullptr; n = n->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 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 return false;
extern "C" unsigned int Jupiter_rehash()
{
return Jupiter::rehash();
} }
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)) while (o_rehash_functions.size() != 0)
{ delete o_rehash_functions.remove(size_t{ 0 });
return Jupiter::addOnRehash(function);
}
extern "C" bool Jupiter_removeOnRehash(int(*function)(void)) return result;
{
return Jupiter::removeOnRehash(function);
} }
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 * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -26,11 +26,8 @@
#include "Jupiter.h" #include "Jupiter.h"
#if defined __cplusplus
namespace Jupiter namespace Jupiter
{ {
class JUPITER_API Rehashable class JUPITER_API Rehashable
{ {
public: public:
@ -72,21 +69,23 @@ namespace Jupiter
* *
* @return Number of objects which returned an error. * @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. * @brief Fetches the number of rehashable objects.
* *
* @return 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. * @brief Adds a function to be called during the rehash process.
* *
* @param function Function to call (@see Jupiter::Rehashable::OnRehash()). * @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. * @brief Removes a function from the rehash list.
@ -94,7 +93,7 @@ namespace Jupiter
* @param function Function to remove. * @param function Function to remove.
* @return True if a function was removed, false otherwise. * @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. * @brief Removes all functions from the rehash list.
@ -102,55 +101,8 @@ namespace Jupiter
* *
* @return Number of functions removed. * @return Number of functions removed.
*/ */
JUPITER_API unsigned int removeAllOnRehash(); JUPITER_API size_t removeAllOnRehash();
} // Jupiter namespace } // 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 #endif // _REHASH_H_HEADER
Loading…
Cancel
Save