1 /* 2 * Hunt - A refined core library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.collection.LinkedHashSet; 13 14 15 import hunt.collection.AbstractSet; 16 import hunt.collection.Collection; 17 import hunt.collection.HashMap; 18 import hunt.collection.HashSet; 19 import hunt.collection.LinkedHashMap; 20 import hunt.collection.Set; 21 22 import std.algorithm; 23 import std.range; 24 25 26 /** 27 * <p>Hash table and linked list implementation of the <tt>Set</tt> interface, 28 * with predictable iteration order. This implementation differs from 29 * <tt>HashSet</tt> in that it maintains a doubly-linked list running through 30 * all of its entries. This linked list defines the iteration ordering, 31 * which is the order in which elements were inserted into the set 32 * (<i>insertion-order</i>). Note that insertion order is <i>not</i> affected 33 * if an element is <i>re-inserted</i> into the set. (An element <tt>e</tt> 34 * is reinserted into a set <tt>s</tt> if <tt>s.add(e)</tt> is invoked when 35 * <tt>s.contains(e)</tt> would return <tt>true</tt> immediately prior to 36 * the invocation.) 37 * 38 * <p>This implementation spares its clients from the unspecified, generally 39 * chaotic ordering provided by {@link HashSet}, without incurring the 40 * increased cost associated with {@link TreeSet}. It can be used to 41 * produce a copy of a set that has the same order as the original, regardless 42 * of the original set's implementation: 43 * <pre> 44 * void foo(Set s) { 45 * Set copy = new LinkedHashSet(s); 46 * ... 47 * } 48 * </pre> 49 * This technique is particularly useful if a module takes a set on input, 50 * copies it, and later returns results whose order is determined by that of 51 * the copy. (Clients generally appreciate having things returned in the same 52 * order they were presented.) 53 * 54 * <p>This class provides all of the optional <tt>Set</tt> operations, and 55 * permits null elements. Like <tt>HashSet</tt>, it provides constant-time 56 * performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and 57 * <tt>remove</tt>), assuming the hash function disperses elements 58 * properly among the buckets. Performance is likely to be just slightly 59 * below that of <tt>HashSet</tt>, due to the added expense of maintaining the 60 * linked list, with one exception: Iteration over a <tt>LinkedHashSet</tt> 61 * requires time proportional to the <i>size</i> of the set, regardless of 62 * its capacity. Iteration over a <tt>HashSet</tt> is likely to be more 63 * expensive, requiring time proportional to its <i>capacity</i>. 64 * 65 * <p>A linked hash set has two parameters that affect its performance: 66 * <i>initial capacity</i> and <i>load factor</i>. They are defined precisely 67 * as for <tt>HashSet</tt>. Note, however, that the penalty for choosing an 68 * excessively high value for initial capacity is less severe for this class 69 * than for <tt>HashSet</tt>, as iteration times for this class are unaffected 70 * by capacity. 71 * 72 * <p><strong>Note that this implementation is not synchronized.</strong> 73 * If multiple threads access a linked hash set concurrently, and at least 74 * one of the threads modifies the set, it <em>must</em> be synchronized 75 * externally. This is typically accomplished by synchronizing on some 76 * object that naturally encapsulates the set. 77 * 78 * If no such object exists, the set should be "wrapped" using the 79 * {@link Collections#synchronizedSet Collections.synchronizedSet} 80 * method. This is best done at creation time, to prevent accidental 81 * unsynchronized access to the set: <pre> 82 * Set s = Collections.synchronizedSet(new LinkedHashSet(...));</pre> 83 * 84 * <p>The iterators returned by this class's <tt>iterator</tt> method are 85 * <em>fail-fast</em>: if the set is modified at any time after the iterator 86 * is created, in any way except through the iterator's own <tt>remove</tt> 87 * method, the iterator will throw a {@link ConcurrentModificationException}. 88 * Thus, in the face of concurrent modification, the iterator fails quickly 89 * and cleanly, rather than risking arbitrary, non-deterministic behavior at 90 * an undetermined time in the future. 91 * 92 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed 93 * as it is, generally speaking, impossible to make any hard guarantees in the 94 * presence of unsynchronized concurrent modification. Fail-fast iterators 95 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis. 96 * Therefore, it would be wrong to write a program that depended on this 97 * exception for its correctness: <i>the fail-fast behavior of iterators 98 * should be used only to detect bugs.</i> 99 * 100 * <p>This class is a member of the 101 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> 102 * Java Collections Framework</a>. 103 * 104 * @param (E) the type of elements maintained by this set 105 * 106 * @author Josh Bloch 107 * @see Object#hashCode() 108 * @see Collection 109 * @see Set 110 * @see HashSet 111 * @see TreeSet 112 * @see Hashtable 113 */ 114 115 class LinkedHashSet(E) : HashSet!(E) { 116 117 // private static final long serialVersionUID = -2851667679971038690L; 118 119 /** 120 * Constructs a new, empty linked hash set with the specified initial 121 * capacity and load factor. 122 * 123 * @param initialCapacity the initial capacity of the linked hash set 124 * @param loadFactor the load factor of the linked hash set 125 * @throws IllegalArgumentException if the initial capacity is less 126 * than zero, or if the load factor is nonpositive 127 */ 128 this(int initialCapacity, float loadFactor) { 129 super(initialCapacity, loadFactor, true); 130 } 131 132 /** 133 * Constructs a new, empty linked hash set with the specified initial 134 * capacity and the default load factor (0.75). 135 * 136 * @param initialCapacity the initial capacity of the LinkedHashSet 137 * @throws IllegalArgumentException if the initial capacity is less 138 * than zero 139 */ 140 this(int initialCapacity) { 141 super(initialCapacity, .75f, true); 142 } 143 144 /** 145 * Constructs a new, empty linked hash set with the default initial 146 * capacity (16) and load factor (0.75). 147 */ 148 this() { 149 super(16, .75f, true); 150 } 151 152 /** 153 * Constructs a new linked hash set with the same elements as the 154 * specified collection. The linked hash set is created with an initial 155 * capacity sufficient to hold the elements in the specified collection 156 * and the default load factor (0.75). 157 * 158 * @param c the collection whose elements are to be placed into 159 * this set 160 * @throws NullPointerException if the specified collection is null 161 */ 162 this(Collection!E c) { 163 super(std.algorithm.max(2*c.size(), 11), .75f, true); 164 addAll(c); 165 } 166 167 this(E[] c) { 168 super(cast(int)std.algorithm.max(2*c.length, 11), .75f, true); 169 addAll(c); 170 } 171 172 173 /** 174 * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> 175 * and <em>fail-fast</em> {@code Spliterator} over the elements in this set. 176 * 177 * <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, 178 * {@link Spliterator#DISTINCT}, and {@code ORDERED}. Implementations 179 * should document the reporting of additional characteristic values. 180 * 181 * @implNote 182 * The implementation creates a 183 * <em><a href="Spliterator.html#binding">late-binding</a></em> spliterator 184 * from the set's {@code Iterator}. The spliterator inherits the 185 * <em>fail-fast</em> properties of the set's iterator. 186 * The created {@code Spliterator} additionally reports 187 * {@link Spliterator#SUBSIZED}. 188 * 189 * @return a {@code Spliterator} over the elements in this set 190 */ 191 // override 192 // Spliterator!(E) spliterator() { 193 // return Spliterators.spliterator(this, Spliterator.DISTINCT | Spliterator.ORDERED); 194 // } 195 }