9

Is there a function to return the size of a multi_index table? If not is there a better way then looping through them? uint64_t count; for (auto itr = table.begin(); itr != table.end(); ++itr) { ++count; }

4

Agree no size is maintained by EOS implementation , the iterators are not random access , so if size state not maintained (which would be best performance compared to calculating size on the fly when needed) then more compact expression to get the size ( if you really need it as I don't see the use case yet) is to use std::count

auto size = std::count(table.cbegin(),table.cend());
  • I had to make that: auto size = std::distance(table.cbegin(),table.cend()); – Mirko Lindner Sep 20 '18 at 9:34
  • @MirkoLindner - it's going to be same complexity as std::count as std::distance has to iterate it can't just calculate distance because those iterators , as multi_index iterators are not random access like in std::vector where all elements are continuous. If you use std::distance on vector it's just end - begin pretty much . stackoverflow.com/a/12530248/2264070 – Vladimir Venediktov Sep 23 '18 at 22:30
1

I didn't find one in the documentation. But maybe you can maintain the variable yourself in the contract state:

Increase on add
Decrease on remove

If you store it in a multi_index you could use a member table which will maintain the variable.

ID (unint64_t) | Attribut (String) | Value (String)
0              | 'mCounter'        | '5' 
  • hmm, that's obnoxious there's no len() or count() for multi-index -- thank you – Alex Jul 21 '18 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.