Hi,

This may be something stupid I I've done, but I've been looking at it for an hour, so ...

While people are looking, if you uncomment out the longhand subscript function, it works. An implementation of a functional array using the Avl Tree. Only creates entries for assigned indices. Returns a supplied default value for unassigned indices. Would be particularly good for sparse array applications.


Jonathan.


$ flx avl/array.flx
avl/array.cpp: In member function `flxusr::array::_poly_1972t_6882 flxusr::array::find::apply(const flxusr::array::_tt6885&)':
avl/array.cpp:2096: error: `ptf' undeclared (first use this function)
avl/array.cpp:2096: error: (Each undeclared identifier is reported only once for each function it appears in.)

#import <flx.flxh>;

open List;
open C_hack;
open Avl;
open Double;

open VArray;

module VArray {
    class aNode[T] {
        var index:int;
        var element:T;

        ctor(idx:int, x:T) {
            index = idx;
            element = x;
        }
    }

    private fun cmp[T](x:aNode[T], y:aNode[T]) = {
        return cmp(x.index, y.index);
    }

    class vArray[T] {
        var arr:avl[aNode[T]];
        var dflt:T;

        ctor(value:T) {
            dflt = value;
            arr = Nil[aNode[T]];
        }
        proc set(i:int, x:T) {
            var z <- new aNode[T](i, x);
            arr = insert(arr, z, cmp of (aNode[T]*aNode[T]));
        }
        fun get(idx:int):T = {
            var z <- new aNode[T](idx, dflt);
            return
                match find(arr, z, cmp of (aNode[T]*aNode[T])) with
                    | None => dflt
                    | Some ?x => x.element
                endmatch
            ;
        }
    }

//    fun subscript[T](arr:vArray[T], idx:int):T = {
//        var z <- new aNode[T](idx, arr.dflt);
//        return
//            match find(arr.arr, z, cmp of (aNode[T]*aNode[T])) with
//                | None[T] => arr.dflt
//                | Some ?x => x.element
//            endmatch
//        ;
//    }

    fun subscript[T](arr:vArray[T], idx:int):T => arr.get(idx); 

}

fun cmp(x:aNode[double], y:aNode[double]) = {
    return cmp(x.index, y.index);
}

proc pr_node[T] (d:int, x:aNode[T]) {
    print$ "  " * d;
    print$ f"B(%d)\n" (x.index);
}

proc pr_treeT[T](tree:avl[aNode[T]]) {
   Avl::iter[aNode[T]] ((pr_node of (int*aNode[T])), tree);
}

proc pr_node (d:int, x:aNode[int]) {
    print$ "  " * d;
    print$ f"B(%d.%d)\n" (x.index, x.element);
}

proc pr_node (d:int, x:aNode[double]) {
    print$ "  " * d;
    print$ f"B(%d=%.1f)\n" (x.index, x.element);
}

proc pr_treeD(tree:avl[aNode[double]]) {
    Avl::iter[aNode[double]] (pr_node of (int*aNode[double]), tree);
}

proc test(ar:vArray[double], n:int, m:int) {
    if n <= m do
        ar.set(Cstdlib::rand() % 30, double(Cstdlib::rand() % 123) / 7.0);
        test(ar, n + 1, m);
    done;
}

proc flx_main() {
    var cnt:int;
    if System::argc == 2 do
        cnt = String::atoi (System::argv 1);
    else
        cnt = 10;
    done;

    var ar <- new vArray[double](0.0);

    Cstdlib::srand (101U);


    test(ar, 1, cnt);
    pr_treeD(ar.arr);
    endl;

    var ix:int;

    forall ix in 1 upto 30 do
        print$ f"%2d: %.1f\n" (ix, ar.get(ix));
    done;
    endl;
    forall ix in 1 upto 30 do
        print$ f"%2d: %.1f\n" (ix, ar.[ix]);
    done;
}


flx_main();
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to