8000 给部分方法添加有界通配符。 · sswssx/javaalgorithm@aeb79ea · GitHub
[go: up one dir, main page]

Skip to content

Commit aeb79ea

Browse files
author
shengshijun
committed
给部分方法添加有界通配符。
1 parent d70b5e9 commit aeb79ea

File tree

19 files changed

+58
-57
lines changed

19 files changed

+58
-57
lines changed

src/main/java/ssj/algorithm/ArrayUtil.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ public static <T> void reverse(T[] source, int start, int end) {
9292
}
9393
}
9494

95-
public static <T extends Comparable<T>> void sort(T[] arr) {
95+
public static <T extends Comparable<? super T>> void sort(T[] arr) {
9696
sort(Comparable::compareTo, arr, 0, arr.length - 1);
9797
}
9898

99-
public static <T> void sort(T[] arr, Comparator<T> comparator) {
99+
public static <T> void sort(T[] arr, Comparator<? super T> comparator) {
100100
sort(comparator, arr, 0, arr.length - 1);
101101
}
102102

103-
private static <T> void sort(Comparator<T> comparator, T[] arr, int start, int end) {
103+
private static <T> void sort(Comparator<? super T> comparator, T[] arr, int start, int end) {
104104
Preconditions.checkNotNull(arr, "arr should not be null");
105105
Preconditions.checkNotNull(comparator, "comparator should not be null");
106106
int length = end - start + 1;
@@ -113,11 +113,11 @@ private static <T> void sort(Comparator<T> comparator, T[] arr, int start, int e
113113
}
114114

115115

116-
private static <T extends Comparable<T>> int partition(T[] arr, int start, int end) {
116+
private static <T extends Comparable<? super T>> int partition(T[] arr, int start, int end) {
117117
return partition(Comparable::compareTo, arr, start, end);
118118
}
119119

120-
public static <T> int partition(Comparator<T> comparator, T[] arr, int start, int end) {
120+
public static <T> int partition(Comparator<? super T> comparator, T[] arr, int start, int end) {
121121
int par_index = MathUtil.randInt(start, end);
122122
ArrayUtil.swap(arr, start, par_index);
123123
par_index = start;
@@ -140,7 +140,7 @@ public static <T> int partition(Comparator<T> comparator, T[] arr, int start, in
140140
* @param <T>
141141
* @return
142142
*/
143-
public static <T extends Comparable<T>> T select(T[] arr, int k) {
143+
public static <T extends Comparable<? super T>> T select(T[] arr, int k) {
144144
Preconditions.checkNotNull(arr);
145145
Preconditions.checkPositionIndex(k, arr.length);
146146
int low = 0;
@@ -166,7 +166,7 @@ public static <T extends Comparable<T>> T select(T[] arr, int k) {
166166
* @param <T>
167167
* @return
168168
*/
169-
public <T extends Comparable<T>> int binarySearch(T[] arr, T ele) {
169+
public <T extends Comparable<? super T>> int binarySearch(T[] arr, T ele) {
170170
Preconditions.checkNotNull(arr);
171171
Preconditions.checkNotNull(ele);
172172
int low = 0;
@@ -192,13 +192,13 @@ public <T extends Comparable<T>> int binarySearch(T[] arr, T ele) {
192192
* @param comparator
193193
* @param <T>
194194
*/
195-
public static <T> void mergeSort(T[] arr, Comparator<T> comparator) {
195+
public static <T> void mergeSort(T[] arr, Comparator<? super T> comparator) {
196196
Preconditions.checkNotNull(arr);
197197
Preconditions.checkNotNull(comparator);
198198
mergeSort(arr, comparator, 0, arr.length - 1);
199199
}
200200

201-
private static <T> void mergeSort(T[] arr, Comparator<T> comparator, int start, int end) {
201+
private static <T> void mergeSort(T[] arr, Comparator<? super T> comparator, int start, int end) {
202202
if (start < end) {
203203
int middle = (end + start) / 2;
204204
mergeSort(arr, comparator, start, middle);
@@ -207,10 +207,11 @@ private static <T> void mergeSort(T[] arr, Comparator<T> comparator, int start,
207207
}
208208
}
209209

210-
private static <T> void merge(T[] arr, int start, int middle, int end, Comparator<T> comparator) {
210+
private static <T> void merge(T[] arr, int start, int middle, int end, Comparator<? super T> comparator) {
211211
int front_start = start;
212212
int end_start = middle + 1;
213213
int cur_pos = 0;
214+
@SuppressWarnings("unchecked")
214215
T[] helper = (T[]) new Object[end - start + 1];
215216
while (front_start <= middle && end_start <= end) {
216217
if (comparator.compare(arr[front_start], arr[end_start]) <= 0) {
@@ -256,7 +257,7 @@ public static void countSort(int[] arr, int start, int end) {
256257
System.arraycopy(result, 0, arr, 0, arr.length);
257258
}
258259

259-
public static <T> void radixSort(T[] arr, Comparator<T> comparator) {
260+
public static <T> void radixSort(T[] arr, Comparator<? extends T> comparator) {
260261
//todo 完成基数排序
261262
}
262263

@@ -310,7 +311,7 @@ public static int[] bitMapSort(int[] un_sort_list) {
310311
* @param <T>
311312
* @return
312313
*/
313-
public static <T extends Comparable<T>> T minInRotate(T[] arr) {
314+
public static <T extends Comparable<? super T>> T minInRotate(T[] arr) {
314315
Preconditions.checkNotNull(arr);
315316
if (arr.length == 0) {
316317
return null;
@@ -336,13 +337,13 @@ public static <T extends Comparable<T>> T minInRotate(T[] arr) {
336337
return arr[mid];
337338
}
338339

339-
public static <T> T[] group(T[] arr, Function<T, Integer> func) {
340+
public static <T> T[] group(T[] arr, Function<? super T, ? extends Integer> func) {
340341
Preconditions.checkNotNull(arr);
341342
Preconditions.checkNotNull(func);
342343
TreeMap<Integer, HashSet<T>> static_map = new TreeMap<>();
343344
for (T ele : arr) {
344-
Preconditions.checkNotNull(ele);
345-
int group_num = func.apply(ele);
345+
Integer group_num = func.apply(ele);
346+
Preconditions.checkNotNull(group_num);
346347
HashSet<T> set = static_map.get(group_num);
347348
if (set == null) {
348349
set = new HashSet<>();
@@ -452,7 +453,7 @@ private static <T> boolean checkMoreThanHalf(T[] arr, T most_ele) {
452453
* @param <T>
453454
* @return
454455
*/
455-
public static <T extends Comparable<T>> int countEle(T[] arr, T ele) {
456+
public static <T extends Comparable<? super T>> int countEle(T[] arr, T ele) {
456457
Preconditions.checkNotNull(arr);
457458
Preconditions.checkNotNull(ele);
458459
int start = getFirstEle(arr, ele);
@@ -468,7 +469,7 @@ public static <T extends Comparable<T>> int countEle(T[] arr, T ele) {
468469
* @param <T>
469470
* @return
470471
*/
471-
public static <T extends Comparable<T>> int getFirstEle(T[] arr, T ele) {
472+
public static <T extends Comparable<? super T>> int getFirstEle(T[] arr, T ele) {
472473
Preconditions.checkNotNull(arr);
473474
Preconditions.checkNotNull(ele);
474475
int start = 0;
@@ -499,7 +500,7 @@ public static <T extends Comparable<T>> int getFirstEle(T[] arr, T ele) {
499500
* @param <T>
500501
* @return
501502
*/
502-
public static <T extends Comparable<T>> int getLastEle(T[] arr, T ele) {
503+
public static <T extends Comparable<? super T>> int getLastEle(T[] arr, T ele) {
503504
Preconditions.checkNotNull(arr);
504505
Preconditions.checkNotNull(ele);
505506
int start = 0;

src/main/java/ssj/algorithm/Collection.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public interface Collection<T> extends Iterable<T>, Cloneable {
1515
public void add(T ele);
1616

17-
public default <R> Collection<R> map(Function<T, R> func) {
17+
public default <R> Collection<R> map(Function<? super T, ? extends R> func) {
1818
Preconditions.checkNotNull(func);
1919
Collection<R> new_collection = newWithCapacity(size());
2020
for (T ele : this) {
@@ -23,7 +23,7 @@ public default <R> Collection<R> map(Function<T, R> func) {
2323
return new_collection;
2424
}
2525

26-
public default Collection<T> filter(Predicate<T> is_func) {
26+
public default Collection<T> filter(Predicate<? super T> is_func) {
2727
Preconditions.checkNotNull(is_func);
2828
Collection<T> new_collection = this.newWithZero();
2929
for (T ele : this) {
@@ -34,7 +34,7 @@ public default Collection<T> filter(Predicate<T> is_func) {
3434
return new_collection;
3535
}
3636

37-
public default <R> R reduce(BiFunction<T, R, R> func, R start) {
37+
public default <R> R reduce(BiFunction<? super T, ? super R, ? extends R> func, R start) {
3838
Preconditions.checkNotNull(func);
3939
Preconditions.checkNotNull(start);
4040
R new_start = start;
@@ -85,7 +85,7 @@ public default Vector<T> toVector() {
8585
return result;
8686
}
8787

88-
public default Vector<T> sort(Comparator<T> comparator) {
88+
public default Vector<T> sort(Comparator<? super T> comparator) {
8989
return toVector().sortThis(comparator);
9090
}
9191
}

src/main/java/ssj/algorithm/Graph.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ public interface Graph<T> {
1515

1616
Iterator<T> nodes();
1717

18-
void dfs(Consumer<T> func);
18+
void dfs(Consumer<? super T> func);
1919

20-
<R> Vector<R> dfs(Function<T, R> func);
20+
<R> Vector<R> dfs(Function<? super T, ? extends R> func);
2121

22-
void bfs(Consumer<T> func);
22+
void bfs(Consumer<? super T> func);
2323

24-
<R> Vector<R> bfs(Function<T, R> func);
24+
<R> Vector<R> bfs(Function<? super T, ? extends R> func);
2525

2626
Vector<T> next(T ele);
2727

28-
void topologicalSort(Consumer<T> func);
28+
void topologicalSort(Consumer<? super T> func);
2929

30-
<R> Vector<R> topologicalSort(Function<T, R> func);
30+
<R> Vector<R> topologicalSort(Function<? super T, ? extends R> func);
3131

3232
int size();
3333

src/main/java/ssj/algorithm/List.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public default T getReverse(int index) {
5252
return get(normal_index);
5353
}
5454

55-
List<T> partition(T par_ele, Comparator<T> comparator);
55+
List<T> partition(T par_ele, Comparator<? super T> comparator);
5656

5757
public default Set<T> chainEle() {
5858
HashSet<T> result = new HashSet<>();

src/main/java/ssj/algorithm/SearchTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Created by shenshijun on 15/2/5.
99
*/
10-
public interface SearchTree<T extends Comparable<T>> extends Iterable<T> {
10+
public interface SearchTree<T extends Comparable<? super T>> extends Iterable<T> {
1111
void add(T ele);
1212

1313
default void addAll(Iterable<? extends T> iter) {

src/main/java/ssj/algorithm/Stack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public default boolean isEmpty() {
2020

2121
T head();
2222

23-
public default Stack<T> sortStack(Comparator<T> comparator) {
23+
public default Stack<T> sortStack(Comparator<? super T> comparator) {
2424
LinkedStack<T> result = new LinkedStack<>();
2525
while (!isEmpty()) {
2626
T cur_ele = pop();

src/main/java/ssj/algorithm/collections/AVLTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Created by shenshijun on 15/2/5.
1414
*/
15-
public class AVLTree<T extends Comparable<T>> implements SearchTree<T> {
15+
public class AVLTree<T extends Comparable<? super T>> implements SearchTree<T> {
1616
private Node _head;
1717
private int _size;
1818

src/main/java/ssj/algorithm/collections/BTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Created by shenshijun on 15/2/5.
99
*/
10-
public class BTree<T extends Comparable<T>> implements SearchTree<T> {
10+
public class BTree<T extends Comparable<? super T>> implements SearchTree<T> {
1111
//TODO 完成B树
1212
@Override
1313
public void add(T ele) {

src/main/java/ssj/algorithm/collections/ExtensiveStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Created by shenshijun on 15/2/4.
55
*/
6-
public class ExtensiveStack<T extends Comparable<T>> extends LinkedStack<T> {
6+
public class ExtensiveStack<T extends Comparable<? super T>> extends LinkedStack<T> {
77
private LinkedStack<T> _min_stack;
88
private LinkedStack<T> _max_stack;
99

src/main/java/ssj/algorithm/collections/LinkedList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void remove(int index) {
103103
}
104104

105105
@Override
106-
public List<T> partition(T par_ele, Comparator<T> comparator) {
106+
public List<T> partition(T par_ele, Comparator<? super T> comparator) {
107107
Preconditions.checkNotNull(par_ele);
108108
Preconditions.checkNotNull(comparator);
109109
Node less_node = _head.getNext();

src/main/java/ssj/algorithm/collections/PriorityQueue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* 使用最大堆实现优先队列
1111
* Created by shenshijun on 15/2/4.
1212
*/
13-
public class PriorityQueue<T extends Comparable<T>> implements Queue<T> {
13+
public class PriorityQueue<T extends Comparable<? super T>> implements Queue<T> {
1414
//使用归纳法可以证明_values肯定是满的,也就是说二叉堆是一个完全二叉树
1515
private Vector<T> _values;
1616

src/main/java/ssj/algorithm/collections/SkipList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Created by shenshijun on 15/2/5.
1414
*/
15-
public class SkipList<T extends Comparable<T>> implements Set<T> {
15+
public class SkipList<T extends Comparable<? super T>> implements Set<T> {
1616
private LinkedList<Node> values;
1717
private int size;
1818
private Random rand;

src/main/java/ssj/algorithm/collections/TreeMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* Created by shenshijun on 15/2/3.
1313
*/
14-
public class TreeMap<K extends Comparable<K>, V> implements Map<K, V> {
14+
public class TreeMap<K extends Comparable<? super K>, V> implements Map<K, V> {
1515
AVLTree<Node> _tree;
1616

1717
public TreeMap() {

src/main/java/ssj/algorithm/collections/TreeSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Created by shenshijun on 15/2/3.
1010
*/
11-
public class TreeSet<T extends Comparable<T>> implements Set<T> {
11+
public class TreeSet<T extends Comparable<? super T>> implements Set<T> {
1212

1313
//这行来自java.util.HashSet
1414
private static final Object PRESENT = new Object();

src/main/java/ssj/algorithm/collections/Vector.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public void remove(int index) {
5151
_cur_pointer--;
5252
}
5353

54-
public T binarySearch(T ele, Comparator<T> comparator) {
54+
public T binarySearch(T ele, Comparator<? super T> comparator) {
5555
return binarySearch(ele, 0, size() - 1, comparator);
5656
}
5757

58-
public T binarySearch(T ele, int from, int to, Comparator<T> comparator) {
58+
public T binarySearch(T ele, int from, int to, Comparator<? super T> comparator) {
5959
Preconditions.checkNotNull(ele);
6060
Preconditions.checkNotNull(comparator);
6161
@SuppressWarnings("unchecked")
@@ -67,7 +67,7 @@ public T binarySearch(T ele, int from, int to, Comparator<T> comparator) {
6767
}
6868

6969
@Override
70-
public List<T> partition(T par_ele, Comparator<T> comparator) {
70+
public List<T> partition(T par_ele, Comparator<? super T> comparator) {
7171
Preconditions.checkNotNull(par_ele);
7272
Preconditions.checkNotNull(comparator);
7373
int less_par = 0;
@@ -130,7 +130,7 @@ private int extendSize() {
130130
return (capacity() + 1) * 2;
131131
}
132132

133-
public Vector<T> sortThis(Comparator<T> comparator) {
133+
public Vector<T> sortThis(Comparator<? super T> comparator) {
134134
@SuppressWarnings("unchecked")
135135
T[] new_arr = (T[]) _values;
136136
ArrayUtil.sort(new_arr, comparator);

src/main/java/ssj/algorithm/math/MathUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static double max(double... vals) {
3535
return max_value;
3636
}
3737

38-
public static <T extends Comparable<T>> T max(T... vals) {
38+
public static <T extends Comparable<? super T>> T max(T... vals) {
3939
Preconditions.checkNotNull(vals, "vals should not be null");
4040
Preconditions.checkArgument(vals.length > 0, "vals should not be empty");
4141

@@ -87,7 +87,7 @@ private static boolean doubleEqual(double one, double two) {
8787
}
8888

8989
@SafeVarargs
90-
public static <T extends Comparable<T>> T min(T... arr) {
90+
public static <T extends Comparable<? super T>> T min(T... arr) {
9191
Preconditions.checkNotNull(arr);
9292
if (arr.length == 0) {
9393
return null;

src/main/java/ssj/algorithm/matrix/MatrixUtil.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ public static void clearZero(int[][] matrix) {
8686
* @param ele
8787
* @return
8888
*/
89-
public static <T extends Comparable<T>> Tuple2<Integer, Integer> matrixSearch(T[][] matrix, T ele) {
89+
public static <T extends Comparable<? super T>> Tuple2<Integer, Integer> matrixSearch(T[][] matrix, T ele) {
9090
return binarySearch(matrix, ele);
9191
}
9292

93-
static <T extends Comparable<T>> Tuple2<Integer, Integer> simpleMatrixSearch(T[][] matrix, T ele) {
93+
static <T extends Comparable<? super T>> Tuple2<Integer, Integer> simpleMatrixSearch(T[][] matrix, T ele) {
9494
Preconditions.checkNotNull(matrix);
9595
Preconditions.checkNotNull(ele);
9696
if (matrix.length == 0) {
@@ -112,7 +112,7 @@ static <T extends Comparable<T>> Tuple2<Integer, Integer> simpleMatrixSearch(T[]
112112
return null;
113113
}
114114

115-
private static <T extends Comparable<T>> Tuple2<Integer, Integer> binaryMatrixSearch(T[][] matrix, Point src, Point dest, T ele) {
115+
private static <T extends Comparable<? super T>> Tuple2<Integer, Integer> binaryMatrixSearch(T[][] matrix, Point src, Point dest, T ele) {
116116

117117
if (dest.compareTo(src) < 0) {
118118
return null;
@@ -181,7 +181,7 @@ private static <T extends Comparable<T>> Tuple2<Integer, Integer> binaryMatrixSe
181181
return null;
182182
}
183183

184-
public static <T extends Comparable<T>> int matrixRowBinarySearch(T[][] matrix, int col_start, int col_end, int row, T ele) {
184+
public static <T extends Comparable<? super T>> int matrixRowBinarySearch(T[][] matrix, int col_start, int col_end, int row, T ele) {
185185
Preconditions.checkNotNull(matrix);
186186
Preconditions.checkNotNull(ele);
187187
if (matrix.length == 0) return -1;
@@ -202,7 +202,7 @@ else if (com_res > 0) {
202202
return -1;
203203
}
204204

205-
static <T extends Comparable<T>> Tuple2<Integer, Integer> binarySearch(T[][] matrix, T ele) {
205+
static <T extends Comparable<? super T>> Tuple2<Integer, Integer> binarySearch(T[][] matrix, T ele) {
206206
Preconditions.checkNotNull(matrix);
207207
Preconditions.checkNotNull(ele);
208208
if (matrix.length == 0) {

0 commit comments

Comments
 (0)
0