From db6c4f54a2036210c1f5c5aed9aa6c210aaae0ba Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sun, 6 Dec 2020 09:17:50 +1000 Subject: [PATCH] emory/chunk/map: move chunking to a free function --- emory/chunk/map.cpp | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/emory/chunk/map.cpp b/emory/chunk/map.cpp index bdcebf9..f14601f 100644 --- a/emory/chunk/map.cpp +++ b/emory/chunk/map.cpp @@ -16,13 +16,17 @@ using emory::chunk::map; /////////////////////////////////////////////////////////////////////////////// -map::map ( - cruft::view src, +template +static +OutputT +find_chunks ( + OutputT &&dst, + cruft::view src, emory::chunk::params const &p ) { using hash_type = cruft::hash::buzhash; if (src.size () < p.window) - return; + return dst; using digest_type = hash_type::digest_type ; digest_type const mask = ~digest_type (0) >> (sizeof (digest_type) * 8 - p.bits); @@ -39,20 +43,20 @@ map::map ( for ( ; cursor < src.end () - p.window; ++cursor) { if (likely (hash_state & mask)) { hash_state = cruft::rotatel (hash_state, 1) - ^ cruft::rotatel (u64 (*(cursor - p.window)), p.window) - ^ *cursor; + ^ cruft::rotatel (u64 (*(cursor - p.window)), p.window) + ^ *cursor; continue; } cruft::view const region { start, cursor }; - elements.push_back ({ + *dst = { .offset = { std::distance (src.begin (), start), std::distance (src.begin (), cursor) }, - .digest = static_hash {} (region) - }); + .digest = HashT {} (region) + }; start = cursor; break; @@ -62,12 +66,22 @@ map::map ( if (start != src.end ()) { cruft::view const region { start, src.end () }; - elements.push_back ({ + *dst++ = { .offset = { std::distance (src.begin (), start), std::distance (src.begin (), src.end ()) }, - .digest = static_hash {} (region) - }); + .digest = HashT {} (region) + }; } + + return dst; +} + +/////////////////////////////////////////////////////////////////////////////// +map::map ( + cruft::view src, + emory::chunk::params const &p +) { + ::find_chunks (std::back_inserter (elements), src, p); }