e5bbcb8bcf
Also turn off -Wsign-compare warnings in the future, we do not consider them important.
Fixes the following GCC 5 warnings:
```
core/node_path.cpp:279:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
core/oa_hash_map.h:169:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
core/oa_hash_map.h:314:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
drivers/gles2/shader_gles2.cpp:985:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
drivers/gles3/rasterizer_storage_gles3.cpp:1075:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
drivers/pulseaudio/audio_driver_pulseaudio.cpp:343:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
editor/editor_plugin.cpp:525:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
editor/editor_properties_array_dict.cpp:747:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
editor/plugins/spatial_editor_plugin.cpp:2078:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
editor/plugins/spatial_editor_plugin.cpp:4096:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
editor/plugins/sprite_editor_plugin.cpp💯20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
modules/cvtt/image_compress_cvtt.cpp:122:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
modules/cvtt/image_compress_cvtt.cpp:134:77: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
modules/cvtt/image_compress_cvtt.cpp:339:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
modules/etc/image_etc.cpp:222:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
modules/gdnative/register_types.cpp:242:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
modules/gdnative/register_types.cpp:258:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
modules/opensimplex/simplex_noise.cpp:200:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
modules/opensimplex/simplex_noise.cpp:222:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
modules/opensimplex/simplex_noise.cpp:246:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
platform/android/export/export.cpp:1085:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
platform/android/export/export.cpp:1489:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
platform/android/export/export.cpp:1623:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
platform/iphone/export/export.cpp:206:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
platform/iphone/export/export.cpp:356:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
platform/iphone/export/export.cpp:406:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
platform/iphone/export/export.cpp:493:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
scene/3d/audio_stream_player_3d.cpp:420:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
scene/resources/audio_stream_sample.cpp:565:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
scene/resources/audio_stream_sample.cpp:571:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
servers/audio/audio_rb_resampler.cpp:156:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
```
The following warnings were not fixed, as they implied casting for no gain:
```
core/io/packet_peer.cpp:228:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
core/io/resource_format_binary.cpp:109:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
drivers/gles2/rasterizer_scene_gles2.cpp:144:57: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
drivers/unix/file_access_unix.cpp:249:46: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
scene/3d/voxel_light_baker.cpp:889:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
scene/3d/voxel_light_baker.cpp:1020:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
scene/3d/voxel_light_baker.cpp:1154:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
scene/3d/voxel_light_baker.cpp:2255:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
scene/resources/bit_mask.cpp:336:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
servers/audio/audio_stream.cpp:141:49: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
servers/audio/audio_stream.cpp:150:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
servers/audio/audio_stream.cpp:154:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
servers/audio_server.cpp:86:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
servers/audio_server.cpp:89:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
```
328 lines
8.2 KiB
C++
328 lines
8.2 KiB
C++
/*************************************************************************/
|
|
/* oa_hash_map.h */
|
|
/*************************************************************************/
|
|
/* This file is part of: */
|
|
/* GODOT ENGINE */
|
|
/* https://godotengine.org */
|
|
/*************************************************************************/
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
|
/* */
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
/* a copy of this software and associated documentation files (the */
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
/* the following conditions: */
|
|
/* */
|
|
/* The above copyright notice and this permission notice shall be */
|
|
/* included in all copies or substantial portions of the Software. */
|
|
/* */
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
/*************************************************************************/
|
|
|
|
#ifndef OA_HASH_MAP_H
|
|
#define OA_HASH_MAP_H
|
|
|
|
#include "core/hashfuncs.h"
|
|
#include "core/math/math_funcs.h"
|
|
#include "core/os/copymem.h"
|
|
#include "core/os/memory.h"
|
|
|
|
/**
|
|
* A HashMap implementation that uses open addressing with robinhood hashing.
|
|
* Robinhood hashing swaps out entries that have a smaller probing distance
|
|
* than the to-be-inserted entry, that evens out the average probing distance
|
|
* and enables faster lookups.
|
|
*
|
|
* The entries are stored inplace, so huge keys or values might fill cache lines
|
|
* a lot faster.
|
|
*/
|
|
template <class TKey, class TValue,
|
|
class Hasher = HashMapHasherDefault,
|
|
class Comparator = HashMapComparatorDefault<TKey> >
|
|
class OAHashMap {
|
|
|
|
private:
|
|
TValue *values;
|
|
TKey *keys;
|
|
uint32_t *hashes;
|
|
|
|
uint32_t capacity;
|
|
|
|
uint32_t num_elements;
|
|
|
|
static const uint32_t EMPTY_HASH = 0;
|
|
static const uint32_t DELETED_HASH_BIT = 1 << 31;
|
|
|
|
_FORCE_INLINE_ uint32_t _hash(const TKey &p_key) {
|
|
uint32_t hash = Hasher::hash(p_key);
|
|
|
|
if (hash == EMPTY_HASH) {
|
|
hash = EMPTY_HASH + 1;
|
|
} else if (hash & DELETED_HASH_BIT) {
|
|
hash &= ~DELETED_HASH_BIT;
|
|
}
|
|
|
|
return hash;
|
|
}
|
|
|
|
_FORCE_INLINE_ uint32_t _get_probe_length(uint32_t p_pos, uint32_t p_hash) {
|
|
p_hash = p_hash & ~DELETED_HASH_BIT; // we don't care if it was deleted or not
|
|
|
|
uint32_t original_pos = p_hash % capacity;
|
|
|
|
return p_pos - original_pos;
|
|
}
|
|
|
|
_FORCE_INLINE_ void _construct(uint32_t p_pos, uint32_t p_hash, const TKey &p_key, const TValue &p_value) {
|
|
memnew_placement(&keys[p_pos], TKey(p_key));
|
|
memnew_placement(&values[p_pos], TValue(p_value));
|
|
hashes[p_pos] = p_hash;
|
|
|
|
num_elements++;
|
|
}
|
|
|
|
bool _lookup_pos(const TKey &p_key, uint32_t &r_pos) {
|
|
uint32_t hash = _hash(p_key);
|
|
uint32_t pos = hash % capacity;
|
|
uint32_t distance = 0;
|
|
|
|
while (42) {
|
|
if (hashes[pos] == EMPTY_HASH) {
|
|
return false;
|
|
}
|
|
|
|
if (distance > _get_probe_length(pos, hashes[pos])) {
|
|
return false;
|
|
}
|
|
|
|
if (hashes[pos] == hash && Comparator::compare(keys[pos], p_key)) {
|
|
r_pos = pos;
|
|
return true;
|
|
}
|
|
|
|
pos = (pos + 1) % capacity;
|
|
distance++;
|
|
}
|
|
}
|
|
|
|
void _insert_with_hash(uint32_t p_hash, const TKey &p_key, const TValue &p_value) {
|
|
|
|
uint32_t hash = p_hash;
|
|
uint32_t distance = 0;
|
|
uint32_t pos = hash % capacity;
|
|
|
|
TKey key = p_key;
|
|
TValue value = p_value;
|
|
|
|
while (42) {
|
|
if (hashes[pos] == EMPTY_HASH) {
|
|
_construct(pos, hash, p_key, p_value);
|
|
|
|
return;
|
|
}
|
|
|
|
// not an empty slot, let's check the probing length of the existing one
|
|
uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos]);
|
|
if (existing_probe_len < distance) {
|
|
|
|
if (hashes[pos] & DELETED_HASH_BIT) {
|
|
// we found a place where we can fit in!
|
|
_construct(pos, hash, p_key, p_value);
|
|
|
|
return;
|
|
}
|
|
|
|
SWAP(hash, hashes[pos]);
|
|
SWAP(key, keys[pos]);
|
|
SWAP(value, values[pos]);
|
|
distance = existing_probe_len;
|
|
}
|
|
|
|
pos = (pos + 1) % capacity;
|
|
distance++;
|
|
}
|
|
}
|
|
void _resize_and_rehash() {
|
|
|
|
TKey *old_keys = keys;
|
|
TValue *old_values = values;
|
|
uint32_t *old_hashes = hashes;
|
|
|
|
uint32_t old_capacity = capacity;
|
|
|
|
capacity = old_capacity * 2;
|
|
num_elements = 0;
|
|
|
|
keys = memnew_arr(TKey, capacity);
|
|
values = memnew_arr(TValue, capacity);
|
|
hashes = memnew_arr(uint32_t, capacity);
|
|
|
|
for (uint32_t i = 0; i < capacity; i++) {
|
|
hashes[i] = 0;
|
|
}
|
|
|
|
for (uint32_t i = 0; i < old_capacity; i++) {
|
|
if (old_hashes[i] == EMPTY_HASH) {
|
|
continue;
|
|
}
|
|
if (old_hashes[i] & DELETED_HASH_BIT) {
|
|
continue;
|
|
}
|
|
|
|
_insert_with_hash(old_hashes[i], old_keys[i], old_values[i]);
|
|
}
|
|
|
|
memdelete_arr(old_keys);
|
|
memdelete_arr(old_values);
|
|
memdelete_arr(old_hashes);
|
|
}
|
|
|
|
public:
|
|
_FORCE_INLINE_ uint32_t get_capacity() const { return capacity; }
|
|
_FORCE_INLINE_ uint32_t get_num_elements() const { return num_elements; }
|
|
|
|
void insert(const TKey &p_key, const TValue &p_value) {
|
|
|
|
if ((float)num_elements / (float)capacity > 0.9) {
|
|
_resize_and_rehash();
|
|
}
|
|
|
|
uint32_t hash = _hash(p_key);
|
|
|
|
_insert_with_hash(hash, p_key, p_value);
|
|
}
|
|
|
|
void set(const TKey &p_key, const TValue &p_data) {
|
|
uint32_t pos = 0;
|
|
bool exists = _lookup_pos(p_key, pos);
|
|
|
|
if (exists) {
|
|
values[pos].~TValue();
|
|
memnew_placement(&values[pos], TValue(p_data));
|
|
} else {
|
|
insert(p_key, p_data);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* returns true if the value was found, false otherwise.
|
|
*
|
|
* if r_data is not NULL then the value will be written to the object
|
|
* it points to.
|
|
*/
|
|
bool lookup(const TKey &p_key, TValue &r_data) {
|
|
uint32_t pos = 0;
|
|
bool exists = _lookup_pos(p_key, pos);
|
|
|
|
if (exists) {
|
|
r_data.~TValue();
|
|
memnew_placement(&r_data, TValue(values[pos]));
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
_FORCE_INLINE_ bool has(const TKey &p_key) {
|
|
uint32_t _pos = 0;
|
|
return _lookup_pos(p_key, _pos);
|
|
}
|
|
|
|
void remove(const TKey &p_key) {
|
|
uint32_t pos = 0;
|
|
bool exists = _lookup_pos(p_key, pos);
|
|
|
|
if (!exists) {
|
|
return;
|
|
}
|
|
|
|
hashes[pos] |= DELETED_HASH_BIT;
|
|
values[pos].~TValue();
|
|
keys[pos].~TKey();
|
|
num_elements--;
|
|
}
|
|
|
|
struct Iterator {
|
|
bool valid;
|
|
|
|
const TKey *key;
|
|
const TValue *value;
|
|
|
|
private:
|
|
uint32_t pos;
|
|
friend class OAHashMap;
|
|
};
|
|
|
|
Iterator iter() const {
|
|
Iterator it;
|
|
|
|
it.valid = true;
|
|
it.pos = 0;
|
|
|
|
return next_iter(it);
|
|
}
|
|
|
|
Iterator next_iter(const Iterator &p_iter) const {
|
|
|
|
if (!p_iter.valid) {
|
|
return p_iter;
|
|
}
|
|
|
|
Iterator it;
|
|
it.valid = false;
|
|
it.pos = p_iter.pos;
|
|
it.key = NULL;
|
|
it.value = NULL;
|
|
|
|
for (uint32_t i = it.pos; i < capacity; i++) {
|
|
it.pos = i + 1;
|
|
|
|
if (hashes[i] == EMPTY_HASH) {
|
|
continue;
|
|
}
|
|
if (hashes[i] & DELETED_HASH_BIT) {
|
|
continue;
|
|
}
|
|
|
|
it.valid = true;
|
|
it.key = &keys[i];
|
|
it.value = &values[i];
|
|
return it;
|
|
}
|
|
|
|
return it;
|
|
}
|
|
|
|
OAHashMap(uint32_t p_initial_capacity = 64) {
|
|
|
|
capacity = p_initial_capacity;
|
|
num_elements = 0;
|
|
|
|
keys = memnew_arr(TKey, p_initial_capacity);
|
|
values = memnew_arr(TValue, p_initial_capacity);
|
|
hashes = memnew_arr(uint32_t, p_initial_capacity);
|
|
|
|
for (uint32_t i = 0; i < p_initial_capacity; i++) {
|
|
hashes[i] = 0;
|
|
}
|
|
}
|
|
|
|
~OAHashMap() {
|
|
|
|
memdelete_arr(keys);
|
|
memdelete_arr(values);
|
|
memdelete_arr(hashes);
|
|
}
|
|
};
|
|
|
|
#endif
|