dragonfly/src/core/string_set.cc
Braydn b8d791961e
feat(core): Added DenseSet and StringSet types (#268)
* feat(core): Added DenseSet & StringSet types with docs

- Improved documentation by adding labels to chain types & pointer tagging table
- Added potential improvements to the DenseSet types in the docs
- Added excalidraw save file for future editing
- Removed ambiguous overloading types
- Renamed iterators to be more clear


* feat(core): Cleaned up DenseSet and Docs
* feat(core): Made DenseSet more ergonomic
* feat(server): Integration of DenseSet into Server

- Integrated DenseSet with CompactObj and the Set Family commands

Signed-off-by: Braydn <braydn.moore@uwaterloo.ca>
2022-09-14 08:41:54 +03:00

102 lines
No EOL
2 KiB
C++

#include "core/string_set.h"
#include "core/compact_object.h"
#include "redis/sds.h"
extern "C" {
#include "redis/zmalloc.h"
}
namespace dfly {
uint64_t StringSet::Hash(const void* ptr) const {
sds s = (sds)ptr;
return CompactObj::HashCode(std::string_view{s, sdslen(s)});
}
bool StringSet::Equal(const void* ptr1, const void* ptr2) const {
sds s1 = (sds)ptr1;
sds s2 = (sds)ptr2;
if (sdslen(s1) != sdslen(s2)) {
return false;
}
return sdslen(s1) == 0 || memcmp(s1, s2, sdslen(s1)) == 0;
}
size_t StringSet::ObjectAllocSize(const void* s1) const {
return zmalloc_usable_size(sdsAllocPtr((sds)s1));
}
bool StringSet::AddSds(sds s1) {
return AddInternal(s1);
}
bool StringSet::Add(std::string_view s1) {
sds newsds = sdsnewlen(s1.data(), s1.size());
if (!AddInternal(newsds)) {
sdsfree(newsds);
return false;
}
return true;
}
bool StringSet::EraseSds(sds s1) {
void* ret = EraseInternal(s1);
if (ret == nullptr) {
return false;
} else {
sdsfree((sds)ret);
return true;
}
}
bool StringSet::Erase(std::string_view s1) {
sds to_erase = sdsnewlen(s1.data(), s1.size());
bool ret = EraseSds(to_erase);
sdsfree(to_erase);
return ret;
}
bool StringSet::ContainsSds(sds s1) const {
return ContainsInternal(s1);
}
bool StringSet::Contains(std::string_view s1) const {
sds to_search = sdsnewlen(s1.data(), s1.size());
bool ret = ContainsInternal(to_search);
sdsfree(to_search);
return ret;
}
void StringSet::Clear() {
for (auto it = begin(); it != end(); ++it) {
sdsfree((sds)*it);
}
ClearInternal();
}
std::optional<std::string> StringSet::Pop() {
sds str = (sds)PopInternal();
if (str == nullptr) {
return std::nullopt;
}
std::string ret{str, sdslen(str)};
sdsfree(str);
return ret;
}
sds StringSet::PopRaw() {
return (sds)PopInternal();
}
uint32_t StringSet::Scan(uint32_t cursor, const std::function<void(const sds)>& func) const {
return DenseSet::Scan(cursor, [func](const void* ptr) { func((sds)ptr); });
}
}; // namespace dfly