Ignore:
Timestamp:
Oct 11, 2011, 5:42:20 PM (9 years ago)
Author:
tranquil
Message:
  • changed implementation of constant indexings with extensible arrays
  • work on ASM completed
  • next: optimizations!
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Deliverables/D2.2/8051-indexed-labels-branch/src/utilities/extArray.ml

    r1349 r1357  
    1 type +'a t = {
     1type 'a t = {
    22        mutable t_cont : 'a array;
    33        mutable t_length : int;
     
    55}
    66
    7 let default_buffer_size = 4
    8 
    9 let make n a =
    10         let m = min default_buffer_size n in
     7let make ?(buff = 16) n a =
     8        let m = max 1 (max buff n) in
    119        {t_cont = Array.make m a; t_length = n; t_def = a}
    1210       
     
    2018let length v = v.t_length
    2119
    22 let chop v n = v.t_length <- max 0 (v.t_length - n)
     20let chop v n =
     21        let n = min n v.t_length in
     22        Array.fill v.t_cont (v.t_length - n) v.t_length v.t_def; 
     23        v.t_length <- v.t_length - n
    2324
    24 let assure v n =
     25let ensure v n =
    2526        let m = Array.length v.t_cont in
    26         (if n >= m then
     27        if n >= m then
    2728                let d = n / m + 1 in
    2829                let new_arr = Array.make (d * m) v.t_def in
    2930                Array.blit v.t_cont 0 new_arr 0 m;
    30                 v.t_cont <- new_arr);
    31    v.t_length <- max v.t_length (m + 1)
     31                v.t_cont <- new_arr;
     32        else ();
     33  v.t_length <- max (n + 1) v.t_length
    3234
    3335       
    3436       
    35 let extend v n = assure v (v.t_length + n)
     37let extend v n = ensure v (v.t_length + n - 1)
    3638
    3739let set v n a =
    3840          (if n < 0 then invalid_arg "out of bounds");
    39     assure v n;
    40                 v.t_cont.(n) <- a;
     41    ensure v n;
     42                v.t_cont.(n) <- a
    4143
    4244let append v a = set v (v.t_length) a
    43        
     45
     46let reclaim ?(packet = 16) v =
     47  let new_l = v.t_length - (v.t_length - 1) mod packet + packet - 1 in
     48        let new_l = max 1 new_l in
     49        if new_l <> Array.length v.t_cont then
     50                let new_cont = Array.make new_l v.t_def in
     51                Array.blit v.t_cont 0 new_cont 0 v.t_length;
     52                v.t_cont <- new_cont
     53
     54exception Finish_iter
     55
     56let iteri f v =
     57        let f' i a = if i < v.t_length then f i a else raise Finish_iter in
     58        try Array.iteri f' v.t_cont with Finish_iter -> ()
     59       
     60let iter f = iteri (fun _ -> f)
     61
     62let fold_left f a v =
     63        let res = ref a in
     64        let f' b = res := f !res b in
     65        iter f' v; !res
     66
     67let fold_right f v a =
     68        let funct = ref (fun x -> x) in
     69        let f' b = let funct' = !funct in funct := fun x -> funct' (f b x) in
     70        iter f' v; !funct a
     71
     72       
    4473       
    4574
Note: See TracChangeset for help on using the changeset viewer.