<commit_before>package model;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import location.Location;
import state.SegState;
import cell.Cell;
import cell.SegCell;
import grid.SquareGrid;
import gui.CellSocietyGUI;

public class SegModel extends Model {
	
	private double mySimilarity = 0.5;
	private Map<Location,Cell> myCells;

	public SegModel(int rows, int columns, CellSocietyGUI CSGUI) {
		super(rows, columns, CSGUI);
		myCells = new HashMap<>();
	}
	
	@Override
	public void setParameters(Map<String,String> map){
		super.setParameters(map);
		if(map.containsKey("PERCENT_SIMILAR"))
			mySimilarity = Double.parseDouble(map.get("PERCENT_SIMILAR"));
	}

	@Override
	public void buildGrid(List<Map<String, String>> cells, CellSocietyGUI CSGUI) {
		myCells.clear();
		cells.forEach(map -> {
			int x = Integer.parseInt(map.get("x"));
			int y = Integer.parseInt(map.get("y"));
			int state = Integer.parseInt(map.get("state"));
			SegCell cell = new SegCell(new SegState(state), new Location(x, y, getWidth(), getHeight()), CSGUI);
			cell.setSimilarity(mySimilarity);
			myCells.put(cell.getLocation(), cell);
		});
		if (myCells.size() < getWidth() * getHeight())
			System.err.println("Missing Cell Info!");
		SquareGrid grid = new SquareGrid(getWidth(), getHeight(), myCells);
		grid.setNeighbors();
		setMyGrid(grid);
	}
	
	@Override
	public void step(){
		Stack<Cell> dissatisfiedList = getDissatisfiedCells();
		Stack<Cell> vacentList = getVacentCells();
		Collections.shuffle(vacentList);
		while(!dissatisfiedList.isEmpty() && !vacentList.isEmpty()){
			SegCell dissatisfiedCell = (SegCell)dissatisfiedList.pop();
			SegCell vacentCell = (SegCell)vacentList.pop();
			vacentCell.determineNextState(dissatisfiedCell.getState());
			dissatisfiedCell.determineNextState(vacentCell.getState());
		}
		myCells.forEach((loc,cell)->{cell.goToNextState();});
	}
	
	private Stack<Cell> getDissatisfiedCells(){
		Stack<Cell> list = new Stack<>();
		myCells.forEach((loc,cell)->{
			if(!((SegCell)cell).isEmpty() && !((SegCell)cell).isSatisfied())<commit_msg>Change algorithm for SegModel<commit_after>package model;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import location.Location;
import state.SegState;
import cell.Cell;
import cell.SegCell;
import grid.SquareGrid;
import gui.CellSocietyGUI;

public class SegModel extends Model {
	
	private double mySimilarity = 0.5;
	private Map<Location,Cell> myCells;

	public SegModel(int rows, int columns, CellSocietyGUI CSGUI) {
		super(rows, columns, CSGUI);
		myCells = new HashMap<>();
	}
	
	@Override
	public void setParameters(Map<String,String> map){
		super.setParameters(map);
		if(map.containsKey("PERCENT_SIMILAR"))
			mySimilarity = Double.parseDouble(map.get("PERCENT_SIMILAR"));
	}

	@Override
	public void buildGrid(List<Map<String, String>> cells, CellSocietyGUI CSGUI) {
		myCells.clear();
		cells.forEach(map -> {
			int x = Integer.parseInt(map.get("x"));
			int y = Integer.parseInt(map.get("y"));
			int state = Integer.parseInt(map.get("state"));
			SegCell cell = new SegCell(new SegState(state), new Location(x, y, getWidth(), getHeight()), CSGUI);
			cell.setSimilarity(mySimilarity);
			myCells.put(cell.getLocation(), cell);
		});
		if (myCells.size() < getWidth() * getHeight())
			System.err.println("Missing Cell Info!");
		SquareGrid grid = new SquareGrid(getWidth(), getHeight(), myCells);
		grid.setNeighbors();
		setMyGrid(grid);
	}
	
	@Override
	public void step(){
		Stack<Cell> exchangeList = getDissatisfiedCells();
		exchangeList.addAll(getVacentCells());
		Collections.shuffle(exchangeList);
		
		while(exchangeList.size()>=2){
			SegCell cell1 = (SegCell)exchangeList.pop();
			SegCell cell2 = (SegCell)exchangeList.pop();
			cell2.determineNextState(cell1.getState());
			cell1.determineNextState(cell2.getState());
		}
		myCells.forEach((loc,cell)->{cell.goToNextState();});
	}
	
	private Stack<Cell> getDissatisfiedCells(){
		Stack<Cell> list = new Stack<>();
		myCells.forEach((loc,cell)->{
			if(!((SegCell)cell).isEmpty() && !((SegCell)cell).isSatisfied())
{'fraction_non_alphanumeric': 0.1514576913960654, 'fraction_numerical': 0.0026072529035316427, 'mean_word_length': 5.53250773993808, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 29, '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': '6165807', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1377, 'n_words': 275}