diff options
| author | Glenn Smith <glenn@vector35.com> | 2020-09-14 17:15:37 -0400 |
|---|---|---|
| committer | Glenn Smith <glenn@vector35.com> | 2021-07-13 15:04:49 -0400 |
| commit | ce08fff22e4bed3d977cfd662ed2a6d8caa82997 (patch) | |
| tree | 9fed8c3070837f38099d13ff78a7577fbc4ac168 /binaryninjaapi.cpp | |
| parent | 10e668db4d76658882bee7b75d47063b054a730d (diff) | |
Database api
Co-Authored-By: Josh Ferrell <josh@vector35.com>
Diffstat (limited to 'binaryninjaapi.cpp')
| -rw-r--r-- | binaryninjaapi.cpp | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/binaryninjaapi.cpp b/binaryninjaapi.cpp index 6b2497a9..d2815e8f 100644 --- a/binaryninjaapi.cpp +++ b/binaryninjaapi.cpp @@ -19,6 +19,7 @@ // IN THE SOFTWARE. #include "binaryninjaapi.h" +#include <numeric> using namespace BinaryNinja; using namespace std; @@ -385,4 +386,41 @@ map<string, uint64_t> BinaryNinja::GetMemoryUsageInfo() result[info[i].name] = info[i].value; BNFreeMemoryUsageInfo(info, count); return result; -}
\ No newline at end of file +} + + +std::function<bool(size_t, size_t)> +BinaryNinja::SplitProgress(std::function<bool(size_t, size_t)> originalFn, size_t subpart, size_t subpartCount) +{ + return SplitProgress(originalFn, subpart, std::vector<double>(subpartCount, 1.0 / (double)subpartCount)); +} + + +std::function<bool(size_t, size_t)> +BinaryNinja::SplitProgress(std::function<bool(size_t, size_t)> originalFn, size_t subpart, std::vector<double> subpartWeights) +{ + if (!originalFn) + return [](size_t, size_t){ return true; }; + + // Normalize weights + double weightSum = std::accumulate(subpartWeights.begin(), subpartWeights.end(), 0.0); + if (weightSum < 0.0001f) + return [](size_t, size_t){ return true; }; + // Keep a running count of weights for the start + std::vector<double> subpartStarts; + double start = 0.0; + for (auto i = 0; i < subpartWeights.size(); ++i) + { + subpartStarts.push_back(start); + subpartWeights[i] /= weightSum; + start += subpartWeights[i]; + } + + return [=](size_t current, size_t max) { + // Just use a large number for easy divisibility + size_t steps = 1000000; + double subpartSize = steps * subpartWeights[subpart]; + double subpartProgress = ((double)current / (double)max) * subpartSize; + return originalFn(subpartStarts[subpart] * steps + subpartProgress, steps); + }; +} |
