package de.jangobrick.scratchlib.objects;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;


/**
 * Maps instances of {@link ScratchObject} to reference IDs, offering insertion,
 * lookup and iteration methods.
 */
public class ScratchReferenceTable implements Iterable<ScratchObject>
{
    private final Map<ScratchObject, Integer> references = new LinkedHashMap<>();
    private int nextReferenceID = 1;

    /**
     * Performs a reference lookup for the given {@link ScratchObject}.
     * 
     * @param object The object to look up.
     * @return The object's reference ID, or -1 if not found.
     */
    public int lookup(ScratchObject object)
    {
        return references.getOrDefault(object, -1);
    }

    /**
     * Inserts the given {@link ScratchObject} into this reference table. The ID
     * is determined automatically.
     * 
     * @param object The object to insert.
     */
    public void insert(ScratchObject object)
    {
        if (references.containsKey(object)) {
            throw new IllegalArgumentException("Object already referenced");
        }
        references.put(object, nextReferenceID);
        nextReferenceID++;
    }

    /**
     * @return The number of references stored.
     */
    public int size()
    {
        return references.size();
    }

    @Override
    public Iterator<ScratchObject> iterator()
    {
        return references.keySet().iterator();
    }
}

@@ -53,24 +53,25 @@
 va.util.
-Iterator
+ArrayList
 ;%0Aimport
@@ -81,29 +81,24 @@
 va.util.
-LinkedHashMap
+Iterator
 ;%0Aimport
@@ -108,19 +108,20 @@
 va.util.
-Map
+List
 ;%0A%0A%0A/**%0A
@@ -328,19 +328,20 @@
 e final 
-Map
+List
 %3CScratch
@@ -350,17 +350,8 @@
 ject
-, Integer
 %3E re
@@ -369,62 +369,21 @@
 new 
-LinkedHashMap%3C%3E();%0A    private int nextReferenceID = 1
+ArrayList%3C%3E()
 ;%0A%0A 
@@ -645,48 +645,392 @@
     
-return references.getOrDefault(object,
+int index = references.indexOf(object);%0A        return index == -1 ? index : index + 1;%0A    %7D%0A%0A    /**%0A     * Performs an object lookup for the given reference ID.%0A     * %0A     * @param referenceID The ID to look up.%0A     * @return The referenced object, or null if not found.%0A     */%0A    public ScratchObject lookup(int referenceID)%0A    %7B%0A        return references.get(referenceID
  -
+ 
 1);%0A
@@ -1306,11 +1306,8 @@
 ains
-Key
 (obj
@@ -1424,63 +1424,19 @@
 ces.
-put
+add
 (object
-, nextReferenceID);%0A        nextReferenceID++
+)
 ;%0A  
@@ -1631,32 +1631,32 @@
 terator()%0A    %7B%0A
+
         return r
@@ -1669,17 +1669,8 @@
 ces.
-keySet().
 iter

Add reverse lookup support to ScratchReferenceTable