array.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. var RTL$ = {
  2. makeArray: function (/*dimensions, initializer*/){
  3. var forward = Array.prototype.slice.call(arguments);
  4. var result = new Array(forward.shift());
  5. var i;
  6. if (forward.length == 1){
  7. var init = forward[0];
  8. if (typeof init == "function")
  9. for(i = 0; i < result.length; ++i)
  10. result[i] = init();
  11. else
  12. for(i = 0; i < result.length; ++i)
  13. result[i] = init;
  14. }
  15. else
  16. for(i = 0; i < result.length; ++i)
  17. result[i] = this.makeArray.apply(this, forward);
  18. return result;
  19. },
  20. extend: function extend(methods){
  21. function Type(){
  22. for(var m in methods)
  23. this[m] = methods[m];
  24. }
  25. Type.prototype = this.prototype;
  26. var result = methods.init;
  27. result.prototype = new Type(); // inherit this.prototype
  28. result.prototype.constructor = result; // to see constructor name in diagnostic
  29. result.extend = extend;
  30. return result;
  31. },
  32. cloneArrayOfRecords: function (from){
  33. var length = from.length;
  34. var result = new Array(length);
  35. if (length){
  36. var method = from[0] instanceof Array
  37. ? this.cloneArrayOfRecords // this is array of arrays, go deeper
  38. : this.cloneRecord;
  39. for(var i = 0; i < result.length; ++i)
  40. result[i] = method.call(this, from[i]);
  41. }
  42. return result;
  43. },
  44. cloneRecord: function (from){
  45. var Ctr = from.constructor;
  46. var result = new Ctr();
  47. this.copyRecord(from, result);
  48. return result;
  49. },
  50. copyRecord: function (from, to){
  51. for(var prop in to){
  52. if (to.hasOwnProperty(prop)){
  53. var v = from[prop];
  54. var isScalar = prop[0] != "$";
  55. if (isScalar)
  56. to[prop] = v;
  57. else
  58. to[prop] = v instanceof Array ? this.cloneArrayOfRecords(v)
  59. : this.cloneRecord(v);
  60. }
  61. }
  62. },
  63. cloneArrayOfScalars: function (from){
  64. var length = from.length;
  65. if (!length)
  66. return [];
  67. if (!(from[0] instanceof Array))
  68. return from.slice();
  69. // this is array of arrays, go deeper
  70. var result = new Array(length);
  71. for(var i = 0; i < length; ++i)
  72. result[i] = this.cloneArrayOfScalars(from[i]);
  73. return result;
  74. }
  75. };
  76. var m = function (){
  77. var arraySize = 10;
  78. var a1 = RTL$.makeArray(10, 0);var a11 = RTL$.makeArray(10, 0);
  79. var a2 = RTL$.makeArray(5, function(){return RTL$.makeArray(10, 0);});
  80. var a3 = RTL$.makeArray(5, false);
  81. var a4 = RTL$.makeArray(3, 4, false);
  82. var anonymous$1 = RTL$.extend({
  83. init: function anonymous$1(){
  84. }
  85. });
  86. var a5 = RTL$.makeArray(3, function(){return new anonymous$1();});
  87. function p(){
  88. var a3 = RTL$.makeArray(1, 0);
  89. a3[0] = 1;
  90. }
  91. function p1(a/*ARRAY 10 OF INTEGER*/){
  92. }
  93. function p2(a/*VAR ARRAY 10 OF INTEGER*/){
  94. p1(a);
  95. }
  96. function testAssign(){
  97. var T = RTL$.extend({
  98. init: function T(){
  99. }
  100. });
  101. var aInts1 = RTL$.makeArray(3, 0);var aInts2 = RTL$.makeArray(3, 0);
  102. var aRecords1 = RTL$.makeArray(3, function(){return new T();});var aRecords2 = RTL$.makeArray(3, function(){return new T();});
  103. var aPointers1 = RTL$.makeArray(3, null);var aPointers2 = RTL$.makeArray(3, null);
  104. var arrayOfArray1 = RTL$.makeArray(3, 5, false);var arrayOfArray2 = RTL$.makeArray(3, 5, false);
  105. aInts2 = aInts1.slice();
  106. aRecords2 = RTL$.cloneArrayOfRecords(aRecords1);
  107. aPointers2 = aPointers1.slice();
  108. arrayOfArray2 = RTL$.cloneArrayOfScalars(arrayOfArray1);
  109. }
  110. function testPassOpenArray(a/*ARRAY OF INTEGER*/){
  111. }
  112. a1[0] = 1;
  113. a3[1] = true;
  114. a4[1][2] = true;
  115. a4[1][2] = true;
  116. p1(a1);
  117. p2(a1);
  118. a1 = a11.slice();
  119. testPassOpenArray(a1);
  120. }();