<commit_before>

var Node = function(f, handle){
	//ids of children
	this.NODE ={
			base: {type:'unknown', code:'?'},
			children: {
				OP: {
					base: {type:'op', code:'?', type:'op', op:'unknown'},
					children: {
						MULT: {base:{op:'mult', code:'*'}, children:{}}, //
						DIV: {base:{op:'div', code:'/'}, children:{}}, //top -> bottom, numerator to denominator
						ADD: {base:{op:'plus', code:'+'}, children:{}},
						SUB: {base:{op:'sub', code:'-'}, children:{}},
						EXPO: {base:{op:'power', code:'^'}, children:{}},
						EQ: {base:{op:'eq', code:'='}, children:{}},
						PAREN: {base:{op:'paren',code:'()', children:{}}}
					}
				},
				VAR: {base:{type:'variable', name:null},children:{}},
				NUMBER: {base:{type:'number', value:null},children:{}}
			}
	}
	this.foreach_child = function(cbk){
		var f = this.formula;
		for(var i=0; i < this.children.length; i++){
			cbk(f.get(this.children[i],i));
		}
	}
	this.add_child = function(child_id, tofront){
		if(isUndefined(tofront) || ! tofront){
			if(this.children.indexOf(child_id) < 0)
				this.children.push(child_id);
		}
		else {
			if(this.children.indexOf(child_id) < 0)
				this.children.unshift(child_id);
		}
	}
	this.init = function(f,handle){
		var that = this;
		var cobble_data = function(data,name){
			var node = {}; //copy base info.
			for(var b in data.base){
				node[b] = copyData(data.base[b]);
			}
			if(name == handle){
				return {found:true, data:node};
			}
			else{
				for(child in data.children){
					var res = cobble_data(data.children[child], child);
					//copy returned structure.
					if(res.found){
						for(var k in node){
							if(!(k in res.data))
								res.data[k] = node[k];
						}
						return {found:true, data:res.data}; //exists in one of our children.
					}
				}
			}
			//did not find this.

			return {found:false, data:{}};
		}
		var data = cobble_data(this.NODE,'NODE').data;
		this.HANDLE = handle;
		this.id = f.fresh_id();
		this.parent_id = -1;
		this.children = [];
		this.formula = f;
		this._data = data;
		return data;
	}
	this.replace = function(new_id){
		var f = this.formula;
		var par = this.parent();
		if(par != null){
			delete par[this.id];
			par[new_id] = null;
			f.get(new_id).parent_id = this.parent_id;
			this.parent_id = -1;
		}
		else{
			newnode.parent_id = -1;
		}
	}
	this.childAt = function(i){
		return this.children[i];
	}
	this.remove = function(child_to_moveup){
		this.replace(child_to_moveup);
	}
	this.data = function(key){
		return this._data[key];
	}
	this.set = function(key, value){
		this._data[key] = value;
	}
	this.set_parent = function(id,tofront){
		var f = this.formula;
		this.parent_id = id;
		if(f.has(id)){
			f.get(id).add_child(this.id,tofront);
		}
	}
	this.parent = function(){
		var f = this.formula;
		return get(this.parent_id);
	}
	this.child = function(i){
		var f = this.formula;
		return f.get(this.children[i]);
	}
	this.print = function(){
		var f = this.formula;
		var str = "";
		var type = this.data('type');
		if(type == 'op'){
			for(var i in this.children){
				var chl = f.get(this.children[i]);
				str += (this.data('code'));
				str += ("(");
				str += chl.print(f);
				str += (")");
			}
		}
		else if(type == 'variable'){
			str = this.data('code');
		}
		else if(type == 'number'){
			str = (this.data('code'));
		}
		return str;
	}
	/*
	Given a formula, find all the ancestors.
	*/
	this.ancestors = function(){
		var f = this.formula;
		var ancestors = function(d){
			var a = {};
			d.foreach_child(function(child){
				var child_id = child.id;
				a[child_id] = child;

				var anc = ancestors(child);<commit_msg>added commit<commit_after>

var Node = function(f, handle){
	//ids of children
	this.NODE ={
			base: {type:'unknown', code:'?'},
			children: {
				OP: {
					base: {type:'op', code:'?', type:'op', op:'unknown'},
					children: {
						MULT: {base:{op:'mult', code:'*'}, children:{}}, //
						DIV: {base:{op:'div', code:'/'}, children:{}}, //top -> bottom, numerator to denominator
						ADD: {base:{op:'plus', code:'+'}, children:{}},
						SUB: {base:{op:'sub', code:'-'}, children:{}},
						EXPO: {base:{op:'power', code:'^'}, children:{}},
						EQ: {base:{op:'eq', code:'='}, children:{}},
						PAREN: {base:{op:'paren',code:'()', children:{}}}
					}
				},
				VAR: {base:{type:'variable', name:null},children:{}},
				NUMBER: {base:{type:'number', value:null},children:{}}
			}
	}
	this.foreach_child = function(cbk){
		var f = this.formula;
		for(var i=0; i < this.children.length; i++){
			cbk(f.get(this.children[i],i));
		}
	}
	this.add_child = function(child_id, tofront){
		if(isUndefined(tofront) || ! tofront){
			if(this.children.indexOf(child_id) < 0)
				this.children.push(child_id);
		}
		else {
			if(this.children.indexOf(child_id) < 0)
				this.children.unshift(child_id);
		}
	}
	this.init = function(f,handle){
		var that = this;
		var cobble_data = function(data,name){
			var node = {}; //copy base info.
			for(var b in data.base){
				node[b] = copyData(data.base[b]);
			}
			if(name == handle){
				return {found:true, data:node};
			}
			else{
				for(child in data.children){
					var res = cobble_data(data.children[child], child);
					//copy returned structure.
					if(res.found){
						for(var k in node){
							if(!(k in res.data))
								res.data[k] = node[k];
						}
						return {found:true, data:res.data}; //exists in one of our children.
					}
				}
			}
			//did not find this.

			return {found:false, data:{}};
		}
		var data = cobble_data(this.NODE,'NODE').data;
		this.HANDLE = handle;
		this.id = f.fresh_id();
		this.parent_id = -1;
		this.children = [];
		this.formula = f;
		this._data = data;
		return data;
	}
	this.replace = function(new_id){
		var f = this.formula;
		var par = this.parent();
		if(par != null){
			delete par[this.id];
			par[new_id] = null;
			f.get(new_id).parent_id = this.parent_id;
			this.parent_id = -1;
		}
		else{
			newnode.parent_id = -1;
		}
	}
	this.childAt = function(i){
		return this.children[i];
	}
	this.remove = function(child_to_moveup){
		this.replace(child_to_moveup);
	}
	this.data = function(key){
		return this._data[key];
	}
	this.set = function(key, value){
		this._data[key] = value;
	}
	this.set_parent = function(id,tofront){
		var f = this.formula;
		this.parent_id = id;
		if(f.has(id)){
			f.get(id).add_child(this.id,tofront);
		}
	}
	this.parent = function(){
		var f = this.formula;
		return get(this.parent_id);
	}
	this.child = function(i){
		var f = this.formula;
		return f.get(this.children[i]);
	}
	this.print = function(){
		var f = this.formula;
		var str = "";
		var type = this.data('type');
		if(type == 'op'){
			for(var i in this.children){
				var chl = f.get(this.children[i]);
				if(i > 0){
					str += (this.data('code'));
				}
				str += chl.print(f);
			}
		}
		else if(type == 'variable'){
			str = this.data('code');
		}
		else if(type == 'number'){
			str = (this.data('code'));
		}
		return str;
	}
	/*
	Given a formula, find all the ancestors.
	*/
	this.ancestors = function(){
		var f = this.formula;
		var ancestors = function(d){
			var a = {};
			d.foreach_child(function(child){
				var child_id = child.id;
				a[child_id] = child;

				var anc = ancestors(child);
{'fraction_non_alphanumeric': 0.200496757278874, 'fraction_numerical': 0.001793845729267283, 'mean_word_length': 3.060504201680672, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 6, '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': '3126676', 'n_tokens_mistral': 3277, 'n_tokens_neox': 2732, 'n_words': 562}