diff options
4 files changed, 75 insertions, 0 deletions
diff --git a/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt b/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt index 20d47c90453..d5d47ed5c61 100644 --- a/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt +++ b/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt @@ -19,5 +19,6 @@ vespa_add_library(staging_vespalib_vespalib_util OBJECT timer.cpp xmlserializable.cpp xmlstream.cpp + zncurve.cpp DEPENDS ) diff --git a/staging_vespalib/src/vespa/vespalib/util/zncurve.cpp b/staging_vespalib/src/vespa/vespalib/util/zncurve.cpp new file mode 100644 index 00000000000..64157468f7e --- /dev/null +++ b/staging_vespalib/src/vespa/vespalib/util/zncurve.cpp @@ -0,0 +1,12 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "zncurve.hpp" + +namespace vespalib { + +template class ZNPoint<2, uint32_t>; +template class ZNPoint<4, uint32_t>; +template class ZNPoint<8, uint32_t>; + +} // namespace vespalib + diff --git a/staging_vespalib/src/vespa/vespalib/util/zncurve.h b/staging_vespalib/src/vespa/vespalib/util/zncurve.h new file mode 100644 index 00000000000..e8b82e77e96 --- /dev/null +++ b/staging_vespalib/src/vespa/vespalib/util/zncurve.h @@ -0,0 +1,21 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include <cstdint> + +namespace vespalib { + +template<int numDim, class T> +class ZNPoint { +public: + ZNPoint(const T * begin); + uint64_t distance(const ZNPoint & rhs) const; + bool operator < (const ZNPoint & rhs) const; +private: + T _vector[numDim]; + uint8_t _zCurve[sizeof(T)*numDim]; +}; + +} // namespace vespalib + diff --git a/staging_vespalib/src/vespa/vespalib/util/zncurve.hpp b/staging_vespalib/src/vespa/vespalib/util/zncurve.hpp new file mode 100644 index 00000000000..896b668ddcc --- /dev/null +++ b/staging_vespalib/src/vespa/vespalib/util/zncurve.hpp @@ -0,0 +1,41 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include "zncurve.h" +#include <cstring> +#include <cmath> + +namespace vespalib { + +template<int numDim, class T> +ZNPoint<numDim, T>::ZNPoint(const T * begin) + : _vector(), + _zCurve() +{ + for (uint32_t i(0); i < numDim; i++) { + _vector[i] = begin[i]; + } +} + +template<int numDim, class T> +uint64_t +ZNPoint<numDim, T>::distance(const ZNPoint & rhs) const +{ + uint64_t sum(0); + for (unsigned i(0); i < numDim; i++) { + int64_t diff = _vector[i] - rhs._vector[i]; + sum += diff*diff; + } + return sqrt(sum); +} + +template<int numDim, class T> +bool +ZNPoint<numDim, T>::operator < (const ZNPoint & rhs) const +{ + return memcmp(_zCurve, rhs._zCurve, sizeof(_zCurve)) < 0; +} + +} // namespace vespalib + |