summaryrefslogtreecommitdiff
path: root/chapter17/src/main/java/com/seaofnodes/simple/Utils.java
blob: 1346c3eaab320235dec4c85b9ec440ed85a173de (plain)
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
package com.seaofnodes.simple;

import java.util.ArrayList;

public class Utils {
    public static RuntimeException TODO() { return TODO("Not yet implemented"); }
    public static RuntimeException TODO(String msg) { return new RuntimeException(msg); }

    /**
     *  Fast, constant-time, element removal.  Does not preserve order
     *
     *  @param array ArrayList to modify
     *  @param i element to be removed
     *  @return element removed
     */
    public static <E> void del(ArrayList<E> array, int i) {
        E last = array.removeLast();
        if (i < array.size()) array.set(i, last);
    }

    /**
     * Search a list for an element by reference
     *
     * @param ary List to search in
     * @param x Object to be searched
     * @return >= 0 on success, -1 on failure
     */
    public static <E> int find( ArrayList<E> ary, E x ) {
        for( int i=0; i<ary.size(); i++ )
            if( ary.get(i)==x )
                return i;
        return -1;
    }
    /**
     * Search a list for an element by reference
     *
     * @param ary List to search in
     * @param x Object to be searched
     * @return >= 0 on success, -1 on failure
     */
    public static <E> int find( E[] ary, E x ) {
        for( int i=0; i<ary.length; i++ )
            if( ary[i]==x )
                return i;
        return -1;
    }

    // Rotate a long, nice for hashes
    public static long rot( long x, int n ) { return (x<<n) | (x>>>n); }

    // Fold a 64bit hash into 32 bits
    public static int fold( long x ) { return (int)((x>>32) ^ x); }
}