a b/network/ST.java
1
package network;
2
import java.util.Collections;
3
import java.util.HashSet;
4
import java.util.Iterator;
5
import java.util.NoSuchElementException;
6
import java.util.Set;
7
import java.util.TreeMap;
8
9
public class ST<Key extends Comparable<Key>, Value> implements Iterable<Key> {
10
11
    private TreeMap<Key, Value> st;
12
13
    /**
14
     * Initializes an empty symbol table.
15
     */
16
    public ST() {
17
        st = new TreeMap<Key, Value>();
18
        Collections.synchronizedSortedMap(st);
19
    }
20
21
22
    /**
23
     * Returns the value associated with the given key in this symbol table.
24
     *
25
     * @param  key the key
26
     * @return the value associated with the given key if the key is in this symbol table;
27
     *         <tt>null</tt> if the key is not in this symbol table
28
     * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>
29
     */
30
    public Set<Key> getKeys() {
31
        return st.keySet();
32
    }
33
    
34
    
35
    public Value get(Key key) {
36
        if (key == null) throw new NullPointerException("called get() with null key");
37
        return st.get(key);
38
    }
39
40
    /**
41
     * Inserts the specified key-value pair into the symbol table, overwriting the old 
42
     * value with the new value if the symbol table already contains the specified key.
43
     * Deletes the specified key (and its associated value) from this symbol table
44
     * if the specified value is <tt>null</tt>.
45
     *
46
     * @param  key the key
47
     * @param  val the value
48
     * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>
49
     */
50
    public void put(Key key, Value val) {
51
        if (key == null) throw new NullPointerException("called put() with null key");
52
        if (val == null) st.remove(key);
53
        else             st.put(key, val);
54
    }
55
56
    /**
57
     * Removes the specified key and its associated value from this symbol table     
58
     * (if the key is in this symbol table).
59
     *
60
     * @param  key the key
61
     * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>
62
     */
63
    public void delete(Key key) {
64
        if (key == null) throw new NullPointerException("called delete() with null key");
65
        st.remove(key);
66
    }
67
68
    /**
69
     * Returns true if this symbol table contain the given key.
70
     *
71
     * @param  key the key
72
     * @return <tt>true</tt> if this symbol table contains <tt>key</tt> and
73
     *         <tt>false</tt> otherwise
74
     * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>
75
     */
76
    public boolean contains(Key key) {
77
        if (key == null) throw new NullPointerException("called contains() with null key");
78
        return st.containsKey(key);
79
    }
80
81
    /**
82
     * Returns the number of key-value pairs in this symbol table.
83
     *
84
     * @return the number of key-value pairs in this symbol table
85
     */
86
    public int size() {
87
        return st.size();
88
    }
89
90
    /**
91
     * Returns true if this symbol table is empty.
92
     *
93
     * @return <tt>true</tt> if this symbol table is empty and <tt>false</tt> otherwise
94
     */
95
    public boolean isEmpty() {
96
        return size() == 0;
97
    }
98
99
    /**
100
     * Returns all keys in this symbol table.
101
     * <p>
102
     * To iterate over all of the keys in the symbol table named <tt>st</tt>,
103
     * use the foreach notation: <tt>for (Key key : st.keys())</tt>.
104
     *
105
     * @return all keys in this symbol table
106
     */
107
    public Iterable<Key> keys() {
108
        return st.keySet();
109
    }
110
111
    /**
112
     * Returns all of the keys in this symbol table.
113
     * To iterate over all of the keys in a symbol table named <tt>st</tt>, use the
114
     * foreach notation: <tt>for (Key key : st)</tt>.
115
     * <p>
116
     * This method is provided for backward compatibility with the version from
117
     * <em>Introduction to Programming in Java: An Interdisciplinary Approach.</em>
118
     *
119
     * @return     an iterator to all of the keys in this symbol table
120
     * @deprecated Replaced by {@link #keys()}.
121
     */
122
    public Iterator<Key> iterator() {
123
        return st.keySet().iterator();
124
    }
125
126
    /**
127
     * Returns the smallest key in this symbol table.
128
     *
129
     * @return the smallest key in this symbol table
130
     * @throws NoSuchElementException if this symbol table is empty
131
     */
132
    public Key min() {
133
        if (isEmpty()) throw new NoSuchElementException("called min() with empty symbol table");
134
        return st.firstKey();
135
    }
136
137
    /**
138
     * Returns the largest key in this symbol table.
139
     *
140
     * @return the largest key in this symbol table
141
     * @throws NoSuchElementException if this symbol table is empty
142
     */
143
    public Key max() {
144
        if (isEmpty()) throw new NoSuchElementException("called max() with empty symbol table");
145
        return st.lastKey();
146
    }
147
148
    /**
149
     * Returns the smallest key in this symbol table greater than or equal to <tt>key</tt>.
150
     *
151
     * @param  key the key
152
     * @return the smallest key in this symbol table greater than or equal to <tt>key</tt>
153
     * @throws NoSuchElementException if there is no such key
154
     * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>
155
     */
156
    public Key ceiling(Key key) {
157
        if (key == null) throw new NullPointerException("called ceiling() with null key");
158
        Key k = st.ceilingKey(key);
159
        if (k == null) throw new NoSuchElementException("all keys are less than " + key);
160
        return k;
161
    }
162
163
    /**
164
     * Returns the largest key in this symbol table less than or equal to <tt>key</tt>.
165
     *
166
     * @param  key the key
167
     * @return the largest key in this symbol table less than or equal to <tt>key</tt>
168
     * @throws NoSuchElementException if there is no such key
169
     * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>
170
     */
171
    public Key floor(Key key) {
172
        if (key == null) throw new NullPointerException("called floor() with null key");
173
        Key k = st.floorKey(key);
174
        if (k == null) throw new NoSuchElementException("all keys are greater than " + key);
175
        return k;
176
    }
177
178
}
179