<commit_before>import java.util.Arrays;


public class Lamport extends Process {

	private static final boolean DEBUG = true;
	
	private int acc;
	private int[] q;
	
	public Lamport(int pid, int t, CommMatrix cm, int p) {
		super(pid, t, cm, p);
		this.setAcc(1);
		this.setQ(new int[(cm.getSize() / 2) - 1]);
	}
	
	public void handleMessage() {
		for (int i = 0; i < cm.getSize(); i++) {
			Message m = cm.getM(pid, i);
			if (m != null) {
				if (m.getM().equals(Message.Start)) {
					int pos = pid - ((cm.getSize() / 2) + 1);
					q[pos] = t;
					m.brodcastMessage(pid, p, cm, new Message(Message.Request, t));
				} 
				else if (m.getM().equals(Message.Request)) {
					int pos = i - ((cm.getSize() / 2) + 1);
					q[pos] = m.getT();
					if (DEBUG) System.out.printf("[%d] (%d) %s [%d, %d, %d]\n", System.currentTimeMillis(), pid, m.getM(), q[0], q[1], q[2]);
					t = (m.getT() > t) ? m.getT() + 1 : t + 1;
					m.sendMessage(i, pid, cm, new Message(Message.Acknowledge, t));
				}
				else if (m.getM().equals(Message.Acknowledge)) {
					int pos = i - ((cm.getSize() / 2) + 1);
					if (pid != i) q[pos] = m.getT();
					t = m.getT();
					if (DEBUG) System.out.printf("[%d] (%d) %s [%d, %d, %d]\n", System.currentTimeMillis(), pid, m.getM(), q[0], q[1], q[2]);
					if (acc == 3) {
						boolean allowed = true;
						if (q[pos] < this.getMinValue(q)) allowed = false;
						else if (q[pos] == this.getMinValue(q) && pos < this.getMinValueIndex(q)) allowed = false;
						if (allowed) {
							int index = pid - ((cm.getSize() / 2) + 1);
							if (DEBUG) for (int j = 0; j < 10; j++) System.out.printf("[%d] (%d) I'm thread B%d\n", System.currentTimeMillis(), pid, index);
							if (!DEBUG) for (int j = 0; j < 10; j++) System.out.printf("I'm thread B%d\n", index);
							acc = 1;
							m.brodcastMessage(pid, p, cm, new Message(Message.Release, t));
						}
					} else acc++;
				}
				else if (m.getM().equals(Message.Release)) {
					int pos = i - ((cm.getSize() / 2) + 1);
					q[pos] = 9999;
					if (DEBUG) System.out.printf("[%d] (%d) %s [%d, %d, %d]\n", System.currentTimeMillis(), pid, m.getM(), q[0], q[1], q[2]);
					if (acc == 3) {
						cm.setM(0, pid, new Message(Message.Finish, t));
						acc = 1;
					} else acc++;
				}
			}
		}
	}

	public int getAcc() {
		return acc;
	}

	public void setAcc(int acc) {
		this.acc = acc;
	}

	public int[] getQ() {
		return q;
	}

	public void setQ(int[] q) {
		this.q = q;
		for (int i = 0; i < q.length; i++) this.q[i] = 9999;
	}
	
	private int getMinValue(int[] a) {
		int[] b = a.clone();
		Arrays.sort(b);
		return b[0];
	}
	
	private int getMinValueIndex(int[] a) {
		int min = 9999;
		int index = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] < min) {
				min = a[i];
				index = i;
			}
		}
		return index;
	}
	
}
<commit_msg>Fix a problem with critical section access on network delay<commit_after>import java.util.Arrays;


public class Lamport extends Process {

	private static final boolean DEBUG = true;
	
	private int acc;
	private int[] q;
	
	public Lamport(int pid, int t, CommMatrix cm, int p) {
		super(pid, t, cm, p);
		this.setAcc(1);
		this.setQ(new int[(cm.getSize() / 2) - 1]);
	}
	
	public void handleMessage() {
		for (int i = 0; i < cm.getSize(); i++) {
			Message m = cm.getM(pid, i);
			if (m != null) {
				if (m.getM().equals(Message.Start)) {
					int pos = pid - ((cm.getSize() / 2) + 1);
					q[pos] = pid;
					m.brodcastMessage(pid, p, cm, new Message(Message.Request, pid));
				} else if (m.getM().equals(Message.Request)) {
					int pos = i - ((cm.getSize() / 2) + 1);
					q[pos] = m.getT();
					t = (m.getT() > t) ? m.getT() + 1 : t + 1;
					if (DEBUG) System.out.printf("[%d] (%d,%d) %s from (%d,%d) [%d, %d, %d]\n", System.currentTimeMillis(), pid, t, m.getM(), i, m.getT(), q[0], q[1], q[2]);
					m.sendMessage(i, pid, cm, new Message(Message.Acknowledge, t));
				} else if (m.getM().equals(Message.Acknowledge)) {
					t = (m.getT() > t) ? m.getT() + 1 : t + 1;
					if (DEBUG) System.out.printf("[%d] (%d,%d) %s from (%d,%d) [%d, %d, %d]\n", System.currentTimeMillis(), pid, t, m.getM(), i, m.getT(), q[0], q[1], q[2]);
					if (acc == 3) {
						boolean allowed = true;
						int index = pid - ((cm.getSize() / 2) + 1);
						if (index != this.getMinValueIndex(q)) allowed = false;
						else if (q[index] < this.getMinValue(q)) allowed = false;
						else if (q[index] == this.getMinValue(q) && index < this.getMinValueIndex(q)) allowed = false;
						if (allowed) {
							if (DEBUG) for (int j = 0; j < 10; j++) {
								System.out.printf("[%d] (%d) I'm thread B%d\n", System.currentTimeMillis(), pid, index);
								//try { Thread.sleep(10); } catch (InterruptedException e) {}
							}
							if (!DEBUG) for (int j = 0; j < 10; j++) {
								System.out.printf("I'm thread B%d\n", index);
								//try { Thread.sleep(10); } catch (InterruptedException e) {}
							}
							acc = 1;
							m.brodcastMessage(pid, p, cm, new Message(Message.Release, t));
						}
					} else acc++;
				} else if (m.getM().equals(Message.Release)) {
					int pos = i - ((cm.getSize() / 2) + 1);
					q[pos] = 9999;
					if (DEBUG) System.out.printf("[%d] (%d,%d) %s from (%d,%d) [%d, %d, %d] | acc: %d\n", System.currentTimeMillis(), pid, t, m.getM(), i, m.getT(), q[0], q[1], q[2], acc);
					boolean allowed = true;
					int index = pid - ((cm.getSize() / 2) + 1);
					if (index != this.getMinValueIndex(q) || q[index] == 9999) allowed = false;
					else if (q[index] < this.getMinValue(q)) allowed = false;
					else if (q[index] == this.getMinValue(q) && index < this.getMinValueIndex(q)) allowed = false;
					if (allowed) {
						if (DEBUG) for (int j = 0; j < 10; j++) {
							System.out.printf("[%d] (%d) I'm thread B%d\n", System.currentTimeMillis(), pid, index);
							//try { Thread.sleep(10); } catch (InterruptedException e) {}
						}
						if (!DEBUG) for (int j = 0; j < 10; j++) {
							System.out.printf("I'm thread B%d\n", index);
							//try { Thread.sleep(10); } catch (InterruptedException e) {}
						}
						acc = 1;
						m.brodcastMessage(pid, p, cm, new Message(Message.Release, t));
					}
					if (acc == 3) {	
						cm.setM(0, pid, new Message(Message.Finish, t));
						acc = 1;
						if (DEBUG) System.out.printf("[%d] (%d,%d) Finished to 1\n", System.currentTimeMillis(), pid, t);
					} else acc++;
				}
			}
		}
	}

	public int getAcc() {
		return acc;
	}

	public void setAcc(int acc) {
		this.acc = acc;
	}

	public int[] getQ() {
		return q;
	}

	public void setQ(int[] q) {
		this.q = q;
		for (int i = 0; i < q.length; i++) this.q[i] = 9999;
	}
	
	private int getMinValue(int[] a) {
		int[] b = a.clone();
		Arrays.sort(b);
		return b[0];
	}
	
	private int getMinValueIndex(int[] a) {
		int min = 9999;
		int index = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] < min) {
				min = a[i];
				index = i;
			}
		}
		return index;
	}
	
}

{'fraction_non_alphanumeric': 0.20865287223267254, 'fraction_numerical': 0.018231804369845176, 'mean_word_length': 2.5684047496128035, 'pattern_counts': {'":': 0, '<': 23, '<?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': '1019829', 'n_tokens_mistral': 3356, 'n_tokens_neox': 2731, 'n_words': 745}