Hi all!

I've been testing the new varray a little,
by making a very basic vector implementation.
You can find the code below.

A few questions showed up during the hacking...

In std.flx you find:
fun subscript[t]: varray[t] * !ints -> lvalue[t] = "$1[$2]";
What does !ints mean?

Is there a way to put string or bool in a varray?

Cheers Klas

//-------------------------------------------------

#import <flx.flxh>

print_line "The program has started!!!!";

proc pr[t] : t = "std::cout<<$1;";

var int_vector <- new Felix_Vector::flxVec[int](4ul);

var ul_index: ulong = 0ul;
int_vector.print_vector();
int_vector.insert(ul_index,1);
int_vector.insert(ul_index,2);
int_vector.insert(ul_index,3);
int_vector.insert(ul_index,4);
int_vector.insert(ul_index,5);
int_vector.insert(ul_index,6);
int_vector.print_vector();

var double_vector <- new Felix_Vector::flxVec[double](4ul);
double_vector.print_vector();
double_vector.insert(ul_index,3.14);
double_vector.insert(ul_index,4.14);
double_vector.insert(10ul, 5.14);
double_vector.print_vector();

// The two rows below are not allowed ????
//var bool_vector <- new Felix_Vector::flxVec[bool](4ul);
//var string_vector <- new Felix_Vector::flxVec[string](4ul);

module Felix_Vector
{

 //------------------------------------

 class flxVec[t]
 {
   // variables
   var length: ulong;
   var max_length: ulong;
   var flx_varray: varray[t];

   // constructor
   ctor ()
     {
       length = 0ul;
       max_length = 5ul;
       flx_varray = mkuiv[t](5ul);
     }

   ctor (initial_max_length:ulong)
     {
       length = 0ul;
       max_length = initial_max_length;
       flx_varray = mkuiv[t](initial_max_length);
     }

   // procedures

   proc print_vector()
   {
     print "The vector contains: ";
     if length == 0ul do
       print "Nothing!";
       endl;
     else
       var i : ulong;
       forall i in 0ul upto length - 1ul do
         pr[t] flx_varray.[i];
         print " , ";
       done;
       print_line "";
     done;
   }

   // Inserts at position pos a copy of elem
   proc insert(pos:ulong, elem:t)
   {
     // checks if the varray is long enough
     if (pos + 1ul) <= max_length and (length + 1ul) <= max_length do

       // checks if any existing elements needs to be moved forward
       if (pos + 1ul <= length) do
         var i: ulong = length;
         while {i>0ul and i>=pos+1ul}
           {
             flx_varray.[i] = flx_varray.[i-1ul];
             if i > 0ul do
                --i;
             done;
           };
       done;

       // inserts the new element
       flx_varray.[pos] = elem;
       // updates length
       if pos + 1ul > length do
         length = pos + 1ul;
       else
         ++length;
       done;
       print "Inserted "; pr[t](elem); print " at index "; print pos;
       print "  length = "; print length;
       print "  max_length = "; print_line max_length;

     else
       // The varray isn't long enough, so we make a longer one
       print_line "The vector is expanded!";
       var new_max_length: ulong;
       if (pos + 1ul) > max_length do
         new_max_length = pos + 10ul;
       else
        new_max_length = max_length + 10ul;
       done;

       var temp_varray: varray[t] = mkuiv[t](new_max_length);
       var j: ulong;
       forall j in 0ul upto length - 1ul do
         temp_varray.[j] = flx_varray.[j];
       done;
       flx_varray = temp_varray;
       // Does garbage collection make sense here?
       svc svc_collect;
       max_length = new_max_length;
       insert(pos,elem);
     done;
   }

 } //-- end of class flxVec

} //-- end of module Felix_Vector

//-------------------------------------------------
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to