Changeset 15397


Ignore:
Timestamp:
2013-02-28T19:11:04+01:00 (11 years ago)
Author:
jfrenkel
Message:
  • speed up List.flatten implementation, now the already flattened list is not traversed again and again
  • add List.uniqueIntNArr, basically the same as List.uniqueIntN but the user has to provide the flag array, this is faster because the flag array needs no to allocated each time the function is called
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/Util/List.mo

    r15249 r15397  
    11511151end uniqueIntN_work;
    11521152
     1153public function uniqueIntNArr
     1154  "Takes a list of integes and returns a list with duplicates removed, so that
     1155   each element in the new list is unique. O(listLength(inList)). The function
     1156   also takes an array of Integer of size N+1 to mark the already selected entries <= N.
     1157   The last entrie of the array is used for the mark index. It will be updated after
     1158   each call"
     1159  input list<Integer> inList;
     1160  input array<Integer> markarr;
     1161  input list<Integer> iAcc;
     1162  output list<Integer> oAcc;
     1163algorithm
     1164  oAcc := matchcontinue(inList,markarr,iAcc)
     1165    local
     1166      Integer i,len,mark;
     1167      list<Integer> ilst,acc;
     1168    case ({},_,_)
     1169      equation
     1170        len = arrayLength(markarr);
     1171        _=arrayUpdate(markarr,len,markarr[len]+1);
     1172      then iAcc;
     1173    case (i::ilst,_,_)
     1174      equation
     1175        len = arrayLength(markarr);
     1176        true = intLt(i,len);
     1177        mark = markarr[len];
     1178        acc = consOnTrue(intNe(markarr[i],mark),i,iAcc);
     1179        _=arrayUpdate(markarr,i,mark);
     1180      then
     1181        uniqueIntNArr(ilst,markarr,acc);
     1182    else
     1183      equation
     1184        print("List.uniqueIntNArr failed entrie to large\n");
     1185      then
     1186        fail();
     1187  end matchcontinue;
     1188end uniqueIntNArr;
     1189
    11531190public function uniqueOnTrue
    11541191  "Takes a list of elements and a comparison function over two elements of the
     
    54115448public function flatten
    54125449  "Takes a list of lists and flattens it out, producing one list of all elements
    5413    of the sublists.
     5450   of the sublists. O(len(outList))
    54145451     Example: flatten({{1, 2}, {3, 4, 5}, {6}, {}}) => {1, 2, 3, 4, 5, 6}"
    54155452  input list<list<ElementType>> inList;
    54165453  output list<ElementType> outList;
    54175454algorithm
    5418   outList := flatten_tail(inList, {});
     5455  outList := flatten_tail(listReverse(inList), {});
    54195456end flatten;
    54205457
     
    54335470    case (e :: rest, _)
    54345471      equation
    5435         res = listAppend(inAccumList, e);
     5472        res = listAppend(e,inAccumList);
    54365473      then
    54375474        flatten_tail(rest, res);
Note: See TracChangeset for help on using the changeset viewer.