<commit_before>#include "tween.h"

namespace tween {

std::vector<std::shared_ptr<TweenImpl>> Tween::allTweens;
double Tween::currentTime = 0;
std::mutex Tween::tweenMutex;

double Tween::linear_fn(double t) {
	return t;
}

double Tween::smoothStep_fn(double t) {
	return (t*t *(3 - 2*t));
}

double Tween::easeInSine_fn (double t) {
	return 1 - cos(t * (M_PI/2));
}

double Tween::sine_fn(double t) {
	return (sin(t * (M_PI*2) - M_PI/2) + 1.0)/2.0;        
}

double Tween::easeOutSine_fn(double t) {
	return sin(t * (M_PI/2));        
}

double Tween::easeInOutSine_fn(double t) {
	return -0.5 * (cos(M_PI*t) - 1);
}

double Tween::easeInBack_fn (double t) {
	double s = 1.70158f;
	return t*t*((s+1)*t - s);
}

double Tween::easeOutBack_fn(double t) {        
	double s = 1.70158f;
	t--;
	return (t*t*((s+1)*t + s) + 1);
}

double Tween::easeInOutBack_fn(double t) {
	double s = 1.70158f * 1.525f;
	//double s2 = s * 1.525f;
	t *= 2;
	if (t < 1) return 1.0/2*(t*t*((s+1)*t - s));
	double postFix = t-=2;
	return 1.0/2*((postFix)*t*((s+1)*t + s) + 2);
}

TweenImpl::~TweenImpl() {
	if(!isTweening) {
		Tween::addTween(*this);
	}
}

Tween Tween::make() {
	return Tween(std::make_shared<TweenImpl>(currentTime, smoothStep_fn, false));
}

void Tween::addTween(const TweenImpl& ti) {
	std::lock_guard<std::mutex> guard(tweenMutex);
	allTweens.push_back(std::make_shared<TweenImpl>(ti));
	allTweens.back()->isTweening = true;
}

void Tween::cancel() {
	std::lock_guard<std::mutex> guard(tweenMutex);

	auto it = Tween::allTweens.begin();
	while(it != Tween::allTweens.end()) {
		if(*it == impl) {
			Tween::allTweens.erase(it);
			break;
		}
		++it;
	}
}

bool TweenImpl::step() {
		size_t ended = 0;
		Tween tween;
		for(auto &a : args) {
			float t = (float)((Tween::currentTime - startTime - a->delay) / totalTime);
			if(t < 0.0)
				continue;
			if(t > 1.0) {
				if(do_rep)
					t -= 1.0;
				else if(backto) {
					ended++;
					a->set(0.0, tween);
				} else {
					ended++;
					a->set(1.0, tween);
					continue;
				}
			}
			a->set(tween_func(t), tween);
		}
		return ended < args.size();
	}

int Tween::updateTweens(double t) {

	std::lock_guard<std::mutex> guard(tweenMutex);

	static std::vector<std::shared_ptr<TweenImpl>> doneTweens;

	currentTime = t;
	auto it = allTweens.begin();
	while(it != allTweens.end()) {
		if(!(*it)->step()) {
			doneTweens.push_back(*it);
			it = allTweens.erase(it);
		} else {
			it++;
		}
	}

	for(auto &dt : doneTweens) {
		Tween tw;
		dt->on_complete_cb.call(tw, 1.0);
	}
	doneTweens.clear();

	return (int)allTweens.size();
}

Tween::Tween(int dummy) : impl(std::make_shared<TweenImpl>(currentTime, smoothStep_fn)) {
}

Tween::Tween(std::shared_ptr<TweenImpl> i) : impl(i) {
}

void Tween::finish() {
	for(auto &a : impl->args) {
		a->set(1.0, *this);<commit_msg>Tween mutex fix<commit_after>#include "tween.h"

namespace tween {

std::vector<std::shared_ptr<TweenImpl>> Tween::allTweens;
double Tween::currentTime = 0;
std::mutex Tween::tweenMutex;

double Tween::linear_fn(double t) {
	return t;
}

double Tween::smoothStep_fn(double t) {
	return (t*t *(3 - 2*t));
}

double Tween::easeInSine_fn (double t) {
	return 1 - cos(t * (M_PI/2));
}

double Tween::sine_fn(double t) {
	return (sin(t * (M_PI*2) - M_PI/2) + 1.0)/2.0;        
}

double Tween::easeOutSine_fn(double t) {
	return sin(t * (M_PI/2));        
}

double Tween::easeInOutSine_fn(double t) {
	return -0.5 * (cos(M_PI*t) - 1);
}

double Tween::easeInBack_fn (double t) {
	double s = 1.70158f;
	return t*t*((s+1)*t - s);
}

double Tween::easeOutBack_fn(double t) {        
	double s = 1.70158f;
	t--;
	return (t*t*((s+1)*t + s) + 1);
}

double Tween::easeInOutBack_fn(double t) {
	double s = 1.70158f * 1.525f;
	//double s2 = s * 1.525f;
	t *= 2;
	if (t < 1) return 1.0/2*(t*t*((s+1)*t - s));
	double postFix = t-=2;
	return 1.0/2*((postFix)*t*((s+1)*t + s) + 2);
}

TweenImpl::~TweenImpl() {
	if(!isTweening) {
		Tween::addTween(*this);
	}
}

Tween Tween::make() {
	return Tween(std::make_shared<TweenImpl>(currentTime, smoothStep_fn, false));
}

void Tween::addTween(const TweenImpl& ti) {
	std::lock_guard<std::mutex> guard(tweenMutex);
	allTweens.push_back(std::make_shared<TweenImpl>(ti));
	allTweens.back()->isTweening = true;
	allTweens.back()->startTime = currentTime;
}

void Tween::cancel() {
	std::lock_guard<std::mutex> guard(tweenMutex);

	auto it = Tween::allTweens.begin();
	while(it != Tween::allTweens.end()) {
		if(*it == impl) {
			Tween::allTweens.erase(it);
			break;
		}
		++it;
	}
}

bool TweenImpl::step() {
		size_t ended = 0;
		Tween tween;
		for(auto &a : args) {
			float t = (float)((Tween::currentTime - startTime - a->delay) / totalTime);
			if(t < 0.0)
				continue;
			if(t > 1.0) {
				if(do_rep)
					t -= 1.0;
				else if(backto) {
					ended++;
					a->set(0.0, tween);
				} else {
					ended++;
					a->set(1.0, tween);
					continue;
				}
			}
			a->set(tween_func(t), tween);
		}
		return ended < args.size();
	}

int Tween::updateTweens(double t) {


	static std::vector<std::shared_ptr<TweenImpl>> doneTweens;

	{
		std::lock_guard<std::mutex> guard(tweenMutex);
		currentTime = t;
		auto it = allTweens.begin();
		while(it != allTweens.end()) {
			if(!(*it)->step()) {
				doneTweens.push_back(*it);
				it = allTweens.erase(it);
			} else {
				it++;
			}
		}
	}

	for(auto &dt : doneTweens) {
		Tween tw;
		dt->on_complete_cb.call(tw, 1.0);
	}
	doneTweens.clear();

	return (int)allTweens.size();
}

Tween::Tween(int dummy) : impl(std::make_shared<TweenImpl>(currentTime, smoothStep_fn)) {
}

Tween::Tween(std::shared_ptr<TweenImpl> i) : impl(i) {
}

void Tween::finish() {
	for(auto &a : impl->args) {
		a->set(1.0, *this);
{'fraction_non_alphanumeric': 0.2, 'fraction_numerical': 0.025329815303430078, 'mean_word_length': 3.794266441821248, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 46, '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': '5168169', 'n_tokens_mistral': 2763, 'n_tokens_neox': 2527, 'n_words': 433}