emory/emory/chunk/map.cpp

51 lines
1.5 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2019, Danny Robson <danny@nerdcruft.net>
*/
#include "map.hpp"
#include "params.hpp"
#include <cruft/util/hash/buzhash.hpp>
using emory::chunk::map;
///////////////////////////////////////////////////////////////////////////////
map::map (cruft::view<u08 const *> data, const emory::chunk::params &p)
{
using hash_type = cruft::hash::buzhash<u64>;
hash_type h (p.window, data);
auto remain = data.consume (p.window);
using digest_type = hash_type::digest_type ;
digest_type const mask = ~digest_type (0) >> (sizeof (digest_type) * 8 - p.bits);
for (u08 const *cursor = remain.begin (), *start = data.begin (); cursor != remain.end (); cursor++) {
auto const digest = h (cursor);
if (std::distance (start, cursor) < p.minimum)
continue;
if (unlikely (digest & mask))
continue;
cruft::view<u08 const*> const region { start, cursor };
start = cursor + 1;
elements.push_back ({
.offset = {
std::pair<std::size_t,std::size_t> {
std::distance (data.begin (), region.begin ()),
std::distance (data.begin (), region.end ())
},
},
.digest = static_hash {} (region),
});
}
}