Giter Club home page Giter Club logo

coding's Introduction

coding

coding tips

clamp
int clamp(int x, int low, int high) {
  assert(low <= high);
  return std::min(std::max(x, low), high);
}
wrap around
if (x >= 10) {
  x = 0;
}
x = x % 10


if (x >= 10) {
  x = 0;
} else if (x < 0) {
  x = 9;
}
int wrap(int x, int low, int high) {
  assert(low < high);
  const int n = (x - low) % (high - low);
  return (n >= 0) ? (n + low) : (n + high);
}
Range class
template<typename T>
class Range {
public:
  Range(T min, T max) : min(_min), max(_max) {
    assert(min_ <= max_);
  }
  
  bool isInside(T value) const {
    return (min_ <= value) && (value <= max_);
  }
  
  T clamp(T value) const {
    return ::clamp(value, min_, max_);
  }
  
  T wrap(T value) const {
    return ::wrap(value, min_, max_);
  }
  
  T min() const {
    return min_;
  }
  
  T max() const {
    return max_;
  }
  
private:
  T min_;
  T max_;
}
Tuple for-each
template<size_t Index, typename Tuple, typename Functor>
auto tuple_at(const Tuple& tpl, const Functor& func) -> void
{
	const auto& v = std::get<Index>(tpl);
	func( v );
}

template<typename Tuple, typename Functor, size_t Index = 0>
auto tuple_for_each(const Tuple& tpl, const Functor& f) -> void
{
	constexpr auto tuple_size = std::tuple_size_v<Tuple>;
	if constexpr(Index < tuple_size) {
		tuple_at<Index>(tpl, f);
		tuple_for_each<Tuple, Functor, Index+i>(tpl, f);
	}
}

// example
auto tpl = std::make_tuple(1, true, std::string{"Jedi"});
tuple_for_earch(tpl, [](const auto& v) { std::cout << v << " "; });
Tuple any_of
template<typename Tuple, typename Functor, size_t Index = 0>
auto tuple_any_of(const Tuple& tp1, const Functor& f) -> bool
{
    constexpr auto tuple_size = std::tuple_size_v<Tuple>;
    if constexpr(Index < tuple_size)
    {
        bool success = f(std::get<Index>(tp1));
        return success ? true : tuple_any_of<Tuple, Functor, Index+1>(tp1, f);
    }
    else
    {
        return false;
    }
}

coding's People

Contributors

pfepark avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.