<commit_before>"""
This module contains all kinds of fitness functions.
"""

from bitset cimport uint128, index, size
from bitset import iterate
from utils cimport randomint


def shuffle(uint128 x):
    """Shuffle bits 0 to 99 using fisher-yates shuffle."""
    cdef int i = 99, j
    cdef uint128 t

    for i in range(99, -1 ,-1):
        j = randomint(i)

        # Swap bit positions i and j
        t = ((x >> i) ^ (x >> j)) & 1 # XOR temporary
        x = x ^ ((t << i) | (t << j))

    return x

def count_ones(uint128 x):
    return size(x)


def lin_scaled_count_ones(uint128 x):
    cdef int result = 0
    for y in iterate(x):
        result += index(y)
    return result


def td_trap(uint128 x):
    cdef int result = 0, y

    for i in range(25):
        x >>= 4
        y = x & 15

        if y != 15:
            result += 3 - size(y)
        else:
            result += 4

    return result


def tn_trap(uint128 x):
    cdef double result = 0
    cdef int y

    for i in range(25):
        x >>= 4
        y = x & 15

        if y != 15:
            result += (3 - size(y)) / 2
        else:
            result += 4

    return result
<commit_msg>Add randomly linked trap functions<commit_after>"""
This module contains all kinds of fitness functions.
"""

from bitset cimport uint128, index, size
from bitset import iterate
from utils cimport randomint


def shuffle(uint128 x):
    """Shuffle bits 0 to 99 using fisher-yates shuffle."""
    cdef int i = 99, j
    cdef uint128 t

    for i in range(99, -1 ,-1):
        j = randomint(i)

        # Swap bit positions i and j
        t = ((x >> i) ^ (x >> j)) & 1 # XOR temporary
        x = x ^ ((t << i) | (t << j))

    return x


def count_ones(uint128 x):
    return size(x)


def lin_scaled_count_ones(uint128 x):
    cdef int result = 0
    for y in iterate(x):
        result += index(y)
    return result


def td_trap(uint128 x):
    cdef int result = 0, y

    for i in range(25):
        x >>= 4
        y = x & 15

        if y != 15:
            result += 3 - size(y)
        else:
            result += 4

    return result


def tn_trap(uint128 x):
    cdef double result = 0
    cdef int y

    for i in range(25):
        x >>= 4
        y = x & 15

        if y != 15:
            result += (3 - size(y)) / 2
        else:
            result += 4

    return result


def rd_trap(uint128 x):
    x = shuffle(x)
    return td_trap(x)


def rn_trap(uint128 x):
    x = shuffle(x)
    return tn_trap(x)


{'fraction_non_alphanumeric': 0.1099959855479727, 'fraction_numerical': 0.044961862705740666, 'mean_word_length': 1.7719688542825363, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '4121892', 'n_tokens_mistral': 962, 'n_tokens_neox': 838, 'n_words': 320}