From 66fdd1aa7bcef46fe3318f854954d95b4c1f2846 Mon Sep 17 00:00:00 2001 From: Jessica James Date: Wed, 21 Dec 2016 22:38:08 -0500 Subject: [PATCH] Cleaned up Rehash a bit; also not completely happy with this --- Jupiter/Rehash.cpp | 137 +++++++++++++++++++++------------------------ Jupiter/Rehash.h | 64 +++------------------ 2 files changed, 72 insertions(+), 129 deletions(-) diff --git a/Jupiter/Rehash.cpp b/Jupiter/Rehash.cpp index d2bf0a9..aab2464 100644 --- a/Jupiter/Rehash.cpp +++ b/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 rehashables; +Jupiter::DLList o_rehashables; /** List of RehashFunction objects */ -Jupiter::DLList rehashFunctions; +Jupiter::DLList 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::Node *node = o_rehashables.getHead(); node != nullptr; node = node->next) { - for (Jupiter::DLList::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::Node *n = rehashables.getNode(0); - Jupiter::DLList::Node *d; - while (n != nullptr) + size_t total_errors = 0; + int rehash_result; + Jupiter::DLList::Node *node = o_rehashables.getHead(); + Jupiter::DLList::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::Node *n = rehashFunctions.getNode(0); n != nullptr; n = n->next) + for (Jupiter::DLList::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(); -} \ No newline at end of file diff --git a/Jupiter/Rehash.h b/Jupiter/Rehash.h index a3ad218..fbacf81 100644 --- a/Jupiter/Rehash.h +++ b/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 // 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 \ No newline at end of file