<commit_before>package protocolsupport.protocol.storage.netcache.chunk;

import it.unimi.dsi.fastutil.shorts.Short2ByteMap;
import it.unimi.dsi.fastutil.shorts.Short2ByteOpenCustomHashMap;
import it.unimi.dsi.fastutil.shorts.ShortHash;
import protocolsupport.protocol.types.chunk.ChunkConstants;

public class BlockStoragePaletted extends BlockStorage {

	protected static class MaxSizeReachedException extends RuntimeException {
		protected static final MaxSizeReachedException instance = new MaxSizeReachedException();
		private static final long serialVersionUID = 1L;
		@Override
		public synchronized Throwable fillInStackTrace() {
			return this;
		}
	}

	protected final byte[] blocks = new byte[ChunkConstants.BLOCKS_IN_SECTION];
	protected final BlockStoragePaletted.Palette palette;

	public BlockStoragePaletted() {
		this.palette = new Palette();
	}

	public BlockStoragePaletted(short[] runtimeToGlobal) {
		this.palette = new Palette(runtimeToGlobal);
	}

	@Override
	public short getBlockData(int index) {
		return palette.getGlobalId(blocks[index]);
	}

	@Override
	public void setBlockData(int index, short blockdata) {
		setRuntimeId(index, palette.getOrComputeRuntimeId(blockdata));
	}

	public void setRuntimeId(int index, byte runtimeId) {
		blocks[index] = runtimeId;
	}

	protected static class Palette {

		protected static final short initial_size = 32;

		protected static final short max_size = 256;

		protected final Short2ByteMap globalToRuntime = new Short2ByteOpenCustomHashMap(initial_size, new ShortHash.Strategy() {
			@Override
			public int hashCode(short e) {
				return e;
			}

			@Override
			public boolean equals(short a, short b) {
				return a == b;
			}
		});
		// TODO: make it grow initial -> max with power of two
		protected short[] runtimeToGlobal = new short[max_size];
		protected short nextRuntimeId = 0;

		public Palette() {
			getOrComputeRuntimeId((short) 0);
		}

		public Palette(short[] runtimeToGlobal) {
			if (runtimeToGlobal.length > max_size) {
				throw MaxSizeReachedException.instance;
			}
			int rtgLenth = runtimeToGlobal.length;
			System.arraycopy(runtimeToGlobal, 0, this.runtimeToGlobal, 0, rtgLenth);
			this.nextRuntimeId = (short) rtgLenth;
			for (int r = 0; r < rtgLenth; r++) {
				this.globalToRuntime.put(runtimeToGlobal[r], (byte) r);
			}
		}

		public byte getOrComputeRuntimeId(short globalId) {
			return globalToRuntime.computeIfAbsent(globalId, k -> {
				short cRuntimeId = nextRuntimeId++;
				if (cRuntimeId == max_size) {
					throw MaxSizeReachedException.instance;
				}
				runtimeToGlobal[cRuntimeId] = (short) k;
				return cRuntimeId;
			});
		}

		public short getGlobalId(byte runtimeId) {
			return runtimeToGlobal[runtimeId & 0xFF];
		}

	}

}<commit_msg>Cast to byte<commit_after>package protocolsupport.protocol.storage.netcache.chunk;

import it.unimi.dsi.fastutil.shorts.Short2ByteMap;
import it.unimi.dsi.fastutil.shorts.Short2ByteOpenCustomHashMap;
import it.unimi.dsi.fastutil.shorts.ShortHash;
import protocolsupport.protocol.types.chunk.ChunkConstants;

public class BlockStoragePaletted extends BlockStorage {

	protected static class MaxSizeReachedException extends RuntimeException {
		protected static final MaxSizeReachedException instance = new MaxSizeReachedException();
		private static final long serialVersionUID = 1L;
		@Override
		public synchronized Throwable fillInStackTrace() {
			return this;
		}
	}

	protected final byte[] blocks = new byte[ChunkConstants.BLOCKS_IN_SECTION];
	protected final BlockStoragePaletted.Palette palette;

	public BlockStoragePaletted() {
		this.palette = new Palette();
	}

	public BlockStoragePaletted(short[] runtimeToGlobal) {
		this.palette = new Palette(runtimeToGlobal);
	}

	@Override
	public short getBlockData(int index) {
		return palette.getGlobalId(blocks[index]);
	}

	@Override
	public void setBlockData(int index, short blockdata) {
		setRuntimeId(index, palette.getOrComputeRuntimeId(blockdata));
	}

	public void setRuntimeId(int index, byte runtimeId) {
		blocks[index] = runtimeId;
	}

	protected static class Palette {

		protected static final short initial_size = 32;

		protected static final short max_size = 256;

		protected final Short2ByteMap globalToRuntime = new Short2ByteOpenCustomHashMap(initial_size, new ShortHash.Strategy() {
			@Override
			public int hashCode(short e) {
				return e;
			}

			@Override
			public boolean equals(short a, short b) {
				return a == b;
			}
		});
		// TODO: make it grow initial -> max with power of two
		protected short[] runtimeToGlobal = new short[max_size];
		protected short nextRuntimeId = 0;

		public Palette() {
			getOrComputeRuntimeId((short) 0);
		}

		public Palette(short[] runtimeToGlobal) {
			if (runtimeToGlobal.length > max_size) {
				throw MaxSizeReachedException.instance;
			}
			int rtgLenth = runtimeToGlobal.length;
			System.arraycopy(runtimeToGlobal, 0, this.runtimeToGlobal, 0, rtgLenth);
			this.nextRuntimeId = (short) rtgLenth;
			for (int r = 0; r < rtgLenth; r++) {
				this.globalToRuntime.put(runtimeToGlobal[r], (byte) r);
			}
		}

		public byte getOrComputeRuntimeId(short globalId) {
			return globalToRuntime.computeIfAbsent(globalId, k -> {
				short cRuntimeId = nextRuntimeId++;
				if (cRuntimeId == max_size) {
					throw MaxSizeReachedException.instance;
				}
				runtimeToGlobal[cRuntimeId] = (short) k;
				return (byte) cRuntimeId;
			});
		}

		public short getGlobalId(byte runtimeId) {
			return runtimeToGlobal[runtimeId & 0xFF];
		}

	}

}
{'fraction_non_alphanumeric': 0.0919290626131017, 'fraction_numerical': 0.00579080709373869, 'mean_word_length': 4.5491967871485945, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '3636326', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1688, 'n_words': 415}