Commits

dtrg  committed e6afc76

Changed Array.ch to use the New Way of doing APIs. Added Array.Sort().

  • Participants
  • Parent commits 2e9d16d

Comments (0)

Files changed (8)

File include/Array.ch

 	function bounds(): (low: int, high: int);
 };
 
-/** Creates a 0-based one-dimensional array.
- **
- ** All entries in the array are initialised to the same, specified, value.
- **/
- 
-function Array<T>(size: int, initialiser: T): Array<T>
-{
-	var ptr: __extern = extern(__extern);
-	extern "${ptr} = calloc(${size}, sizeof(${initialiser}));";
-	
-	return
-	{
-		implements Array<T>;
-
-		function _boundscheck(i: int)
-		{
-			if (i < 0)
-				Application.AbortOutOfBounds();
-			else if (i >= size)
-				Application.AbortOutOfBounds(); 
-		}
-				
-		function get(i: int): (result: T)
-		{
-			_boundscheck(i);
-			result = extern(initialiser);
-			extern "${result} = ((typeof(${result})*)${ptr})[${i}];";
-		}
-		
-		function set(i: int, value: T)
-		{
-			_boundscheck(i);
-			extern "((typeof(${value})*)${ptr})[${i}] = ${value};";
-		}
-		
-		function bounds(): (low: int, high: int)
-		{
-			low = 0;
-			high = size;
-		}
-	};
-}
-
 /** Represents a mutable two-dimensional array.
  **/
  
 	function bounds(): (lowx: int, highx: int, lowy: int, highy: int);
 };
 
-/** Creates a 0-based two-dimensional array.
- **
- ** All entries in the array are initialised to the same, specified, value.
- **/
- 
-function Array2D<T>(width: int, height: int, initialiser: T): Array2D<T>
+var Array =
 {
-	var ptr: __extern = extern(__extern);
-	extern "${ptr} = calloc(${width}*${height}, sizeof(${initialiser}));";
+	/** Creates a 0-based one-dimensional array.
+	 **
+	 ** All entries in the array are initialised to the same, specified, value.
+	 **/
+	 
+	function New<T>(size: int, initialiser: T): Array<T>
+	{
+		var ptr: __extern = extern(__extern);
+		extern "${ptr} = calloc(${size}, sizeof(${initialiser}));";
+		
+		return
+		{
+			implements Array<T>;
 	
-	return
+			function _boundscheck(i: int)
+			{
+				if (i < 0)
+					Application.AbortOutOfBounds();
+				else if (i >= size)
+					Application.AbortOutOfBounds(); 
+			}
+					
+			function get(i: int): (result: T)
+			{
+				_boundscheck(i);
+				result = extern(initialiser);
+				extern "${result} = ((typeof(${result})*)${ptr})[${i}];";
+			}
+			
+			function set(i: int, value: T)
+			{
+				_boundscheck(i);
+				extern "((typeof(${value})*)${ptr})[${i}] = ${value};";
+			}
+			
+			function bounds(): (low: int, high: int)
+			{
+				low = 0;
+				high = size;
+			}
+		};
+	}
+	
+	/** Creates a 0-based two-dimensional array.
+	 **
+	 ** All entries in the array are initialised to the same, specified, value.
+	 **/
+	 
+	function New2D<T>(width: int, height: int, initialiser: T): Array2D<T>
 	{
-		implements Array2D<T>;
-
-		function _boundscheck(x: int, y: int)
+		var ptr: __extern = extern(__extern);
+		extern "${ptr} = calloc(${width}*${height}, sizeof(${initialiser}));";
+		
+		return
 		{
-			if (x < 0)
-				Application.AbortOutOfBounds();
-			if (x >= width)
-				Application.AbortOutOfBounds();
-			if (y < 0)
-				Application.AbortOutOfBounds();
-			if (y >= height)
-				Application.AbortOutOfBounds();
-		}
-				
-		function get(x: int, y: int): (result: T)
+			implements Array2D<T>;
+	
+			function _boundscheck(x: int, y: int)
+			{
+				if (x < 0)
+					Application.AbortOutOfBounds();
+				if (x >= width)
+					Application.AbortOutOfBounds();
+				if (y < 0)
+					Application.AbortOutOfBounds();
+				if (y >= height)
+					Application.AbortOutOfBounds();
+			}
+					
+			function get(x: int, y: int): (result: T)
+			{
+				_boundscheck(x, y);
+				result = extern(initialiser);
+				extern "${result} = ((typeof(${result})*)${ptr})[${x} + ${y}*${width}];";
+			}
+			
+			function set(x: int, y: int, value: T)
+			{
+				_boundscheck(x, y);
+				extern "((typeof(${value})*)${ptr})[${x} + ${y}*${width}] = ${value};";
+			}
+			
+			function bounds(): (lowx: int, highx: int, lowy: int, highy: int)
+			{
+				lowx = 0;
+				highx = width;
+				lowy = 0;
+				highy = height;
+			}
+		};
+	}
+	
+	/** Sorts a 1D array.
+	 **/
+	 
+	function Sort<T>(array: Array<T>)
+	{
+		function swap(a: int, b: int)
 		{
-			_boundscheck(x, y);
-			result = extern(initialiser);
-			extern "${result} = ((typeof(${result})*)${ptr})[${x} + ${y}*${width}];";
+			var t = array.get(a);
+			array.set(a, array.get(b));
+			array.set(b, t);
 		}
 		
-		function set(x: int, y: int, value: T)
+		function sortlet(left: int, right: int)
 		{
-			_boundscheck(x, y);
-			extern "((typeof(${value})*)${ptr})[${x} + ${y}*${width}] = ${value};";
+		    if (left < right)
+		    {
+				var pivot = array.get((left + right) / 2);
+				var ln = left;
+				var rn = right;
+ 
+				do
+				{
+					while (array.get(ln) < pivot)
+						ln = ln + 1;
+					while (pivot < array.get(rn))
+						rn = rn - 1;
+						
+					if (ln <= rn)
+					{
+						swap(ln, rn);
+						ln = ln + 1;
+						rn = rn - 1;
+					}
+				}
+				while (ln <= rn);
+
+				sortlet(left, rn);
+				sortlet(ln, right);
+			}
 		}
 		
-		function bounds(): (lowx: int, highx: int, lowy: int, highy: int)
-		{
-			lowx = 0;
-			highx = width;
-			lowy = 0;
-			highy = height;
-		}
-	};
-}
+		var lo, hi = array.bounds();
+		sortlet(lo, hi-1); 
+	}
+};
 
 #endif

File include/PCRE.ch

 				if (num_captures < 0)
 					num_captures = 0;
 
-				var captures = Array<string>(num_captures, "");
+				var captures = Array.New<string>(num_captures, "");
 				for i = 0, num_captures
 				{
 					var startoffset = extern(int);
 # information.
 
 for f in \
+	test/integer-test.cow \
+	test/sort-test.cow \
 	test/file-test.cow \
 	test/pcre-test.cow \
 	test/buffer-test.cow \
 	test/bug-function-scopes.cow \
 	test/bug-inherited-stack-frame.cow \
 	test/deep-upvalues-test.cow \
-	test/integer-test.cow \
 	test/interfaces-test.cow \
 	test/life.cow \
 	test/linked-list-test.cow \

File test/array-test.cow

 
 function test()
 {
-	var i = Array<int>(4, 7);
+	var i = Array.New<int>(4, 7);
 	var min, length = i.bounds();
 	println("i.length() = " + length.toString());
 	
 
 function multitest()
 {
-	var array = Array<Array<int>>(4, Array<int>(0, 0));
+	var array = Array.New<Array<int>>(4, Array.New<int>(0, 0));
 	
 	for i = 0, 4
-		array.set(i, Array<int>(4, 0));
+		array.set(i, Array.New<int>(4, 0));
 	
 	dump_matrix(array);
 	

File test/generics-example.cow

 	var low2, high2 = a2.bounds();
 	var length1 = high1 - low1;
 	var length2 = high2 - low2;
-	var result = Array<T>(length1 + length2, 0);
+	var result = Array.New<T>(length1 + length2, 0);
 
 	var o = 0;
 	for i = low1, high1
 	return result;
 }
 
-var a1 = Array<int>(6, 1);
-var a2 = Array<int>(3, 2);
+var a1 = Array.New<int>(6, 1);
+var a2 = Array.New<int>(3, 2);
 var a3 = append<int>(a1, a2);
 

File test/life.cow

 var width = 10;
 var height = 10;
 
-var buffera = Array2D<int>(width, height, 0);
-var bufferb = Array2D<int>(width, height, 0);
+var buffera = Array.New2D<int>(width, height, 0);
+var bufferb = Array.New2D<int>(width, height, 0);
 var front = buffera;
 var back = bufferb;
 

File test/sort-test.cow

+/* cowbel test suite
+ *
+ * Written in 2012 by David Given.
+ *
+ * To the extent possible under law, the author has dedicated all copyright
+ * and related and neighboring rights to this software to the public domain
+ * worldwide. This software is distributed without any warranty.
+ *
+ * Please see the file COPYING.CC0 in the distribution package for more
+ * information.
+ */
+
+#include "SimpleIO.ch"
+#include "Array.ch"
+
+var next = 1;
+var RAND_MAX = 65535;
+
+function rand(): int
+{
+	next = (next * 1103515245) + 12345;
+    var v = (next / (2 * (RAND_MAX + 1)) % (RAND_MAX+1));
+    return (v & 65535);
+}
+
+var COUNT = 20;
+var a = Array.New<int>(COUNT, 0);
+for i = 0, COUNT
+	a.set(i, rand());
+
+Array.Sort<int>(a);
+
+for i = 0, COUNT
+	println(a.get(i).toString());
+	

File test/sort-test.cow.log.pristine

+1374
+2043
+4542
+5056
+6383
+8419
+16112
+51966
+52032
+52862
+55183
+57259
+57910
+57924
+59744
+60658
+61697
+61721
+61936
+64678