[Mumps2Py:] [112] added Mf_order to the compatibility library.

[ Thread Index | Date Index | More lists.mumps2py.org/discuss Archives ]


Revision: 112
Author:   pgallot
Date:     2008-02-15 21:17:19 +0000 (Fri, 15 Feb 2008)

Log Message:
-----------
added Mf_order to the compatibility library.

Modified Paths:
--------------
    trunk/mumps2py/mumpsCL.py


Modified: trunk/mumps2py/mumpsCL.py
===================================================================
--- trunk/mumps2py/mumpsCL.py	2008-02-15 21:15:40 UTC (rev 111)
+++ trunk/mumps2py/mumpsCL.py	2008-02-15 21:17:19 UTC (rev 112)
@@ -75,7 +75,56 @@
     else:
         return val + len(substr) + 1
 
+def Mf_order(var, var_index, direction = 1):
+    """Python-equivalent of the Mumps $order intrinsic"""
+    assert(direction == 1 or direction == -1)
+    varindex = var_index
+    varindexlen = len(varindex)
+    if varindexlen <= 1:
+        varkeys = var.keys()
+    else:
+        varkeys = []
+        varindexmatch = varindex[:varindexlen]
+        for key in var.keys():
+            if len(key) >= varindexlen and key[:varindexlen] == varindexmatch:
+                varkeys.append(key)
 
+    next_index = ""
+    if not len(varkeys):
+        return next_index
+    
+    varkeys.sort()
+    
+    if direction == 1:
+        start, end = 0, len(varkeys)
+        test = lambda v, n, i: v[n] <= i
+    else:
+        start, end = len(varkeys) - 1, -1
+        test = lambda v, n, i: v[n] >= i
+
+    if varindex == "":
+        return varkeys[start]
+
+    try:
+        nodeindex = varkeys.index(varindex)
+        nextnode = nodeindex + direction
+        if nextnode >= 0 and nextnode < len(varkeys):
+            if len(varkeys[nextnode]) > varindexlen:
+                next_index = varkeys[nextnode][:varindexlen]
+            else:
+                next_index = varkeys[nextnode]
+    except ValueError:
+        nextnode = start
+        while nextnode != end and test(varkeys, nextnode, varindex):
+            nextnode = nextnode + (direction)
+    
+        if nextnode != end:
+            if len(varkeys[nextnode]) > varindexlen:
+                next_index = varkeys[nextnode][:varindexlen]
+            else:
+                next_index = varkeys[nextnode]
+    return next_index
+    
 def Mf_select(testlist):
     """Python-equivalent of the Mumps $Select intrinsic"""
     for testcase, value in testlist:


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/