based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

commit 55a31868b5138b027c944454c5aa1d941ba06d55
parent a6da8343a8e66450b18f7f88d7890f829c29a17a
author Dimitrije Dobrota < mail@dimitrijedobrota.com >
date Wed, 19 Mar 2025 22:19:45 +0100

min and max function, should revisit...

Diffstat:
M example/empty_example.cpp | +++++++
A include/based/algorithm.hpp | ++++++++++++++++++++++++++++++++++++

2 files changed, 43 insertions(+), 0 deletions(-)


diff --git a/ example/empty_example.cpp b/ example/empty_example.cpp

@@ -1,5 +1,7 @@

#include <algorithm>
#include <iostream>

#include "based/algorithm.hpp"
#include "based/instrumentation.hpp"

int main()

@@ -24,5 +26,10 @@ int main()

based::normalize_nlogn);
}

const int a = based::max(3, 4);
const int b = based::min(3, 4);

std::cout << a << ' ' << b << '\n';

return 0;
}

diff --git a/ include/based/algorithm.hpp b/ include/based/algorithm.hpp

@@ -0,0 +1,36 @@

#pragma once

#include <functional>

namespace based
{

// need to deal with returned reference to temporary object...

// returns min element, first if equal
template<typename T, typename Cmp>
const T& min(const T& lhs, const T& rhs, Cmp cmp)
{
return cmp(rhs, lhs) ? rhs : lhs;
}

template<typename T>
const T& min(const T& lhs, const T& rhs)
{
return based::min(lhs, rhs, std::less());
}

// returns max element, second if equal
template<typename T, typename Cmp>
const T& max(const T& lhs, const T& rhs, Cmp cmp)
{
return cmp(rhs, lhs) ? lhs : rhs;
}

template<typename T>
const T& max(const T& lhs, const T& rhs)
{
return based::max(lhs, rhs, std::less());
}

} // namespace based