MCPcopy Create free account
hub / github.com/SeaOfNodes/Simple / Utils

Class Utils

chapter20/src/main/java/com/seaofnodes/simple/Utils.java:5–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3import java.util.ArrayList;
4
5public class Utils {
6 public static RuntimeException TODO() { return TODO("Not yet implemented"); }
7 public static RuntimeException TODO(String msg) { return new RuntimeException(msg); }
8
9 /**
10 * Fast, constant-time, element removal. Does not preserve order
11 *
12 * @param array ArrayList to modify
13 * @param i element to be removed
14 * @return element removed
15 */
16 public static <E> void del(ArrayList<E> array, int i) {
17 E last = array.removeLast();
18 if (i < array.size()) array.set(i, last);
19 }
20
21 /**
22 * Search a list for an element by reference
23 *
24 * @param ary List to search in
25 * @param x Object to be searched
26 * @return >= 0 on success, -1 on failure
27 */
28 public static <E> int find( ArrayList<E> ary, E x ) {
29 for( int i=0; i<ary.size(); i++ )
30 if( ary.get(i)==x )
31 return i;
32 return -1;
33 }
34 /**
35 * Search a list for an element by reference
36 *
37 * @param ary List to search in
38 * @param x Object to be searched
39 * @return >= 0 on success, -1 on failure
40 */
41 public static <E> int find( E[] ary, E x ) {
42 for( int i=0; i<ary.length; i++ )
43 if( ary[i]==x )
44 return i;
45 return -1;
46 }
47
48 // Rotate a long, nice for hashes
49 public static long rot( long x, int n ) { return (x<<n) | (x>>>n); }
50
51 // Fold a 64bit hash into 32 bits
52 public static int fold( long x ) { return (int)((x>>32) ^ x); }
53}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected