Browse Source

Removed Queue, DLList, and List

Temporarily added size() to ArrayList; ArrayList will be removed later
release/0.19
Jessica James 8 years ago
parent
commit
89e4050c55
  1. 60
      Jupiter/ArrayList.h
  2. 318
      Jupiter/DLList.h
  3. 4
      Jupiter/Jupiter.vcxproj
  4. 15
      Jupiter/Jupiter.vcxproj.filters
  5. 89
      Jupiter/List.h
  6. 77
      Jupiter/Queue.cpp
  7. 80
      Jupiter/Queue.h
  8. 55
      Jupiter/Rehash.cpp
  9. 41
      Jupiter/Timer.cpp
  10. 35
      Tester/Test.cpp

60
Jupiter/ArrayList.h

@ -25,14 +25,14 @@
*/
#include "Jupiter.h"
#include "List.h"
#include "DataBuffer.h"
namespace Jupiter
{
/**
* @brief Provides an array-based list implementation using the List interface.
*/
template<typename T> class ArrayList : public List<T>
template<typename T> class ArrayList
{
public:
@ -74,6 +74,13 @@ namespace Jupiter
*/
void add(T *data);
/**
* @brief Returns the size of the list.
*
* @return Number of nodes in the list.
*/
size_t size() const { return m_length; };
/**
* @brief Empties the ArrayList of all elements.
*/
@ -120,6 +127,7 @@ namespace Jupiter
private:
T **data;
size_t dataSize;
size_t m_length;
size_t expandArray();
};
@ -130,7 +138,9 @@ namespace Jupiter
template<typename T> size_t Jupiter::ArrayList<T>::expandArray()
{
T **tmp = new T *[Jupiter::ArrayList<T>::dataSize * 2];
for (size_t i = 0; i < Jupiter::ArrayList<T>::dataSize; i++) tmp[i] = data[i];
for (size_t i = 0; i < Jupiter::ArrayList<T>::dataSize; i++)
tmp[i] = data[i];
delete[] Jupiter::ArrayList<T>::data;
Jupiter::ArrayList<T>::data = tmp;
Jupiter::ArrayList<T>::dataSize *= 2;
@ -145,18 +155,18 @@ template<typename T> Jupiter::ArrayList<T>::ArrayList(size_t length_)
{
Jupiter::ArrayList<T>::dataSize = length_;
Jupiter::ArrayList<T>::data = new T*[Jupiter::ArrayList<T>::dataSize];
Jupiter::List<T>::length = 0;
m_length = 0;
}
template<typename T> Jupiter::ArrayList<T>::ArrayList(const Jupiter::ArrayList<T> &source)
{
Jupiter::ArrayList<T>::dataSize = source.dataSize;
Jupiter::ArrayList<T>::data = new T*[Jupiter::ArrayList<T>::dataSize];
Jupiter::List<T>::length = 0;
while (Jupiter::List<T>::length != source.length)
m_length = 0;
while (m_length != source.m_length)
{
Jupiter::ArrayList<T>::data[Jupiter::List<T>::length] = source.data[Jupiter::List<T>::length];
++Jupiter::List<T>::length;
Jupiter::ArrayList<T>::data[m_length] = source.data[m_length];
++m_length;
}
}
@ -164,10 +174,10 @@ template<typename T> Jupiter::ArrayList<T>::ArrayList(ArrayList<T> &&source)
{
Jupiter::ArrayList<T>::dataSize = source.dataSize;
Jupiter::ArrayList<T>::data = source.data;
Jupiter::List<T>::length = source.length;
m_length = source.m_length;
source.dataSize = Jupiter::ArrayList<T>::start_size;
source.data = new T*[source.dataSize];
source.length = 0;
source.m_length = 0;
}
template<typename T> Jupiter::ArrayList<T>::~ArrayList()
@ -184,45 +194,53 @@ template<typename T> T *Jupiter::ArrayList<T>::remove(size_t index)
{
T *r = Jupiter::ArrayList<T>::data[index];
Jupiter::ArrayList<T>::data[index] = nullptr;
for (size_t i = index + 1; i < Jupiter::List<T>::length; i++) Jupiter::ArrayList<T>::data[i - 1] = Jupiter::ArrayList<T>::data[i];
Jupiter::List<T>::length--;
for (size_t i = index + 1; i < m_length; i++)
Jupiter::ArrayList<T>::data[i - 1] = Jupiter::ArrayList<T>::data[i];
--m_length;
return r;
}
template<typename T> T *Jupiter::ArrayList<T>::pop()
{
return Jupiter::ArrayList<T>::data[--Jupiter::List<T>::length];
return Jupiter::ArrayList<T>::data[--m_length];
}
template<typename T> void Jupiter::ArrayList<T>::add(T *ndata, size_t index)
{
if (Jupiter::List<T>::length == Jupiter::ArrayList<T>::dataSize) Jupiter::ArrayList<T>::expandArray();
for (size_t i = Jupiter::List<T>::length; i > index; i--) Jupiter::ArrayList<T>::data[i] = Jupiter::ArrayList<T>::data[i - 1];
if (m_length == Jupiter::ArrayList<T>::dataSize) Jupiter::ArrayList<T>::expandArray();
for (size_t i = m_length; i > index; i--)
Jupiter::ArrayList<T>::data[i] = Jupiter::ArrayList<T>::data[i - 1];
Jupiter::ArrayList<T>::data[index] = ndata;
Jupiter::List<T>::length++;
++m_length;
}
template<typename T> void Jupiter::ArrayList<T>::add(T *ndata)
{
Jupiter::ArrayList<T>::add(ndata, Jupiter::List<T>::length);
Jupiter::ArrayList<T>::add(ndata, m_length);
}
template<typename T> void Jupiter::ArrayList<T>::empty()
{
Jupiter::List<T>::length = 0;
m_length = 0;
}
template<typename T> void Jupiter::ArrayList<T>::emptyAndDelete()
{
while (Jupiter::List<T>::length != 0)
delete Jupiter::ArrayList<T>::data[--Jupiter::List<T>::length];
while (m_length != 0)
delete Jupiter::ArrayList<T>::data[--m_length];
}
template<> struct _Jupiter_DataBuffer_partial_specialization_impl<Jupiter::ArrayList>
{
template<typename Y> static void push(Jupiter::DataBuffer *buffer, const Jupiter::ArrayList<Y> *data)
{
_Jupiter_DataBuffer_partial_specialization_impl<Jupiter::List>::push<Y>(buffer, data);
buffer->push<size_t>(data->size());
for (size_t index = 0; index != data->size(); index++)
buffer->push<Y>(*data->get(index));
};
template<typename Y> static Jupiter::ArrayList<Y> interpret(uint8_t *&head)

318
Jupiter/DLList.h

@ -1,318 +0,0 @@
/**
* Copyright (C) 2013-2015 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
* 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.
*
* Written by Jessica James <jessica.aj@outlook.com>
*/
#if !defined _DLLIST_H_HEADER
#define _DLLIST_H_HEADER
/**
* @file DLList.h
* @brief Provides a generic Doubly Linked List implementation using the List interface.
*/
#include "Jupiter.h"
#include "List.h"
namespace Jupiter
{
/**
* @brief Provides a Doubly Linked List implementation using the List interface.
*/
template<typename T> class DLList : public List<T>
{
public:
/**
* @brief Stores a pointer to data, and a pointer to the next node in the list.
*/
struct Node
{
Node *next;
Node *previous;
T *data;
};
/*
* @brief Returns the head of the list
*
* @return Head of the list
*/
Node *getHead() const;
/*
* @brief Returns the tail of the list
*
* @return Tail of the list
*/
Node *getTail() const;
/**
* @brief Returns the Node at the specified index in the list.
*
* @param index Index of the node to return.
* @return Node at specified index in the list.
*/
Node *getNode(size_t index) const;
/**
* @brief Gets the data at a specified index.
*
* @param index Index of the data to get.
* @return Data stored at the specified index.
*/
T *get(size_t index) const;
/**
* @brief Removes the n'th Node in the list, and returns its contents.
*
* @param n Index of the node to remove.
* @return Contents of the node removed.
*/
T *remove(size_t n);
/**
* @brief Removes a node from the list.
*
* @param data Node to remove.
* @return Contents of the node removed.
*/
T *remove(Node *data);
/**
* @brief Adds data to the list at a specified index.
*
* @param data Data to add to the list.
* @param index Position in the list to add the data to.
*/
void add(T *data, size_t index);
/**
* @brief Adds data to the tail of the list.
*
* @param data Data to add to the list.
*/
void add(T *data);
/**
* @brief Default constructor for the DLList class.
*/
DLList() = default;
/**
* @brief Copy constructor for the DLList class.
*/
DLList(const DLList<T> &);
/**
* @brief Destructor for the DLList class.
* Note: This does not delete data added to the list.
*/
~DLList();
/** Private members */
private:
Node *m_head = nullptr;
Node *m_tail = nullptr;
};
}
// Implementation
template<typename T> Jupiter::DLList<T>::DLList(const Jupiter::DLList<T> &source)
{
Jupiter::List<T>::length = source.length;
if (Jupiter::List<T>::length == 0)
{
m_head = nullptr;
m_tail = nullptr;
}
else if (Jupiter::List<T>::length == 1)
{
Jupiter::DLList<T>::Node *n = new Jupiter::DLList<T>::Node;
n->data = source.getNode(0)->data;
m_head = n;
m_tail = n;
}
else
{
Jupiter::DLList<T>::Node *sourceNode = source.getNode(0);
Jupiter::DLList<T>::Node *n = new Jupiter::DLList<T>::Node;
n->data = sourceNode->data;
m_head = n;
sourceNode = sourceNode->next;
while (sourceNode->next != nullptr)
{
n->next = new Jupiter::DLList<T>::Node;
n = n->next;
n->data = sourceNode->data;
sourceNode = sourceNode->next;
}
n->next = new Jupiter::DLList<T>::Node;
n = n->next;
n->data = sourceNode->data;
m_tail = n;
}
}
template<typename T> Jupiter::DLList<T>::~DLList()
{
Jupiter::DLList<T>::Node *p;
Jupiter::DLList<T>::Node *c = m_head;
while (c != nullptr)
{
p = c;
c = c->next;
delete p;
}
}
template<typename T> typename Jupiter::DLList<T>::Node *Jupiter::DLList<T>::getHead() const
{
return m_head;
}
template<typename T> typename Jupiter::DLList<T>::Node *Jupiter::DLList<T>::getTail() const
{
return m_tail;
}
template<typename T> typename Jupiter::DLList<T>::Node *Jupiter::DLList<T>::getNode(size_t index) const
{
Jupiter::DLList<T>::Node *r;
if (index * 2 < Jupiter::List<T>::length)
{
r = m_head;
for (size_t i = 0; i < index; i++) r = r->next;
return r;
}
r = m_tail;
for (size_t i = Jupiter::List<T>::length - 1; i > index; i--) r = r->previous;
return r;
}
template<typename T> T *Jupiter::DLList<T>::get(size_t index) const
{
return Jupiter::DLList<T>::getNode(index)->data;
}
template<typename T> T *Jupiter::DLList<T>::remove(size_t index)
{
return Jupiter::DLList<T>::remove(Jupiter::DLList<T>::getNode(index));
}
template<typename T> T *Jupiter::DLList<T>::remove(Node *data)
{
if (m_head == data)
{
m_head = data->next;
if (data->next != nullptr) data->next->previous = data->previous;
else m_tail = nullptr;
}
else if (m_tail == data)
{
m_tail = data->previous;
m_tail->next = nullptr;
}
else
{
data->next->previous = data->previous;
data->previous->next = data->next;
}
T *r = data->data;
delete data;
Jupiter::List<T>::length--;
return r;
}
template<typename T> void Jupiter::DLList<T>::add(T *data, size_t index)
{
Jupiter::DLList<T>::Node *node = new Jupiter::DLList<T>::Node();
node->data = data;
if (index == 0)
{
node->next = m_head;
m_head->previous = node;
m_head = node;
node->previous = nullptr;
}
else if (index == Jupiter::List<T>::length)
{
node->previous = m_tail;
m_tail->next = node;
m_tail = node;
node->next = nullptr;
}
else
{
Jupiter::DLList<T>::Node *n = Jupiter::DLList<T>::getNode(index);
node->next = n;
node->previous = n->previous;
node->previous->next = node;
n->previous = node;
}
Jupiter::List<T>::length++;
}
template<typename T> void Jupiter::DLList<T>::add(T *data)
{
Jupiter::DLList<T>::Node *n = new Jupiter::DLList<T>::Node();
n->data = data;
n->next = nullptr;
if (Jupiter::List<T>::length == 0)
{
m_head = n;
m_tail = n;
n->previous = nullptr;
}
else
{
n->previous = m_tail;
m_tail->next = n;
m_tail = n;
}
Jupiter::List<T>::length++;
}
template<> struct _Jupiter_DataBuffer_partial_specialization_impl<Jupiter::DLList>
{
template<typename Y> static void push(Jupiter::DataBuffer *buffer, const Jupiter::DLList<Y> *data)
{
buffer->push<size_t>(data->size());
Jupiter::DLList<Y>::Node *head = data->getNode(0);
while (head != nullptr)
{
buffer->push<Y>(*head->data);
head++;
}
};
template<typename Y> static Jupiter::DLList<Y> interpret(uint8_t *&head)
{
size_t size_ = *reinterpret_cast<size_t *>(head);
head += sizeof(size_t);
Jupiter::DLList<Y> r;
while (size_-- != 0)
r.add(Jupiter::DataBuffer::interpret_data<Y>(head));
return r;
}
};
#endif // _DLLIST_H_HEADER

4
Jupiter/Jupiter.vcxproj

@ -192,7 +192,6 @@
<ClCompile Include="IRC_Client.cpp" />
<ClCompile Include="Jupiter.cpp" />
<ClCompile Include="Plugin.cpp" />
<ClCompile Include="Queue.cpp" />
<ClCompile Include="Rehash.cpp" />
<ClCompile Include="SecureSocket.cpp" />
<ClCompile Include="Socket.cpp" />
@ -212,7 +211,6 @@
<ClInclude Include="Database.h" />
<ClInclude Include="DataBuffer.h" />
<ClInclude Include="DataBuffer_Imp.h" />
<ClInclude Include="DLList.h" />
<ClInclude Include="File.h" />
<ClInclude Include="GenericCommand.h" />
<ClInclude Include="Hash.h" />
@ -227,9 +225,7 @@
<ClInclude Include="Jupiter.h" />
<ClInclude Include="Functions.h" />
<ClInclude Include="IRC_Client.h" />
<ClInclude Include="List.h" />
<ClInclude Include="Plugin.h" />
<ClInclude Include="Queue.h" />
<ClInclude Include="Readable_String.h" />
<ClInclude Include="Readable_String_Imp.h" />
<ClInclude Include="Reference_String.h" />

15
Jupiter/Jupiter.vcxproj.filters

@ -22,9 +22,6 @@
<Filter Include="Header Files\IRC">
<UniqueIdentifier>{a7ca0e66-d9b7-43e2-a3f8-d89744bd3c06}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Lists">
<UniqueIdentifier>{b0dabf12-3147-44f6-911e-01142cc4e3a4}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\IRC">
<UniqueIdentifier>{6f290a91-7cd8-495e-ae20-22c956fcfbcb}</UniqueIdentifier>
</Filter>
@ -72,9 +69,6 @@
<ClCompile Include="Jupiter.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Queue.cpp">
<Filter>Source Files\Lists</Filter>
</ClCompile>
<ClCompile Include="TCPSocket.cpp">
<Filter>Source Files\Sockets</Filter>
</ClCompile>
@ -140,15 +134,6 @@
<ClInclude Include="Jupiter.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="List.h">
<Filter>Header Files\Lists</Filter>
</ClInclude>
<ClInclude Include="Queue.h">
<Filter>Header Files\Lists</Filter>
</ClInclude>
<ClInclude Include="DLList.h">
<Filter>Header Files\Lists</Filter>
</ClInclude>
<ClInclude Include="UDPSocket.h">
<Filter>Header Files\Sockets</Filter>
</ClInclude>

89
Jupiter/List.h

@ -1,89 +0,0 @@
/**
* Copyright (C) 2013-2015 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
* 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.
*
* Written by Jessica James <jessica.aj@outlook.com>
*/
#if !defined _LIST_H_HEADER
#define _LIST_H_HEADER
/**
* @file List.h
* @brief Provides a generic List interface.
*/
#include "Jupiter.h"
#include "DataBuffer.h"
namespace Jupiter
{
template<typename T> class List
{
public:
/**
* @brief Gets the data at a specified index.
*
* @param index Index of the data to get.
* @return Data stored at the specified index.
*/
virtual T *get(size_t index) const = 0;
/**
* @brief Removes the n'th Node in the list, and returns its contents.
*
* @param n Index of the node to remove.
* @return Contents of the node removed.
*/
virtual T *remove(size_t n) = 0;
/**
* @brief Adds data to the list at a specified index.
*
* @param data Data to add to the list.
* @param index Position in the list to add the data to.
*/
virtual void add(T *data, size_t index) = 0;
/**
* @brief Adds data to the list in an efficient manner.
*
* @param data Data to add to the list.
*/
virtual void add(T *data) = 0;
/**
* @brief Returns the size of the list.
*
* @return Number of nodes in the list.
*/
size_t size() const { return Jupiter::List<T>::length; };
protected:
size_t length = 0; /** Length (size) of the list. Returned by size(). Must be managed by extending classes. */
};
}
template<> struct _Jupiter_DataBuffer_partial_specialization_impl<Jupiter::List>
{
template<typename Y> static void push(Jupiter::DataBuffer *buffer, const Jupiter::List<Y> *data)
{
buffer->push<size_t>(data->size());
for (size_t index = 0; index != data->size(); index++)
buffer->push<Y>(*data->get(index));
};
};
#endif // _LIST_H_HEADER

77
Jupiter/Queue.cpp

@ -1,77 +0,0 @@
/**
* Copyright (C) 2013-2015 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
* 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.
*
* Written by Jessica James <jessica.aj@outlook.com>
*/
#include "Queue.h"
struct Jupiter::Queue::Data
{
Data *next;
void *data;
};
Jupiter::Queue::Queue()
{
Jupiter::Queue::end = new Jupiter::Queue::Data();
Jupiter::Queue::end->next = nullptr;
Jupiter::Queue::head = new Jupiter::Queue::Data();
Jupiter::Queue::head->next = Jupiter::Queue::end;
Jupiter::Queue::head->data = nullptr;
Jupiter::Queue::length = 0;
}
Jupiter::Queue::~Queue()
{
Jupiter::Queue::Data *p;
Jupiter::Queue::Data *c = head;
do
{
p = c;
c = c->next;
delete p;
}
while (c != nullptr);
}
void Jupiter::Queue::enqueue(void *data)
{
Jupiter::Queue::Data *nEnd = new Jupiter::Queue::Data();
nEnd->next = nullptr;
end->data = data;
end->next = nEnd;
end = nEnd;
Jupiter::Queue::length++;
}
void *Jupiter::Queue::dequeue()
{
if (Jupiter::Queue::length > 0)
{
Jupiter::Queue::Data *tmp = Jupiter::Queue::head->next;
Jupiter::Queue::head->next = tmp->next;
void *data = tmp->data;
delete tmp;
Jupiter::Queue::length--;
return data;
}
return nullptr;
}
size_t Jupiter::Queue::size() const
{
return Jupiter::Queue::length;
}

80
Jupiter/Queue.h

@ -1,80 +0,0 @@
/**
* Copyright (C) 2013-2015 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
* 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.
*
* Written by Jessica James <jessica.aj@outlook.com>
*/
#if !defined _QUEUE_H_HEADER
#define _QUEUE_H_HEADER
/**
* @file Queue.h
* @brief Provides a simple and efficient queue.
*/
#include "Jupiter.h"
namespace Jupiter
{
/**
* @brief Provides a simple and efficient queue.
*/
class JUPITER_API Queue
{
public:
/**
* @brief Adds data to the end of the Queue.
*
* @param data Pointer to the data to add.
*/
void enqueue(void *data);
/**
* @brief Removes and returns data from the front of the Queue.
*
* @return Data in the front of the Queue.
*/
void *dequeue();
/**
* @brief Returns the number of elements waiting for processing in the Queue.
*
* @return Number of elements in the Queue.
*/
size_t size() const;
/**
* @brief Default constructor for the Queue class.
*/
Queue();
/**
* @brief Destructor for the Queue class.
* Note: This does not delete data which was added to the queue.
*/
~Queue();
/** Private members */
private:
struct Data;
Data *head;
Data *end;
size_t length;
};
}
#endif // _QUEUE_H_HEADER

55
Jupiter/Rehash.cpp

@ -1,5 +1,5 @@
/**
* Copyright (C) 2014-2016 Jessica James.
* Copyright (C) 2014-2017 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
@ -16,8 +16,8 @@
* Written by Jessica James <jessica.aj@outlook.com>
*/
#include <list>
#include "Rehash.h"
#include "DLList.h"
/**
* Considerations:
@ -47,28 +47,28 @@ public:
};
/** List of Rehashable objects */
Jupiter::DLList<Jupiter::Rehashable> o_rehashables;
std::list<Jupiter::Rehashable *> o_rehashables;
/** List of RehashFunction objects */
Jupiter::DLList<RehashFunction> o_rehash_functions;
std::list<RehashFunction *> o_rehash_functions;
Jupiter::Rehashable::Rehashable()
{
o_rehashables.add(this);
o_rehashables.push_back(this);
}
Jupiter::Rehashable::Rehashable(const Jupiter::Rehashable &)
{
o_rehashables.add(this);
o_rehashables.push_back(this);
}
Jupiter::Rehashable::~Rehashable()
{
for (Jupiter::DLList<Jupiter::Rehashable>::Node *node = o_rehashables.getHead(); node != nullptr; node = node->next)
for (auto node = o_rehashables.begin(); node != o_rehashables.end(); ++node)
{
if (node->data == this)
if (*node == this)
{
o_rehashables.remove(node);
o_rehashables.erase(node);
break;
}
}
@ -78,32 +78,31 @@ size_t Jupiter::rehash()
{
size_t total_errors = 0;
int rehash_result;
Jupiter::DLList<Jupiter::Rehashable>::Node *node = o_rehashables.getHead();
Jupiter::DLList<Jupiter::Rehashable>::Node *dead_node;
auto node = o_rehashables.begin();
while (node != nullptr)
while (node != o_rehashables.end())
{
rehash_result = node->data->OnRehash();
rehash_result = (*node)->OnRehash();
if (rehash_result != 0)
{
++total_errors;
if (rehash_result < 0)
{
dead_node = node;
node = node->next;
auto dead_node = node;
++node;
if (dead_node->data->OnBadRehash(true))
delete o_rehashables.remove(dead_node);
else
o_rehashables.remove(dead_node);
if ((*dead_node)->OnBadRehash(true))
delete *dead_node;
o_rehashables.erase(dead_node);
continue;
}
o_rehashables.remove(node)->OnBadRehash(false);
(*node)->OnBadRehash(false);
}
node = node->next;
++node;
}
return total_errors;
@ -116,16 +115,17 @@ size_t Jupiter::getRehashableCount()
void Jupiter::addOnRehash(OnRehashFunctionType in_function)
{
o_rehash_functions.add(new RehashFunction(in_function));
o_rehash_functions.push_back(new RehashFunction(in_function));
}
bool Jupiter::removeOnRehash(OnRehashFunctionType in_function)
{
for (Jupiter::DLList<RehashFunction>::Node *node = o_rehash_functions.getHead(); node != nullptr; node = node->next)
for (auto node = o_rehash_functions.begin(); node != o_rehash_functions.end(); ++node)
{
if (node->data->m_function == in_function)
if ((*node)->m_function == in_function)
{
delete o_rehash_functions.remove(node);
delete *node;
o_rehash_functions.erase(node);
return true;
}
}
@ -138,7 +138,10 @@ size_t Jupiter::removeAllOnRehash()
size_t result = o_rehash_functions.size();
while (o_rehash_functions.size() != 0)
delete o_rehash_functions.remove(size_t{ 0 });
{
delete o_rehash_functions.front();
o_rehash_functions.pop_front();
}
return result;
}

41
Jupiter/Timer.cpp

@ -1,5 +1,5 @@
/**
* Copyright (C) 2014-2016 Jessica James.
* Copyright (C) 2014-2017 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
@ -16,10 +16,10 @@
* Written by Jessica James <jessica.aj@outlook.com>
*/
#include <list>
#include "Timer.h"
#include "DLList.h"
Jupiter::DLList<Jupiter::Timer> o_timers;
std::list<Jupiter::Timer *> o_timers;
/** Deallocates timers when the library is unloaded. */
struct TimerKiller
@ -43,7 +43,7 @@ Jupiter::Timer::Timer(unsigned int in_iterations, std::chrono::milliseconds in_d
if (in_immediate == false)
m_next_call += m_delay;
o_timers.add(this);
o_timers.push_back(this);
}
Jupiter::Timer::Timer(unsigned int in_iterations, std::chrono::milliseconds in_delay, FunctionType in_function, bool in_immediate)
@ -56,7 +56,7 @@ Jupiter::Timer::Timer(unsigned int in_iterations, std::chrono::milliseconds in_d
if (in_immediate == false)
m_next_call += m_delay;
o_timers.add(this);
o_timers.push_back(this);
}
int Jupiter::Timer::think()
@ -79,11 +79,11 @@ int Jupiter::Timer::think()
bool Jupiter::Timer::removeFromList()
{
for (Jupiter::DLList<Jupiter::Timer>::Node *node = o_timers.getHead(); node != nullptr; node = node->next)
for (auto node = o_timers.begin(); node != o_timers.end(); ++node)
{
if (node->data == this)
if (*node == this)
{
o_timers.remove(node);
o_timers.erase(node);
return true;
}
}
@ -111,22 +111,22 @@ size_t Jupiter::Timer::total()
size_t Jupiter::Timer::check()
{
size_t result = 0;
Jupiter::Timer *timer;
Jupiter::DLList<Jupiter::Timer>::Node *dead_node;
Jupiter::DLList<Jupiter::Timer>::Node *node = o_timers.getHead();
auto node = o_timers.begin();
while (node != nullptr)
while (node != o_timers.end())
{
timer = node->data;
if (timer->think() != 0)
if ((*node)->think() != 0)
{
dead_node = node;
node = node->next;
delete o_timers.remove(dead_node);
auto dead_node = node;
++node;
delete *dead_node;
o_timers.erase(dead_node);
++result;
}
else
node = node->next;
++node;
}
return result;
@ -137,7 +137,10 @@ size_t Jupiter::Timer::killAll()
size_t result = o_timers.size();
while (o_timers.size() != 0)
delete o_timers.remove(size_t{ 0 });
{
delete o_timers.front();
o_timers.pop_front();
}
return result;
}

35
Tester/Test.cpp

@ -15,44 +15,45 @@
#include "Jupiter/HTTP_QueryString.h"
#include "Jupiter/Hash.h"
#include "Jupiter/Hash_Table.h"
#include "Jupiter/Algorithm.h"
using namespace Jupiter;
using namespace Jupiter::literals;
unsigned int goodTests = 0;
unsigned int totalTests = 0;
unsigned int good_tests = 0;
unsigned int total_tests = 0;
void test(bool expr)
{
++totalTests;
++total_tests;
if (expr)
++goodTests;
++good_tests;
else
printf("Test number %u failed!" ENDL, totalTests);
std::cout << "Test number " << total_tests << " failed!" << std::endl;
}
Jupiter::StringS randstr(size_t length)
template<typename T> void test(T result, T expected_result)
{
StringS str;
++total_tests;
while (length != 0)
if (result == expected_result)
++good_tests;
else
{
str += ' ' + rand() % ('z' - ' ');
--length;
std::cout << "Test number " << total_tests << " failed!" << std::endl;
std::cout << "\tExpected '" << expected_result << "' but got '" << result << "' instead" << std::endl;
}
return str;
}
int main()
{
if (goodTests == totalTests)
printf("All %u tests succeeded." ENDL, totalTests);
if (good_tests == total_tests)
std::cout << "All " << total_tests << " tests succeeded." << std::endl;
else
printf("ERROR: Only %u/%u tests succeeded. %u tests failed." ENDL, goodTests, totalTests, totalTests - goodTests);
std::cout << "ERROR: Only " << good_tests << "/" << total_tests << " tests succeeded. " << total_tests - good_tests << " tests failed." << std::endl;
puts("Press any key to continue...");
fgetc(stdin);
std::cout << "Press any key to continue..." << std::endl;
std::cin.get();
return 0;
}

Loading…
Cancel
Save