Menu

[r11]: / trunk / SWFInvestigator / src / decompiler / swfdump / util / IntMap.as  Maximize  Restore  History

Download this file

213 lines (190 with data), 4.3 kB

  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
package decompiler.swfdump.util
{
public class IntMap
{
private var keys:Vector.<int>;
private var values:Array;
private var mapSize:int;
public function IntMap(capacity:int = 10)
{
keys = new Vector.<int>(capacity);
values = new Array(capacity);
}
/**
* An approximation of Java's System.arraycopy
*/
private function arrayCopy (a1:*, s1:int, a2:*, s2:int, size:int):void {
var temp:Array;
var i:int;
for (i=s1; i< s1 + size; i++) {
temp[i-s1] = a1[i];
}
for (i=0; i < size; i++) {
a2[s2 + i] = temp[i];
}
}
public function capacity():int
{
return keys.length;
}
private function find(k:int):int
{
var lo:int = 0;
var hi:int = this.mapSize-1;
while (lo <= hi)
{
var i:int = (lo + hi)/2;
var m:int = keys[i];
if (k > m)
lo = i + 1;
else if (k < m)
hi = i - 1;
else
return i; // key found
}
return -(lo + 1); // key not found, low is the insertion point
}
public function remove(k:int):*
{
var old:* = null;
var i:int = find(k);
if (i >= 0)
{
old = values[i];
arrayCopy(keys, i+1, keys, i, this.mapSize-i-1);
arrayCopy(values, i+1, values, i, this.mapSize-i-1);
this.mapSize--;
}
return old;
}
public function clear():void
{
this.mapSize = 0;
}
public function put(k:int, v:*):*
{
if (this.mapSize == 0 || k > keys[this.mapSize-1])
{
if (this.mapSize == keys.length)
grow();
keys[this.mapSize] = k;
values[this.mapSize] = v;
this.mapSize++;
return null;
}
else
{
var i:int = find(k);
if (i >= 0)
{
var old:* = values[i];
values[i] = v;
return old;
}
else
{
i = -i - 1; // recover the insertion point
if (this.mapSize == keys.length)
grow();
arrayCopy(keys,i,keys,i+1,this.mapSize-i);
arrayCopy(values,i,values,i+1,this.mapSize-i);
keys[i] = k;
values[i] = v;
this.mapSize++;
return null;
}
}
}
private function grow():void
{
var newkeys:Vector.<int> = new Vector.<int>(this.mapSize*2);
arrayCopy(keys,0,newkeys,0,this.mapSize);
keys = newkeys;
var newvalues:Array = new Array(this.mapSize*2);
arrayCopy(values,0,newvalues,0,this.mapSize);
values = newvalues;
}
public function get(k:int):*
{
var i:int = find(k);
return i >= 0 ? values[i] : null;
}
public function contains(k:int):Boolean
{
return find(k) >= 0;
}
/**
* A bit of an aberration from an academic point of view,
* but since this is an ordered Map, why not!
*
* @return the element immediately following element k.
*/
public function getNextAdjacent(k:int):*
{
var i:int = find(k);
return ( (i >= 0) && (i+1 < this.mapSize) ) ? values[i+1] : null;
}
/**
public Iterator iterator()
{
return new Iterator()
{
private int i = 0;
public boolean hasNext()
{
return i < size;
}
public Object next()
{
if (i >= size)
{
throw new NoSuchElementException();
}
final int j = i++;
return new Map.Entry()
{
public Object getKey()
{
return new Integer(keys[j]);
}
public Object getValue()
{
return values[j];
}
public Object setValue(Object value)
{
Object old = values[j];
values[j] = value;
return old;
}
};
}
public void remove()
{
arrayCopy(keys, i, keys, i-1, size-i);
arrayCopy(values, i, values, i-1, size-i);
size--;
}
};
}
*/
public function size():int
{
return this.mapSize;
}
/**
* @param ar must be of size size().
*/
public function valuesToArray(ar:Vector.<Object>):Vector.<Object>
{
arrayCopy(values, 0, ar, 0, this.mapSize);
return ar;
}
public function keySetToArray():Vector.<int>
{
var ar:Vector.<int> = new Vector.<int>(this.size());
arrayCopy(keys, 0, ar, 0, this.mapSize);
return ar;
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.