Revision: 4055
          http://vexi.svn.sourceforge.net/vexi/?rev=4055&view=rev
Author:   mkpg2
Date:     2011-03-19 19:16:36 +0000 (Sat, 19 Mar 2011)

Log Message:
-----------
Fix. Incorrect prime values were being returned (non-deterministically) due to 
threading issues.

Modified Paths:
--------------
    trunk/org.vexi-vexi.demo/src_main/org/vexi/demo/feature/table_dynamic.t

Modified: 
trunk/org.vexi-vexi.demo/src_main/org/vexi/demo/feature/table_dynamic.t
===================================================================
--- trunk/org.vexi-vexi.demo/src_main/org/vexi/demo/feature/table_dynamic.t     
2011-03-17 10:33:21 UTC (rev 4054)
+++ trunk/org.vexi-vexi.demo/src_main/org/vexi/demo/feature/table_dynamic.t     
2011-03-19 19:16:36 UTC (rev 4055)
@@ -24,41 +24,56 @@
             </table>
         </wi:bevel>
         
-        var N_ROWS = 10000;
-        var size = 1;
-        var primes = [2];
+        var N_ROWS = 20000;
+               
+        var primes = function(){
+               var size = 1;
+               var array = [2];
+               var r = {};
+               
+               var isPrime = function(n) {
+                   var rootn = vexi.math.floor(vexi.math.sqrt(n));
+                   var isPrime=true;
+                   for (var i=0;;i++){
+                       var d = array[i];
+                       if(d>rootn) break;
+                       if ((n%d)==0) {
+                          return false;
+                       }
+                   }
+                   return true;
+               };
+                       
+                       var fillNext = function(){
+                               for (var i=array[size-1]+1; true; i++) {
+                       if (isPrime(i)) {
+//                         vexi.log.info("prime " + size + " = " + i);
+                           array[size++] = i;
+                           return;
+                       }
+                   }
+               };
+               
+               r.get = function(n){
+                       while(true){
+                               if(n>=size){
+                                       fillNext();
+                                               vexi.thread.yield();            
        
+                               }else{
+                                       return array[n];
+                               }
+                       }
+               };
+               
+               r.has = function(n){ return array[n]!=null; }
+               
+               return r;
+        }();
         
-        var isPrime = function(n) {
-            var rootn = vexi.math.floor(vexi.math.sqrt(n));
-            var isPrime=true;
-            for (var i=0;;i++){
-                   var d = primes[i];
-                if(d>rootn) break;
-                if ((n%d)==0) {
-                   return false;
-                }
-            }
-            return true;
-        }
-        
-        var fill = function(n) {
-            for (var i=primes[size-1]+1; n>=size; i++) {
-                if (isPrime(i)) {
-                    //vexi.log.info("prime " + size + " = " + i);
-                    primes[size++] = i;
-                }
-                vexi.thread.yield();
-            }
-        }
-        
-        var p = function(n) {
-            if (size>n) return primes[n];
-            fill(n); 
-            return primes[n];
-        }
-
+  
         $table.fetchData = function(start, count, sortcol, sortasc) {
             var data = [count];
+            var update = false;
             /* this only works because all columns obey same order
              * i.e. this is a cheap example since typical usage of
              * dynamic tables will be to grab data from a source
@@ -71,28 +86,29 @@
                 data[i][0] = ind;
                 data[i][1] = 0 - ind;
                 data[i][2] = ind + 1;
-                data[i][3] = primes[ind]==null?"calculating":primes[ind];
+                
+                if(primes.has(ind)){
+                       data[i][3] = primes.get(ind);
+                }else{
+                       data[i][3] = "calculating";
+                       update = true;
+                       
+                       // update display when value is available
+                       vexi.thread = function() {
+                           var p = primes.get(ind);
+                           
+                           var n = $body.numchildren;
+                           for (var j=0; n>j; j++) {
+                                   // only if request still corresponds to the 
display  
+                            if ($body[j].rowindex == ind) {
+                                   $body[j][3].value = p; 
+                               }
+                           }
+                       };
+                }
             }
-            /* reuse data array - don't try this a home */
-            vexi.thread = function() {
-                var max = sortasc ? start + count : N_ROWS - (start);
-                /* values cached/already calculated */
-                if (primes[max]!=null) return;
-                for (var i=0; count>i; i++) {
-                    var ind = sortasc ? start + i : N_ROWS - (start + i);
-                    var prime = p(ind);
-                    var n = $body.numchildren;
-                    for (var i=0; n>i; i++) {
-                        if ($body[i].rowindex == ind) {
-                            $body[i][3].value = prime; 
-                        }
-                    }
-                    //data[i][3] = prime;
-                    //$table.loadData(data);
-                }        
-            }
             return data;
-        }
+        };
         
         $table.rowcount = N_ROWS;       
         


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to