You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2022/07/13 12:23:02 UTC

[GitHub] [commons-collections] garydgregory commented on a diff in pull request #321: Adding IndexedLinkedList and its tests.

garydgregory commented on code in PR #321:
URL: https://github.com/apache/commons-collections/pull/321#discussion_r920010604


##########
src/main/java/org/apache/commons/collections4/list/IndexedLinkedList.java:
##########
@@ -0,0 +1,3460 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.github.coderodde.util;
+
+import java.lang.reflect.Array;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.ConcurrentModificationException;
+import java.util.Deque;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.Spliterator;
+import java.util.function.Consumer;
+import java.util.function.IntFunction;
+import java.util.function.Predicate;
+import java.util.function.UnaryOperator;
+
+/**
+ * <p>
+ * This class implements the indexed, heuristic doubly-linked list data 
+ * structure that runs all the single-element operations in expected
+ * \(\mathcal{O}(\sqrt{n})\) time. Under the hood, the actual elements are
+ * stored in a doubly-linked list. However, we also maintain a list of so-called 
+ * <i>"fingers"</i> stored in a random access array. Each finger {@code F} 
+ * contains two data fields:
+ * <ul>
+ *  <li>{@code F.node} - the actual element node,</li>
+ *  <li>{@code F.index} - the appearance index of {@code F.node} in the actual 
+ *       list.</li>
+ * </ul>
+ * 
+ * <p>
+ * 
+ * For the list of size \(n\), we maintain 
+ * \(\bigg \lceil \sqrt{n} \bigg \rceil + 1\) fingers. The rightmost finger in 
+ * the finger list is a <i>special end-of-list sentinel</i>. It always has 
+ * {@code F.node = null} and {@code F.index = } \(n\). The fingers are sorted by 
+ * their indices. That arrangement allows simpler and faster code in the method 
+ * that accesses a finger via element index; see 
+ * {@link FingerList#getFingerIndexImpl(int)}. Since number of fingers is 
+ * \(\sqrt{n}\), and assuming that the fingers are evenly distributed, each 
+ * finger "covers" \(n / \sqrt{n} = \sqrt{n}\) elements. In order to access an 
+ * element in the actual list, we first consult the finger list for the index 
+ * {@code i} of the finger {@code fingerArray[i]} that is closest to the index 
+ * of the target element. This runs in 
+ * 
+ * \[ 
+ * \mathcal{O}(\log \sqrt{n}) = \mathcal{O}(\log n^{1/2}) = \mathcal{O}(\frac{1}{2} \log n) = \mathcal{O}(\log n).
+ * \]
+ * 
+ * The rest is to <i>"rewind"</i> the closest finger to point to the target 
+ * element (which requires \(\mathcal{O}(\sqrt{n})\) on evenly distributed 
+ * finger lis). 
+ * 
+ * @author Rodion "rodde" Efremov
+ * @version 1.61 (Sep 26, 2021)
+ * @since 1.6 (Sep 1, 2021)
+ */
+public class IndexedLinkedList<E> implements Deque<E>, 

Review Comment:
   This should fit in the rest of the framework by subclassing AbstractLinkedList



##########
src/main/java/org/apache/commons/collections4/list/IndexedLinkedList.java:
##########
@@ -0,0 +1,3460 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.github.coderodde.util;
+
+import java.lang.reflect.Array;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.ConcurrentModificationException;
+import java.util.Deque;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.Spliterator;
+import java.util.function.Consumer;
+import java.util.function.IntFunction;
+import java.util.function.Predicate;
+import java.util.function.UnaryOperator;
+
+/**
+ * <p>
+ * This class implements the indexed, heuristic doubly-linked list data 
+ * structure that runs all the single-element operations in expected
+ * \(\mathcal{O}(\sqrt{n})\) time. Under the hood, the actual elements are
+ * stored in a doubly-linked list. However, we also maintain a list of so-called 
+ * <i>"fingers"</i> stored in a random access array. Each finger {@code F} 
+ * contains two data fields:
+ * <ul>
+ *  <li>{@code F.node} - the actual element node,</li>
+ *  <li>{@code F.index} - the appearance index of {@code F.node} in the actual 
+ *       list.</li>
+ * </ul>
+ * 
+ * <p>
+ * 
+ * For the list of size \(n\), we maintain 
+ * \(\bigg \lceil \sqrt{n} \bigg \rceil + 1\) fingers. The rightmost finger in 
+ * the finger list is a <i>special end-of-list sentinel</i>. It always has 
+ * {@code F.node = null} and {@code F.index = } \(n\). The fingers are sorted by 
+ * their indices. That arrangement allows simpler and faster code in the method 
+ * that accesses a finger via element index; see 
+ * {@link FingerList#getFingerIndexImpl(int)}. Since number of fingers is 
+ * \(\sqrt{n}\), and assuming that the fingers are evenly distributed, each 
+ * finger "covers" \(n / \sqrt{n} = \sqrt{n}\) elements. In order to access an 
+ * element in the actual list, we first consult the finger list for the index 
+ * {@code i} of the finger {@code fingerArray[i]} that is closest to the index 
+ * of the target element. This runs in 
+ * 
+ * \[ 
+ * \mathcal{O}(\log \sqrt{n}) = \mathcal{O}(\log n^{1/2}) = \mathcal{O}(\frac{1}{2} \log n) = \mathcal{O}(\log n).
+ * \]
+ * 
+ * The rest is to <i>"rewind"</i> the closest finger to point to the target 
+ * element (which requires \(\mathcal{O}(\sqrt{n})\) on evenly distributed 
+ * finger lis). 
+ * 
+ * @author Rodion "rodde" Efremov
+ * @version 1.61 (Sep 26, 2021)
+ * @since 1.6 (Sep 1, 2021)

Review Comment:
   The next version of Collections is 4.5



##########
src/main/java/org/apache/commons/collections4/list/IndexedLinkedList.java:
##########
@@ -0,0 +1,3460 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.github.coderodde.util;
+
+import java.lang.reflect.Array;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.ConcurrentModificationException;
+import java.util.Deque;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.Spliterator;
+import java.util.function.Consumer;
+import java.util.function.IntFunction;
+import java.util.function.Predicate;
+import java.util.function.UnaryOperator;
+
+/**
+ * <p>
+ * This class implements the indexed, heuristic doubly-linked list data 
+ * structure that runs all the single-element operations in expected
+ * \(\mathcal{O}(\sqrt{n})\) time. Under the hood, the actual elements are
+ * stored in a doubly-linked list. However, we also maintain a list of so-called 
+ * <i>"fingers"</i> stored in a random access array. Each finger {@code F} 
+ * contains two data fields:
+ * <ul>
+ *  <li>{@code F.node} - the actual element node,</li>
+ *  <li>{@code F.index} - the appearance index of {@code F.node} in the actual 
+ *       list.</li>
+ * </ul>
+ * 
+ * <p>
+ * 
+ * For the list of size \(n\), we maintain 
+ * \(\bigg \lceil \sqrt{n} \bigg \rceil + 1\) fingers. The rightmost finger in 
+ * the finger list is a <i>special end-of-list sentinel</i>. It always has 
+ * {@code F.node = null} and {@code F.index = } \(n\). The fingers are sorted by 
+ * their indices. That arrangement allows simpler and faster code in the method 
+ * that accesses a finger via element index; see 
+ * {@link FingerList#getFingerIndexImpl(int)}. Since number of fingers is 
+ * \(\sqrt{n}\), and assuming that the fingers are evenly distributed, each 
+ * finger "covers" \(n / \sqrt{n} = \sqrt{n}\) elements. In order to access an 
+ * element in the actual list, we first consult the finger list for the index 
+ * {@code i} of the finger {@code fingerArray[i]} that is closest to the index 
+ * of the target element. This runs in 
+ * 
+ * \[ 
+ * \mathcal{O}(\log \sqrt{n}) = \mathcal{O}(\log n^{1/2}) = \mathcal{O}(\frac{1}{2} \log n) = \mathcal{O}(\log n).
+ * \]
+ * 
+ * The rest is to <i>"rewind"</i> the closest finger to point to the target 
+ * element (which requires \(\mathcal{O}(\sqrt{n})\) on evenly distributed 
+ * finger lis). 
+ * 
+ * @author Rodion "rodde" Efremov

Review Comment:
   FYI, we do not use author tags. Attribution is usually done in changes.xml, and/or pom.xml.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org