Created
November 27, 2016 20:17
-
-
Save gbluma/5256dece2d967fdde30b3d571b2ce374 to your computer and use it in GitHub Desktop.
Work in progress of a non-initialized array w/ iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include "std/gc"; | |
open Gc; | |
// doesn't seem to do anything yet. | |
private body raw_array_cleanup_func = | |
""" | |
static void _raw_array_cleanup ( ::flx::gc::generic::collector_t *, void*) { | |
printf("Cleaning up\\n"); | |
} | |
"""; | |
_gc_pointer type raw_array [T] = "?1 *" | |
requires finaliser "_raw_array_cleanup", | |
raw_array_cleanup_func; | |
fun _ctor_raw_array [T] : size -> raw_array[T] = "(?1 *)(new ?1[$1])"; | |
fun zero_element [T] : raw_array[T] -> &T = "&$1[0]"; | |
fun incr_ptr [T] : &T * size -> &T = "(?1 *) ($1 + $2)"; | |
fun at[T] : raw_array[T] * size -> T = "*(&$1[$2])"; | |
fun get[T] (a:raw_array[T], s:size) => incr_ptr (a.zero_element, s); | |
proc set[T] (a:raw_array[T], s:size, v:T) = { | |
var pt = get(a, s); | |
pt <- v; | |
} | |
proc set[T] (a:raw_array[T])(s:size, v:T) = { set[T](a,s,v); } | |
proc delete[T] : raw_array[T] = "delete [] $1;"; | |
struct Array[T] | |
{ | |
data:raw_array[T]; | |
count:size; | |
}; | |
ctor[T, S in ints] Array[T] (s:S) => | |
Array ( (raw_array[T] s.size), s.size); | |
proc set[T,S in ints] (a:Array[T], s:S, v:T) => a.data.set(s.size, v); | |
proc set[T,S in ints] (a:Array[T])(s:S, v:T) => a.data.set(s.size, v); | |
fun get[T, S in ints] (a:Array[T], s:S) => get(a.data, s.size); | |
fun get[T, S in ints] (a:Array[T])(s:S) => get(a.data, s.size); | |
gen iterator[T](arr:Array[T]) () : opt[T] = | |
{ | |
if arr.count > 0uz do | |
for var j in 0uz upto arr.count - 1uz do | |
yield Some (arr.data `at` j); | |
done | |
done | |
return None[T]; | |
} | |
proc memcpy[T] : &T * &T * size = "::std::memcpy($1,$2,$3);"; | |
fun copyto[T] (a:Array[T], b:Array[T]) = { | |
if a.count > b.count do | |
eprintln$ "Cannot copy memory to smaller destination"; | |
System::exit(1); | |
done | |
memcpy(a.data.zero_element, b.data.zero_element, a.count); | |
} | |
instance[T] Str[Array[T]] | |
{ | |
fun str(t:Array[T]) = { | |
var o = "Array[data=["; | |
for i in 0uz ..< t.count - 1uz do | |
var it = (*get(t.data, i.size)); | |
o += it.str + ","; | |
done | |
o += (*get(t.data, t.count - 1uz)).str; | |
o += "], count=" + t.count.str + "]"; | |
return o; | |
} | |
} | |
// DEMO | |
module B | |
{ | |
var a = Array[int] 10; | |
println$ "array before zeroing:"; | |
for i in a perform println$ i; | |
println$ "zeroing memory"; | |
for i in 0uz ..< a.count perform set(a.data,i,0); | |
println$ "same array after zeroing:"; | |
for i in a perform println$ i; | |
// delete a; // free memory | |
println$ a; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment