Ocorrence.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package middlewares;
  7. import java.util.HashMap;
  8. import java.util.LinkedHashMap;
  9. import java.util.Map;
  10. import java.util.NavigableMap;
  11. import java.util.TreeMap;
  12. /**
  13. *
  14. * @author Eugenio
  15. */
  16. public class Ocorrence implements Comparable {
  17. public static int WRITE_ACCESS = 0;
  18. public static int READ_ACCESS = 1;
  19. public String var;
  20. public NavigableMap<Integer, String> ocorrencias;
  21. public HashMap<Integer, Integer[]> vizinhos;
  22. public Ocorrence(String var) {
  23. this.var = var;
  24. vizinhos = new LinkedHashMap<>();
  25. ocorrencias = new TreeMap<>();
  26. }
  27. @Override
  28. public int compareTo(Object o) {
  29. return ((Ocorrence) o).ocorrencias.size() - ocorrencias.size();
  30. }
  31. /**
  32. *
  33. * @param i linha da ocorrencia
  34. * @param accesso se variavel e acessada como leitura o valor é igual a 1,
  35. * caso contrario terá valor igual a 0
  36. *
  37. */
  38. public void add(Integer i, Integer accesso) {
  39. ocorrencias.put(i, "");
  40. vizinhos.put(i, new Integer[]{-1, -1, accesso});
  41. }
  42. public void setRegister(Integer i, String register) {
  43. int last = last();
  44. ocorrencias.put(i, register);
  45. if (last >= 0) {
  46. vizinhos.get(last)[1] = i;
  47. vizinhos.get(i)[0] = last;
  48. }
  49. }
  50. public int ocorrenceCount() {
  51. return ocorrencias.size();
  52. }
  53. public int first() {
  54. if (!ocorrencias.isEmpty()) {
  55. for (Map.Entry<Integer, String> entry : ocorrencias.entrySet()) {
  56. return entry.getKey();
  57. }
  58. }
  59. return -1;
  60. }
  61. public int last() {
  62. if (ocorrencias.isEmpty()) {
  63. return -1;
  64. }
  65. return ocorrencias.lastKey();
  66. }
  67. public int numeroDeOcorrenciasApartirDe(int i) {
  68. int count = 0;
  69. for (Map.Entry<Integer, String> integer : ocorrencias.entrySet()) {
  70. if (integer.getKey() < i) {
  71. continue;
  72. }
  73. count++;
  74. }
  75. return count;
  76. }
  77. @Override
  78. public String toString() {
  79. return "ID: " + var + " > ocorre em:" + ocorrencias;
  80. }
  81. public boolean ocorreNoIntervalo(int init, int end) {
  82. int pos = -1;
  83. for (Map.Entry<Integer, String> integer : ocorrencias.entrySet()) {
  84. pos = integer.getKey();
  85. if (pos >= init && pos <= end) {
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. protected void printVizinhos() {
  92. for (Map.Entry<Integer, Integer[]> viz : vizinhos.entrySet()) {
  93. System.out.println("Ocorrencia var{" + var + "}" + viz.getKey() + ";" + viz.getValue()[0] + ";" + viz.getValue()[1]);
  94. }
  95. }
  96. protected boolean _compararOcorrencia(int linha, int pos) throws Exception {
  97. String reg = ocorrencias.get(linha);
  98. // System.out.println("V:" + vizinhos + ":" + linha + ":" + vizinhos.get(linha));
  99. if (!vizinhos.containsKey(linha)) {
  100. throw new Exception("Ocorrencia var{" + var + "} linha {" + linha + "} não definida.");
  101. }
  102. // printVizinhos();
  103. int key = vizinhos.get(linha)[pos];
  104. // System.out.println("Vizinhos de linha:" + linha + ";" + key);
  105. if (ocorrencias.containsKey(key)) {
  106. String reg2 = ocorrencias.get(key);
  107. // System.out.println("Ocorrencia L{" + linha + ":" + pos + "} Reg1:{" + reg + "}:{" + reg2 + "}");
  108. return reg.equals(reg2);
  109. }
  110. return false;
  111. }
  112. public boolean ocorreEm(int i) {
  113. return ocorrencias.containsKey(i);
  114. }
  115. public boolean proximaOcorrenciaIgual(int linha) throws Exception {
  116. return _compararOcorrencia(linha, 1);
  117. }
  118. public boolean anteriorOcorrenciaIgual(int linha) throws Exception {
  119. return _compararOcorrencia(linha, 0);
  120. }
  121. public int nextOcorrenceAfter(int line) {
  122. int next = -1;
  123. for (Map.Entry<Integer, String> oc : ocorrencias.entrySet()) {
  124. if (oc.getKey() > line) {
  125. next = oc.getKey();
  126. break;
  127. }
  128. }
  129. return next;
  130. }
  131. public void removerOcorrenciaLinha(int posicao) {
  132. if (ocorrencias.containsKey(posicao)) {
  133. ocorrencias.remove(posicao);
  134. vizinhos.remove(posicao);
  135. }
  136. }
  137. public Integer ocorreApos(Integer i) {
  138. int pos = -1;
  139. for (Map.Entry<Integer, String> entry : ocorrencias.entrySet()) {
  140. Integer integer = entry.getKey();
  141. // System.out.println("ocorreApos:" + i + ":" + integer);
  142. if (integer > i) {
  143. pos = integer;
  144. break;
  145. }
  146. }
  147. return pos;
  148. }
  149. public boolean eAcessadaLeituraApos(Integer i) {
  150. Integer pos = ocorreApos(i);
  151. // System.out.println("eAcessadaLeituraApos[" + var + ":" + pos + "]" + ocorrencias);
  152. if (pos >= 0) {
  153. return vizinhos.get(pos)[2] == READ_ACCESS;
  154. }
  155. return false;
  156. }
  157. public boolean eAcessadaEscritaApos(Integer i) {
  158. Integer pos = ocorreApos(i);
  159. if (pos >= 0) {
  160. return vizinhos.get(pos)[2] == WRITE_ACCESS;
  161. }
  162. return false;
  163. }
  164. }