string_pool.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "string_pool.h"
  2. #include <util/generic/ylimits.h>
  3. namespace NMonitoring {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. // TStringPoolBuilder
  6. ////////////////////////////////////////////////////////////////////////////////
  7. const TStringPoolBuilder::TValue* TStringPoolBuilder::PutIfAbsent(TStringBuf str) {
  8. Y_ENSURE(!IsBuilt_, "Cannot add more values after string has been built");
  9. auto [it, isInserted] = StrMap_.try_emplace(str, Max<ui32>(), 0);
  10. if (isInserted) {
  11. BytesSize_ += str.size();
  12. it->second.Index = StrVector_.size();
  13. StrVector_.emplace_back(it->first, &it->second);
  14. }
  15. TValue* value = &it->second;
  16. ++value->Frequency;
  17. return value;
  18. }
  19. const TStringPoolBuilder::TValue* TStringPoolBuilder::GetByIndex(ui32 index) const {
  20. return StrVector_.at(index).second;
  21. }
  22. TStringPoolBuilder& TStringPoolBuilder::Build() {
  23. if (RequiresSorting_) {
  24. // sort in reversed order
  25. std::sort(StrVector_.begin(), StrVector_.end(), [](auto& a, auto& b) {
  26. return a.second->Frequency > b.second->Frequency;
  27. });
  28. ui32 i = 0;
  29. for (auto& value : StrVector_) {
  30. value.second->Index = i++;
  31. }
  32. }
  33. IsBuilt_ = true;
  34. return *this;
  35. }
  36. ////////////////////////////////////////////////////////////////////////////////
  37. // TStringPool
  38. ////////////////////////////////////////////////////////////////////////////////
  39. void TStringPool::InitIndex(const char* data, ui32 size) {
  40. const char* begin = data;
  41. const char* end = begin + size;
  42. for (const char* p = begin; p != end; ++p) {
  43. if (*p == '\0') {
  44. Index_.push_back(TStringBuf(begin, p));
  45. begin = p + 1;
  46. }
  47. }
  48. }
  49. }