summaryrefslogtreecommitdiffstats
path: root/src/org/jetbrains/java/decompiler/util/SFormsFastMapOld.java
blob: 7d428a43d3798ff0659daa50e468c3d0847a3ed7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.java.decompiler.util;

import org.jetbrains.java.decompiler.modules.decompiler.exps.VarExprent;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

public class SFormsFastMapOld<E> {

  private List<ArrayList<Entry<Integer, E>>> lstElements = new ArrayList<ArrayList<Entry<Integer, E>>>(3);

  {
    lstElements.add(new ArrayList<Entry<Integer, E>>());
    lstElements.add(new ArrayList<Entry<Integer, E>>());
    lstElements.add(new ArrayList<Entry<Integer, E>>());
  }

  public SFormsFastMapOld() {
  }

  public SFormsFastMapOld(SFormsFastMapOld<E> map) {
    for (int i = 2; i >= 0; i--) {
      lstElements.set(i, new ArrayList<Entry<Integer, E>>(map.lstElements.get(i)));
    }
  }

  public int size() {
    int size = 0;
    for (int i = 2; i >= 0; i--) {
      size += lstElements.get(i).size();
    }
    return size;
  }

  public boolean isEmpty() {

    for (int i = 2; i >= 0; i--) {
      if (!lstElements.get(i).isEmpty()) {
        return false;
      }
    }

    return true;
  }

  public void put(int key, E value) {
    putInternal(key, value, false);
  }

  public void remove(int key) {
    putInternal(key, null, true);
  }

  public void removeAllFields() {
    lstElements.get(2).clear();
  }

  public void putInternal(final int key, final E value, boolean remove) {

    int index = 0;
    int ikey = key;
    if (ikey < 0) {
      index = 2;
      ikey = -ikey;
    }
    else if (ikey >= VarExprent.STACK_BASE) {
      index = 1;
      ikey -= VarExprent.STACK_BASE;
    }

    ArrayList<Entry<Integer, E>> lst = lstElements.get(index);
    if (ikey >= lst.size()) {
      if (remove) {
        return;
      }
      else {
        ensureCapacity(lst, ikey + 1, false);
      }
    }

    lst.set(ikey, value == null ? null : new Entry<Integer, E>() {

      private Integer var = key;
      private E val = value;

      public Integer getKey() {
        return var;
      }

      public E getValue() {
        return val;
      }

      public E setValue(E newvalue) {
        val = newvalue;
        return null;
      }
    });
  }

  public boolean containsKey(int key) {
    return get(key) != null;
  }

  public E get(int key) {

    int index = 0;
    if (key < 0) {
      index = 2;
      key = -key;
    }
    else if (key >= VarExprent.STACK_BASE) {
      index = 1;
      key -= VarExprent.STACK_BASE;
    }

    ArrayList<Entry<Integer, E>> lst = lstElements.get(index);

    Entry<Integer, E> ent;
    if (key < lst.size() && (ent = lst.get(key)) != null) {
      return ent.getValue();
    }
    return null;
  }

  public void union(SFormsFastMapOld<E> map, IElementsUnion<E> union) {

    for (int i = 2; i >= 0; i--) {
      ArrayList<Entry<Integer, E>> lstOwn = lstElements.get(i);
      ArrayList<Entry<Integer, E>> lstExtern = map.lstElements.get(i);

      int ownsize = lstOwn.size();
      int externsize = lstExtern.size();

      int minsize = ownsize > externsize ? externsize : ownsize;

      for (int j = minsize - 1; j >= 0; j--) {
        Entry<Integer, E> second = lstExtern.get(j);

        if (second != null) {
          Entry<Integer, E> first = lstOwn.get(j);

          if (first == null) {
            putInternal(second.getKey(), union.copy(second.getValue()), false);
          }
          else {
            first.setValue(union.union(first.getValue(), second.getValue()));
          }
        }
      }

      if (externsize > minsize) {
        //				ensureCapacity(lstOwn, externsize, true);
        //				lstOwn.addAll(lstExtern.subList(minsize, externsize));

        for (int j = minsize; j < externsize; j++) {
          Entry<Integer, E> second = lstExtern.get(j);
          //					if(first != null) {
          //						first.setValue(union.copy(first.getValue()));
          //					}

          if (second != null) {
            putInternal(second.getKey(), union.copy(second.getValue()), false);
          }
          //					lstOwn.add(lstExtern.get(j));
        }
      }
    }
  }

  public List<Entry<Integer, E>> entryList() {
    List<Entry<Integer, E>> list = new ArrayList<Entry<Integer, E>>();

    for (int i = 2; i >= 0; i--) {
      for (Entry<Integer, E> ent : lstElements.get(i)) {
        if (ent != null) {
          list.add(ent);
        }
      }
    }

    return list;
  }

  //	public SFormsFastMapIterator iterator() {
  //		return new SFormsFastMapIterator();
  //	}

  private void ensureCapacity(ArrayList<Entry<Integer, E>> lst, int size, boolean exact) {

    if (!exact) {
      int minsize = 2 * lst.size() / 3 + 1;
      if (minsize > size) {
        size = minsize;
      }
    }

    lst.ensureCapacity(size);
    for (int i = size - lst.size(); i > 0; i--) {
      lst.add(null);
    }
  }

  public interface IElementsUnion<E> {
    E union(E first, E second);

    E copy(E element);
  }

  //	public class SFormsFastMapIterator implements Iterator<Entry<Integer, E>> {
  //
  //		private int[] pointer = new int[]{0, -1};
  //		private int[] next_pointer = null;
  //
  //		private int[] getNextIndex(int list, int index) {
  //
  //			while(list < 3) {
  //				ArrayList<E> lst = lstElements.get(list);
  //
  //				while(++index < lst.size()) {
  //					E element = lst.get(index);
  //					if(element != null) {
  //						return new int[] {list, index};
  //					}
  //				}
  //
  //				index = -1;
  //				list++;
  //			}
  //
  //			return null;
  //		}
  //
  //		public boolean hasNext() {
  //			next_pointer = getNextIndex(pointer[0], pointer[1]);
  //			return (next_pointer != null);
  //		}
  //
  //		public Entry<Integer, E> next() {
  //			if(next_pointer != null) {
  //				pointer = next_pointer;
  //			} else {
  //				int[] nextpointer = getNextIndex(pointer[0], pointer[1]);
  //				if(nextpointer != null) {
  //					pointer = nextpointer;
  //				} else {
  //					return null;
  //				}
  //			}
  //
  //			next_pointer = null;
  //
  //			return new Entry<Integer, E>() {
  //				public Integer getKey() {
  //					return null;
  //				}
  //
  //				public E getValue() {
  //					return null;
  //				}
  //
  //				public E setValue(E value) {
  //					throw new RuntimeException("not implemented!");
  //				}
  //			};
  //			//lstElements.get(pointer[0]).get(pointer[1]);
  //		}
  //
  //		public void remove() {
  //			throw new RuntimeException("not implemented!");
  //		}
  //
  //	}
}