From 6e8baafeb9cc2d79b42fc38ef8059698e73827d9 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Thu, 1 Apr 2021 17:33:37 -0700 Subject: [PATCH] Added TableVal::ToMap to retrieve a table's entire contents as a unordered_map --- src/Val.cc | 16 ++++++++++++++++ src/Val.h | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/src/Val.cc b/src/Val.cc index 1aba36b12b..fe7f0a4c9b 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -2311,6 +2311,22 @@ ListValPtr TableVal::ToPureListVal() const return ToListVal(tl[0]->Tag()); } +std::unordered_map TableVal::ToMap() const + { + std::unordered_map res; + + for ( const auto& iter : *table_val ) + { + auto k = iter.GetHashKey(); + auto v = iter.GetValue(); + auto vl = table_hash->RecoverVals(*k); + + res[vl.release()] = v->GetVal(); + } + + return res; + } + const detail::AttrPtr& TableVal::GetAttr(detail::AttrTag t) const { return attrs ? attrs->Find(t) : detail::Attr::nil; diff --git a/src/Val.h b/src/Val.h index bfbca2b6a6..ec0a5db158 100644 --- a/src/Val.h +++ b/src/Val.h @@ -895,6 +895,11 @@ public: // with non-composite index type). ListValPtr ToPureListVal() const; + // Returns a map of index-to-value's. The value is nil for sets. + // It's up to the caller to Unref() the index Val* when done + // with it. + std::unordered_map ToMap() const; + void SetAttrs(detail::AttributesPtr attrs); const detail::AttrPtr& GetAttr(detail::AttrTag t) const;