<commit_before># This file is part of NIT ( http://www.nitlanguage.org ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Table layout builders
module layout_builders

import abstract_compiler

# Layouts

class Layout[E: Object]
	# Ids or each element
	var ids: Map[E, Int] = new HashMap[E, Int]
	# Fixed positions of each element in all tables
	var pos: Map[E, Int] = new HashMap[E, Int]
end

class PHLayout[HOLDER: Object, E: Object]
	super Layout[E]
	# Masks used by hash function
	var masks: Map[HOLDER, Int] = new HashMap[HOLDER, Int]
	# Positions of each element for each tables
	var hashes: Map[HOLDER, Map[E, Int]] = new HashMap[HOLDER, Map[E, Int]]
end

class PropertyLayout[E: Object]
	# Fixed positions of each element in all tables
	var pos: Map[E, Int] = new HashMap[E, Int]
end

# Builders

interface TypingLayoutBuilder[E: Object]
	# Build typing table layout
	fun build_layout(elements: Set[E]): Layout[E] is abstract
end

abstract class PropertyLayoutBuilder[E: MProperty]

	type LAYOUT: PropertyLayout[E]

	# Compute properties ids and position
	fun build_layout(mclasses: Set[MClass]): LAYOUT is abstract
end

# Layout builder for MProperty using Coloring (CL)
class CLPropertyLayoutBuilder[E: MProperty]
	super PropertyLayoutBuilder[E]

	private var colorer: MPropertyColorer[E]

	init(colorer: MPropertyColorer[E]) do
		self.colorer = colorer
	end

	# Compute mclasses ids and position using BM
	redef fun build_layout(mclasses) do
		var result = new PropertyLayout[E]
		result.pos = self.colorer.colorize(mclasses)
		return result
	end
end

# Layout builder for MProperty using Perfect Hashing (PH)
# TODO implement this class without sublcassing CL builder
class PHPropertyLayoutBuilder[E: MProperty]
	super CLPropertyLayoutBuilder[E]
end

abstract class ResolutionLayoutBuilder

	type LAYOUT: Layout[MType]

	init do end

	fun build_layout(elements: Map[MClassType, Set[MType]]): LAYOUT is abstract

	fun compute_ids(elements: Map[MClassType, Set[MType]]): Map[MType, Int] do
		var ids = new HashMap[MType, Int]
		var color = 0
		for mclasstype, mclasstypes in elements do
			for element in mclasstypes do
				if ids.has_key(element) then continue
				ids[element] = color
				color += 1
			end
		end
		return ids
	end
end

# Layout builder for MClass using Binary Matrix (BM)
class BMResolutionLayoutBuilder
	super ResolutionLayoutBuilder

	init do super

	# Compute resolved types position using BM
	redef fun build_layout(elements) do
		var result = new Layout[MType]
		result.ids = self.compute_ids(elements)
		result.pos = result.ids
		return result
	end
end

# Layout builder for resolution tables using Coloring (CL)
class CLResolutionLayoutBuilder
	super ResolutionLayoutBuilder

	private var colorer: ResolutionColorer = new ResolutionColorer

	init do super

	# Compute resolved types colors
	redef fun build_layout(elements) do
		var result = new Layout[MType]
		result.ids = self.compute_ids(elements)
		result.pos = self.colorer.colorize(elements)
		return result
	end
end

# Layout builder for resolution table using Perfect Hashing (PH)
class PHResolutionLayoutBuilder
	super ResolutionLayoutBuilder

	redef type LAYOUT: PHLayout[MClassType, MType]

	private var hasher: PerfectHasher[MClassType, MType]

	init(operator: PHOperator) do self.hasher = new PerfectHasher[MClassType, MType](operator)

	# Compute resolved types masks and hashes
	redef fun build_layout(elements) do
		var result = new PHLayout[MClassType, MType]
		result.ids = self.compute_ids(elements)
		result.pos = result.ids
		result.masks = self.hasher.compute_masks(elements, result.ids)
		result.hashes = self.hasher.compute_hashes(elements, result.ids, result.masks)
		return result
	end

	redef fun compute_ids(elements) do
		var ids = new HashMap[MType, Int]<commit_msg>layout_builders: simplified ResolutionLayoutBuilder (for future refactoring)<commit_after># This file is part of NIT ( http://www.nitlanguage.org ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Table layout builders
module layout_builders

import abstract_compiler

# Layouts

class Layout[E: Object]
	# Ids or each element
	var ids: Map[E, Int] = new HashMap[E, Int]
	# Fixed positions of each element in all tables
	var pos: Map[E, Int] = new HashMap[E, Int]
end

class PHLayout[HOLDER: Object, E: Object]
	super Layout[E]
	# Masks used by hash function
	var masks: Map[HOLDER, Int] = new HashMap[HOLDER, Int]
	# Positions of each element for each tables
	var hashes: Map[HOLDER, Map[E, Int]] = new HashMap[HOLDER, Map[E, Int]]
end

class PropertyLayout[E: Object]
	# Fixed positions of each element in all tables
	var pos: Map[E, Int] = new HashMap[E, Int]
end

# Builders

interface TypingLayoutBuilder[E: Object]
	# Build typing table layout
	fun build_layout(elements: Set[E]): Layout[E] is abstract
end

abstract class PropertyLayoutBuilder[E: MProperty]

	type LAYOUT: PropertyLayout[E]

	# Compute properties ids and position
	fun build_layout(mclasses: Set[MClass]): LAYOUT is abstract
end

# Layout builder for MProperty using Coloring (CL)
class CLPropertyLayoutBuilder[E: MProperty]
	super PropertyLayoutBuilder[E]

	private var colorer: MPropertyColorer[E]

	init(colorer: MPropertyColorer[E]) do
		self.colorer = colorer
	end

	# Compute mclasses ids and position using BM
	redef fun build_layout(mclasses) do
		var result = new PropertyLayout[E]
		result.pos = self.colorer.colorize(mclasses)
		return result
	end
end

# Layout builder for MProperty using Perfect Hashing (PH)
# TODO implement this class without sublcassing CL builder
class PHPropertyLayoutBuilder[E: MProperty]
	super CLPropertyLayoutBuilder[E]
end

abstract class ResolutionLayoutBuilder
	type LAYOUT: Layout[MType]
	init do end
	fun build_layout(elements: Map[MClassType, Set[MType]]): LAYOUT is abstract
end

# Layout builder for MClass using Binary Matrix (BM)
class BMResolutionLayoutBuilder
	super ResolutionLayoutBuilder

	init do super

	# Compute resolved types position using BM
	redef fun build_layout(elements) do
		var result = new Layout[MType]
		result.ids = self.compute_ids(elements)
		result.pos = result.ids
		return result
	end

	fun compute_ids(elements: Map[MClassType, Set[MType]]): Map[MType, Int] do
		var ids = new HashMap[MType, Int]
		var color = 0
		for mclasstype, mclasstypes in elements do
			for element in mclasstypes do
				if ids.has_key(element) then continue
				ids[element] = color
				color += 1
			end
		end
		return ids
	end
end

# Layout builder for resolution tables using Coloring (CL)
class CLResolutionLayoutBuilder
	super ResolutionLayoutBuilder

	private var colorer: ResolutionColorer = new ResolutionColorer

	init do super

	# Compute resolved types colors
	redef fun build_layout(elements) do
		var result = new Layout[MType]
		result.ids = self.compute_ids(elements)
		result.pos = self.colorer.colorize(elements)
		return result
	end

	fun compute_ids(elements: Map[MClassType, Set[MType]]): Map[MType, Int] do
		var ids = new HashMap[MType, Int]
		var color = 0
		for mclasstype, mclasstypes in elements do
			for element in mclasstypes do
				if ids.has_key(element) then continue
				ids[element] = color
				color += 1
			end
		end
		return ids
	end
end

# Layout builder for resolution table using Perfect Hashing (PH)
class PHResolutionLayoutBuilder
	super ResolutionLayoutBuilder

	redef type LAYOUT: PHLayout[MClassType, MType]

	private var hasher: PerfectHasher[MClassType, MType]

	init(operator: PHOperator) do self.hasher = new PerfectHasher[MClassType, MType](operator)

	# Compute resolved types masks and hashes
	redef fun build_layout(elements) do
		var result = new PHLayout[MClassType, MType]
		result.ids = self.compute_ids(elements)
		result.pos = result.ids
		result.masks = self.hasher.compute_masks(elements, result.ids)
		result.hashes = self.hasher.compute_hashes(elements, result.ids, result.masks)
		return result
	end

	fun compute_ids(elements: Map[MClassType, Set[MType]]): Map[MType, Int] do
		var ids = new HashMap[MType, Int]
{'fraction_non_alphanumeric': 0.07465509568313307, 'fraction_numerical': 0.001557632398753894, 'mean_word_length': 4.78815196394076, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5413801', 'n_tokens_mistral': 2911, 'n_tokens_neox': 2741, 'n_words': 1006}