From fbb107dbaf9b0a708b9513357d0c3d9cf13e190a Mon Sep 17 00:00:00 2001 From: vng Date: Sun, 13 Feb 2011 14:01:31 +0200 Subject: [PATCH] Replace vector to buffer_vector where possible in feature's geometry Encoding\Decoding. --- indexer/geometry_coding.cpp | 36 +++++++++++-------- indexer/geometry_coding.hpp | 34 +++++++++--------- indexer/geometry_serialization.cpp | 23 +++++++----- indexer/geometry_serialization.hpp | 25 +++++++------ .../indexer_tests/geometry_coding_test.cpp | 23 ++++++++---- 5 files changed, 83 insertions(+), 58 deletions(-) diff --git a/indexer/geometry_coding.cpp b/indexer/geometry_coding.cpp index 361e17b20f..987315fc1b 100644 --- a/indexer/geometry_coding.cpp +++ b/indexer/geometry_coding.cpp @@ -69,23 +69,29 @@ namespace geo_coding bool TestDecoding(InPointsT const & points, m2::PointU const & basePoint, m2::PointU const & maxPoint, - DeltasT const & deltas, - void (* fnDecode)(DeltasT const & deltas, + OutDeltasT const & deltas, + void (* fnDecode)(InDeltasT const & deltas, m2::PointU const & basePoint, m2::PointU const & maxPoint, OutPointsT & points)) { + size_t const count = points.size(); + vector decoded; - decoded.reserve(points.size()); - fnDecode(deltas, basePoint, maxPoint, decoded); - ASSERT_EQUAL(points, decoded, (basePoint, maxPoint)); + decoded.resize(count); + + OutPointsT decodedA(decoded); + fnDecode(make_read_adapter(deltas), basePoint, maxPoint, decodedA); + + for (size_t i = 0; i < count; ++i) + ASSERT_EQUAL(points[i], decoded[i], ()); return true; } void EncodePolylinePrev1(InPointsT const & points, m2::PointU const & basePoint, - m2::PointU const & /*maxPoint*/, - DeltasT & deltas) + m2::PointU const & maxPoint, + OutDeltasT & deltas) { size_t const count = points.size(); if (count > 0) @@ -95,10 +101,10 @@ void EncodePolylinePrev1(InPointsT const & points, deltas.push_back(EncodeDelta(points[i], points[i-1])); } - ASSERT(TestDecoding(points, basePoint, m2::PointU(), deltas, &DecodePolylinePrev1), ()); + ASSERT(TestDecoding(points, basePoint, maxPoint, deltas, &DecodePolylinePrev1), ()); } -void DecodePolylinePrev1(DeltasT const & deltas, +void DecodePolylinePrev1(InDeltasT const & deltas, m2::PointU const & basePoint, m2::PointU const & /*maxPoint*/, OutPointsT & points) @@ -115,7 +121,7 @@ void DecodePolylinePrev1(DeltasT const & deltas, void EncodePolylinePrev2(InPointsT const & points, m2::PointU const & basePoint, m2::PointU const & maxPoint, - DeltasT & deltas) + OutDeltasT & deltas) { size_t const count = points.size(); if (count > 0) @@ -133,7 +139,7 @@ void EncodePolylinePrev2(InPointsT const & points, ASSERT(TestDecoding(points, basePoint, maxPoint, deltas, &DecodePolylinePrev2), ()); } -void DecodePolylinePrev2(DeltasT const & deltas, +void DecodePolylinePrev2(InDeltasT const & deltas, m2::PointU const & basePoint, m2::PointU const & maxPoint, OutPointsT & points) @@ -158,7 +164,7 @@ void DecodePolylinePrev2(DeltasT const & deltas, void EncodePolylinePrev3(InPointsT const & points, m2::PointU const & basePoint, m2::PointU const & maxPoint, - DeltasT & deltas) + OutDeltasT & deltas) { ASSERT_LESS_OR_EQUAL(basePoint.x, maxPoint.x, (basePoint, maxPoint)); ASSERT_LESS_OR_EQUAL(basePoint.y, maxPoint.y, (basePoint, maxPoint)); @@ -187,7 +193,7 @@ void EncodePolylinePrev3(InPointsT const & points, ASSERT(TestDecoding(points, basePoint, maxPoint, deltas, &DecodePolylinePrev3), ()); } -void DecodePolylinePrev3(DeltasT const & deltas, +void DecodePolylinePrev3(InDeltasT const & deltas, m2::PointU const & basePoint, m2::PointU const & maxPoint, OutPointsT & points) @@ -222,7 +228,7 @@ void DecodePolylinePrev3(DeltasT const & deltas, void EncodeTriangleStrip(InPointsT const & points, m2::PointU const & basePoint, m2::PointU const & maxPoint, - DeltasT & deltas) + OutDeltasT & deltas) { size_t const count = points.size(); if (count > 0) @@ -242,7 +248,7 @@ void EncodeTriangleStrip(InPointsT const & points, } } -void DecodeTriangleStrip(DeltasT const & deltas, +void DecodeTriangleStrip(InDeltasT const & deltas, m2::PointU const & basePoint, m2::PointU const & maxPoint, OutPointsT & points) diff --git a/indexer/geometry_coding.hpp b/indexer/geometry_coding.hpp index 215c8aeca5..13700134be 100644 --- a/indexer/geometry_coding.hpp +++ b/indexer/geometry_coding.hpp @@ -1,12 +1,12 @@ #pragma once #include "../geometry/point2d.hpp" + #include "../coding/varint.hpp" + #include "../base/base.hpp" #include "../base/bits.hpp" -#include "../std/vector.hpp" -#include "../std/tuple.hpp" - +#include "../base/array_adapters.h" //@{ inline uint64_t EncodeDelta(m2::PointU const & actual, m2::PointU const & prediction) @@ -45,16 +45,18 @@ m2::PointU PredictPointInTriangle(m2::PointU const & maxPoint, /// Geometry Coding-Decoding functions. namespace geo_coding { - typedef vector InPointsT; - typedef vector OutPointsT; - typedef vector DeltasT; + typedef array_read InPointsT; + typedef array_write OutPointsT; + + typedef array_read InDeltasT; + typedef array_write OutDeltasT; void EncodePolylinePrev1(InPointsT const & points, m2::PointU const & basePoint, m2::PointU const & maxPoint, - DeltasT & deltas); + OutDeltasT & deltas); -void DecodePolylinePrev1(DeltasT const & deltas, +void DecodePolylinePrev1(InDeltasT const & deltas, m2::PointU const & basePoint, m2::PointU const & maxPoint, OutPointsT & points); @@ -62,9 +64,9 @@ void DecodePolylinePrev1(DeltasT const & deltas, void EncodePolylinePrev2(InPointsT const & points, m2::PointU const & basePoint, m2::PointU const & maxPoint, - DeltasT & deltas); + OutDeltasT & deltas); -void DecodePolylinePrev2(DeltasT const & deltas, +void DecodePolylinePrev2(InDeltasT const & deltas, m2::PointU const & basePoint, m2::PointU const & maxPoint, OutPointsT & points); @@ -72,9 +74,9 @@ void DecodePolylinePrev2(DeltasT const & deltas, void EncodePolylinePrev3(InPointsT const & points, m2::PointU const & basePoint, m2::PointU const & maxPoint, - DeltasT & deltas); + OutDeltasT & deltas); -void DecodePolylinePrev3(DeltasT const & deltas, +void DecodePolylinePrev3(InDeltasT const & deltas, m2::PointU const & basePoint, m2::PointU const & maxPoint, OutPointsT & points); @@ -82,12 +84,12 @@ void DecodePolylinePrev3(DeltasT const & deltas, inline void EncodePolyline(InPointsT const & points, m2::PointU const & basePoint, m2::PointU const & maxPoint, - DeltasT & deltas) + OutDeltasT & deltas) { EncodePolylinePrev2(points, basePoint, maxPoint, deltas); } -inline void DecodePolyline(DeltasT const & deltas, +inline void DecodePolyline(InDeltasT const & deltas, m2::PointU const & basePoint, m2::PointU const & maxPoint, OutPointsT & points) @@ -98,9 +100,9 @@ inline void DecodePolyline(DeltasT const & deltas, void EncodeTriangleStrip(InPointsT const & points, m2::PointU const & basePoint, m2::PointU const & maxPoint, - DeltasT & deltas); + OutDeltasT & deltas); -void DecodeTriangleStrip(DeltasT const & deltas, +void DecodeTriangleStrip(InDeltasT const & deltas, m2::PointU const & basePoint, m2::PointU const & maxPoint, OutPointsT & points); diff --git a/indexer/geometry_serialization.cpp b/indexer/geometry_serialization.cpp index cd83bb24bf..03a9b457fe 100644 --- a/indexer/geometry_serialization.cpp +++ b/indexer/geometry_serialization.cpp @@ -36,20 +36,24 @@ namespace serial { return m2::Uint64ToPointU(base); } + + typedef buffer_vector upoints_t; } void Encode(EncodeFunT fn, vector const & points, int64_t base, DeltasT & deltas) { size_t const count = points.size(); - PointsT upoints; + pts::upoints_t upoints; upoints.reserve(count); transform(points.begin(), points.end(), back_inserter(upoints), &pts::D2U); ASSERT ( deltas.empty(), () ); - deltas.reserve(count); - (*fn)(upoints, pts::GetBasePoint(base), pts::GetMaxPoint(), deltas); + deltas.resize(count); + + geo_coding::OutDeltasT adapt(deltas); + (*fn)(make_read_adapter(upoints), pts::GetBasePoint(base), pts::GetMaxPoint(), adapt); } template @@ -57,15 +61,16 @@ namespace serial { size_t const count = deltas.size() * reserveF; - PointsT upoints; - upoints.reserve(count); + pts::upoints_t upoints; + upoints.resize(count); - (*fn)(deltas, pts::GetBasePoint(base), pts::GetMaxPoint(), upoints); + geo_coding::OutPointsT adapt(upoints); + (*fn)(make_read_adapter(deltas), pts::GetBasePoint(base), pts::GetMaxPoint(), adapt); // It is may be not empty, when storing triangles. if (points.empty()) points.reserve(count); - transform(upoints.begin(), upoints.end(), back_inserter(points), &pts::U2D); + transform(upoints.begin(), upoints.begin() + adapt.size(), back_inserter(points), &pts::U2D); } void Decode(DecodeFunT fn, DeltasT const & deltas, int64_t base, OutPointsT & points, size_t reserveF) @@ -183,10 +188,10 @@ namespace serial } } - void DecodeTriangles(DeltasT const & deltas, + void DecodeTriangles(geo_coding::InDeltasT const & deltas, m2::PointU const & basePoint, m2::PointU const & maxPoint, - PointsT & points) + geo_coding::OutPointsT & points) { size_t const count = deltas.size(); ASSERT_GREATER ( count, 2, () ); diff --git a/indexer/geometry_serialization.hpp b/indexer/geometry_serialization.hpp index fe42a4b772..134388f0e3 100644 --- a/indexer/geometry_serialization.hpp +++ b/indexer/geometry_serialization.hpp @@ -18,8 +18,8 @@ namespace serial { - template - inline void WriteVarUintArray(vector const & v, TSink & sink) + template + inline void WriteVarUintArray(TCont const & v, TSink & sink) { for (size_t i = 0; i != v.size(); ++i) WriteVarUint(sink, v[i]); @@ -27,18 +27,21 @@ namespace serial namespace pts { m2::PointU D2U(m2::PointD const & p); } - typedef vector PointsT; - typedef vector DeltasT; - /// @name Encode and Decode function types. //@{ - typedef void (*EncodeFunT)(PointsT const &, m2::PointU const &, m2::PointU const &, DeltasT &); - typedef void (*DecodeFunT)(DeltasT const &, m2::PointU const &, m2::PointU const &, PointsT &); + typedef void (*EncodeFunT)( geo_coding::InPointsT const &, + m2::PointU const &, m2::PointU const &, + geo_coding::OutDeltasT &); + typedef void (*DecodeFunT)( geo_coding::InDeltasT const &, + m2::PointU const &, m2::PointU const &, + geo_coding::OutPointsT &); //@} + typedef buffer_vector DeltasT; + typedef buffer_vector OutPointsT; + void Encode(EncodeFunT fn, vector const & points, int64_t base, DeltasT & deltas); - typedef buffer_vector OutPointsT; /// @name Overloads for different out container types. //@{ void Decode(DecodeFunT fn, DeltasT const & deltas, int64_t base, OutPointsT & points, size_t reserveF = 1); @@ -92,7 +95,7 @@ namespace serial } - /// @name Pathes. + /// @name Paths. //@{ template void SaveInnerPath(vector const & points, int64_t base, TSink & sink) @@ -166,10 +169,10 @@ namespace serial } }; - void DecodeTriangles(DeltasT const & deltas, + void DecodeTriangles(geo_coding::InDeltasT const & deltas, m2::PointU const & basePoint, m2::PointU const & maxPoint, - PointsT & triangles); + geo_coding::OutPointsT & triangles); template void LoadOuterTriangles(TSource & src, int64_t base, OutPointsT & triangles) diff --git a/indexer/indexer_tests/geometry_coding_test.cpp b/indexer/indexer_tests/geometry_coding_test.cpp index 34317161e3..879906e0b5 100644 --- a/indexer/indexer_tests/geometry_coding_test.cpp +++ b/indexer/indexer_tests/geometry_coding_test.cpp @@ -78,22 +78,31 @@ namespace void TestPolylineEncode(string testName, vector const & points, m2::PointU const & maxPoint, - void (* fnEncode)(vector const & points, + void (* fnEncode)(geo_coding::InPointsT const & points, m2::PointU const & basePoint, m2::PointU const & maxPoint, - vector & deltas), - void (* fnDecode)(vector const & deltas, + geo_coding::OutDeltasT & deltas), + void (* fnDecode)(geo_coding::InDeltasT const & deltas, m2::PointU const & basePoint, m2::PointU const & maxPoint, - vector & points)) + geo_coding::OutPointsT & points)) { - m2::PointU const basePoint = (points.empty() ? m2::PointU(0, 0) : points[points.size() / 2]); + size_t const count = points.size(); + if (count == 0) return; + + m2::PointU const basePoint = points[count / 2]; vector deltas; - fnEncode(points, basePoint, maxPoint, deltas); + deltas.resize(count); + + geo_coding::OutDeltasT deltasA(deltas); + fnEncode(make_read_adapter(points), basePoint, maxPoint, deltasA); vector decodedPoints; - fnDecode(deltas, basePoint, maxPoint, decodedPoints); + decodedPoints.resize(count); + + geo_coding::OutPointsT decodedPointsA(decodedPoints); + fnDecode(make_read_adapter(deltas), basePoint, maxPoint, decodedPointsA); TEST_EQUAL(points, decodedPoints, ());