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

import random, math


class TSCircularBuffer():


    def __init__(self, size):
        self._data = dict()
        self._cum_prob = list()
        
        self._size = size
        self._cursor = 0
        self._rolled_over_once = False
        self._zero = 0
    
    
    def insert(self, item, log_prob):
        if self._rolled_over_once:
            self._zero = self._cum_prob[self._cursor]
        
        self._data[self._cursor] = item
        
        if self._rolled_over_once:
            self._cum_prob[self._cursor] = self._cum_prob[self._cursor-1] + math.exp(log_prob)
        elif self._cursor != 0:
            self._cum_prob.append(self._cum_prob[self._cursor-1] + math.exp(log_prob))
        else:
            self._cum_prob.append(math.exp(log_prob))
        
        self._cursor += 1
        if self._cursor == self._size:
            self._rolled_over_once = True
            self._cursor = 0
    
    
    def rand(self):
        if not self._rolled_over_once and self._cursor == 0:
            return None
        
        threshold = random.random() * (self._cum_prob[self._cursor - 1] - self._zero) + self._zero
        
        if self._rolled_over_once:<commit_msg>Fix in TSCircularBuffer to prevent math range error.<commit_after>#!/usr/bin/env python
#coding: utf-8

import random, math


class TSCircularBuffer():


    def __init__(self, size):
        self._data = dict()
        self._cum_prob = list()
        
        self._size = size
        self._cursor = 0
        self._rolled_over_once = False
        self._prob_zero = 0
        self._exp_zero = 0
    
    
    def insert(self, item, log_prob):
        if self._rolled_over_once:
            self._prob_zero = self._cum_prob[self._cursor]
        
        self._data[self._cursor] = item
        
        if self._rolled_over_once:
            self._cum_prob[self._cursor] = self._cum_prob[self._cursor-1] + math.exp(log_prob - self._exp_zero)
        elif self._cursor != 0:
            self._cum_prob.append(self._cum_prob[self._cursor-1] + math.exp(log_prob))
        else:
            self._cum_prob.append(math.exp(log_prob))
        
        self._cursor += 1
        if self._cursor == self._size:
            self._rolled_over_once = True
            self._cursor = 0
            self._adjustSums()
    
    
    def _adjustSums(self):
        ratio = self._cum_prob[-1] - self._cum_prob[-2]
        self._exp_zero = math.log(ratio)
        
        for index in xrange(self._size):
            self._cum_prob[index] /= ratio
        self._prob_zero /= ratio
    
    
    def rand(self):
        if not self._rolled_over_once and self._cursor == 0:
            return None
        
        threshold = random.random() * (self._cum_prob[self._cursor - 1] - self._prob_zero) + self._prob_zero
        
        if self._rolled_over_once:
{'fraction_non_alphanumeric': 0.10435383552176918, 'fraction_numerical': 0.007947477539737388, 'mean_word_length': 1.816147859922179, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6482169', 'n_tokens_mistral': 941, 'n_tokens_neox': 896, 'n_words': 171}