<commit_before>//
// Copyright (c) 2016 <NAME>
// All rights reserved.
// Copyrights licensed under the MIT License.
// See the accompanying LICENSE.txt file for terms
//

#include "graph.h"

#include <vector>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <algorithm>
#include <numeric>
#include <base/compiler_support.h>
#include <base/contract.h>
#include <base/scope_guard.h>
#include <base/compiler_abi.h>
#include <iomanip>
#include <fstream>
#include <sstream>

#include "registry.h"

////////////////////////////////////////

namespace engine
{

////////////////////////////////////////

std::ostream &
operator<<( std::ostream &os, const dimensions &d )
{
	os << "[ (" << d.x1 << ',' << d.y1 << " - " << d.x2 << ',' << d.y2 << ") " << (d.x2 - d.x1 + 1) << 'x' << (d.y2 - d.y1 + 1) << 'x' << d.planes << 'x' << d.images << 'x' << d.bytes_per_item << " ]";
	return os;
}

////////////////////////////////////////
graph::graph( const registry &r )
	: _ops( r ), _computing( 0 )
{
}

////////////////////////////////////////

graph::~graph( void )
{
}

////////////////////////////////////////

node_id
graph::add_node( const base::cstring &opname, const dimensions &d, std::initializer_list<node_id> inputs )
{
	return add_node( _ops.find( opname ), any(), d, inputs );
}

////////////////////////////////////////

void
graph::tag_rvalue( node_id n )
{
	precondition( n < _nodes.size(), "invalid node id {0}", n );
	_nodes[n].set_rvalue();
}

////////////////////////////////////////

const any &
graph::get_value( node_id n )
{
	precondition( n < _nodes.size(), "invalid node id {0}", n );
	++_computing;
	on_scope_exit{ --_computing; };

	any &v = _nodes[n].value();
	if ( ! v.has_value() )
		return process( n );

	return v;
}

////////////////////////////////////////

node_id
graph::copy_node( const graph &o, node_id n )
{
	precondition( n < o.size(), "invalid node id {0}", n );
	if ( &o == this )
		return n;

	std::vector<node_id> tocopy;
	std::map<node_id, node_id> nodemap;
	tocopy.push_back( n );
	std::vector<node_id> inputs;
<commit_msg>use less global interlock increment / decrement<commit_after>//
// Copyright (c) 2016 <NAME>
// All rights reserved.
// Copyrights licensed under the MIT License.
// See the accompanying LICENSE.txt file for terms
//

#include "graph.h"

#include <vector>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <algorithm>
#include <numeric>
#include <base/compiler_support.h>
#include <base/contract.h>
#include <base/scope_guard.h>
#include <base/compiler_abi.h>
#include <iomanip>
#include <fstream>
#include <sstream>

#include "registry.h"

////////////////////////////////////////

namespace engine
{

////////////////////////////////////////

std::ostream &
operator<<( std::ostream &os, const dimensions &d )
{
	os << "[ (" << d.x1 << ',' << d.y1 << " - " << d.x2 << ',' << d.y2 << ") " << (d.x2 - d.x1 + 1) << 'x' << (d.y2 - d.y1 + 1) << 'x' << d.planes << 'x' << d.images << 'x' << d.bytes_per_item << " ]";
	return os;
}

////////////////////////////////////////
graph::graph( const registry &r )
	: _ops( r ), _computing( 0 )
{
}

////////////////////////////////////////

graph::~graph( void )
{
}

////////////////////////////////////////

node_id
graph::add_node( const base::cstring &opname, const dimensions &d, std::initializer_list<node_id> inputs )
{
	return add_node( _ops.find( opname ), any(), d, inputs );
}

////////////////////////////////////////

void
graph::tag_rvalue( node_id n )
{
	precondition( n < _nodes.size(), "invalid node id {0}", n );
	_nodes[n].set_rvalue();
}

////////////////////////////////////////

const any &
graph::get_value( node_id n )
{
	precondition( n < _nodes.size(), "invalid node id {0}", n );
	_computing.fetch_add( 1, std::memory_order_relaxed );
	on_scope_exit{ _computing.fetch_sub( 1, std::memory_order_relaxed ); };

	any &v = _nodes[n].value();
	if ( ! v.has_value() )
		return process( n );

	return v;
}

////////////////////////////////////////

node_id
graph::copy_node( const graph &o, node_id n )
{
	precondition( n < o.size(), "invalid node id {0}", n );
	if ( &o == this )
		return n;

	std::vector<node_id> tocopy;
	std::map<node_id, node_id> nodemap;
	tocopy.push_back( n );
	std::vector<node_id> inputs;

{'fraction_non_alphanumeric': 0.3133177570093458, 'fraction_numerical': 0.008878504672897197, 'mean_word_length': 2.1157205240174672, 'pattern_counts': {'":': 0, '<': 127, '<?xml version=': 0, '>': 41, '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': '314937', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1460, 'n_words': 363}