-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sample_rgb impl based on a "min_stack" idea.
Will it perform? Who can say
- Loading branch information
Showing
6 changed files
with
177 additions
and
2 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
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 |
---|---|---|
|
@@ -8,3 +8,7 @@ set(SOURCES | |
|
||
add_library(util INTERFACE) | ||
|
||
if(NOT DEFINED DISABLE_TESTS) | ||
add_subdirectory(test) | ||
endif() | ||
|
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,63 @@ | ||
#pragma once | ||
|
||
#include <array> | ||
#include <cstdint> | ||
|
||
template <typename Integer=uint16_t> | ||
class sampling_min_stack | ||
{ | ||
public: | ||
sampling_min_stack() | ||
: _vals{255, 255, 255, 255} | ||
, _minSum(255*4) | ||
, _totalSum(-_minSum) | ||
{} | ||
|
||
Integer min_sum() const | ||
{ | ||
return _minSum; | ||
} | ||
|
||
Integer avg() const | ||
{ | ||
return _minSum >> 2; // /4, the length of _vals | ||
} | ||
|
||
Integer total() const | ||
{ | ||
return _totalSum; | ||
} | ||
|
||
// replace max in _vals with val. Returns previous max value. | ||
// unconditional -- assumes val < avg(), or none of this makes sense | ||
Integer push_pop(Integer replacement) | ||
{ | ||
// get max idx | ||
int8_t il = _vals[0] > _vals[1]? 0 : 1; | ||
int8_t ir = _vals[2] > _vals[3]? 2 : 3; | ||
int8_t i = _vals[il] > _vals[ir]? il : ir; | ||
|
||
Integer res = _vals[i]; | ||
_vals[i] = replacement; | ||
_minSum += replacement - res; | ||
return res; | ||
} | ||
|
||
void add(Integer prospect) | ||
{ | ||
if (prospect < avg()) | ||
prospect = push_pop(prospect); | ||
_totalSum += prospect; | ||
} | ||
|
||
sampling_min_stack& operator+=(Integer prospect) | ||
{ | ||
add(prospect); | ||
return *this; | ||
} | ||
|
||
protected: | ||
std::array<Integer, 4> _vals; | ||
Integer _minSum; | ||
Integer _totalSum; | ||
}; |
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,22 @@ | ||
cmake_minimum_required(VERSION 2.6) | ||
|
||
project(util_test) | ||
|
||
set (SOURCES | ||
test.cpp | ||
sampling_min_stackTest.cpp | ||
) | ||
|
||
include_directories( | ||
${libcimbar_SOURCE_DIR}/test | ||
${libcimbar_SOURCE_DIR}/test/lib | ||
${CMAKE_CURRENT_SOURCE_DIR}/.. | ||
) | ||
|
||
add_executable ( | ||
util_test | ||
${SOURCES} | ||
) | ||
|
||
add_test(util_test util_test) | ||
|
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,29 @@ | ||
/* This code is subject to the terms of the Mozilla Public License, v.2.0. http://mozilla.org/MPL/2.0/. */ | ||
#include "unittest.h" | ||
|
||
#include "sampling_min_stack.h" | ||
|
||
TEST_CASE( "sample_min_stackTest/testDefault", "[unit]" ) | ||
{ | ||
sampling_min_stack<uint16_t> blue; | ||
assertEquals(255, blue.avg()); | ||
|
||
blue += 200; | ||
blue += 200; | ||
blue += 200; | ||
blue += 200; | ||
assertEquals(200, blue.avg()); | ||
assertEquals(0, blue.total()); | ||
|
||
blue += 100; | ||
blue += 250; | ||
blue += 100; | ||
blue += 250; | ||
assertEquals(900, blue.total()); | ||
|
||
blue += 220; | ||
assertEquals(1120, blue.total()); | ||
|
||
blue += 60; | ||
assertEquals(1320, blue.total()); | ||
} |
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,4 @@ | ||
/* This code is subject to the terms of the Mozilla Public License, v.2.0. http://mozilla.org/MPL/2.0/. */ | ||
#define CATCH_CONFIG_MAIN | ||
#include "catch.hpp" | ||
|