<commit_before>#!/usr/bin/env python
# coding=utf-8

"""
pattern base class.

    generates a test pattern.

    history:
        see git commits

    todo:
        ~ all fine :-)
"""

# https://docs.python.org/2.7/howto/pyporting.html#division
from __future__ import division

import collections
import array
import struct

import configdict

##########################################
# globals


##########################################
# functions

def map(value, in_low, in_high, out_low, out_high):
    """
    Map value from on range to another.

    ((value - in_low) * (out_high - out_low)) / (in_high - in_low) + out_low
    """
    # example from /animation_nodes/nodes/number/map_range.py
    # if inMin == inMax:
    #     newValue = 0
    # # with clamping
    #     if inMin < inMax:
    #         _value = min(max(value, inMin), inMax)
    #     else:
    #         _value = min(max(value, inMax), inMin)
    #     with interpolation
    #         newValue = outMin + interpolation(
    #             (_value - inMin) / (inMax - inMin)
    #         ) * (outMax - outMin)
    #     without interpolation
    #         newValue = outMin + (
    #             (_value - inMin) / (inMax - inMin)
    #         ) * (outMax - outMin)
    # # without clamping
    #     newValue = outMin + (
    #         (value - inMin) / (inMax - inMin)
    #     ) * (outMax - outMin)

    result = None

    # based on http://arduino.cc/en/Reference/Map
    result = ((value - in_low) * (out_high - out_low)) / \
        (in_high - in_low) + out_low

    # http://stackoverflow.com/a/5650012/574981
    # result = out_low + \
    #     ((out_high - out_low) * (value - in_low)) / \
    #     (in_high - in_low)

    return result


def map_bound(value, in_low, in_high, out_low, out_high):
    """Map value with high and low bound handling."""
    result = None

    if value <= in_low:
        result = out_low
    else:
        if value >= in_high:
            result = out_high
        else:
            # http://stackoverflow.com/a/5650012/574981
            result = out_low + (
                (out_high - out_low) * (value - in_low) / (in_high - in_low)
            )
    return result


def map_01_to_8bit(value):
    """Map value from 0-1 range to 0-255 range."""
    result = None
    result = int(map_bound(value, 0.0, 1.0, 0, 255))
    return result


def map_01_to_16bit(value):
    """Map value from 0-1 range to 0-65535 range."""
    # result = None
    # result = int(map_bound(value, 0.0, 1.0, 0, 65535))
    # return result
    # return int(map_bound(value, 0.0, 1.0, 0, 65535))
    # result = None
    if value <= 0:
        # result = 0
        return 0
    else:
        if value >= 1:
            # result = 65535
            return 65535
        else:
            # simplified
            # result = 65535 * value / 1
            return int(65535 * value)
    # return result


def map_16bit_to_01(value):
    """Map value from 0-65535 range to 0-1 range."""
    result = None
    result = map_bound(value, 0, 65535, 0.0, 1.0)
    return result


def calculate_16bit_parts(value):
    """Calculate the low and high part representations of value."""
    if not (0 <= value < 65535):
        value = min(max(value, 0), 65535)
    # high_byte = value // 256
    # low_byte = value % 256
    # return high_byte, low_byte
    # faster:<commit_msg>optimized map_01_to_16bit to the possible minimum ;-)<commit_after>#!/usr/bin/env python
# coding=utf-8

"""
pattern base class.

    generates a test pattern.

    history:
        see git commits

    todo:
        ~ all fine :-)
"""

# https://docs.python.org/2.7/howto/pyporting.html#division
from __future__ import division

import collections
import array
import struct

import configdict

##########################################
# globals


##########################################
# functions

def map(value, in_low, in_high, out_low, out_high):
    """
    Map value from on range to another.

    ((value - in_low) * (out_high - out_low)) / (in_high - in_low) + out_low
    """
    # example from /animation_nodes/nodes/number/map_range.py
    # if inMin == inMax:
    #     newValue = 0
    # # with clamping
    #     if inMin < inMax:
    #         _value = min(max(value, inMin), inMax)
    #     else:
    #         _value = min(max(value, inMax), inMin)
    #     with interpolation
    #         newValue = outMin + interpolation(
    #             (_value - inMin) / (inMax - inMin)
    #         ) * (outMax - outMin)
    #     without interpolation
    #         newValue = outMin + (
    #             (_value - inMin) / (inMax - inMin)
    #         ) * (outMax - outMin)
    # # without clamping
    #     newValue = outMin + (
    #         (value - inMin) / (inMax - inMin)
    #     ) * (outMax - outMin)

    result = None

    # based on http://arduino.cc/en/Reference/Map
    result = ((value - in_low) * (out_high - out_low)) / \
        (in_high - in_low) + out_low

    # http://stackoverflow.com/a/5650012/574981
    # result = out_low + \
    #     ((out_high - out_low) * (value - in_low)) / \
    #     (in_high - in_low)

    return result


def map_bound(value, in_low, in_high, out_low, out_high):
    """Map value with high and low bound handling."""
    result = None

    if value <= in_low:
        result = out_low
    else:
        if value >= in_high:
            result = out_high
        else:
            # http://stackoverflow.com/a/5650012/574981
            result = out_low + (
                (out_high - out_low) * (value - in_low) / (in_high - in_low)
            )
    return result


def map_01_to_8bit(value):
    """Map value from 0-1 range to 0-255 range."""
    result = None
    result = int(map_bound(value, 0.0, 1.0, 0, 255))
    return result


def map_01_to_16bit(value):
    """Map value from 0-1 range to 0-65535 range."""
    # result = None
    # result = int(map_bound(value, 0.0, 1.0, 0, 65535))
    # return result
    # return int(map_bound(value, 0.0, 1.0, 0, 65535))
    # result = None
    # if value <= 0:
    #     # result = 0
    #     return 0
    # else:
    #     if value >= 1:
    #         # result = 65535
    #         return 65535
    #     else:
    #         # simplified
    #         # result = 65535 * value / 1
    #         return int(65535 * value)
    # return result
    return int(65535 * value)


def map_16bit_to_01(value):
    """Map value from 0-65535 range to 0-1 range."""
    result = None
    result = map_bound(value, 0, 65535, 0.0, 1.0)
    return result


def calculate_16bit_parts(value):
    """Calculate the low and high part representations of value."""
    if not (0 <= value < 65535):
        value = min(max(value, 0), 65535)
    # high_byte = value // 256
    # low_byte = value % 256
    # return high_byte, low_byte
    # faster:
{'fraction_non_alphanumeric': 0.14497214892993257, 'fraction_numerical': 0.04412195836997948, 'mean_word_length': 1.967812092214006, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '467919', 'n_tokens_mistral': 2509, 'n_tokens_neox': 2247, 'n_words': 592}