StdArrayExtensions.icl 2.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
implementation module StdArrayExtensions

import StdEnv

// extensions for StdArray

createStrictArr :: !Int !a -> .{!a}
createStrictArr size el
	= createArray size el

createLazyArr :: !Int !a -> .{a}
createLazyArr size el
	= createArray size el

createString :: !Int !Char -> .String
createString size el
	= createArray size el

createUnboxedIntArr :: !Int !Int -> .{#Int}
createUnboxedIntArr size el
	= createArray size el

createUnboxedRealArr :: !Int !Real -> .{#Real}
createUnboxedRealArr size el
	= createArray size el

class updateArrElt a :: !(.e -> .e) !Int !*(a .e) -> *(a .e)

instance updateArrElt {}
  where
31 32 33
	updateArrElt f index array
		# (e,array) = array![index]
		= {array & [index] = f e}
34 35 36

instance updateArrElt {!}
  where
37 38 39
	updateArrElt f index array
		# (e,array) = array![index]
		= {array & [index] = f e}
40

41
class accArrElt a :: !(.e -> (.x, .e)) !Int !*(a .e) -> (!.x, !*(a .e))
42 43 44

instance accArrElt {}
  where
45 46 47 48
	accArrElt f index array
		# (e,array) = array![index]
		  (x,e) = f e
		= (x, {array & [index] = e})
49 50 51

instance accArrElt {!}
  where
52 53 54 55
	accArrElt f index array
		# (e,array) = array![index]
		  (x,e) = f e
		= (x, {array & [index] = e})
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

findlArrElt pred array i
	:== findl array i
  where
	findl array i
		| i>=size array || pred array.[i]
			= i
		= findl array (i+1)

findrArrElt pred array i
	:== findr array i
  where
	findr array i
		| i<0 || pred array.[i]
			= i
		= findr array (i-1)

createStrictArrIncFoldSt :: !Int !(Int .st -> (.a, .st)) !.st -> (!.{!.a}, !.st)
createStrictArrIncFoldSt size create_element st
	| size<0
		= abort "createStrictArrIncFoldSt: called with negative size parameter\n"
77 78 79 80 81 82 83 84 85 86 87
	# new_array = createUninitializedStrictArray size
	= createStrictArrIncFoldSt_loop 0 size create_element new_array st
	where
		createStrictArrIncFoldSt_loop :: !Int !Int !(Int .st -> (.a, .st)) !*{!.a} !.st -> (!.{!.a.}, !.st)
		createStrictArrIncFoldSt_loop frm to create_element new_array st
			| frm<>to
				# (new_element, st) = create_element frm st
				# new_array = {new_array & [frm] = new_element}
				= createStrictArrIncFoldSt_loop (frm+1) to create_element new_array st
				= (new_array, st)

88 89 90 91
createStrictArrDecFoldSt :: !Int !(Int .st -> (.a, .st)) !.st -> (!.{!.a}, !.st)
createStrictArrDecFoldSt size create_element st
	| size<0
		= abort "createStrictArrDecFoldSt: called with negative size parameter\n"
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	# new_array = createUninitializedStrictArray size
	= createStrictArrDecFoldSt_loop (size-1) create_element new_array st
	where
		createStrictArrDecFoldSt_loop :: !Int !(Int .st -> (.a, .st)) !*{!.a} !.st -> (!.{!.a.}, !.st)
		createStrictArrDecFoldSt_loop frm create_element new_array st
			| frm>=0
				# (new_element, st) = create_element frm st
				# new_array = {new_array & [frm] = new_element}
				= createStrictArrDecFoldSt_loop (frm-1) create_element new_array st
				= (new_array, st)

createUninitializedStrictArray :: !Int -> *{!.e}
createUninitializedStrictArray size
	= code inline {
		create_array_ _ 1 0
107
	}