diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..85b0978 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..0ef0504 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,91 @@ + + + + \ No newline at end of file diff --git a/.idea/smartfox_info.xml b/.idea/smartfox_info.xml new file mode 100644 index 0000000..1c2584f --- /dev/null +++ b/.idea/smartfox_info.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/src/java/util/PriorityQueue.java b/src/java/util/PriorityQueue.java index ce58af8..6926098 100644 --- a/src/java/util/PriorityQueue.java +++ b/src/java/util/PriorityQueue.java @@ -273,6 +273,9 @@ private void initFromCollection(Collection c) { * * @param minCapacity the desired minimum capacity */ + /** + * 若原长度小于64,就扩容大概为2倍,否则1.5倍。 + */ private void grow(int minCapacity) { int oldCapacity = queue.length; // Double size if small; else grow by 50% @@ -315,6 +318,13 @@ public boolean add(E e) { * according to the priority queue's ordering * @throws NullPointerException if the specified element is null */ + //添加元素 + + /** + * 1.检查是否需要扩容 + * 2.若队列为空,就直接添加第一个元素到queue[0] + * 3.若不为空,就按照堆的方式,先添加到最后一个位置,然后自底向上调整。 + */ public boolean offer(E e) { if (e == null) throw new NullPointerException(); @@ -330,6 +340,7 @@ public boolean offer(E e) { return true; } +// 返回数组第一个元素 public E peek() { if (size == 0) return null; @@ -566,15 +577,18 @@ public void clear() { size = 0; } + //删除头结点 + //思想是把最后一个元素放到根节点,删掉最后一个元素,然后根节点自顶向下调整。 public E poll() { if (size == 0) return null; int s = --size; modCount++; - E result = (E) queue[0]; - E x = (E) queue[s]; - queue[s] = null; + E result = (E) queue[0]; //头结点,result记录方便后面返回 + E x = (E) queue[s]; //最后一个元素 + queue[s] = null; //最后一个元素置为null if (s != 0) + //向下调整 siftDown(0, x); return result; } @@ -622,6 +636,11 @@ private E removeAt(int i) { * @param k the position to fill * @param x the item to insert */ + /** + * 自底向上调整 + * 分为用comparator和comparable两种 + * 若小于父节点,就交换位置,继续递归比较,指定大于或等于父节点 + */ private void siftUp(int k, E x) { if (comparator != null) siftUpUsingComparator(k, x); @@ -641,14 +660,14 @@ private void siftUpComparable(int k, E x) { } queue[k] = key; } - + //若小于父节点,就交换位置,继续递归比较,指定大于父节点 private void siftUpUsingComparator(int k, E x) { while (k > 0) { int parent = (k - 1) >>> 1; Object e = queue[parent]; if (comparator.compare(x, (E) e) >= 0) break; - queue[k] = e; + queue[k] = e; //父节点元素移到当前元素位置 k = parent; } queue[k] = x; @@ -669,15 +688,16 @@ private void siftDown(int k, E x) { siftDownComparable(k, x); } + //选出最小的孩子节点与当前节点比较,若小于孩子节点则不变,否则孩子节点上移,继续循环直到当前节点小于孩子节点。 private void siftDownComparable(int k, E x) { Comparable key = (Comparable)x; int half = size >>> 1; // loop while a non-leaf - while (k < half) { + while (k < half) { //表示k节点右孩子节点。 int child = (k << 1) + 1; // assume left child is least Object c = queue[child]; int right = child + 1; - if (right < size && - ((Comparable) c).compareTo((E) queue[right]) > 0) + //选择最小的孩子节点用于比较 + if (right < size && ((Comparable) c).compareTo((E) queue[right]) > 0) c = queue[child = right]; if (key.compareTo((E) c) <= 0) break; diff --git a/src/java/util/TreeMap.java b/src/java/util/TreeMap.java index 67eaaf8..bb30df8 100644 --- a/src/java/util/TreeMap.java +++ b/src/java/util/TreeMap.java @@ -606,8 +606,8 @@ else if (cmp > 0) parent.left = e; else parent.right = e; - - fixAfterInsertion(e);//璋冩暣鏍戠殑缁撴瀯锛屼娇涔嬬鍚堢孩榛戞爲鐨勭害鏉 + //璋冩暣鏍戠殑缁撴瀯锛屼娇涔嬬鍚堢孩榛戞爲鐨勭害鏉 + fixAfterInsertion(e); size++; modCount++; return null; @@ -2311,7 +2311,6 @@ private void deleteEntry(Entry p) { // Start fixup at replacement node, if it exists. Entry replacement = (p.left != null ? p.left : p.right);//p鐨勫瀛愯妭鐐 - if (replacement != null) {//涓嶄负鍙跺瓙鑺傜偣 // Link replacement to parent // 寤虹珛琚垹鑺傜偣鐖惰妭鐐逛笌琚垹鑺傜偣瀛╁瓙鑺傜偣鐨勫叧绯伙紝琚垹鑺傜偣鍙湁涓涓瀛愯妭鐐 diff --git a/src/java/util/TreeSet.java b/src/java/util/TreeSet.java index dba7122..43572eb 100644 --- a/src/java/util/TreeSet.java +++ b/src/java/util/TreeSet.java @@ -30,10 +30,10 @@ * The elements are ordered using their {@linkplain Comparable natural * ordering}, or by a {@link Comparator} provided at set creation * time, depending on which constructor is used. - * + *

*

This implementation provides guaranteed log(n) time cost for the basic * operations ({@code add}, {@code remove} and {@code contains}). - * + *

*

Note that the ordering maintained by a set (whether or not an explicit * comparator is provided) must be consistent with equals if it is to * correctly implement the {@code Set} interface. (See {@code Comparable} @@ -45,7 +45,7 @@ * are, from the standpoint of the set, equal. The behavior of a set * is well-defined even if its ordering is inconsistent with equals; it * just fails to obey the general contract of the {@code Set} interface. - * + *

*

Note that this implementation is not synchronized. * If multiple threads access a tree set concurrently, and at least one * of the threads modifies the set, it must be synchronized @@ -56,7 +56,7 @@ * method. This is best done at creation time, to prevent accidental * unsynchronized access to the set:

  *   SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
- * + *

*

The iterators returned by this class's {@code iterator} method are * fail-fast: if the set is modified at any time after the iterator is * created, in any way except through the iterator's own {@code remove} @@ -64,7 +64,7 @@ * Thus, in the face of concurrent modification, the iterator fails quickly * and cleanly, rather than risking arbitrary, non-deterministic behavior at * an undetermined time in the future. - * + *

*

Note that the fail-fast behavior of an iterator cannot be guaranteed * as it is, generally speaking, impossible to make any hard guarantees in the * presence of unsynchronized concurrent modification. Fail-fast iterators @@ -72,30 +72,28 @@ * Therefore, it would be wrong to write a program that depended on this * exception for its correctness: the fail-fast behavior of iterators * should be used only to detect bugs. - * + *

*

This class is a member of the * * Java Collections Framework. * * @param the type of elements maintained by this set - * - * @author Josh Bloch - * @see Collection - * @see Set - * @see HashSet - * @see Comparable - * @see Comparator - * @see TreeMap - * @since 1.2 + * @author Josh Bloch + * @see Collection + * @see Set + * @see HashSet + * @see Comparable + * @see Comparator + * @see TreeMap + * @since 1.2 */ - +//鍩轰簬TreeMap锛屾墍鏈夋柟娉曠殑璋冪敤閮界敤鍒板唴閮ㄧ殑map public class TreeSet extends AbstractSet - implements NavigableSet, Cloneable, java.io.Serializable -{ + implements NavigableSet, Cloneable, java.io.Serializable { /** * The backing map. */ - private transient NavigableMap m; + private transient NavigableMap m; // Dummy value to associate with an Object in the backing Map // 浣滀负姣忎釜閿殑鍊兼坊鍔犲埌NavigableMap涓 @@ -105,7 +103,7 @@ public class TreeSet extends AbstractSet * Constructs a set backed by the specified navigable map. */ //涓嶆槸public鐨勶紝鏄唴閮ㄧ敤鐨 - TreeSet(NavigableMap m) { + TreeSet(NavigableMap m) { this.m = m; } @@ -123,7 +121,7 @@ public class TreeSet extends AbstractSet * {@code ClassCastException}. */ public TreeSet() { - this(new TreeMap()); + this(new TreeMap()); } /** @@ -136,8 +134,8 @@ public TreeSet() { * {@code add} call will throw a {@code ClassCastException}. * * @param comparator the comparator that will be used to order this set. - * If {@code null}, the {@linkplain Comparable natural - * ordering} of the elements will be used. + * If {@code null}, the {@linkplain Comparable natural + * ordering} of the elements will be used. */ public TreeSet(Comparator comparator) { this(new TreeMap<>(comparator)); @@ -153,8 +151,8 @@ public TreeSet(Comparator comparator) { * {@code e2} in the set. * * @param c collection whose elements will comprise the new set - * @throws ClassCastException if the elements in {@code c} are - * not {@link Comparable}, or are not mutually comparable + * @throws ClassCastException if the elements in {@code c} are + * not {@link Comparable}, or are not mutually comparable * @throws NullPointerException if the specified collection is null */ @@ -228,11 +226,11 @@ public boolean isEmpty() { * * @param o object to be checked for containment in this set * @return {@code true} if this set contains the specified element - * @throws ClassCastException if the specified object cannot be compared - * with the elements currently in the set + * @throws ClassCastException if the specified object cannot be compared + * with the elements currently in the set * @throws NullPointerException if the specified element is null - * and this set uses natural ordering, or its comparator - * does not permit null elements + * and this set uses natural ordering, or its comparator + * does not permit null elements */ public boolean contains(Object o) { return m.containsKey(o); @@ -248,15 +246,15 @@ public boolean contains(Object o) { * * @param e element to be added to this set * @return {@code true} if this set did not already contain the specified - * element - * @throws ClassCastException if the specified object cannot be compared - * with the elements currently in this set + * element + * @throws ClassCastException if the specified object cannot be compared + * with the elements currently in this set * @throws NullPointerException if the specified element is null - * and this set uses natural ordering, or its comparator - * does not permit null elements + * and this set uses natural ordering, or its comparator + * does not permit null elements */ public boolean add(E e) { - return m.put(e, PRESENT)==null; + return m.put(e, PRESENT) == null; } /** @@ -270,14 +268,14 @@ public boolean add(E e) { * * @param o object to be removed from this set, if present * @return {@code true} if this set contained the specified element - * @throws ClassCastException if the specified object cannot be compared - * with the elements currently in this set + * @throws ClassCastException if the specified object cannot be compared + * with the elements currently in this set * @throws NullPointerException if the specified element is null - * and this set uses natural ordering, or its comparator - * does not permit null elements + * and this set uses natural ordering, or its comparator + * does not permit null elements */ public boolean remove(Object o) { - return m.remove(o)==PRESENT; + return m.remove(o) == PRESENT; } /** @@ -293,22 +291,22 @@ public void clear() { * * @param c collection containing elements to be added to this set * @return {@code true} if this set changed as a result of the call - * @throws ClassCastException if the elements provided cannot be compared - * with the elements currently in the set + * @throws ClassCastException if the elements provided cannot be compared + * with the elements currently in the set * @throws NullPointerException if the specified collection is null or - * if any element is null and this set uses natural ordering, or - * its comparator does not permit null elements + * if any element is null and this set uses natural ordering, or + * its comparator does not permit null elements */ - public boolean addAll(Collection c) { + public boolean addAll(Collection c) { // Use linear-time version if applicable - if (m.size()==0 && c.size() > 0 && - c instanceof SortedSet && - m instanceof TreeMap) { + if (m.size() == 0 && c.size() > 0 && + c instanceof SortedSet && + m instanceof TreeMap) { SortedSet set = (SortedSet) c; - TreeMap map = (TreeMap) m; + TreeMap map = (TreeMap) m; Comparator cc = (Comparator) set.comparator(); Comparator mc = map.comparator(); - if (cc==mc || (cc != null && cc.equals(mc))) { + if (cc == mc || (cc != null && cc.equals(mc))) { map.addAllForTreeSet(set, PRESENT); return true; } @@ -317,24 +315,24 @@ public boolean addAll(Collection c) { } /** - * @throws ClassCastException {@inheritDoc} - * @throws NullPointerException if {@code fromElement} or {@code toElement} - * is null and this set uses natural ordering, or its comparator - * does not permit null elements + * @throws ClassCastException {@inheritDoc} + * @throws NullPointerException if {@code fromElement} or {@code toElement} + * is null and this set uses natural ordering, or its comparator + * does not permit null elements * @throws IllegalArgumentException {@inheritDoc} * @since 1.6 */ public NavigableSet subSet(E fromElement, boolean fromInclusive, - E toElement, boolean toInclusive) { + E toElement, boolean toInclusive) { return new TreeSet<>(m.subMap(fromElement, fromInclusive, - toElement, toInclusive)); + toElement, toInclusive)); } /** - * @throws ClassCastException {@inheritDoc} - * @throws NullPointerException if {@code toElement} is null and - * this set uses natural ordering, or its comparator does - * not permit null elements + * @throws ClassCastException {@inheritDoc} + * @throws NullPointerException if {@code toElement} is null and + * this set uses natural ordering, or its comparator does + * not permit null elements * @throws IllegalArgumentException {@inheritDoc} * @since 1.6 */ @@ -343,10 +341,10 @@ public NavigableSet headSet(E toElement, boolean inclusive) { } /** - * @throws ClassCastException {@inheritDoc} - * @throws NullPointerException if {@code fromElement} is null and - * this set uses natural ordering, or its comparator does - * not permit null elements + * @throws ClassCastException {@inheritDoc} + * @throws NullPointerException if {@code fromElement} is null and + * this set uses natural ordering, or its comparator does + * not permit null elements * @throws IllegalArgumentException {@inheritDoc} * @since 1.6 */ @@ -355,10 +353,10 @@ public NavigableSet tailSet(E fromElement, boolean inclusive) { } /** - * @throws ClassCastException {@inheritDoc} - * @throws NullPointerException if {@code fromElement} or - * {@code toElement} is null and this set uses natural ordering, - * or its comparator does not permit null elements + * @throws ClassCastException {@inheritDoc} + * @throws NullPointerException if {@code fromElement} or + * {@code toElement} is null and this set uses natural ordering, + * or its comparator does not permit null elements * @throws IllegalArgumentException {@inheritDoc} */ public SortedSet subSet(E fromElement, E toElement) { @@ -366,10 +364,10 @@ public SortedSet subSet(E fromElement, E toElement) { } /** - * @throws ClassCastException {@inheritDoc} - * @throws NullPointerException if {@code toElement} is null - * and this set uses natural ordering, or its comparator does - * not permit null elements + * @throws ClassCastException {@inheritDoc} + * @throws NullPointerException if {@code toElement} is null + * and this set uses natural ordering, or its comparator does + * not permit null elements * @throws IllegalArgumentException {@inheritDoc} */ public SortedSet headSet(E toElement) { @@ -377,10 +375,10 @@ public SortedSet headSet(E toElement) { } /** - * @throws ClassCastException {@inheritDoc} - * @throws NullPointerException if {@code fromElement} is null - * and this set uses natural ordering, or its comparator does - * not permit null elements + * @throws ClassCastException {@inheritDoc} + * @throws NullPointerException if {@code fromElement} is null + * and this set uses natural ordering, or its comparator does + * not permit null elements * @throws IllegalArgumentException {@inheritDoc} */ public SortedSet tailSet(E fromElement) { @@ -408,10 +406,10 @@ public E last() { // NavigableSet API methods /** - * @throws ClassCastException {@inheritDoc} + * @throws ClassCastException {@inheritDoc} * @throws NullPointerException if the specified element is null - * and this set uses natural ordering, or its comparator - * does not permit null elements + * and this set uses natural ordering, or its comparator + * does not permit null elements * @since 1.6 */ public E lower(E e) { @@ -419,10 +417,10 @@ public E lower(E e) { } /** - * @throws ClassCastException {@inheritDoc} + * @throws ClassCastException {@inheritDoc} * @throws NullPointerException if the specified element is null - * and this set uses natural ordering, or its comparator - * does not permit null elements + * and this set uses natural ordering, or its comparator + * does not permit null elements * @since 1.6 */ public E floor(E e) { @@ -430,10 +428,10 @@ public E floor(E e) { } /** - * @throws ClassCastException {@inheritDoc} + * @throws ClassCastException {@inheritDoc} * @throws NullPointerException if the specified element is null - * and this set uses natural ordering, or its comparator - * does not permit null elements + * and this set uses natural ordering, or its comparator + * does not permit null elements * @since 1.6 */ public E ceiling(E e) { @@ -441,10 +439,10 @@ public E ceiling(E e) { } /** - * @throws ClassCastException {@inheritDoc} + * @throws ClassCastException {@inheritDoc} * @throws NullPointerException if the specified element is null - * and this set uses natural ordering, or its comparator - * does not permit null elements + * and this set uses natural ordering, or its comparator + * does not permit null elements * @since 1.6 */ public E higher(E e) { @@ -455,7 +453,7 @@ public E higher(E e) { * @since 1.6 */ public E pollFirst() { - Map.Entry e = m.pollFirstEntry(); + Map.Entry e = m.pollFirstEntry(); return (e == null) ? null : e.getKey(); } @@ -463,7 +461,7 @@ public E pollFirst() { * @since 1.6 */ public E pollLast() { - Map.Entry e = m.pollLastEntry(); + Map.Entry e = m.pollLastEntry(); return (e == null) ? null : e.getKey(); } @@ -490,15 +488,15 @@ public Object clone() { * serialize it). * * @serialData Emits the comparator used to order this set, or - * {@code null} if it obeys its elements' natural ordering - * (Object), followed by the size of the set (the number of - * elements it contains) (int), followed by all of its - * elements (each an Object) in order (as determined by the - * set's Comparator, or by the elements' natural ordering if - * the set has no Comparator). + * {@code null} if it obeys its elements' natural ordering + * (Object), followed by the size of the set (the number of + * elements it contains) (int), followed by all of its + * elements (each an Object) in order (as determined by the + * set's Comparator, or by the elements' natural ordering if + * the set has no Comparator). */ private void writeObject(java.io.ObjectOutputStream s) - throws java.io.IOException { + throws java.io.IOException { // Write out any hidden stuff s.defaultWriteObject(); @@ -518,7 +516,7 @@ private void writeObject(java.io.ObjectOutputStream s) * deserialize it). */ private void readObject(java.io.ObjectInputStream s) - throws java.io.IOException, ClassNotFoundException { + throws java.io.IOException, ClassNotFoundException { // Read in any hidden stuff s.defaultReadObject(); @@ -526,8 +524,8 @@ private void readObject(java.io.ObjectInputStream s) Comparator c = (Comparator) s.readObject(); // Create backing TreeMap - TreeMap tm; - if (c==null) + TreeMap tm; + if (c == null) tm = new TreeMap<>(); else tm = new TreeMap<>(c);