[m-rev.] For review: Java implementation of array library

James Goddard goddardjames at yahoo.com
Thu Jan 8 17:22:21 AEDT 2004


Estimated hours taken: 4
Branches: main

Implement some library procedures for the Java back end.

library/array.m:
	Implement the following procedures:
		bounds_checks/0
		array__init_2/3
		array__make_empty_array/1
		array__min/2
		array__max/2
		array__size/2
		array__unsafe_lookup/3
		array__unsafe_set/4
		array__resize/4
		array__shrink_2/3
		array__copy/2

only in patch2:
--- array.m	27 Nov 2003 13:27:03 -0000	1.128
+++ array.m	8 Jan 2004 06:14:42 -0000
@@ -483,6 +483,14 @@
 #endif
 ").		
 
+:- pragma foreign_proc("Java",
+	bounds_checks,
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	// never do bounds checking for Java (throw exceptions instead)
+	succeeded = false;
+").
+
 %-----------------------------------------------------------------------------%
 
 :- pragma foreign_decl("C", "
@@ -577,6 +585,25 @@
 	Array = null;
 ").
 
+:- pragma foreign_proc("Java", 
+	array__init_2(Size::in, Item::in, Array::array_uo),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	java.lang.Class itemClass = Item.getClass();
+
+	Array = java.lang.reflect.Array.newInstance(itemClass, Size);
+	for (int i = 0; i < Size; i++) {
+		java.lang.reflect.Array.set(Array, i, Item);
+	}
+").
+:- pragma foreign_proc("Java",
+	array__make_empty_array(Array::array_uo),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	// XXX as per C#
+	Array = null;
+").
+
 %-----------------------------------------------------------------------------%
 
 :- pragma foreign_proc("C",
@@ -609,6 +636,21 @@
 	Min = 0;
 ").
 
+:- pragma foreign_proc("Java",
+	array__min(_Array::array_ui, Min::out),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	/* Array not used */
+	Min = 0;
+").
+:- pragma foreign_proc("Java", 
+	array__min(_Array::in, Min::out),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	/* Array not used */
+	Min = 0;
+").
+
 :- pragma promise_pure(array__max/2).
 :- pragma foreign_proc("C", 
 	array__max(Array::array_ui, Max::out), 
@@ -643,6 +685,27 @@
 	}
 ").
 
+:- pragma foreign_proc("Java", 
+	array__max(Array::array_ui, Max::out), 
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	if (Array != null) {
+		Max = java.lang.reflect.Array.getLength(Array) - 1;
+	} else {
+		Max = -1;
+	}
+").
+:- pragma foreign_proc("Java",
+	array__max(Array::in, Max::out), 
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	if (Array != null) {
+		Max = java.lang.reflect.Array.getLength(Array) - 1;
+	} else {
+		Max = -1;
+	}
+").
+
 array__bounds(Array, Min, Max) :-
 	array__min(Array, Min),
 	array__max(Array, Max).
@@ -683,6 +746,27 @@
 	}
 ").
 
+:- pragma foreign_proc("Java",
+	array__size(Array::array_ui, Max::out),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	if (Array != null) {
+		Max = java.lang.reflect.Array.getLength(Array);
+	} else {
+		Max = 0;
+	}
+").
+:- pragma foreign_proc("Java",
+	array__size(Array::in, Max::out),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	if (Array != null) {
+		Max = java.lang.reflect.Array.getLength(Array);
+	} else {
+		Max = 0;
+	}
+").
+
 %-----------------------------------------------------------------------------%
 
 array__in_bounds(Array, Index) :-
@@ -753,6 +837,19 @@
 	Item = Array.GetValue(Index);
 }").
 
+:- pragma foreign_proc("Java",
+	array__unsafe_lookup(Array::array_ui, Index::in, Item::out),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	Item = java.lang.reflect.Array.get(Array, Index);
+").
+:- pragma foreign_proc("Java",
+	array__unsafe_lookup(Array::in, Index::in, Item::out),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	Item = java.lang.reflect.Array.get(Array, Index);
+").
+
 %-----------------------------------------------------------------------------%
 
 array__set(Array0, Index, Item, Array) :-
@@ -783,6 +880,15 @@
 	Array = Array0;
 }").
 
+:- pragma foreign_proc("Java",
+	array__unsafe_set(Array0::array_di, Index::in,
+		Item::in, Array::array_uo),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	java.lang.reflect.Array.set(Array0, Index, Item);
+	Array = Array0;			/* destructive update! */
+").
+
 %-----------------------------------------------------------------------------%
 
 /****
@@ -874,6 +980,43 @@
 	}
 ").
 
+:- pragma foreign_proc("Java",
+	array__resize(Array0::array_di, Size::in, Item::in,
+		Array::array_uo),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	java.lang.Class itemClass = Item.getClass();
+
+	if (Array0 == null) {
+		Array = java.lang.reflect.Array.newInstance(itemClass, Size);
+		for (int i = 0; i < Size; i++) {
+			java.lang.reflect.Array.set(Array, i, Item);
+		}
+	} else if (java.lang.reflect.Array.getLength(Array0) == Size) {
+		Array = Array0;
+	} else if (java.lang.reflect.Array.getLength(Array0) > Size) {
+		Array = java.lang.reflect.Array.newInstance(itemClass, Size);
+		for (int i = 0; i < Size; i++) {
+			java.lang.reflect.Array.set(Array, i,
+					java.lang.reflect.Array.get(Array0, i)
+					);
+		}
+	} else {
+		Array = java.lang.reflect.Array.newInstance(itemClass, Size);
+
+		int i;
+		for (i = 0; i < java.lang.reflect.Array.getLength(Array0); i++)
+		{
+			java.lang.reflect.Array.set(Array, i,
+					java.lang.reflect.Array.get(Array0, i)
+					);
+		}
+		for (/*i = Array0.length*/; i < Size; i++) {
+			java.lang.reflect.Array.set(Array, i, Item);
+		}
+	}
+").
+
 %-----------------------------------------------------------------------------%
 
 :- pragma foreign_decl("C", "
@@ -938,6 +1081,24 @@
 	System.Array.Copy(Array0, Array, Size);
 ").
 
+:- pragma foreign_proc("Java",
+	array__shrink_2(Array0::array_di, Size::in, Array::array_uo),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	if (Array0 == null) {
+		Array = null;
+	} else {
+		java.lang.Class itemClass = java.lang.reflect.Array.
+				get(Array0, 0).getClass();
+		Array = java.lang.reflect.Array.newInstance(itemClass, Size);
+		for (int i = 0; i < Size; i++) {
+			java.lang.reflect.Array.set(Array, i,
+					java.lang.reflect.Array.get(Array0, i)
+					);
+		}
+	}
+").
+
 %-----------------------------------------------------------------------------%
 
 :- pragma foreign_decl("C", "
@@ -1004,6 +1165,43 @@
 	Array = System.Array.CreateInstance(
 			Array0.GetType().GetElementType(), Array0.Length);
 	System.Array.Copy(Array0, Array, Array0.Length); 
+").
+
+:- pragma foreign_proc("Java",
+	array__copy(Array0::array_ui, Array::array_uo),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	if (Array0 == null) {
+		Array = null;
+	} else {
+		java.lang.Class itemClass = java.lang.reflect.Array.
+				get(Array0, 0).getClass();
+		int length = java.lang.reflect.Array.getLength(Array0);
+		Array = java.lang.reflect.Array.newInstance(itemClass, length);
+		for (int i = 0; i < length; i++) {
+			java.lang.reflect.Array.set(Array, i,
+					java.lang.reflect.Array.get(Array0, i)
+					);
+		}
+	}
+").
+:- pragma foreign_proc("Java",
+	array__copy(Array0::in, Array::array_uo),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	if (Array0 == null) {
+		Array = null;
+	} else {
+		java.lang.Class itemClass = java.lang.reflect.Array.
+				get(Array0, 0).getClass();
+		int length = java.lang.reflect.Array.getLength(Array0);
+		Array = java.lang.reflect.Array.newInstance(itemClass, length);
+		for (int i = 0; i < length; i++) {
+			java.lang.reflect.Array.set(Array, i,
+					java.lang.reflect.Array.get(Array0, i)
+					);
+		}
+	}
 ").
 
 %-----------------------------------------------------------------------------%
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list