pastebin

Paste multiset, #vdE -- näytä pelkkänä tekstinä -- uusi tämän pohjalta

Värjäys: Tyyli: ensimmäinen rivinumero: Tabin korvaus:

 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
import java.util.TreeSet;

/**
 *
 * @author ilari
 */
public class MultiSet {
    
    public static void main(String[] args) {
        IO io = new IO();
        
        TreeSet<UniqueInteger> set = new TreeSet<>();
        set.add(new UniqueInteger(2));
        set.add(new UniqueInteger(2));
        set.add(new UniqueInteger(3));
        set.add(new UniqueInteger(1));
        
        io.println("floor(2)=" + set.floor(new UniqueInteger(2)).getArvo());
        set.remove(set.floor(new UniqueInteger(2)));
        io.println("floor(2)=" + set.floor(new UniqueInteger(2)).getArvo());
        set.remove(set.floor(new UniqueInteger(2)));
        io.println("floor(2)=" + set.floor(new UniqueInteger(2)).getArvo());
        
        io.close();
    }
    
    public static boolean contains(TreeSet<UniqueInteger> set, int arvo) {
        UniqueInteger floor = set.floor(new UniqueInteger(arvo));
        if (floor == null) {
            return false;
        }
        return floor.getArvo() == arvo;
    }
}

class UniqueInteger implements Comparable<UniqueInteger> {
        
        static int counter = 0;
    
        private int id;
        private int arvo;
        
        public UniqueInteger(int arvo) {
            this.id = counter++;
            this.arvo = arvo;
        }
        
        public int getArvo() {
            return arvo;
        }
        
        @Override
        public int compareTo(UniqueInteger other) {
            if (other.getArvo() == this.arvo) {
                return this.id - other.id;
            }
            return this.arvo - other.arvo;
        }
    }