]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/IntArray.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / IntArray.java
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/IntArray.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/IntArray.java
new file mode 100644 (file)
index 0000000..8770b4d
--- /dev/null
@@ -0,0 +1,133 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.impl.query;\r
+\r
+import java.util.Arrays;\r
+\r
+\r
+final public class IntArray {\r
+\r
+       public static IntArray EMPTY = new IntArray();\r
+       \r
+    public int[] data;\r
+\r
+    /** the index after the last entry in the list */\r
+    public int sizeOrData;\r
+\r
+    /** the default capacity for new lists */\r
+    protected static final int DEFAULT_CAPACITY = 3;\r
+\r
+    protected static final int NO_DATA = -1;\r
+\r
+    public IntArray() {\r
+        data = null;\r
+        sizeOrData = NO_DATA;\r
+    }\r
+\r
+    /**\r
+     * Returns the number of values in the list.\r
+     *\r
+     * @return the number of values in the list.\r
+     */\r
+    public int size() {\r
+        return data != null ? sizeOrData : (sizeOrData != NO_DATA ? 1 : 0);\r
+    }\r
+\r
+    /**\r
+     * Tests whether this list contains any values.\r
+     *\r
+     * @return true if the list is empty.\r
+     */\r
+    public boolean isEmpty() {\r
+        return sizeOrData == NO_DATA;\r
+    }\r
+\r
+//    /**\r
+//     * Sheds any excess capacity above and beyond the current size of\r
+//     * the list.\r
+//     */\r
+//    public void trimToSize() {\r
+//        if (_data.length > size()) {\r
+//            int[] tmp = new int[size()];\r
+//            toNativeArray(tmp, 0, tmp.length);\r
+//            _data = tmp;\r
+//        }\r
+//    }\r
+\r
+    // modifying\r
+\r
+    public void trim() {\r
+       if(data != null) {\r
+               int[] newData = new int[sizeOrData];\r
+               System.arraycopy(data, 0, newData, 0, sizeOrData);\r
+               data = newData;\r
+       }\r
+    }\r
+    \r
+    /**\r
+     * Adds <tt>val</tt> to the end of the list, growing as needed.\r
+     *\r
+     * @param val an <code>int</code> value\r
+     */\r
+    public void add(int val) {\r
+        if(data == null) {\r
+            if(sizeOrData == NO_DATA) {\r
+                sizeOrData = val;\r
+            } else {\r
+                data = new int[DEFAULT_CAPACITY];\r
+                data[0] = sizeOrData;\r
+                data[1] = val;\r
+                sizeOrData = 2;\r
+            }\r
+        } else {\r
+            if(data.length == sizeOrData) {\r
+                int newCap = data.length << 1;\r
+                int[] tmp = new int[newCap];\r
+                System.arraycopy(data, 0, tmp, 0, data.length);\r
+                data = tmp;\r
+                data[sizeOrData++] = val;\r
+            } else {\r
+                data[sizeOrData++] = val;\r
+            }\r
+        }\r
+    }\r
+    \r
+    int[] toArray() {\r
+       int size = size();\r
+       int[] result = new int[size];\r
+       if(size == 1) {\r
+               result[0] = sizeOrData; \r
+       } else {\r
+            System.arraycopy(data, 0, result, 0, size);\r
+       }\r
+       return result;\r
+    }\r
+    \r
+    @Override\r
+    public boolean equals(Object object) {\r
+        if (this == object)\r
+            return true;\r
+        else if (object == null)\r
+            return false;\r
+        else if (IntArray.class != object.getClass())\r
+            return false;\r
+        IntArray r = (IntArray)object;\r
+//        System.out.println("equals " + this + " vs. " + r);\r
+        return sizeOrData == r.sizeOrData && Arrays.equals(data, r.data);\r
+    }\r
+    \r
+    @Override\r
+    public String toString() {\r
+        return "IntArray " + sizeOrData + " " + Arrays.toString(data);\r
+    }\r
+    \r
+}
\ No newline at end of file