132
|
1 /*
|
|
2 File: LLPair.d
|
|
3
|
|
4 Originally written by Doug Lea and released into the public domain.
|
|
5 Thanks for the assistance and support of Sun Microsystems Labs, Agorics
|
|
6 Inc, Loral, and everyone contributing, testing, and using this code.
|
|
7
|
|
8 History:
|
|
9 Date Who What
|
|
10 24Sep95 dl@cs.oswego.edu Create from tango.util.collection.d working file
|
|
11
|
|
12 */
|
|
13
|
|
14
|
|
15 module tango.util.collection.impl.LLPair;
|
|
16
|
|
17 private import tango.util.collection.impl.LLCell;
|
|
18
|
|
19 private import tango.util.collection.model.Iterator;
|
|
20
|
|
21
|
|
22 /**
|
|
23 *
|
|
24 *
|
|
25 * LLPairs are LLCells with keys, and operations that deal with them.
|
|
26 * As with LLCells, the are pure implementation tools.
|
|
27 *
|
|
28 author: Doug Lea
|
|
29 * @version 0.93
|
|
30 *
|
|
31 * <P> For an introduction to this package see <A HREF="index.html"> Overview </A>.
|
|
32 **/
|
|
33
|
|
34 public class LLPair(K, T) : LLCell!(T)
|
|
35 {
|
|
36 alias LLCell!(T).find find;
|
|
37 alias LLCell!(T).count count;
|
|
38 alias LLCell!(T).element element;
|
|
39
|
|
40
|
|
41 // instance variables
|
|
42
|
|
43 private K key_;
|
|
44
|
|
45 /**
|
|
46 * Make a cell with given key, elment, and next link
|
|
47 **/
|
|
48
|
|
49 public this (K k, T v, LLPair n)
|
|
50 {
|
|
51 super(v, n);
|
|
52 key_ = k;
|
|
53 }
|
|
54
|
|
55 /**
|
|
56 * Make a pair with given key and element, and null next link
|
|
57 **/
|
|
58
|
|
59 public this (K k, T v)
|
|
60 {
|
|
61 super(v, null);
|
|
62 key_ = k;
|
|
63 }
|
|
64
|
|
65 /**
|
|
66 * Make a pair with null key, elment, and next link
|
|
67 **/
|
|
68
|
|
69 public this ()
|
|
70 {
|
|
71 super(T.init, null);
|
|
72 key_ = K.init;
|
|
73 }
|
|
74
|
|
75 /**
|
|
76 * return the key
|
|
77 **/
|
|
78
|
|
79 public final K key()
|
|
80 {
|
|
81 return key_;
|
|
82 }
|
|
83
|
|
84 /**
|
|
85 * set the key
|
|
86 **/
|
|
87
|
|
88 public final void key(K k)
|
|
89 {
|
|
90 key_ = k;
|
|
91 }
|
|
92
|
|
93
|
|
94 /**
|
|
95 * set the key
|
|
96 **/
|
|
97
|
|
98 public final int keyHash()
|
|
99 {
|
|
100 return typeid(K).getHash(&key_);
|
|
101 }
|
|
102
|
|
103
|
|
104 /**
|
|
105 * return a cell with key() key or null if no such
|
|
106 **/
|
|
107
|
|
108 public final LLPair findKey(K key)
|
|
109 {
|
|
110 for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_)
|
|
111 if (p.key() == key)
|
|
112 return p;
|
|
113 return null;
|
|
114 }
|
|
115
|
|
116 /**
|
|
117 * return a cell holding the indicated pair or null if no such
|
|
118 **/
|
|
119
|
|
120 public final LLPair find(K key, T element)
|
|
121 {
|
|
122 for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_)
|
|
123 if (p.key() == key && p.element() == element)
|
|
124 return p;
|
|
125 return null;
|
|
126 }
|
|
127
|
|
128 /**
|
|
129 * Return the number of cells traversed to find a cell with key() key,
|
|
130 * or -1 if not present
|
|
131 **/
|
|
132
|
|
133 public final int indexKey(K key)
|
|
134 {
|
|
135 int i = 0;
|
|
136 for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_)
|
|
137 {
|
|
138 if (p.key() == key)
|
|
139 return i;
|
|
140 else
|
|
141 ++i;
|
|
142 }
|
|
143 return -1;
|
|
144 }
|
|
145
|
|
146 /**
|
|
147 * Return the number of cells traversed to find a cell with indicated pair
|
|
148 * or -1 if not present
|
|
149 **/
|
|
150 public final int index(K key, T element)
|
|
151 {
|
|
152 int i = 0;
|
|
153 for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_)
|
|
154 {
|
|
155 if (p.key() == key && p.element() == element)
|
|
156 return i;
|
|
157 else
|
|
158 ++i;
|
|
159 }
|
|
160 return -1;
|
|
161 }
|
|
162
|
|
163 /**
|
|
164 * Return the number of cells with key() key.
|
|
165 **/
|
|
166 public final int countKey(K key)
|
|
167 {
|
|
168 int c = 0;
|
|
169 for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_)
|
|
170 if (p.key() == key)
|
|
171 ++c;
|
|
172 return c;
|
|
173 }
|
|
174
|
|
175 /**
|
|
176 * Return the number of cells with indicated pair
|
|
177 **/
|
|
178 public final int count(K key, T element)
|
|
179 {
|
|
180 int c = 0;
|
|
181 for (auto p=this; p; p = cast(LLPair)cast(void*) p.next_)
|
|
182 if (p.key() == key && p.element() == element)
|
|
183 ++c;
|
|
184 return c;
|
|
185 }
|
|
186
|
|
187 protected final LLPair duplicate()
|
|
188 {
|
|
189 return new LLPair(key(), element(), cast(LLPair)cast(void*)(next()));
|
|
190 }
|
|
191 }
|