Braid.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* File : Braid.java
  2. * Program : Handle Reduction Aimation - Applet
  3. * By Jean Fromentin <jfroment@info.unicaen.fr>
  4. * Copyright 2008 Jean Fromentin
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. class Braid extends List{
  17. public int handleType;
  18. public int strandNumber=4;
  19. public Node handleBegin;
  20. public Node handleEnd;
  21. public int handleBeginIndice;
  22. public int handleEndIndice;
  23. public int handleStrand;
  24. public int trivialNumber;
  25. public Braid(){}
  26. public Braid(String braidWord){
  27. int length;
  28. int value;
  29. char letter;
  30. strandNumber=4;
  31. value=0;
  32. length=braidWord.length();
  33. addLast(0);
  34. for(int i=0;i<length;i++){
  35. letter=braidWord.charAt(i);
  36. if(letter>='a' && letter<='z'){
  37. value=(int)(letter-'a')+1;
  38. if(value+1>strandNumber){
  39. strandNumber=value+1;
  40. }
  41. }
  42. else if(letter>='A' && letter<='Z'){
  43. value=-(int)(letter-'A')-1;
  44. if(-value+1>strandNumber){
  45. strandNumber=-value+1;
  46. }
  47. }
  48. else{
  49. value=0;
  50. }
  51. addLast(value);
  52. }
  53. addLast(0);
  54. }
  55. public void findHandle(){
  56. Node handles[]=new Node[strandNumber];
  57. int indices[]=new int[strandNumber];
  58. int signs[]=new int[strandNumber];
  59. int sign,value;
  60. boolean stop;
  61. int indice=0;
  62. for(int i=0;i<strandNumber;i++){
  63. signs[i]=0;
  64. }
  65. stop=false;
  66. value=0;
  67. sign=0;
  68. initCurrent();
  69. while(!stop){
  70. value=value();
  71. sign=1;
  72. if(value<0){
  73. value=-value;
  74. sign=-1;
  75. }
  76. else if(value==0){
  77. sign=0;
  78. }
  79. if(signs[value]*sign<0){
  80. stop=true;
  81. }
  82. else{
  83. signs[value]=sign;
  84. indices[value]=indice;
  85. handles[value]=getCurrent();
  86. for(int j=1;j<value;j++){
  87. signs[j]=0;
  88. }
  89. }
  90. if(isEnd()){
  91. stop=true;
  92. }
  93. if(!stop){
  94. indice++;
  95. shift();
  96. }
  97. }
  98. if(signs[value]*sign<0){
  99. handleBegin=handles[value];
  100. handleEnd=getCurrent();
  101. handleBeginIndice=indices[value];
  102. handleEndIndice=indice;
  103. handleStrand=value;
  104. handleType=-sign;
  105. }
  106. else{
  107. handleStrand=0;
  108. }
  109. }
  110. public void insertTrivials(){
  111. int value;
  112. boolean stop;
  113. stop=false;
  114. setCurrent(handleBegin);
  115. trivialNumber=0;
  116. while(!stop){
  117. value=value();
  118. if(Math.abs(value)==handleStrand-1){
  119. addBefore(0);
  120. setCurrent(addAfter(0));
  121. trivialNumber+=2;
  122. }
  123. shift();
  124. if(getCurrent()==handleEnd){
  125. stop=true;
  126. }
  127. }
  128. handleEndIndice+=trivialNumber;
  129. }
  130. public void removeHandle(){
  131. int sign;
  132. int value;
  133. boolean stop;
  134. sign=-handleType;
  135. stop=false;
  136. setCurrent(handleBegin);
  137. while(!stop){
  138. value=value();
  139. if(Math.abs(value)==handleStrand){
  140. (getCurrent()).setValue(0);
  141. }
  142. if(value==0){
  143. (getCurrent()).setValue((handleStrand-1)*sign);
  144. sign=-sign;
  145. }
  146. if(value==handleStrand-1){
  147. (getCurrent()).setValue(value+1);
  148. }
  149. if(value==-(handleStrand-1)){
  150. (getCurrent()).setValue(value-1);
  151. }
  152. if(getCurrent()==handleEnd){
  153. stop=true;
  154. }
  155. else{
  156. shift();
  157. }
  158. }
  159. trivialNumber=2;
  160. }
  161. public void removeTrivials(){
  162. remove(handleBegin);
  163. remove(handleEnd);
  164. trivialNumber=0;
  165. }
  166. public int indice(){
  167. int value;
  168. int indice=0;
  169. boolean stop=false;
  170. initCurrent();
  171. while(!stop){
  172. value=value();
  173. if(Math.abs(value)>Math.abs(indice)){
  174. indice=value;
  175. }
  176. if(isEnd()){
  177. stop=true;
  178. }
  179. else{
  180. shift();
  181. }
  182. }
  183. return indice;
  184. }
  185. }