-
Notifications
You must be signed in to change notification settings - Fork 946
/
LinkedCharArray.java
198 lines (166 loc) · 4.76 KB
/
LinkedCharArray.java
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
package org.nutz.lang.util;
import java.io.Serializable;
import java.util.ArrayList;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
public class LinkedCharArray implements Serializable {
private static final long serialVersionUID = 1L;
public LinkedCharArray() {
this(256);
}
public LinkedCharArray(int size) {
if (size <= 0)
Lang.makeThrow("width must >0!");
this.width = size;
cache = new ArrayList<char[]>();
}
public LinkedCharArray(String s) {
this(s.length());
char[] cs = s.toCharArray();
cache.add(cs);
cursor = cs.length;
}
private int offset;
private int cursor;
private int width;
private ArrayList<char[]> cache;
public LinkedCharArray push(int e) {
return push((char) e);
}
public LinkedCharArray push(char e) {
char[] array;
int row = cursor / width;
int i = cursor % width;
if (cache.size() == 0 || (cursor != offset && i == 0)) {
array = new char[width];
cache.add(array);
} else {
array = cache.get(row);
}
array[i] = e;
cursor++;
return this;
}
public LinkedCharArray push(String s) {
char[] cs = s.toCharArray();
for (char c : cs)
push(c);
return this;
}
public char popFirst() {
return innerGet(offset++);
}
public LinkedCharArray popFirst(int num) {
for (int i = 0; i < num; i++)
popFirst();
return this;
}
public char popLast() {
return innerGet(--cursor);
}
public LinkedCharArray popLast(int num) {
for (int i = 0; i < num; i++)
popLast();
return this;
}
public char first() {
if (size() == 0)
return (char) 0;
return innerGet(offset);
}
public char last() {
if (size() == 0)
return (char) 0;
return innerGet(cursor - 1);
}
public LinkedCharArray set(int index, char e) {
checkBound(index);
index += offset;
char[] array = cache.get(index / width);
array[index % width] = e;
return this;
}
private void checkBound(int index) {
if (index >= size() || index < 0)
throw new IndexOutOfBoundsException("Index: "
+ index
+ ", Size: "
+ size());
}
public LinkedCharArray clear() {
cache.clear();
cursor = 0;
offset = 0;
return this;
}
private char innerGet(int index) {
char[] array = cache.get(index / width);
return array[index % width];
}
public char get(int index) {
checkBound(index);
return innerGet(index + offset);
}
public boolean isEmpty() {
return 0 == cursor - offset;
}
public int size() {
return cursor - offset;
}
public boolean startsWith(String s) {
if (null == s)
return false;
if (s.length() > this.size())
return false;
return startsWith(s.toCharArray());
}
public boolean startsWith(char[] cs) {
if (null == cs)
return false;
for (int i = 0; i < cs.length; i++)
if (cs[i] != get(i))
return false;
return true;
}
public boolean endsWith(String s) {
if (null == s)
return false;
if (s.length() > this.size())
return false;
return endsWith(s.toCharArray());
}
public boolean endsWith(char[] cs) {
if (null == cs)
return false;
if (size() < cs.length)
return false;
int of = size() - cs.length;
for (int i = 0; i < cs.length; i++)
if (cs[i] != get(of + i))
return false;
return true;
}
public int[] toIntArray() {
int[] re = new int[size()];
for (int i = 0; i < re.length; i++)
re[i] = this.get(i);
return re;
}
public char[] toArray() {
char[] re = new char[size()];
for (int i = 0; i < re.length; i++)
re[i] = (char) this.get(i);
return re;
}
public String toString() {
return new String(toArray());
}
public String toTrimmed() {
return Strings.trim(toString());
}
public String popAll() {
String re = new String(toArray());
clear();
return re;
}
}