dcapwell commented on code in PR #50:
URL: https://github.com/apache/cassandra-accord/pull/50#discussion_r1261595819


##########
accord-core/src/main/java/accord/utils/DeterministicSet.java:
##########
@@ -0,0 +1,189 @@
+/*
+ * 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 accord.utils;
+
+import java.util.AbstractSet;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.function.BiConsumer;
+import java.util.function.Consumer;
+
+import com.google.common.collect.Iterables;
+
+public class DeterministicSet<T> extends AbstractSet<T>
+{
+    static class Entry<T>
+    {
+        final T item;
+        Entry<T> prev;
+        Entry<T> next;
+
+        Entry(T item)
+        {
+            this.item = item;
+        }
+    }
+
+    // TODO (low priority): an identity hash map that doesn't mind concurrent 
modification / iteration
+    final Map<T, Entry<T>> lookup;
+    final Entry<T> head = new Entry<T>(null);
+
+    public DeterministicSet()
+    {
+        this(0);
+    }
+
+    public DeterministicSet(int size)
+    {
+        head.prev = head.next = head;
+        lookup = new HashMap<>((size * 4)/3);
+    }
+
+    public DeterministicSet(DeterministicSet<T> copy)
+    {
+        this(copy.size());
+        copy.forEach(this::addInternal);
+    }
+
+    DeterministicSet(Map<T, Entry<T>> lookup)
+    {
+        Invariants.checkArgument(lookup.isEmpty());
+        head.prev = head.next = head;
+        this.lookup = lookup;
+    }
+
+    @Override
+    public Iterator<T> iterator()
+    {
+        return new Iterator<T>()
+        {
+            boolean hasComputedNext = true;
+            Entry<T> next = head.next;

Review Comment:
   > Do you mean because we initialise to the first element being present? 
   
   Yeah, that is a slightly different behavior as we only check the current 
state in `hasNext`, so the first element has slightly different semantics than 
the rest of the list.
   
   > But I don't mind setting hasComputedNext=false if you prefer.
   
   yeah, my thinking is that doing so will make sure we are consistent, and 
different access patterns in the future won't have to worry (such as creating 
the iterator then removing a element (before iterating))
   
   will send a feedback branch soon that makes such a change



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to