| 21 | | Iterator module providing iteration services. |
|---|
| 22 | | There is one global function iter() which can be used to iterate over iterable objects synchronously or |
|---|
| 23 | | if given a callback asynchronously. |
|---|
| 24 | | An iterable object is an object which has an iterator function (__iter__) which returns an Iterator object. |
|---|
| 25 | | |
|---|
| 26 | | The Range class is there to create an iterable object over a range of numbers. |
|---|
| 27 | | |
|---|
| | 21 | Iteration module providing functionality for working with iterable objects. |
|---|
| | 22 | |
|---|
| | 23 | To enable any object to be iterable the module introduces a number of iteration protocolls |
|---|
| | 24 | and provides default implementation for it. |
|---|
| | 25 | |
|---|
| | 26 | For an object to be iterable it must provide an __iter__() method that returns an Iterator object. |
|---|
| | 27 | See iter.iter() and iter.Iterator for more info. |
|---|
| | 28 | |
|---|
| | 29 | There are also some function provided for iterating over iterable object and performing operations on it's items. |
|---|
| | 30 | iter() - for iterating over iterable objects, |
|---|
| | 31 | map() - for maping items from an iterable object to a list, |
|---|
| | 32 | filter() - for creating a list caontaining only certain items from an iterable object |
|---|
| | 33 | list() - for creating a list containing all items from an iterable object. |
|---|
| | 34 | |
|---|
| | 35 | The above methods only create an iterator for an iteratable object and call one of it's methods depending on what protocoll is used. |
|---|
| | 36 | See iter.Iterator for more information. |
|---|
| | 37 | |
|---|
| | 38 | There is also a range() method for creating a iterator for a range of numbers. |
|---|
| | 39 | |
|---|
| | 65 | |
|---|
| | 66 | publ.__iterate__=function(thisObj, cb){ |
|---|
| | 67 | var result; |
|---|
| | 68 | thisObj = thisObj==null?this:thisObj; |
|---|
| | 69 | while(((item=this.next()) !== undefined) && result===undefined){ |
|---|
| | 70 | result=cb.call(thisObj, item, this); |
|---|
| | 71 | } |
|---|
| | 72 | return result; |
|---|
| | 73 | }; |
|---|
| | 74 | |
|---|
| | 75 | publ.__filter__ = function(thisObj, cb){ |
|---|
| | 76 | var result=[]; |
|---|
| | 77 | thisObj = thisObj==null?this:thisObj; |
|---|
| | 78 | var item; |
|---|
| | 79 | while((item=this.next()) !== undefined){ |
|---|
| | 80 | if(cb.call(thisObj, item, this)){ |
|---|
| | 81 | result.push(item); |
|---|
| | 82 | } |
|---|
| | 83 | } |
|---|
| | 84 | return result; |
|---|
| | 85 | }; |
|---|
| | 86 | |
|---|
| | 87 | publ.__map__ = function(thisObj, cb){ |
|---|
| | 88 | var result=[]; |
|---|
| | 89 | thisObj = thisObj==null?this:thisObj; |
|---|
| | 90 | var item; |
|---|
| | 91 | while((item=this.next()) !== undefined){ |
|---|
| | 92 | result.push(cb.call(thisObj, item, this)); |
|---|
| | 93 | } |
|---|
| | 94 | return result; |
|---|
| | 95 | }; |
|---|
| | 96 | |
|---|
| | 97 | publ.__list__ = function(){ |
|---|
| | 98 | var list = []; |
|---|
| | 99 | while((item=this.next()) !== undefined){ |
|---|
| | 100 | list.push(item); |
|---|
| | 101 | } |
|---|
| | 102 | return list; |
|---|
| | 103 | }; |
|---|
| | 104 | |
|---|
| | 105 | publ.replace = function(item){ |
|---|
| | 106 | throw new mod.Exception("Iterator::replace() not implemented"); |
|---|
| | 107 | }; |
|---|
| | 190 | |
|---|
| | 191 | publ.__iterate__=function(thisObj, cb){ |
|---|
| | 192 | var result=undefined; |
|---|
| | 193 | thisObj = thisObj==null?this:thisObj; |
|---|
| | 194 | var args = [null,this]; |
|---|
| | 195 | for(this.index++; this.index<this.array.length && result===undefined; this.index++){ |
|---|
| | 196 | result= cb.call(thisObj, this.array[this.index], this); |
|---|
| | 197 | } |
|---|
| | 198 | }; |
|---|
| | 199 | |
|---|
| | 200 | publ.__list__ = function(){ |
|---|
| | 201 | return [].concat(this.array); |
|---|
| | 202 | }; |
|---|
| | 203 | |
|---|
| | 204 | publ.replace=function(item1, item2){ |
|---|
| | 205 | switch(arguments.length){ |
|---|
| | 206 | case 0: |
|---|
| | 207 | this.array.splice(this.index, 1); |
|---|
| | 208 | break; |
|---|
| | 209 | case 1: |
|---|
| | 210 | this.array.splice(this.index, 1, item); |
|---|
| | 211 | break; |
|---|
| | 212 | default: |
|---|
| | 213 | var a=[this.index, arguments.length]; |
|---|
| | 214 | for(var i=0;i<arguments.length;i++){ |
|---|
| | 215 | a.push(arguments[i]); |
|---|
| | 216 | } |
|---|
| | 217 | this.array.splice.apply(this.array,a); |
|---|
| | 218 | } |
|---|
| | 219 | this.index += arguments.length -1; |
|---|
| | 220 | }; |
|---|
| 147 | | Array.prototype.__iter__ = function(){ |
|---|
| 148 | | return new mod.ArrayItereator(this); |
|---|
| 149 | | }; |
|---|
| 150 | | |
|---|
| | 256 | /** |
|---|
| | 257 | Returns the iterator for an iterable object or iterates over al items of the iterable object by |
|---|
| | 258 | using the iterable's iterator's __iterate__ method. |
|---|
| | 259 | |
|---|
| | 260 | An iteration stops if the callback returns any value except undefined. |
|---|
| | 261 | The value returned by the callback will be returned to the caller of iter(). |
|---|
| | 262 | @param iterable The iterable object. |
|---|
| | 263 | @param thisObj=return The this object to use when calling the callback. |
|---|
| | 264 | @param cb An IterationCallback object to call for each step. |
|---|
| | 265 | @return An iterator object or the return value returned by the callback. |
|---|
| | 266 | **/ |
|---|
| | 267 | mod.iter=function(iterable, thisObj, cb){ |
|---|
| | 268 | var iterator; |
|---|
| | 269 | if(iterable.__iter__ !==undefined){ |
|---|
| | 270 | iterator = iterable.__iter__(); |
|---|
| | 271 | }else if(iterable.length != null){ |
|---|
| | 272 | iterator = new mod.ArrayItereator(iterable); |
|---|
| | 273 | }else if(iterable.constructor == Object){ |
|---|
| | 274 | iterator = new mod.ObjectIterator(iterable); |
|---|
| | 275 | }else{ |
|---|
| | 276 | throw new mod.Exception("Iterable object does not provide __iter__ method or no Iterator found.") |
|---|
| | 277 | } |
|---|
| | 278 | if(arguments.length==1){ |
|---|
| | 279 | return iterator; |
|---|
| | 280 | }else{ |
|---|
| | 281 | if(cb == null){ |
|---|
| | 282 | cb = thisObj; |
|---|
| | 283 | thisObj = null; |
|---|
| | 284 | } |
|---|
| | 285 | return iterator.__iterate__(thisObj, cb); |
|---|
| | 286 | } |
|---|
| | 287 | }; |
|---|
| | 288 | |
|---|
| 159 | | Iteration class for handling iteration steps and callbacks. |
|---|
| 160 | | */ |
|---|
| 161 | | mod.Iteration = Class(function(publ,supr){ |
|---|
| 162 | | /** |
|---|
| 163 | | Initializes an Iteration object. |
|---|
| 164 | | @param iterable An itaratable object. |
|---|
| 165 | | @param thisObj |
|---|
| 166 | | @param callback An IterationCallback object. |
|---|
| 167 | | */ |
|---|
| 168 | | publ.__init__=function(iterable, thisObj, callback){ |
|---|
| 169 | | this.doStop = false; |
|---|
| 170 | | this.thisObj=thisObj; |
|---|
| 171 | | if(iterable.__iter__ !==undefined){ |
|---|
| 172 | | this.iterator = iterable.__iter__(); |
|---|
| 173 | | }else{ |
|---|
| 174 | | this.iterator = new mod.ObjectIterator(iterable); |
|---|
| 175 | | } |
|---|
| 176 | | |
|---|
| 177 | | this.callback = callback; |
|---|
| 178 | | }; |
|---|
| 179 | | |
|---|
| 180 | | ///Resumes a stoped iteration. |
|---|
| 181 | | publ.resume = function(){ |
|---|
| 182 | | this.doStop = false; |
|---|
| 183 | | var item; |
|---|
| 184 | | while(!this.doStop){ |
|---|
| 185 | | item=this.iterator.next(); |
|---|
| 186 | | if(item === undefined){ |
|---|
| 187 | | this.stop(); |
|---|
| 188 | | }else{ |
|---|
| 189 | | //let the callback handle the item |
|---|
| 190 | | this.callback.call(this.thisObj==null?this : this.thisObj, item, this); |
|---|
| 191 | | } |
|---|
| 192 | | } |
|---|
| 193 | | }; |
|---|
| 194 | | |
|---|
| 195 | | ///Stops an iteration |
|---|
| 196 | | publ.stop = function(){ |
|---|
| 197 | | this.doStop = true; |
|---|
| 198 | | }; |
|---|
| 199 | | |
|---|
| 200 | | ///Starts/resumes an iteration |
|---|
| 201 | | publ.start = function(){ |
|---|
| 202 | | this.resume(); |
|---|
| 203 | | }; |
|---|
| 204 | | }); |
|---|
| 205 | | |
|---|
| 206 | | /** |
|---|
| 207 | | Class for handling asynchronous iterations. |
|---|
| 208 | | */ |
|---|
| 209 | | mod.AsyncIteration = Class(mod.Iteration, function(publ, supr){ |
|---|
| 210 | | /** |
|---|
| 211 | | Initializes an AsyncIteration object. |
|---|
| 212 | | @param iterable An itaratable object. |
|---|
| 213 | | @param interval The time in ms betwen each step. |
|---|
| 214 | | @param thisObj |
|---|
| 215 | | @param callback An IterationCallback object. |
|---|
| 216 | | */ |
|---|
| 217 | | publ.__init__=function(iterable, interval, thisObj, callback){ |
|---|
| 218 | | this.doStop = false; |
|---|
| 219 | | this.thisObj=thisObj; |
|---|
| 220 | | if(iterable.__iter__ !==undefined){ |
|---|
| 221 | | this.iterator = iterable.__iter__(); |
|---|
| 222 | | }else{ |
|---|
| 223 | | this.iterator = new mod.ObjectIterator(iterable); |
|---|
| 224 | | } |
|---|
| 225 | | this.interval = interval; |
|---|
| 226 | | this.callback = callback; |
|---|
| 227 | | this.isRunning = false; |
|---|
| 228 | | }; |
|---|
| 229 | | |
|---|
| 230 | | publ.stop=function(){ |
|---|
| 231 | | if(this.isRunning){ |
|---|
| 232 | | this.isRunning = false; |
|---|
| 233 | | clearTimeout(this.timeout); |
|---|
| 234 | | delete iter.iterations[this.__hash__()]; |
|---|
| 235 | | } |
|---|
| 236 | | }; |
|---|
| 237 | | |
|---|
| 238 | | publ.resume = function(){ |
|---|
| 239 | | if(this.isRunning == false){ |
|---|
| 240 | | this.isRunning = true; |
|---|
| 241 | | var id = this.__hash__(); |
|---|
| 242 | | iter.iterations[id] = this; |
|---|
| 243 | | //let the iteration be handled using a timer |
|---|
| 244 | | this.timeout = setTimeout("iter.handleAsyncStep('" + id + "')", this.interval); |
|---|
| 245 | | } |
|---|
| 246 | | }; |
|---|
| 247 | | |
|---|
| 248 | | publ.handleAsyncStep = function(){ |
|---|
| 249 | | if(this.isRunning){ |
|---|
| 250 | | var item=this.iterator.next(); |
|---|
| 251 | | if(item === undefined){ |
|---|
| 252 | | this.stop(); |
|---|
| 253 | | }else{ |
|---|
| 254 | | //let the callback handle the item |
|---|
| 255 | | this.callback.call(this.thisObj==null?this : this.thisObj, item, this); |
|---|
| 256 | | this.timeout = setTimeout("iter.handleAsyncStep('" + this.__hash__()+ "')", this.interval); |
|---|
| 257 | | } |
|---|
| 258 | | } |
|---|
| 259 | | }; |
|---|
| 260 | | }); |
|---|
| 261 | | |
|---|
| 262 | | |
|---|
| 263 | | /** |
|---|
| 264 | | Iterates over an iterable object and calls a callback for each item. |
|---|
| | 297 | Returns a list containing all elements from an iteratable object for which the callback returns true. |
|---|
| | 298 | |
|---|
| 268 | | @param cb An IterationCallback object to call for each step. |
|---|
| 269 | | @return An Iteration object. |
|---|
| 270 | | */ |
|---|
| 271 | | iter = function(iterable, delay, thisObj, cb){ |
|---|
| 272 | | cb=arguments[arguments.length-1]; |
|---|
| 273 | | if((arguments.length == 3) && (typeof delay =='object')){ |
|---|
| 274 | | thisObj=delay; |
|---|
| 275 | | delay=-1; |
|---|
| 276 | | }else{ |
|---|
| 277 | | thisObj=null; |
|---|
| | 301 | @param cb An IterationCallback object to call for each item. |
|---|
| | 302 | @return A list containing all elements that were filtered. |
|---|
| | 303 | **/ |
|---|
| | 304 | mod.filter=function(iterable, thisObj,cb){ |
|---|
| | 305 | var iterator = mod.iter(iterable); |
|---|
| | 306 | if(cb == null){ |
|---|
| | 307 | cb = thisObj; |
|---|
| | 308 | thisObj = null; |
|---|
| 279 | | if(delay >-1){ |
|---|
| 280 | | var it = new mod.AsyncIteration(iterable, delay, thisObj, cb); |
|---|
| 281 | | }else{ |
|---|
| 282 | | var it = new mod.Iteration(iterable, thisObj, cb); |
|---|
| | 310 | return iterator.__filter__(thisObj, cb); |
|---|
| | 311 | }; |
|---|
| | 312 | |
|---|
| | 313 | /** |
|---|
| | 314 | Returns a list containing elements returned by the callback for each item form the itrable object. |
|---|
| | 315 | |
|---|
| | 316 | @param iterable The iterable object. |
|---|
| | 317 | @param thisObj=return The this object to use when calling the callback. |
|---|
| | 318 | @param cb An IterationCallback object to call for each item. |
|---|
| | 319 | @return A list containing new elements. |
|---|
| | 320 | **/ |
|---|
| | 321 | mod.map=function(iterable, thisObj, cb){ |
|---|
| | 322 | var iterator = mod.iter(iterable); |
|---|
| | 323 | if(cb == null){ |
|---|
| | 324 | cb = thisObj; |
|---|
| | 325 | thisObj = null; |
|---|
| 284 | | it.start(); |
|---|
| 285 | | return it; |
|---|
| 286 | | }; |
|---|
| 287 | | |
|---|
| 288 | | iter.handleAsyncStep = function(id){ |
|---|
| 289 | | if(iter.iterations[id]){ |
|---|
| 290 | | iter.iterations[id].handleAsyncStep(); |
|---|
| 291 | | } |
|---|
| 292 | | }; |
|---|
| 293 | | ///Helper object containing all async. iteration objects. |
|---|
| 294 | | iter.iterations = {}; |
|---|
| 295 | | |
|---|
| 296 | | |
|---|
| 297 | | mod.__main__=function(){ |
|---|
| 298 | | |
|---|
| 299 | | |
|---|
| 300 | | var testing = imprt('testing'); |
|---|
| 301 | | var task=function(){ |
|---|
| 302 | | var s=''; |
|---|
| 303 | | for(var i=0;i<10;i++){ |
|---|
| 304 | | s+=i; |
|---|
| 305 | | } |
|---|
| 306 | | }; |
|---|
| 307 | | |
|---|
| 308 | | r = []; |
|---|
| 309 | | for(var i=0;i<100;i++){ |
|---|
| 310 | | r[i] = i; |
|---|
| 311 | | } |
|---|
| 312 | | |
|---|
| 313 | | print("for loop \t\t\t" + testing.profile(function(){ |
|---|
| 314 | | var s=''; |
|---|
| 315 | | for(var i=0;i<100;i++){ |
|---|
| 316 | | s+=r[i]; |
|---|
| 317 | | task(); |
|---|
| 318 | | } |
|---|
| 319 | | })); |
|---|
| 320 | | |
|---|
| 321 | | print("Range iter \t\t" + testing.profile(function(){ |
|---|
| 322 | | var s=''; |
|---|
| 323 | | iter(new mod.Range(100), function(item,i){ |
|---|
| 324 | | s+=r[item]; |
|---|
| 325 | | task(); |
|---|
| 326 | | }); |
|---|
| 327 | | })); |
|---|
| 328 | | |
|---|
| 329 | | print("Array iter \t\t\t" + testing.profile(function(){ |
|---|
| 330 | | var s=''; |
|---|
| 331 | | iter(r , function(item,i){ |
|---|
| 332 | | s+=item; |
|---|
| 333 | | task(); |
|---|
| 334 | | }); |
|---|
| 335 | | |
|---|
| 336 | | })); |
|---|
| 337 | | |
|---|
| 338 | | print("for in on Array \t\t" + testing.profile(function(){ |
|---|
| 339 | | var s=''; |
|---|
| 340 | | for(var i in r){ |
|---|
| 341 | | s+=r[i]; |
|---|
| 342 | | task(); |
|---|
| 343 | | } |
|---|
| 344 | | })); |
|---|
| 345 | | |
|---|
| 346 | | r = []; |
|---|
| 347 | | for(var i=0;i<100;i++){ |
|---|
| 348 | | r["k"+i] = i; |
|---|
| 349 | | } |
|---|
| 350 | | |
|---|
| 351 | | print("for in on assoc. Array \t" + testing.profile(function(){ |
|---|
| 352 | | var s=''; |
|---|
| 353 | | for(var i in r){ |
|---|
| 354 | | s+=r[i]; |
|---|
| 355 | | task(); |
|---|
| 356 | | } |
|---|
| 357 | | })); |
|---|
| 358 | | |
|---|
| 359 | | r = {}; |
|---|
| 360 | | for(var i=0;i<100;i++){ |
|---|
| 361 | | r["k"+i] = i; |
|---|
| 362 | | } |
|---|
| 363 | | |
|---|
| 364 | | print("for in on dictionary \t" + testing.profile(function(){ |
|---|
| 365 | | var s=''; |
|---|
| 366 | | for(var i in r){ |
|---|
| 367 | | s+=r[i]; |
|---|
| 368 | | task(); |
|---|
| 369 | | } |
|---|
| 370 | | })); |
|---|
| 371 | | |
|---|
| 372 | | r = []; |
|---|
| 373 | | for(var i=0;i<100;i++){ |
|---|
| 374 | | r[i] = i; |
|---|
| 375 | | } |
|---|
| 376 | | |
|---|
| 377 | | print("for on Array + iter \t" + testing.profile(function(){ |
|---|
| 378 | | var s=''; |
|---|
| 379 | | for(i=r.__iter__(); item=i.next() !==undefined;){ |
|---|
| 380 | | s+= item; |
|---|
| 381 | | task(); |
|---|
| 382 | | } |
|---|
| 383 | | })); |
|---|
| 384 | | }; |
|---|
| | 327 | return iterator.__map__(thisObj, cb); |
|---|
| | 328 | }; |
|---|
| | 329 | |
|---|
| | 330 | /** |
|---|
| | 331 | Returns a list containing all elements from an iteratable object. |
|---|
| | 332 | I.e. this function turns any iterable object into a list(Array). |
|---|
| | 333 | |
|---|
| | 334 | @param iterable The iterable object. |
|---|
| | 335 | @return A list containing all elements. |
|---|
| | 336 | **/ |
|---|
| | 337 | mod.list=function(iterable){ |
|---|
| | 338 | return mod.iter(iterable).__list__(); |
|---|
| | 339 | }; |
|---|
| | 340 | |
|---|