forked from ivanhk/fastText_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
148 lines (131 loc) · 3.79 KB
/
Copy pathUtils.java
File metadata and controls
148 lines (131 loc) · 3.79 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
package fasttext;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Random;
import java.util.RandomAccess;
public class Utils {
/**
* Ensures the truth of an expression involving one or more parameters to
* the calling method.
*
* @param expression
* a boolean expression
* @throws IllegalArgumentException
* if {@code expression} is false
*/
public static void checkArgument(boolean expression) {
if (!expression) {
throw new IllegalArgumentException();
}
}
public static void checkArgument(boolean expression, String message) {
if (!expression) {
throw new IllegalArgumentException(message);
}
}
public static boolean isEmpty(String str) {
return (str == null || str.isEmpty());
}
public static <K, V> V mapGetOrDefault(Map<K, V> map, K key, V defaultValue) {
return map.containsKey(key) ? map.get(key) : defaultValue;
}
public static int randomInt(Random rnd, int lower, int upper) {
checkArgument(lower <= upper & lower > 0);
if (lower == upper) {
return lower;
}
return rnd.nextInt(upper - lower) + lower;
}
public static float randomFloat(Random rnd, float lower, float upper) {
checkArgument(lower <= upper);
if (lower == upper) {
return lower;
}
return (rnd.nextFloat() * (upper - lower)) + lower;
}
public static long sizeLine(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
long count = 0;
int readChars = 0;
boolean endsWithoutNewLine = false;
while ((readChars = is.read(c)) != -1) {
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n')
++count;
}
endsWithoutNewLine = (c[readChars - 1] != '\n');
}
if (endsWithoutNewLine) {
++count;
}
return count;
} finally {
is.close();
}
}
/**
*
* @param br
* @param pos
* line numbers start from 1
* @throws IOException
*/
public static void seekLine(BufferedReader br, long pos) throws IOException {
// br.reset();
String line;
int currentLine = 1;
while (currentLine < pos && (line = br.readLine()) != null) {
if (Utils.isEmpty(line) || line.startsWith("#")) {
continue;
}
currentLine++;
}
}
private static final int SHUFFLE_THRESHOLD = 5;
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void shuffle(List<?> list, Random rnd) {
int size = list.size();
if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
for (int i = size; i > 1; i--)
swap(list, i - 1, rnd.nextInt(i));
} else {
Object arr[] = list.toArray();
// Shuffle array
for (int i = size; i > 1; i--)
swap(arr, i - 1, rnd.nextInt(i));
// Dump array back into list
// instead of using a raw type here, it's possible to capture
// the wildcard but it will require a call to a supplementary
// private method
ListIterator it = list.listIterator();
for (int i = 0; i < arr.length; i++) {
it.next();
it.set(arr[i]);
}
}
}
/**
* Swaps the two specified elements in the specified array.
*/
public static void swap(Object[] arr, int i, int j) {
Object tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void swap(List<?> list, int i, int j) {
// instead of using a raw type here, it's possible to capture
// the wildcard but it will require a call to a supplementary
// private method
final List l = list;
l.set(i, l.set(j, l.get(i)));
}
}