<commit_before>#pragma once

#include <functional>
#include <map>
#include <algorithm>
#include <memory>

#include "debug.hpp"

namespace utki{

template<typename T_A, typename T_B> std::pair<T_B, T_A> flip_pair(const std::pair<T_A, T_B> &p){
    return std::pair<T_B, T_A>(p.second, p.first);
}

template<typename A, typename B> std::map<B, A> flip_map(const std::map<A, B> &m){
    std::map<B, A> ret;
    std::transform(m.begin(), m.end(), std::inserter(ret, ret.begin()), flip_pair<A, B>);
    return ret;
}

/**
 * @brief Execute a function on scope exit.
 * This class can be used to perform some function when the object of this class
 * is destroyed due to leave of the scope where it was created.
 * 
 * Example:
 * @code
 * {
 *     utki::scope_exit some_scope_exit([](){
 *         std::cout << "Leaving scope" << std::endl;
 *     });
 * }
 * @endcode
 */
class scope_exit{
	std::function<void()> f;

public:
	scope_exit(const scope_exit&) = delete;
	scope_exit& operator=(const scope_exit&) = delete;
	
	/**
	 * @brief Constructor.
	 * @param f - function to call on object destruction.
	 */
	scope_exit(decltype(f)&& f) :
			f(std::move(f))
	{}
	
    ~scope_exit()noexcept{
		if(this->f){
			this->f();
		}
	}
	
	/**
	 * @brief Disarm scope exit object.
	 * This function disarms the scope_exit object, so that it will not do any action on destruction.
	 * @return the previous function which had to be executed on object's destruction.
	 */
	decltype(f) reset()noexcept{
		auto ret = std::move(this->f);
		this->f = nullptr;
		return ret;
	}
};

/**
 * @brief Get top-clamped value.
 * @param v - value to get clamped for.
 * @param top - top to clamp to.
 * @return clamped value.
 */
//TODO: deprecated, use std::min/max. Remove.
template <class T> inline T clampedTop(T v, const T top)noexcept{
	if(v > top){
		return top;
	}
	return v;
}

/**
 * @brief Clamp value top.
 * This inline template function can be used to clamp the top of the value.
 * Example:
 * @code
 * int a = 30;
 *
 * //Clamp to 40. Value of 'a' remains unchanged,
 * //since it is already less than 40.
 * utki::clampTop(a, 40);
 * std::cout << a << std::endl;
 *
 * //Clamp to 27. Value of 'a' is changed to 27,
 * //since it is 30 which is greater than 27.
 * utki::clampTop(a, 27);
 * std::cout << a << std::endl;
 * @endcode
 * As a result, this will print:
 * @code
 * 30
 * 27
 * @endcode
 * @param v - reference to the value which top is to be clamped.
 * @param top - value to clamp the top to.
 */
//TODO: deprecated, use std::min/max. Remove.
template <class T> inline void clampTop(T& v, const T top)noexcept{
	v = clampedTop(v, top);
}

/**
 * @brief Get bottom-clamped value.
 * @param v - value to get clamped for.
 * @param bottom - bottom to clamp to.
 * @return clamped value.
 */
//TODO: deprecated, use std::min/max. Remove.
template <class T> inline T clampedBottom(T v, const T bottom)noexcept{
	if(v < bottom){
		return bottom;
	}
	return v;
}

/**
 * @brief Clamp value bottom.
 * Usage is analogous to utki::clampTop.
 * @param v - reference to the value which bottom is to be clamped.
 * @param bottom - value to clamp the bottom to.
 */
//TODO: deprecated, use std::min/max. Remove.
template <class T> inline void clampBottom(T& v, const T bottom)noexcept{
	v = clampedBottom(v, bottom);
}

/**
 * @brief Get range-clamped value.
 * @param v - value to get clamped for.
 * @param bottom - value to clamp the bottom to.
 * @param top - value to clamp the top to.
 * @return clamped value.
 */
//TODO: deprecated, use std::min/max. Remove.
template <class T> inline T clampedRange(T v, const T bottom, const T top)noexcept{
	if(v < bottom){
		return bottom;
	}
	if(v > top){
		return top;
	}
	return v;
}

/**
 * @brief Clamp value to range.
 * Clamps given value to given range.
 * @param v - reference to the value which is to be clamped.
 * @param bottom - value to clamp the bottom to.
 * @param top - value to clamp the top to.
 */
//TODO: deprecated, use std::min/max. Remove.
template <class T> inline void clampRange(T& v, const T bottom, const T top)noexcept{
	v = clampedRange(v, bottom, top);
}

/**
 * @brief serialize 16 bit value, little-endian.
 * Serialize 16 bit value, less significant byte first.
 * @param value - the value.
 * @param out_buf - pointer to the 2 byte buffer where the result will be placed.
 * @return pointer to the next byte after serialized value.
 */
inline std::uint8_t* serialize16le(std::uint16_t value, std::uint8_t* out_buf)noexcept{
	*out_buf = value & 0xff;
	++out_buf;
	*out_buf = value >> 8;
	++out_buf;
	return out_buf;
}

/**
 * @brief serialize 32 bit value, little-endian.
 * Serialize 32 bit value, less significant byte first.
 * @param value - the value.
 * @param out_buf - pointer to the 4 byte buffer where the result will be placed.
 * @return pointer to the next byte after serialized value.
 */
inline std::uint8_t* serialize32le(std::uint32_t value, std::uint8_t* out_buf)noexcept{
	*out_buf = std::uint8_t(value & 0xff);
	++out_buf;
	value >>= 8;<commit_msg>remove deprecated stuff<commit_after>#pragma once

#include <functional>
#include <map>
#include <algorithm>
#include <memory>

#include "debug.hpp"

namespace utki{

template<typename T_A, typename T_B> std::pair<T_B, T_A> flip_pair(const std::pair<T_A, T_B> &p){
    return std::pair<T_B, T_A>(p.second, p.first);
}

template<typename A, typename B> std::map<B, A> flip_map(const std::map<A, B> &m){
    std::map<B, A> ret;
    std::transform(m.begin(), m.end(), std::inserter(ret, ret.begin()), flip_pair<A, B>);
    return ret;
}

/**
 * @brief Execute a function on scope exit.
 * This class can be used to perform some function when the object of this class
 * is destroyed due to leave of the scope where it was created.
 * 
 * Example:
 * @code
 * {
 *     utki::scope_exit some_scope_exit([](){
 *         std::cout << "Leaving scope" << std::endl;
 *     });
 * }
 * @endcode
 */
class scope_exit{
	std::function<void()> f;

public:
	scope_exit(const scope_exit&) = delete;
	scope_exit& operator=(const scope_exit&) = delete;
	
	/**
	 * @brief Constructor.
	 * @param f - function to call on object destruction.
	 */
	scope_exit(decltype(f)&& f) :
			f(std::move(f))
	{}
	
    ~scope_exit()noexcept{
		if(this->f){
			this->f();
		}
	}
	
	/**
	 * @brief Disarm scope exit object.
	 * This function disarms the scope_exit object, so that it will not do any action on destruction.
	 * @return the previous function which had to be executed on object's destruction.
	 */
	decltype(f) reset()noexcept{
		auto ret = std::move(this->f);
		this->f = nullptr;
		return ret;
	}
};

/**
 * @brief serialize 16 bit value, little-endian.
 * Serialize 16 bit value, less significant byte first.
 * @param value - the value.
 * @param out_buf - pointer to the 2 byte buffer where the result will be placed.
 * @return pointer to the next byte after serialized value.
 */
inline std::uint8_t* serialize16le(std::uint16_t value, std::uint8_t* out_buf)noexcept{
	*out_buf = value & 0xff;
	++out_buf;
	*out_buf = value >> 8;
	++out_buf;
	return out_buf;
}

/**
 * @brief serialize 32 bit value, little-endian.
 * Serialize 32 bit value, less significant byte first.
 * @param value - the value.
 * @param out_buf - pointer to the 4 byte buffer where the result will be placed.
 * @return pointer to the next byte after serialized value.
 */
inline std::uint8_t* serialize32le(std::uint32_t value, std::uint8_t* out_buf)noexcept{
	*out_buf = std::uint8_t(value & 0xff);
	++out_buf;
	value >>= 8;
{'fraction_non_alphanumeric': 0.1390587609678277, 'fraction_numerical': 0.010103695825578303, 'mean_word_length': 3.5621588841722254, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '64965', 'n_tokens_mistral': 2718, 'n_tokens_neox': 2580, 'n_words': 908}