string_pool.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include <util/generic/hash.h>
  3. #include <util/generic/vector.h>
  4. namespace NMonitoring {
  5. ////////////////////////////////////////////////////////////////////////////////
  6. // TStringPoolBuilder
  7. ////////////////////////////////////////////////////////////////////////////////
  8. class TStringPoolBuilder {
  9. public:
  10. struct TValue: TNonCopyable {
  11. TValue(ui32 idx, ui32 freq)
  12. : Index{idx}
  13. , Frequency{freq}
  14. {
  15. }
  16. ui32 Index;
  17. ui32 Frequency;
  18. };
  19. public:
  20. const TValue* PutIfAbsent(TStringBuf str);
  21. const TValue* GetByIndex(ui32 index) const;
  22. /// Determines whether pool must be sorted by value frequencies
  23. TStringPoolBuilder& SetSorted(bool sorted) {
  24. RequiresSorting_ = sorted;
  25. return *this;
  26. }
  27. TStringPoolBuilder& Build();
  28. TStringBuf Get(ui32 index) const {
  29. if (RequiresSorting_) {
  30. Y_ENSURE(IsBuilt_, "Pool must be sorted first");
  31. }
  32. return StrVector_.at(index).first;
  33. }
  34. TStringBuf Get(const TValue* value) const {
  35. return StrVector_.at(value->Index).first;
  36. }
  37. template <typename TConsumer>
  38. void ForEach(TConsumer&& c) {
  39. Y_ENSURE(IsBuilt_, "Pool must be built first");
  40. for (const auto& value : StrVector_) {
  41. c(value.first, value.second->Index, value.second->Frequency);
  42. }
  43. }
  44. size_t BytesSize() const noexcept {
  45. return BytesSize_;
  46. }
  47. size_t Count() const noexcept {
  48. return StrMap_.size();
  49. }
  50. private:
  51. THashMap<TString, TValue> StrMap_;
  52. TVector<std::pair<TStringBuf, TValue*>> StrVector_;
  53. bool RequiresSorting_ = false;
  54. bool IsBuilt_ = false;
  55. size_t BytesSize_ = 0;
  56. };
  57. ////////////////////////////////////////////////////////////////////////////////
  58. // TStringPool
  59. ////////////////////////////////////////////////////////////////////////////////
  60. class TStringPool {
  61. public:
  62. TStringPool(const char* data, ui32 size) {
  63. InitIndex(data, size);
  64. }
  65. TStringBuf Get(ui32 i) const {
  66. return Index_.at(i);
  67. }
  68. size_t Size() const {
  69. return Index_.size();
  70. }
  71. size_t SizeBytes() const {
  72. return Index_.capacity() * sizeof(TStringBuf);
  73. }
  74. private:
  75. void InitIndex(const char* data, ui32 size);
  76. private:
  77. TVector<TStringBuf> Index_;
  78. };
  79. }