-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
54 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#include "./chunk.h" | ||
#include <compat.hpp> | ||
#include <functional> | ||
|
||
namespace mcmap { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
#include <map> | ||
|
||
/* Loosely inspired from lower_bound in | ||
* /usr/include/c++/11.1.0/bits/stl_algobase.h | ||
* Returns a const_iterator to the highest integer that is inferior or equal to | ||
* `version` from the keys of a map<int, F> */ | ||
template <typename F> | ||
typename std::map<int, F>::const_iterator | ||
compatible(const std::map<int, F> &hash, int version) { | ||
typedef typename std::map<int, F>::const_iterator _Iterator; | ||
typedef typename _Iterator::difference_type _DistanceType; | ||
|
||
_Iterator __first = hash.begin(), __end = hash.end(); | ||
_DistanceType __len = std::distance(__first, __end); | ||
|
||
while (__len > 1) { | ||
_DistanceType __half = __len >> 1; | ||
_Iterator __middle = __first; | ||
std::advance(__middle, __half); | ||
|
||
if (__middle->first > version) | ||
__end = __middle; | ||
else | ||
__first = __middle; | ||
|
||
__len = __len - __half; | ||
} | ||
|
||
return __first; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#include "./section.h" | ||
#include <compat.hpp> | ||
#include <functional> | ||
|
||
namespace versions { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <cmath> | ||
#include <compat.hpp> | ||
#include <gtest/gtest.h> | ||
|
||
TEST(TestCompat, TestReturn) { | ||
std::map<int, int> versions = { | ||
{0, 0}, {1, 0}, {2, 0}, {4, 0}, {8, 0}, {16, 0}, | ||
}; | ||
|
||
std::map<int, int>::const_iterator it; | ||
|
||
it = compatible(versions, 0); | ||
ASSERT_NE(it, versions.end()); | ||
ASSERT_EQ(it->first, 0); | ||
|
||
for (int i = 1; i < 20; i++) { | ||
it = compatible(versions, i); | ||
ASSERT_NE(it, versions.end()); | ||
ASSERT_EQ(it->first, 1 << int(std::log2(double(i)))); | ||
} | ||
} |