<commit_before>/*
 * Copyright (c) 2015, <NAME>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
package cdb;
import cdb.Data;

class TileBuilder {

	/*

		Bits

		1	2	4
		8	X	16
		32	64	128

		Corners

		┌  ─  ┐		0 1 2
		│  ■  │		3 8 4
		└  ─  ┘		5 6 7

		Lower Corners

		┌ ┐		9  10
		└ ┘		11 12

		U Corners

		   ┌ ┐			XX  13  XX
		┌       ┐		14  XX  15
		└       ┘
		   └ ┘			XX  16  XX

		Bottom

		└ - ┘			17 18 19


	*/


	var groundMap : Array<Int>;
	var groundIds = new Map<String, { id : Int, fill : Array<Int> }>();
	var borders = new Array<Array<Array<Int>>>();

	public function new( t : TilesetProps, stride : Int, total : Int ) {
		groundMap = [];
		for( i in 0...total+1 )
			groundMap[i] = 0;
		groundMap[0] = 0;
		borders = [];

		// get all grounds
		var tmp = new Map();
		for( s in t.sets )
			switch( s.t ) {
			case Ground if( s.opts.name != "" && s.opts.name != null ):
				var g = tmp.get(s.opts.name);
				if( g == null ) {
					g = [];
					tmp.set(s.opts.name, g);
				}
				g.push(s);
			default:
			}

		// sort by priority
		var allGrounds = Lambda.array(tmp);
		inline function ifNull<T>(v:Null<T>, def:T) return v == null ? def : v;
		allGrounds.sort(function(g1, g2) {
			var dp = ifNull(g1[0].opts.priority,0) - ifNull(g2[0].opts.priority,0);
			return dp != 0 ? dp : Reflect.compare(g1[0].opts.name, g2[0].opts.name);
		});

		// allocate group id
		var gid = 0;
		for( g in allGrounds ) {
			var p = ifNull(g[0].opts.priority, 0);
			if( p > 0 ) gid++;
			var fill = [];
			for( s in g )
				for( dx in 0...s.w )
					for( dy in 0...s.h ) {
						var tid = s.x + dx + (s.y + dy) * stride;
						fill.push(tid);
						groundMap[tid + 1] = gid;
					}
			groundIds.set(g[0].opts.name, { id : gid, fill : fill });
		}
		var maxGid = gid + 1;

		// save borders combos
		var allBorders = [];
		for( s in t.sets )
			if( s.t == Border )
				allBorders.push(s);
		inline function bweight(b) {
			var k = 0;
			if( b.opts.borderIn != null ) k += 1;
			if( b.opts.borderOut != null ) k += 2;
			if( b.opts.borderMode != null ) k += 4;
			if( b.opts.borderIn != null && b.opts.borderOut != null && b.opts.borderIn != "lower" && b.opts.borderOut != "upper" ) k += 8;
			return k;
		}
		allBorders.sort(function(b1, b2) {
			return bweight(b1) - bweight(b2);
		});
		for( b in allBorders ) {
			var gid = groundIds.get(b.opts.borderIn);
			var tid = groundIds.get(b.opts.borderOut);
			if( gid == null && tid == null ) continue;
			var gids, tids;
			if( gid != null )
				gids = [gid.id];
			else {
				switch( b.opts.borderIn ) {
				case null: gids = [for( g in tid.id + 1...maxGid ) g];
				case "lower": gids = [for( g in 0...tid.id ) g];
				default: continue;
				}
			}
			if( tid != null )
				tids = [tid.id];
			else {
				switch( b.opts.borderOut ) {
				case null: tids = [for( g in 0...gid.id ) g];
				case "upper": tids = [for( g in gid.id + 1...maxGid ) g];
				default: continue;
				}
			}
			var clear = gid != null && tid != null && b.opts.borderMode == null;
			switch( b.opts.borderMode ) {
			case "corner":<commit_msg>hl fix<commit_after>/*
 * Copyright (c) 2015, <NAME>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
package cdb;
import cdb.Data;

class TileBuilder {

	/*

		Bits

		1	2	4
		8	X	16
		32	64	128

		Corners

		┌  ─  ┐		0 1 2
		│  ■  │		3 8 4
		└  ─  ┘		5 6 7

		Lower Corners

		┌ ┐		9  10
		└ ┘		11 12

		U Corners

		   ┌ ┐			XX  13  XX
		┌       ┐		14  XX  15
		└       ┘
		   └ ┘			XX  16  XX

		Bottom

		└ - ┘			17 18 19


	*/


	var groundMap : Array<Int>;
	var groundIds = new Map<String, { id : Int, fill : Array<Int> }>();
	var borders = new Array<Array<Array<Int>>>();

	public function new( t : TilesetProps, stride : Int, total : Int ) {
		groundMap = [];
		for( i in 0...total+1 )
			groundMap[i] = 0;
		groundMap[0] = 0;
		borders = [];

		// get all grounds
		var tmp = new Map();
		for( s in t.sets )
			switch( s.t ) {
			case Ground if( s.opts.name != "" && s.opts.name != null ):
				var g = tmp.get(s.opts.name);
				if( g == null ) {
					g = [];
					tmp.set(s.opts.name, g);
				}
				g.push(s);
			default:
			}

		// sort by priority
		var allGrounds = Lambda.array(tmp);
		inline function ifNull<T>(v:Null<T>, def:T) return v == null ? def : v;
		allGrounds.sort(function(g1, g2) {
			var dp = ifNull(g1[0].opts.priority,0) - ifNull(g2[0].opts.priority,0);
			return dp != 0 ? dp : Reflect.compare(g1[0].opts.name, g2[0].opts.name);
		});

		// allocate group id
		var gid = 0;
		for( g in allGrounds ) {
			var p = ifNull(g[0].opts.priority, 0);
			if( p > 0 ) gid++;
			var fill = [];
			for( s in g )
				for( dx in 0...s.w )
					for( dy in 0...s.h ) {
						var tid = s.x + dx + (s.y + dy) * stride;
						fill.push(tid);
						groundMap[tid + 1] = gid;
					}
			groundIds.set(g[0].opts.name, { id : gid, fill : fill });
		}
		var maxGid = gid + 1;

		// save borders combos
		var allBorders = [];
		for( s in t.sets )
			if( s.t == Border )
				allBorders.push(s);
		inline function bweight(b) {
			var k = 0;
			if( b.opts.borderIn != null ) k += 1;
			if( b.opts.borderOut != null ) k += 2;
			if( b.opts.borderMode != null ) k += 4;
			if( b.opts.borderIn != null && b.opts.borderOut != null && b.opts.borderIn != "lower" && b.opts.borderOut != "upper" ) k += 8;
			return k;
		}
		allBorders.sort(function(b1, b2) {
			return bweight(b1) - bweight(b2);
		});
		for( b in allBorders ) {
			var gid = b.opts.borderIn == null ? null : groundIds.get(b.opts.borderIn);
			var tid = b.opts.borderOut == null ? null : groundIds.get(b.opts.borderOut);
			if( gid == null && tid == null ) continue;
			var gids, tids;
			if( gid != null )
				gids = [gid.id];
			else {
				switch( b.opts.borderIn ) {
				case null: gids = [for( g in tid.id + 1...maxGid ) g];
				case "lower": gids = [for( g in 0...tid.id ) g];
				default: continue;
				}
			}
			if( tid != null )
				tids = [tid.id];
			else {
				switch( b.opts.borderOut ) {
				case null: tids = [for( g in 0...gid.id ) g];
				case "upper": tids = [for( g in gid.id + 1...maxGid ) g];
				default: continue;
				}
			}
			var clear = gid != null && tid != null && b.opts.borderMode == null;
			switch( b.opts.borderMode ) {
			case "corner":
{'fraction_non_alphanumeric': 0.15183522577237918, 'fraction_numerical': 0.022973329812516503, 'mean_word_length': 2.342894969108561, 'pattern_counts': {'":': 6, '<': 21, '<?xml version=': 0, '>': 23, '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': '464092', 'n_tokens_mistral': 3430, 'n_tokens_neox': 2867, 'n_words': 938}