global classes
authorjihoonl <jihoonl>
Thu, 8 Oct 2009 23:14:45 +0000 (23:14 +0000)
committerjihoonl <jihoonl>
Thu, 8 Oct 2009 23:14:45 +0000 (23:14 +0000)
Robust/src/ClassLibrary/JavaDSM/GlobalString.java [new file with mode: 0644]
Robust/src/ClassLibrary/JavaDSM/GlobalStringBuffer.java [new file with mode: 0644]
Robust/src/ClassLibrary/JavaDSM/Queue.java [new file with mode: 0644]

diff --git a/Robust/src/ClassLibrary/JavaDSM/GlobalString.java b/Robust/src/ClassLibrary/JavaDSM/GlobalString.java
new file mode 100644 (file)
index 0000000..68c117b
--- /dev/null
@@ -0,0 +1,129 @@
+public class GlobalString {
+  char value[];
+  int count;
+  int offset;
+
+  public GlobalString() {
+  }
+
+  public GlobalString(String str) {
+               atomic {
+                       value = global new char[str.count];
+                       for(int i =0; i< str.count;i++) {
+                               value[i] = str.value[i+str.offset];
+                       }
+                 count = str.count;
+                       offset = 0;
+               }
+  }
+
+       public GlobalString(GlobalString gstr) {
+               atomic {
+                       this.value = gstr.value;
+                       this.count = gstr.count;
+                       this.offset = gstr.offset;
+               }
+       }
+
+       public GlobalString(GlobalStringBuffer gsb) {
+               atomic {
+                       value = global new char[gsb.length()];
+                       count = gsb.length();
+                       offset = 0;
+                       for (int i = 0; i < count; i++) 
+                               value[i] = gsb.value[i];
+               }
+       }
+
+       public static char[] toLocalCharArray(GlobalString str) {
+               char[] c;
+               int length;
+
+               atomic { 
+                       length = str.length();
+               }
+
+               c = new char[length];
+
+               atomic {
+                       for (int i = 0; i < length; i++) {
+                               c[i] = str.value[i+str.offset];
+                       }
+               }
+               return c;
+       }
+
+       public String toLocalString() {
+               return new String(toLocalCharArray(this));
+       }
+
+       public int length() {
+               return count;
+       }
+
+       public int indexOf(int ch, int fromIndex) {
+               for (int i = fromIndex; i < count; i++)
+                       if (this.charAt(i) == ch) 
+                               return i;
+               return -1;
+       }
+
+       public int lastindexOf(int ch) {
+               return this.lastindexOf(ch, count - 1);
+       }
+
+       public int lastindexOf(int ch, int fromIndex) {
+               for (int i = fromIndex; i > 0; i--) 
+                       if (this.charAt(i) == ch) 
+                               return i;
+               return -1;
+       }
+
+       public char charAt(int i) {
+               return value[i+offset];
+       }
+
+       public int indexOf(GlobalString str) {
+               return this.indexOf(str, 0);
+       }
+
+       public int indexOf(GlobalString str, int fromIndex) {
+               if (fromIndex < 0) 
+                       fromIndex = 0;
+               for (int i = fromIndex; i <= (count-str.count); i++)
+                       if (regionMatches(i, str, 0, str.count)) 
+                               return i;
+               return -1;
+       }       
+
+       public boolean regionMatches(int toffset, GlobalString other, int ooffset, int len) {
+               if (toffset < 0 || ooffset < 0 || (toffset+len) > count || (ooffset+len) > other.count)
+                       return false;
+
+               for (int i = 0; i < len; i++) {
+                       if (other.value[i+other.offset+ooffset] != this.value[i+this.offset+toffset])
+                               return false;
+               }
+               return true;
+       }
+
+       public GlobalString subString(int beginIndex, int endIndex) {
+               return substring(beginIndex, endIndex);
+       }
+
+       public GlobalString substring(int beginIndex, int endIndex) {
+               GlobalString str;
+               atomic {
+                       str = global new GlobalString();
+//             }
+//             if (beginIndex > this.count || endIndex > this.count || beginIndex > endIndex) {
+//                     System.printString("Index error\n");
+//             }
+//             atomic {
+                       str.value = this.value;
+                       str.count = endIndex-beginIndex;
+                       str.offset = this.offset + beginIndex;
+               }
+               return str;     
+       }
+}
diff --git a/Robust/src/ClassLibrary/JavaDSM/GlobalStringBuffer.java b/Robust/src/ClassLibrary/JavaDSM/GlobalStringBuffer.java
new file mode 100644 (file)
index 0000000..583e04b
--- /dev/null
@@ -0,0 +1,124 @@
+public class GlobalStringBuffer {
+  char value[];
+  int count;
+  //    private static final int DEFAULTSIZE=16;
+
+  public GlobalStringBuffer(String str) {
+               GlobalString gstr;
+
+               atomic {
+                       gstr = global new GlobalString(str);
+               }
+               GlobalStringBuffer(gstr);
+  }
+
+       public GlobalStringBuffer(GlobalString str) {
+               atomic {
+                       value = global new char[str.count+16];
+                       count = str.count;
+                       for (int i = 0; i < count; i++) 
+                               value[i] = str.value[i+str.offset];
+               }
+       }
+
+       public GlobalStringBuffer(StringBuffer sb) {
+               atomic {
+                       value = global new char[sb.count];
+                       for (int i = 0; i < sb.count; i++) 
+                               value[i] = sb.value[i];
+                       count = sb.count;
+               }
+       }
+
+  public GlobalStringBuffer() {
+               atomic {
+                       value = global new char[16];    //16 is DEFAULTSIZE
+                       count = 0;
+               }
+  }
+
+  public int length() {
+    return count;
+  }
+
+  public int capacity() {
+    return value.length;
+  }
+
+  public char charAt(int x) {
+    return value[x];
+  }
+
+  public GlobalStringBuffer append(char c) {
+    return append(String.valueOf(c));
+  }
+
+  public GlobalStringBuffer append(String s) {
+               GlobalString str;
+               atomic {
+                       str = global new GlobalString(s);
+               }
+               return append(str);
+  }
+
+  public GlobalStringBuffer append(GlobalString s) {
+               atomic {
+                       if ((s.count+count) > value.length) {
+                               // Need to allocate
+                               char newvalue[] = global new char[s.count+count+16];       //16 is DEFAULTSIZE
+                               for(int i = 0; i < count; i++)
+                                       newvalue[i] = value[i];
+                               for(int i = 0; i < s.count; i++)
+                                       newvalue[i+count] = s.value[i+s.offset];
+                               value = newvalue;
+                               count += s.count;
+                       } else {
+                               for(int i = 0; i < s.count; i++) {
+                                       value[i+count] = s.value[i+s.offset];
+                               }
+                               count += s.count;
+                       }
+               }
+    return this;
+  }
+
+  public GlobalStringBuffer append(StringBuffer s) {
+               GlobalStringBuffer gsb;
+               atomic {
+                       gsb = global new GlobalStringBuffer(s);
+               }
+               return append(gsb);
+       }
+
+
+  public GlobalStringBuffer append(GlobalStringBuffer s) {
+               atomic {
+                       if ((s.count+count) > value.length) {
+                               // Need to allocate
+                               char newvalue[] = global new char[s.count+count+16];       //16 is DEFAULTSIZE
+                               for(int i = 0; i < count; i++)
+                                       newvalue[i] = value[i];
+                               for(int i = 0; i < s.count; i++)
+                                       newvalue[i+count] = s.value[i];
+                               value = newvalue;
+                               count += s.count;
+                       } else {
+                               for(int i = 0; i < s.count; i++) {
+                                       value[i+count] = s.value[i];
+                               }
+                               count += s.count;
+                       }
+               }
+    return this;
+  }
+
+  public GlobalString toGlobalString() {
+    return global new GlobalString(this);
+  }
+
+       public String toLocalString() {
+               GlobalString gstr = this.toGlobalString();
+               return gstr.toLocalString();
+       }
+       
+}
diff --git a/Robust/src/ClassLibrary/JavaDSM/Queue.java b/Robust/src/ClassLibrary/JavaDSM/Queue.java
new file mode 100644 (file)
index 0000000..7a2afe3
--- /dev/null
@@ -0,0 +1,187 @@
+/* =============================================================================
+ *
+ * queue.java
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006.  All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * Ported to Java
+ * Author:Alokika Dash
+ * University of California, Irvine
+ *
+ * =============================================================================
+ * 
+ * Unless otherwise noted, the following license applies to STAMP files:
+ * 
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * 
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ * 
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ * 
+ *     * Neither the name of Stanford University nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * =============================================================================
+ */
+
+public class Queue {
+  int pop; /* points before element to pop */
+  int push;
+  int capacity;
+  int size;
+  int QUEUE_GROWTH_FACTOR;
+  Object[] elements;
+
+  public Queue() {
+               Queue(10);
+  }
+
+  /* =============================================================================
+   * queue_alloc
+   * =============================================================================
+   */
+  public Queue (int initCapacity)
+  {
+    QUEUE_GROWTH_FACTOR = 2;
+    capacity = ((initCapacity < 2) ? 2 : initCapacity);
+
+    elements = global new Object[capacity];
+    size = 0;
+    pop      = capacity - 1;
+    push     = 0;
+    capacity = capacity;
+  }
+
+  /* =============================================================================
+   * queue_isEmpty
+   * =============================================================================
+   */
+  public boolean
+    isEmpty ()
+    {
+      return (((pop + 1) % capacity == push) ? true : false);
+    }
+
+
+  /* =============================================================================
+   * queue_clear
+   * =============================================================================
+   */
+  public void
+    queue_clear ()
+    {
+      pop  = capacity - 1;
+      push = 0;
+    }
+
+  /* =============================================================================
+   * queue_push
+   * =============================================================================
+   */
+  public boolean
+    push (Object dataPtr)
+    {
+      if(pop == push) {
+//        System.out.println("push == pop in Queue.java");
+        return false;
+      }
+
+      /* Need to resize */
+      int newPush = (push + 1) % capacity;
+      if (newPush == pop) {
+
+        int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
+        Object[] newElements = global new Object[newCapacity];
+
+        if (newElements == null) {
+          return false;
+        }
+
+        int dst = 0;
+        Object[] tmpelements = elements;
+        if (pop < push) {
+          int src;
+          for (src = (pop + 1); src < push; src++, dst++) {
+            newElements[dst] = elements[src];
+          }
+        } else {
+          int src;
+          for (src = (pop + 1); src < capacity; src++, dst++) {
+            newElements[dst] = elements[src];
+          }
+          for (src = 0; src < push; src++, dst++) {
+            newElements[dst] = elements[src];
+          }
+        }
+
+        //elements = null;
+        elements = newElements;
+        pop      = newCapacity - 1;
+        capacity = newCapacity;
+        push = dst;
+        newPush = push + 1; /* no need modulo */
+      }
+      size++;
+      elements[push] = dataPtr;
+      push = newPush;
+
+      return true;
+    }
+
+
+  /* =============================================================================
+   * queue_pop
+   * =============================================================================
+   */
+  public Object
+    pop ()
+    {
+      int newPop = (pop + 1) % capacity;
+      if (newPop == push) {
+        return null;
+      }
+
+      //Object dataPtr = queuePtr.elements[newPop];
+      //queuePtr.pop = newPop;
+      Object dataPtr = elements[newPop];
+      pop = newPop;
+      size--;
+      return dataPtr;
+    }
+  public int size()
+  {
+    return size;
+  }
+
+}
+/* =============================================================================
+ *
+ * End of queue.java
+ *
+ * =============================================================================
+ */