| 1 |
/* |
| 2 |
* Copyright (C)2005-2022 Haxe Foundation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
function string( a : 'a array ) : string { |
| 24 |
neko(" |
| 25 |
var s = '['; |
| 26 |
var items = a[0]; |
| 27 |
var l = a[2]; |
| 28 |
var i = 0; |
| 29 |
while i < l { |
| 30 |
s = s + @Core.string(items[i]); |
| 31 |
i = i + 1; |
| 32 |
if i < l |
| 33 |
s = s + ', '; |
| 34 |
} |
| 35 |
s = s + ']'; |
| 36 |
s |
| 37 |
"); |
| 38 |
}; |
| 39 |
|
| 40 |
neko(" |
| 41 |
@make = function(a) { |
| 42 |
$array(a,string,$asize(a)); |
| 43 |
}; |
| 44 |
@merge_sort = $loader.loadprim('std@merge_sort',3); |
| 45 |
$exports.@make = @make; |
| 46 |
"); |
| 47 |
|
| 48 |
function length( a : 'a array ) : int { |
| 49 |
neko("a[2]"); |
| 50 |
} |
| 51 |
|
| 52 |
function create() : 'a array { |
| 53 |
neko("$array($array(),string,0)"); |
| 54 |
} |
| 55 |
|
| 56 |
function add( a : 'a array, x : 'a ) : void { |
| 57 |
neko(" |
| 58 |
var k = a[0]; |
| 59 |
var n = a[2]; |
| 60 |
if( $asize(k) == n ) { |
| 61 |
k = $amake(n*2+1); |
| 62 |
$ablit(k,0,a[0],0,n); |
| 63 |
a[0] = k; |
| 64 |
} |
| 65 |
k[n] = x; |
| 66 |
a[2] = n + 1; |
| 67 |
"); |
| 68 |
} |
| 69 |
|
| 70 |
function get( a : 'a array, p : int ) : 'a { |
| 71 |
a.[p] |
| 72 |
} |
| 73 |
|
| 74 |
function set( a : 'a array, p : int, x : 'a ) : void { |
| 75 |
a.[p] := x; |
| 76 |
} |
| 77 |
|
| 78 |
function make( size : int, x : 'a ) : 'a array { |
| 79 |
neko(" |
| 80 |
var a = $amake(size); |
| 81 |
var i = 0; |
| 82 |
while( i < size ) { |
| 83 |
a[i] = x; |
| 84 |
i = i + 1; |
| 85 |
} |
| 86 |
$array(a,string,size) |
| 87 |
"); |
| 88 |
} |
| 89 |
|
| 90 |
function init( size : int, f : int -> 'a ) : 'a array { |
| 91 |
neko(" |
| 92 |
var a = $amake(size); |
| 93 |
var i = 0; |
| 94 |
while( i < size ) { |
| 95 |
a[i] = $apply(f,i); |
| 96 |
i = i + 1; |
| 97 |
} |
| 98 |
$array(a,string,size) |
| 99 |
"); |
| 100 |
} |
| 101 |
|
| 102 |
function iter( f : 'a -> void, a : 'a array ) : void { |
| 103 |
neko(" |
| 104 |
var l = a[2]; |
| 105 |
var i = 0; |
| 106 |
a = a[0]; |
| 107 |
while (i < l) { |
| 108 |
f(a[i]); |
| 109 |
i = i + 1; |
| 110 |
} |
| 111 |
"); |
| 112 |
} |
| 113 |
|
| 114 |
function iteri( f : int -> 'a -> void, a : 'a array ) : void { |
| 115 |
neko(" |
| 116 |
var l = a[2]; |
| 117 |
var i = 0; |
| 118 |
a = a[0]; |
| 119 |
while (i < l) { |
| 120 |
f(i,a[i]); |
| 121 |
i = i + 1; |
| 122 |
} |
| 123 |
"); |
| 124 |
} |
| 125 |
|
| 126 |
function map( f : 'a -> 'b, a : 'a array ) : 'b array { |
| 127 |
neko(" |
| 128 |
var l = a[2]; |
| 129 |
var i = 0; |
| 130 |
var a2 = $amake(l); |
| 131 |
a = a[0]; |
| 132 |
while i < l { |
| 133 |
a2[i] = $apply(f,a[i]); |
| 134 |
i = i + 1; |
| 135 |
} |
| 136 |
$array(a2,string,l); |
| 137 |
"); |
| 138 |
} |
| 139 |
|
| 140 |
function sort( f : 'a -> 'a -> int, a : 'a array ) : void { |
| 141 |
neko(" |
| 142 |
@merge_sort(a[0],a[2],f) |
| 143 |
"); |
| 144 |
} |
| 145 |
|
| 146 |
function list( a : 'a array ) : 'a list { |
| 147 |
neko(" |
| 148 |
var i = a[2]; |
| 149 |
var l = @Core.@empty; |
| 150 |
a = a[0]; |
| 151 |
while i > 0 { |
| 152 |
i = i - 1; |
| 153 |
l = @Core.@cons(a[i],l); |
| 154 |
} |
| 155 |
l |
| 156 |
"); |
| 157 |
} |
| 158 |
|
| 159 |
function append( src : 'a array, dst : 'a array ) : void { |
| 160 |
neko(" |
| 161 |
var l = src[2]; |
| 162 |
var i = 0; |
| 163 |
src = src[0]; |
| 164 |
while i < l { |
| 165 |
add(dst,src[i]); |
| 166 |
i = i + 1; |
| 167 |
} |
| 168 |
"); |
| 169 |
} |
| 170 |
|
| 171 |
function sub( a : 'a array, p : int, l : int ) : 'a array { |
| 172 |
neko(" |
| 173 |
var al = a[2]; |
| 174 |
if( p + l > al ) $throw('$asub'); |
| 175 |
$array($asub(a[0],p,l),string,l) |
| 176 |
"); |
| 177 |
} |
| 178 |
|
| 179 |
function blit( dst : 'a array, pdst : int, src : 'a array, psrc : int, len : int ) { |
| 180 |
neko(" |
| 181 |
$ablit(dst[0],pdst,src[0],psrc,len) |
| 182 |
"); |
| 183 |
} |
| 184 |
|
| 185 |
function index( a : 'a array, it : 'a ) : int { |
| 186 |
neko(" |
| 187 |
var al = a[2]; |
| 188 |
a = a[0]; |
| 189 |
var i = 0; |
| 190 |
while( i < al ) { |
| 191 |
if( @Core.@compare(a[i],it) == 0 ) |
| 192 |
return i; |
| 193 |
i = i + 1; |
| 194 |
} |
| 195 |
"); |
| 196 |
throw Not_found |
| 197 |
} |
| 198 |
|