001 package gate.creole.morph;
002
003 import java.util.Arrays;
004
005 public class CharMap {
006 private char[] itemsKeys = null;
007
008 private FSMState[] itemsObjs = null;
009
010 private char[] adjitemsKeys = null;
011
012 private FSMState[] adjitemsObjs = null;
013
014
015 /**
016 * resize the containers by one leavaing empty elemant at position 'index'
017 */
018 private void resizeCHILD(int index) {
019 int newsz = itemsKeys.length + 1;
020 char[] tempKeys = new char[newsz];
021 FSMState[] tempObjs = new FSMState[newsz];
022
023 int i;
024 for (i = 0; i < index; i++) {
025 tempKeys[i] = itemsKeys[i];
026 tempObjs[i] = itemsObjs[i];
027 }
028 for (i = index + 1; i < newsz; i++) {
029 tempKeys[i] = itemsKeys[i - 1];
030 tempObjs[i] = itemsObjs[i - 1];
031 }
032
033 itemsKeys = tempKeys;
034 itemsObjs = tempObjs;
035 } // resize
036
037 /**
038 * resize the containers by one leavaing empty elemant at position 'index'
039 */
040 private void resizeADJ(int index) {
041 int newsz = adjitemsKeys.length + 1;
042 char[] tempKeys = new char[newsz];
043 FSMState[] tempObjs = new FSMState[newsz];
044
045 int i;
046 for (i = 0; i < index; i++) {
047 tempKeys[i] = adjitemsKeys[i];
048 tempObjs[i] = adjitemsObjs[i];
049 }
050 for (i = index + 1; i < newsz; i++) {
051 tempKeys[i] = adjitemsKeys[i - 1];
052 tempObjs[i] = adjitemsObjs[i - 1];
053 }
054
055 adjitemsKeys = tempKeys;
056 adjitemsObjs = tempObjs;
057 } // resize
058
059 /**
060 * get the object from the map using the char key
061 */
062 public FSMState get(char key, byte type) {
063 if(type == FSMState.CHILD_STATE) {
064 if (itemsKeys == null)
065 return null;
066 int index = Arrays.binarySearch(itemsKeys, key);
067 if (index < 0)
068 return null;
069 return itemsObjs[index];
070 } else {
071 if (adjitemsKeys == null)
072 return null;
073 int index = Arrays.binarySearch(adjitemsKeys, key);
074 if (index < 0)
075 return null;
076 return adjitemsObjs[index];
077 }
078 }
079
080 /**
081 * put the object into the char map using the chat as the key
082 */
083 public FSMState put(char key, FSMState value, byte type) {
084 if(type == FSMState.CHILD_STATE) {
085 if (itemsKeys == null) {
086 itemsKeys = new char[1];
087 itemsKeys[0] = key;
088 itemsObjs = new FSMState[1];
089 itemsObjs[0] = value;
090 return value;
091 }// if first time
092 int index = Arrays.binarySearch(itemsKeys, key);
093 if (index < 0) {
094 index = ~index;
095 resizeCHILD(index);
096 itemsKeys[index] = key;
097 itemsObjs[index] = value;
098 }
099 return itemsObjs[index];
100 } else {
101 if (adjitemsKeys == null) {
102 adjitemsKeys = new char[1];
103 adjitemsKeys[0] = key;
104 adjitemsObjs = new FSMState[1];
105 adjitemsObjs[0] = value;
106 return value;
107 }// if first time
108 int index = Arrays.binarySearch(adjitemsKeys, key);
109 if (index < 0) {
110 index = ~index;
111 resizeADJ(index);
112 adjitemsKeys[index] = key;
113 adjitemsObjs[index] = value;
114 }
115 return adjitemsObjs[index];
116 }
117 } // put
118
119 public char[] getItemsKeys() {
120 return itemsKeys;
121 }
122
123 public char[] getAdjitemsKeys() {
124 return adjitemsKeys;
125 }
126 }
|