mirror of
https://github.com/EQEmu/Server.git
synced 2026-04-02 20:42:28 +00:00
- License was intended to be GPLv3 per earlier commit of GPLv3 LICENSE FILE - This is confirmed by the inclusion of libraries that are incompatible with GPLv2 - This is also confirmed by KLS and the agreement of KLS's predecessors - Added GPLv3 license headers to the compilable source files - Removed Folly licensing in strings.h since the string functions do not match the Folly functions and are standard functions - this must have been left over from previous implementations - Removed individual contributor license headers since the project has been under the "developer" mantle for many years - Removed comments on files that were previously automatically generated since they've been manually modified multiple times and there are no automatic scripts referencing them (removed in 2023)
249 lines
6.6 KiB
C++
249 lines
6.6 KiB
C++
/* EQEmu: EQEmulator
|
|
|
|
Copyright (C) 2001-2026 EQEmu Development Team
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
#pragma once
|
|
|
|
#include "common/eqemu_exception.h"
|
|
#include "common/types.h"
|
|
|
|
#include <cstring>
|
|
|
|
namespace EQ {
|
|
|
|
/*! Simple HashSet designed to be used in fixed memory that may be difficult to use an
|
|
allocator for (shared memory), we assume all keys are unsigned int
|
|
*/
|
|
template<class T>
|
|
class FixedMemoryHashSet {
|
|
typedef uint32 key_type;
|
|
typedef T value_type;
|
|
typedef uint8 byte;
|
|
typedef value_type& reference;
|
|
typedef const value_type& const_reference;
|
|
typedef size_t size_type;
|
|
public:
|
|
/*!
|
|
Constructor which initializes the hash set
|
|
\param data Raw data
|
|
\param size Raw data size
|
|
\param element_count Max number of possible unique elements that can be inserted.
|
|
\param max_element_id Number of offsets to store: eg highest "key" that will be used.
|
|
*/
|
|
FixedMemoryHashSet(byte *data, size_type size, key_type element_count, key_type max_element_id) {
|
|
data_ = data;
|
|
size_ = size;
|
|
|
|
byte *ptr = data;
|
|
*reinterpret_cast<key_type*>(ptr) = max_element_id + 1;
|
|
offset_count_ = max_element_id + 1;
|
|
ptr += sizeof(key_type);
|
|
|
|
*reinterpret_cast<key_type*>(ptr) = element_count;
|
|
max_elements_ = element_count;
|
|
ptr += sizeof(key_type);
|
|
|
|
*reinterpret_cast<key_type*>(ptr) = 0;
|
|
current_elements_ = 0;
|
|
ptr += sizeof(key_type);
|
|
|
|
offsets_ = reinterpret_cast<key_type*>(ptr);
|
|
memset(ptr, 0xFFFFFFFFU, sizeof(key_type) * (max_element_id + 1));
|
|
ptr += sizeof(key_type) * (max_element_id + 1);
|
|
|
|
elements_ = reinterpret_cast<value_type*>(ptr);
|
|
}
|
|
|
|
/*!
|
|
Constructor which does not initialize the hash set. Builds the hash set from what data is
|
|
stored in the data pointer passed.
|
|
\param data Raw data
|
|
\param size Raw data size
|
|
*/
|
|
FixedMemoryHashSet(byte *data, size_type size) {
|
|
data_ = data;
|
|
size_ = size;
|
|
|
|
byte *ptr = data;
|
|
|
|
offset_count_ = *reinterpret_cast<key_type*>(ptr);
|
|
ptr += sizeof(key_type);
|
|
|
|
max_elements_ = *reinterpret_cast<key_type*>(ptr);
|
|
ptr += sizeof(key_type);
|
|
|
|
current_elements_ = *reinterpret_cast<key_type*>(ptr);
|
|
ptr += sizeof(key_type);
|
|
|
|
offsets_ = reinterpret_cast<key_type*>(ptr);
|
|
ptr += sizeof(key_type) * offset_count_;
|
|
|
|
elements_ = reinterpret_cast<value_type*>(ptr);
|
|
}
|
|
|
|
//! Copy Constructor
|
|
FixedMemoryHashSet(const FixedMemoryHashSet& other) :
|
|
data_(other.data_),
|
|
size_(other.size_),
|
|
offset_count_(other.offset_count_),
|
|
max_elements_(other.max_elements_),
|
|
current_elements_(other.current_elements_),
|
|
offsets_(other.offsets_),
|
|
elements_(other.elements_)
|
|
{
|
|
}
|
|
|
|
//! RValue-Move Constructor
|
|
#ifdef EQEMU_RVALUE_MOVE
|
|
FixedMemoryHashSet(FixedMemoryHashSet&& other) :
|
|
data_(other.data_),
|
|
size_(other.size_),
|
|
offset_count_(other.offset_count_),
|
|
max_elements_(other.max_elements_),
|
|
current_elements_(other.current_elements_),
|
|
offsets_(other.offsets_),
|
|
elements_(other.elements_)
|
|
{
|
|
}
|
|
#endif
|
|
|
|
//! Destructor
|
|
~FixedMemoryHashSet() {
|
|
}
|
|
|
|
//! Assignment operator
|
|
const FixedMemoryHashSet& operator=(const FixedMemoryHashSet& other) {
|
|
data_ = other.data_;
|
|
size_ = other.size_;
|
|
offset_count_ = other.offset_count_;
|
|
max_elements_ = other.max_elements_;
|
|
current_elements_ = other.current_elements_;
|
|
offsets_ = other.offsets_;
|
|
elements_ = other.elements_;
|
|
return *this;
|
|
}
|
|
|
|
//! Returns whether the set is empty (has 0 elements) or not
|
|
bool empty() const {
|
|
return current_elements_ == 0;
|
|
}
|
|
|
|
//! Returns the number of unique elements in the set currently
|
|
size_type size() const {
|
|
return current_elements_;
|
|
}
|
|
|
|
//! Returns the maximum number of elements one can insert into the set.
|
|
size_type max_size() const {
|
|
return max_elements_;
|
|
}
|
|
|
|
//! Returns the maximum key one can use with the set.
|
|
key_type max_key() const {
|
|
return offset_count_ > 0 ? (offset_count_ - 1) : 0;
|
|
}
|
|
|
|
/*!
|
|
Retrieve value operator
|
|
\param i Index to retrieve the value from
|
|
*/
|
|
reference operator[](const key_type& i) {
|
|
if(i >= offset_count_) {
|
|
EQ_EXCEPT("Fixed Memory Hash Set", "Index out of range");
|
|
}
|
|
|
|
if(offsets_[i] == 0xFFFFFFFFU) {
|
|
EQ_EXCEPT("Fixed Memory Hash Set", "Element not found.");
|
|
}
|
|
|
|
return elements_[offsets_[i]];
|
|
}
|
|
|
|
/*!
|
|
Retrieve value function
|
|
\param i Index to retrieve the value from
|
|
*/
|
|
reference at(const key_type& i) {
|
|
if(i >= offset_count_) {
|
|
EQ_EXCEPT("Fixed Memory Hash Set", "Index out of range.");
|
|
}
|
|
|
|
if(offsets_[i] == 0xFFFFFFFFU) {
|
|
EQ_EXCEPT("Fixed Memory Hash Set", "Element not found.");
|
|
}
|
|
|
|
return elements_[offsets_[i]];
|
|
}
|
|
|
|
/*!
|
|
Checks if there is a value at a certain index
|
|
\param i Index to check for a value
|
|
*/
|
|
bool exists(const key_type& i) const {
|
|
if(i >= offset_count_) {
|
|
return false;
|
|
}
|
|
|
|
if(offsets_[i] == 0xFFFFFFFFU) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/*!
|
|
Inserts a value into the set at a specific index
|
|
\param i Index to insert the value at
|
|
\param v Value to insert
|
|
*/
|
|
void insert(const key_type& i, const_reference v) {
|
|
if(i >= offset_count_) {
|
|
EQ_EXCEPT("Fixed Memory Hash Set", "Index out of range.");
|
|
}
|
|
|
|
if(offsets_[i] != 0xFFFFFFFFU) {
|
|
elements_[offsets_[i]] = v;
|
|
} else {
|
|
if(current_elements_ >= max_elements_) {
|
|
EQ_EXCEPT("Fixed Memory Hash Set", "Insert pointer out of range.");
|
|
}
|
|
|
|
offsets_[i] = current_elements_;
|
|
memcpy(&elements_[current_elements_], &v, sizeof(value_type));
|
|
++current_elements_;
|
|
*reinterpret_cast<key_type*>(data_ + (sizeof(key_type) * 2)) = current_elements_;
|
|
}
|
|
}
|
|
|
|
//! Calculates how much memory we should allocate based on element size and count
|
|
static size_type estimated_size(key_type element_count, key_type max_elements) {
|
|
size_type total_size = 3 * sizeof(key_type);
|
|
total_size += sizeof(key_type) * (max_elements + 1);
|
|
total_size += sizeof(T) * element_count;
|
|
return total_size;
|
|
}
|
|
|
|
private:
|
|
unsigned char *data_;
|
|
size_type size_;
|
|
key_type offset_count_;
|
|
key_type max_elements_;
|
|
key_type current_elements_;
|
|
key_type *offsets_;
|
|
value_type *elements_;
|
|
};
|
|
} // EQEmu
|