Mercurial > repos > rhope
comparison kernel.rhope @ 96:5a08705f7610
Semi-broken cleanup of Array implementation
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Mon, 02 Aug 2010 05:12:19 -0400 |
parents | e73a93fb5de1 |
children | a34a982ecd32 |
comparison
equal
deleted
inserted
replaced
95:f4fd8962c385 | 96:5a08705f7610 |
---|---|
1 Import array.rhope | |
1 Import string.rhope | 2 Import string.rhope |
2 Import list.rhope | 3 Import list.rhope |
3 Import functional.rhope | 4 Import functional.rhope |
4 Import file.rhope | 5 Import file.rhope |
5 Import dict.rhope | 6 Import dict.rhope |
186 Mod[a,b:out] | 187 Mod[a,b:out] |
187 { | 188 { |
188 out <- [a]-[[[a]/[b]]*[b]] | 189 out <- [a]-[[[a]/[b]]*[b]] |
189 } | 190 } |
190 | 191 |
191 | |
192 Blueprint Array | |
193 { | |
194 Eltype(Blueprint) | |
195 Length(Int32,Naked) | |
196 Storage(Int32,Naked) | |
197 } | |
198 | |
199 Foreign C:runtime | 192 Foreign C:runtime |
200 { | 193 { |
201 _internal_array_copyout[array(Array), index(Int32,Naked), dest(Any Type,Boxed,Mutable):dest] | |
202 _internal_array_copyin[array(Array,Boxed,Mutable), index(Int32,Naked), val:array] | |
203 _internal_array_getboxed[array(Array), index(Int32,Naked):out] | |
204 _internal_array_setboxed[array(Array,Boxed,Mutable), index(Int32,Naked), val:array] | |
205 _internal_array_allocboxed[size(Int32,Naked):out(Array)] | |
206 _internal_array_allocnaked[size(Int32,Naked),type(Blueprint):out(Array)] | |
207 _internal_blueprint_eq[left(Blueprint),right(Blueprint):out(Int32,Naked)] | 194 _internal_blueprint_eq[left(Blueprint),right(Blueprint):out(Int32,Naked)] |
208 _internal_worker_alloc[size(Int16,Naked):out(Worker)] | 195 _internal_worker_alloc[size(Int16,Naked):out(Worker)] |
209 _internal_worker_setinput[worker(Worker,Boxed,Mutable),num(Int16,Naked),val:worker] | 196 _internal_worker_setinput[worker(Worker,Boxed,Mutable),num(Int16,Naked),val:worker] |
210 _internal_worker_getinput[worker(Worker),num(Int16,Naked):out] | 197 _internal_worker_getinput[worker(Worker),num(Int16,Naked):out] |
211 _internal_worker_hasinput[worker(Worker),num(Int16,Naked):out(Int32,Naked)] | 198 _internal_worker_hasinput[worker(Worker),num(Int16,Naked):out(Int32,Naked)] |
262 =@Blueprint[left,right:out] | 249 =@Blueprint[left,right:out] |
263 { | 250 { |
264 out <- [_internal_blueprint_eq[left,right]]!=[0] | 251 out <- [_internal_blueprint_eq[left,right]]!=[0] |
265 } | 252 } |
266 | 253 |
267 Array[:out(Array)] | |
268 { | |
269 out <- [[_internal_array_allocboxed[0] | |
270 ]Length <<[0] | |
271 ]Storage <<[0] | |
272 } | |
273 | |
274 First@Array[array:out(Int32),empty] | |
275 { | |
276 ,empty <- If[[array]Length >>] | |
277 { out <- 0 } | |
278 } | |
279 | |
280 Next@Array[array,current:out(Int32),empty] | |
281 { | |
282 next <- [current]+[1] | |
283 ,empty <- If[[next] < [[array]Length >>]] | |
284 { | |
285 out <- Val[next] | |
286 } | |
287 } | |
288 | |
289 Last@Array[array:out(Int32),empty] | |
290 { | |
291 ,empty <- If[[array]Length >>] | |
292 { out <- [[array]Length >>] - [1] } | |
293 } | |
294 | |
295 Append@Array[array,newval:out(Array)] | |
296 { | |
297 out <- [array]Set[[array]Length >>, newval] | |
298 } | |
299 | |
300 Index@Array[array,index(Int32):out,notfound] | |
301 { | |
302 ,notfound <- If[[index] >= [0]] | |
303 { | |
304 ,notfound <- If[[index] < [[array]Length >>]] | |
305 { | |
306 eltype <- [array]Eltype >> | |
307 If[[eltype] = [Any Type()]] | |
308 { | |
309 out <- _internal_array_getboxed[array, index] | |
310 }{ | |
311 out <- _internal_array_copyout[array, index, Build[eltype]] | |
312 } | |
313 } | |
314 } | |
315 } | |
316 | |
317 _Copy to Boxed[source,dest,current:out] | |
318 { | |
319 ndest <- _internal_array_setboxed[dest, current, [source]Index[current]] | |
320 | |
321 [source]Next[current] | |
322 { | |
323 out <- _Copy to Boxed[source, ndest, ~] | |
324 }{ | |
325 out <- Val[ndest] | |
326 } | |
327 } | |
328 | |
329 _Copy Naked[source,dest,current:out] | |
330 { | |
331 ndest <- _internal_array_copyin[dest, current, [source]Index[current]] | |
332 | |
333 [source]Next[current] | |
334 { | |
335 out <- _Copy Naked[source, ndest, ~] | |
336 }{ | |
337 out <- Val[ndest] | |
338 } | |
339 } | |
340 | |
341 Set@Array[array,index(Int32),val:out(Array)] | |
342 { | |
343 If[[index] < [[array]Storage >>]] | |
344 { | |
345 If[[index] > [[array]Length >>]] | |
346 { | |
347 farray <- [[array]Set[[index]-[1], val]]Length <<[ [index]+[1] ] | |
348 }{ | |
349 If[[index] = [[array]Length >>]] | |
350 { | |
351 farray <- [array]Length <<[ [index]+[1] ] | |
352 }{ | |
353 farray <- Val[array] | |
354 } | |
355 } | |
356 eltype <- [array]Eltype >> | |
357 If[[eltype] = [Any Type()]] | |
358 { | |
359 out <- _internal_array_setboxed[farray, index, val] | |
360 }{ | |
361 If[[Blueprint Of[val]] = [eltype]] | |
362 { | |
363 out <- _internal_array_copyin[farray, index, val] | |
364 }{ | |
365 boxed <- _internal_array_allocboxed[[farray]Storage >>] | |
366 [farray]First | |
367 { | |
368 copied <- [_Copy to Boxed[farray, boxed, ~]]Length <<[ [farray]Length >> ] | |
369 }{ | |
370 Print["Uh oh, no First on Naked Array!"] | |
371 //I don't think this case should happen normally | |
372 copied <- Val[boxed] | |
373 } | |
374 out <- _internal_array_setboxed[copied, index, val] | |
375 } | |
376 } | |
377 }{ | |
378 If[[array]Length >>] | |
379 { | |
380 If[[index] < [4]] | |
381 { | |
382 new storage <- [index]+[index] | |
383 }{ | |
384 new storage <- [index]+[[index]RShift[1]] | |
385 } | |
386 | |
387 do boxed <- If[[[array]Eltype >>] = [Any Type()]] | |
388 { | |
389 copied <- _Copy to Boxed[array, _internal_array_allocboxed[new storage], 0] | |
390 }{ | |
391 bp <- Blueprint Of[val] | |
392 If[[[array]Eltype >>] = [bp]] | |
393 { | |
394 copied <- _Copy Naked[array, _internal_array_allocnaked[new storage, bp], 0] | |
395 }{ | |
396 copied <- _Copy to Boxed[array, _internal_array_allocboxed[new storage], 0] | |
397 } | |
398 } | |
399 out <- [[copied]Length <<[[array]Length >>]]Set[index,val] | |
400 }{ | |
401 len <- [index]+[1] | |
402 out <- [_internal_array_allocnaked[len, Blueprint Of[val]] | |
403 ]Set[index,val] | |
404 } | |
405 } | |
406 } | |
407 | |
408 Length@Array[arr:out] | |
409 { | |
410 out <- [arr]Length >> | |
411 } | |
412 | |
413 Call@Array[arr(Array),index(Int32):out] | |
414 { | |
415 out <- [arr]Index[index] | |
416 } | |
417 | |
418 _Copy Part Naked[source,dest,srcindex,destindex:out] | |
419 { | |
420 ndest <- _internal_array_copyin[dest, destindex, [source]Index[srcindex]] | |
421 | |
422 [source]Next[srcindex] | |
423 { | |
424 out <- _Copy Part Naked[source, ndest, ~, [destindex]+[1]] | |
425 }{ | |
426 out <- Val[ndest] | |
427 } | |
428 } | |
429 | |
430 _Copy Part Boxed[source,dest,srcindex,destindex:out] | |
431 { | |
432 ndest <- _internal_array_setboxed[dest, destindex, [source]Index[srcindex]] | |
433 | |
434 [source]Next[srcindex] | |
435 { | |
436 out <- _Copy Part Boxed[source, ndest, ~, [destindex]+[1]] | |
437 }{ | |
438 out <- Val[ndest] | |
439 } | |
440 } | |
441 | |
442 Slice@Array[arr,slicepoint(Int32):left,right] | |
443 { | |
444 If[[slicepoint]<[[arr]Length]] | |
445 { | |
446 If[[slicepoint]>[0i32]] | |
447 { | |
448 eltype <- [arr]Eltype >> | |
449 If[[eltype] = [Any Type()]] | |
450 { | |
451 _Copy Part Boxed[arr, _internal_array_allocboxed[[[arr]Length]-[slicepoint]], slicepoint, 0] | |
452 }{ | |
453 _Copy Part Naked[arr, _internal_array_allocnaked[[[arr]Length]-[slicepoint], eltype], slicepoint, 0] | |
454 } | |
455 left <- [arr]Length <<[slicepoint] | |
456 }{ | |
457 right <- arr | |
458 left <- Array[] | |
459 } | |
460 }{ | |
461 left <- arr | |
462 right <- Array[] | |
463 } | |
464 | |
465 } | |
466 | |
467 And[left,right:out] | 254 And[left,right:out] |
468 { | 255 { |
469 ,out <- If[left] | 256 ,out <- If[left] |
470 { | 257 { |
471 out,out <- If[right] | 258 out,out <- If[right] |