mirror of https://github.com/JAJames/Jupiter.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
3.1 KiB
148 lines
3.1 KiB
11 years ago
|
/**
|
||
8 years ago
|
* Copyright (C) 2014-2017 Jessica James.
|
||
9 years ago
|
*
|
||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||
|
* purpose with or without fee is hereby granted, provided that the above
|
||
|
* copyright notice and this permission notice appear in all copies.
|
||
|
*
|
||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||
|
*
|
||
9 years ago
|
* Written by Jessica James <jessica.aj@outlook.com>
|
||
11 years ago
|
*/
|
||
|
|
||
8 years ago
|
#include <list>
|
||
11 years ago
|
#include "Rehash.h"
|
||
|
|
||
|
/**
|
||
|
* Considerations:
|
||
|
* It may be worth-while making rehashables aware of which node they're in, or integrating them into their own node?
|
||
|
*/
|
||
|
|
||
|
/** Wrapper class to add rehashable functions. */
|
||
|
class RehashFunction : public Jupiter::Rehashable
|
||
|
{
|
||
|
public:
|
||
8 years ago
|
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;
|
||
|
}
|
||
11 years ago
|
};
|
||
|
|
||
|
/** List of Rehashable objects */
|
||
8 years ago
|
std::list<Jupiter::Rehashable *> o_rehashables;
|
||
11 years ago
|
|
||
|
/** List of RehashFunction objects */
|
||
8 years ago
|
std::list<RehashFunction *> o_rehash_functions;
|
||
11 years ago
|
|
||
|
Jupiter::Rehashable::Rehashable()
|
||
|
{
|
||
8 years ago
|
o_rehashables.push_back(this);
|
||
11 years ago
|
}
|
||
|
|
||
|
Jupiter::Rehashable::Rehashable(const Jupiter::Rehashable &)
|
||
|
{
|
||
8 years ago
|
o_rehashables.push_back(this);
|
||
11 years ago
|
}
|
||
|
|
||
|
Jupiter::Rehashable::~Rehashable()
|
||
|
{
|
||
8 years ago
|
for (auto node = o_rehashables.begin(); node != o_rehashables.end(); ++node)
|
||
11 years ago
|
{
|
||
8 years ago
|
if (*node == this)
|
||
11 years ago
|
{
|
||
8 years ago
|
o_rehashables.erase(node);
|
||
8 years ago
|
break;
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
size_t Jupiter::rehash()
|
||
11 years ago
|
{
|
||
8 years ago
|
size_t total_errors = 0;
|
||
|
int rehash_result;
|
||
8 years ago
|
auto node = o_rehashables.begin();
|
||
8 years ago
|
|
||
8 years ago
|
while (node != o_rehashables.end())
|
||
11 years ago
|
{
|
||
8 years ago
|
rehash_result = (*node)->OnRehash();
|
||
8 years ago
|
if (rehash_result != 0)
|
||
11 years ago
|
{
|
||
8 years ago
|
++total_errors;
|
||
|
if (rehash_result < 0)
|
||
10 years ago
|
{
|
||
8 years ago
|
auto dead_node = node;
|
||
|
++node;
|
||
8 years ago
|
|
||
8 years ago
|
if ((*dead_node)->OnBadRehash(true))
|
||
|
delete *dead_node;
|
||
|
|
||
|
o_rehashables.erase(dead_node);
|
||
8 years ago
|
|
||
10 years ago
|
continue;
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
(*node)->OnBadRehash(false);
|
||
11 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
++node;
|
||
11 years ago
|
}
|
||
8 years ago
|
|
||
|
return total_errors;
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
size_t Jupiter::getRehashableCount()
|
||
11 years ago
|
{
|
||
8 years ago
|
return o_rehashables.size();
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
void Jupiter::addOnRehash(OnRehashFunctionType in_function)
|
||
11 years ago
|
{
|
||
8 years ago
|
o_rehash_functions.push_back(new RehashFunction(in_function));
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
bool Jupiter::removeOnRehash(OnRehashFunctionType in_function)
|
||
11 years ago
|
{
|
||
8 years ago
|
for (auto node = o_rehash_functions.begin(); node != o_rehash_functions.end(); ++node)
|
||
11 years ago
|
{
|
||
8 years ago
|
if ((*node)->m_function == in_function)
|
||
11 years ago
|
{
|
||
8 years ago
|
delete *node;
|
||
|
o_rehash_functions.erase(node);
|
||
11 years ago
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
return false;
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
size_t Jupiter::removeAllOnRehash()
|
||
11 years ago
|
{
|
||
8 years ago
|
size_t result = o_rehash_functions.size();
|
||
11 years ago
|
|
||
8 years ago
|
while (o_rehash_functions.size() != 0)
|
||
8 years ago
|
{
|
||
|
delete o_rehash_functions.front();
|
||
|
o_rehash_functions.pop_front();
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
return result;
|
||
11 years ago
|
}
|