commit 7d0ce619466d66a7dd64e2fa78fc773f5ce4168d
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Tue Mar 27 13:21:01 2018 +0100

    New Object Counter class - to count OutputSurfaces lazy way, but keep the counters small (reuse)

diff --git a/src/main/java/org/distorted/library/main/DistortedObjectCounter.java b/src/main/java/org/distorted/library/main/DistortedObjectCounter.java
new file mode 100644
index 0000000..9dbb03e
--- /dev/null
+++ b/src/main/java/org/distorted/library/main/DistortedObjectCounter.java
@@ -0,0 +1,97 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Copyright 2016 Leszek Koltunski                                                               //
+//                                                                                               //
+// This file is part of Distorted.                                                               //
+//                                                                                               //
+// Distorted is free software: you can redistribute it and/or modify                             //
+// it under the terms of the GNU General Public License as published by                          //
+// the Free Software Foundation, either version 2 of the License, or                             //
+// (at your option) any later version.                                                           //
+//                                                                                               //
+// Distorted is distributed in the hope that it will be useful,                                  //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
+// GNU General Public License for more details.                                                  //
+//                                                                                               //
+// You should have received a copy of the GNU General Public License                             //
+// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+package org.distorted.library.main;
+
+import java.util.ArrayList;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Return unique IDs of objects in a lazy way. When we create a new Object, return the lowest unused
+ * integer. When an objects gets deleted, its ID then gets marked unused. We do not try to 'collapse'
+ * unused holes.
+ */
+class DistortedObjectCounter
+  {
+  private ArrayList<Integer> mUsed;
+  private int mFirstUnused;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  DistortedObjectCounter()
+    {
+    mUsed        = new ArrayList<>();
+    mFirstUnused = 0;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  int returnNext()
+    {
+    int size = mUsed.size();
+
+    if( mFirstUnused<size )
+      {
+      int ret = mFirstUnused;
+
+      mUsed.set(mFirstUnused,1);
+
+      int found=-1;
+
+      for(int i=mFirstUnused+1; i<size; i++ )
+        {
+        if( mUsed.get(i)==0 )
+          {
+          found = i;
+          break;
+          }
+        }
+
+      mFirstUnused = found<0 ? size : found;
+
+      return ret;
+      }
+
+    mUsed.add(1);
+    mFirstUnused++;
+
+    return size+1;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void release(int objID)
+    {
+    int size = mUsed.size();
+
+    if( objID<size && objID>=0 && mUsed.get(objID)==1 )
+      {
+      mUsed.set(objID,0);
+      if( objID<mFirstUnused ) mFirstUnused = objID;
+      }
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void releaseAll()
+    {
+    mUsed.clear();
+    mFirstUnused = 0;
+    }
+  }
