Advertisement
Anaristos

Untitled

Nov 27th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function vectorEntry(data)
  2. {
  3.  this.prev = null;
  4.  this.next = null;
  5.  this.data = data;
  6.  return this;
  7. }
  8. //**********************************************************************************
  9. function vectorEnum()
  10. {
  11.  if (arguments.length != 2) return null;
  12.  
  13.  this.curloc          = arguments[0];
  14.  this.list            = arguments[1];
  15. //**********************************************************************************
  16.  this.hasMoreElements = testEnum;                  // S
  17.  this.nextElement     = nextEnum;                  // S
  18. //**********************************************************************************
  19.  return this;
  20. }
  21. //
  22. function testEnum()
  23. {
  24.  return (this.curloc != null);
  25. }
  26. //
  27. function nextEnum()
  28. {
  29.  var temp;
  30.  
  31.  if (this.curloc == null) {return null;}
  32.  
  33.  temp        = this.list[this.curloc].data;
  34.  this.curloc = this.list[this.curloc].next;
  35.  
  36.  return temp;
  37. }
  38. //**********************************************************************************
  39. function Vector(capacity,capacityIncrement)        // S
  40. //**********************************************************************************
  41. //*      This Object generates a reasonable emulation of Java's Vector Class.      *
  42. //* Most methods work exactly as they were designed, while others simulate results.*
  43. //* The ones that fall in the latter category are those dealing with the internal  *
  44. //* array management. Since JavaScript has a dynamically expandable array, there   *
  45. //* is not actual need to mantain it as Java does. However, when methods such as   *
  46. //* capacity(), size(), trimToSize(), etc, are invoked, this object will adjust its*
  47. //* internal structure to reflect the results of the invocation correctly.         *
  48. //* There is a series of unimplemented methods that correspond to new Java release *
  49. //* 1.2 additions. As soon as I understand them, they will be implemented. These   *
  50. //* are marked with the legend "[1.2]".                                            *
  51. //**********************************************************************************
  52. //* VERSION: 1.00                                   KEY  E: TESTED/ERRORS EXIST.   *
  53. //* DATE:    O4/30/1999                                  S: SUCCESSFULLY TESTED.   *
  54. //* AUTHOR:  Antonio F. Gomez                            U: UNTESTED.              *
  55. //*                                                      N: UNIMPLEMENTED.         *
  56. //*                                                      D: PARTIALLY IMPLEMENTED  *
  57. //**********************************************************************************
  58. {
  59.  this.length            = 0;
  60.  this.capacityRequest   = arguments[0];            // S
  61.  this.capacityIncrement = arguments[1];            // S
  62.  
  63.  if (arguments.length < 1)
  64.     {this.capacityRequest   = 10;}                 // S
  65.  
  66.  if (arguments.length < 2)
  67.     {this.capacityIncrement =  0;}                 // S
  68.  
  69.  this.elementCount      = 0;
  70.  this.first             = null;
  71.  this.last              = null;
  72.  this.avail             = null;
  73.  this.elementData       = new Array();
  74. //**********************************************************************************
  75.  this.addElement        = addToEnd;                // S
  76.  this.add               = addToEnd12;              // U  [1.2]
  77.  this.addAll            = addToVector;             // U  [1.2]
  78.  this.elementAt         = getIndexedElement;       // S
  79.  this.get               = getIndexedElement;       // S  [1.2]
  80.  this.firstElement      = getFirstElement;         // S
  81.  this.lastElement       = getLastElement;          // S
  82. //
  83.  this.indexOf           = getIndexOfElement;       // S
  84.  this.insertElementAt   = putIndexedElement;       // S
  85.  this.lastIndexOf       = getLastIndexOfElement;   // S
  86.  this.setElementAt      = updateElement;           // S
  87.  this.set               = updateElement12;         // U  [1.2]
  88. //
  89.  this.removeAllElements = deleteAll;               // S
  90.  this.removeElement     = deleteElement;           // S
  91.  this.removeElementAt   = deleteThisElement;       // S
  92.  this.remove            = deleteVectorElement;     // U  [1.2]
  93.  this.removeAll         = deleteCollection         // N  [1.2]
  94.  this.removeRange       = deleteElementRange       // N  [1.2]
  95. //
  96.  this.toArray           = buildArray;              // N  [1.2]
  97.  this.capacity          = getCapacity;             // S
  98.  this.clear             = clearVector;             // N  [1.2]
  99.  this.clone             = cloneVector;             // S
  100.  this.contains          = findElement;             // D
  101.  this.containsAll       = findAllElements;         // N  [1.2]
  102.  this.copyInto          = extractData;             // S
  103.  this.ensureCapacity    = ensureAllocation;        // N
  104.  this.elements          = enumVector;              // S
  105.  this.equals            = equalsVector;            // N  [1.2]
  106.  this.isEmpty           = testElementCount;        // S
  107.  this.iterator          = itarateVector;           // N  [1.2]
  108.  this.listIterator      = listVectorIterator;      // N  [1.2]
  109.  this.ratainAll         = retainCollection;        // N  [1.2]
  110.  this.setSize           = setArrayLength;          // N
  111.  this.size              = getArrayLength;          // S
  112.  this.trimToSize        = trimCapacity;            // N
  113.  this.toString          = VectorString;            // N
  114. //
  115.  return this;
  116. }
  117. //**********************************************************************************
  118. function addToEnd()
  119. {
  120.  var data = arguments[0];
  121.  var nx   = this.avail;
  122.  var cx;
  123.  
  124.  if (nx == null)
  125.     {nx = this.length++;}
  126.  else
  127.     {this.avail = this.elementData[nx].next;}    
  128.  
  129.  this.elementData[nx] = new vectorEntry(data);
  130.  cx = this.last;
  131.  this.last = nx;
  132.  
  133.  if (cx != null)
  134.     {this.elementData[cx].next = nx; this.elementData[nx].prev = cx;}
  135.  else
  136.     {this.first = nx;}
  137.  
  138.  this.elementCount++;
  139.  return nx;
  140. }
  141. //**********************************************************************************
  142. function addToEnd12()
  143. {
  144.  if (arguments.length == 1)
  145.     {
  146.      this.addElement(arguments[0]);
  147.      return true;
  148.     }
  149.  else
  150.     {
  151.      if (arguments.length == 2)
  152.         {
  153.          this.insertElementAt(arguments[1],arguments[0]);
  154.          return true;
  155.         }
  156.     }
  157.  return false;
  158. }
  159. //**********************************************************************************
  160. function addToVector(index,collection) {}
  161. //**********************************************************************************
  162. function getIndexedElement()
  163. {
  164.  var ox = arguments[0];
  165.  
  166.  if (ox >= this.elementCount || ox < 0) {return null;}
  167.  var cx = this.first;
  168.  while (ox-- > 0) {cx = this.elementData[cx].next;}
  169.  return this.elementData[cx].data;
  170. }
  171. //**********************************************************************************
  172. function getFirstElement()
  173. {
  174.  if (this.first == null) return null;
  175.  return this.elementData[this.first].data;
  176. }
  177. //**********************************************************************************
  178. function getLastElement()
  179. {
  180.  if (this.last == null) return null;
  181.  return this.elementData[this.last].data;
  182. }
  183. //**********************************************************************************
  184. function getIndexOfElement()
  185. {
  186.  var data = arguments[0];
  187.  var cx   = this.first;
  188.  var ox   = 0;
  189.  var k    = 0;
  190.  
  191.  if (arguments.length == 2) ox = arguments[1];
  192.  
  193.  if (ox >= this.elementCount || ox < 0) {return -1;}
  194.  
  195.  while (k < ox) {cx = this.elementData[cx].next; k++;}
  196.  
  197.  while (cx != null)
  198.     {
  199.      if (this.elementData[cx].data == data) {break;}
  200.  
  201.      cx = this.elementData[cx].next; k++;
  202.     }
  203.  
  204.  if (cx == null) {k = -1;}
  205.  
  206.  return k;
  207. }
  208. //**********************************************************************************
  209. function putIndexedElement()
  210. {
  211.  var data = arguments[0];
  212.  var ox   = arguments[1];
  213.  var px;
  214.  
  215.  if (ox > this.elementCount) {return false;}
  216.  
  217.  var cx = this.first;
  218.  while (ox-- > 0) {cx = this.elementData[cx].next;}
  219.  
  220.  var ne = this.avail;
  221.  
  222.  if (ne == null) {ne = this.length++;}
  223.  else {this.avail = this.elementData[ne].next;}
  224.  
  225.  this.elementData[ne] = new vectorEntry(data);
  226.  
  227.  if (cx != null)
  228.     {
  229.      px = this.elementData[cx].prev;
  230.  
  231.      if (px != null)
  232.         {
  233.          this.elementData[ne].prev = px;
  234.          this.elementData[px].next = ne;
  235.         }
  236.      else
  237.         {
  238.          this.first = ne;
  239.         }
  240.        
  241.      this.elementData[ne].next = cx;
  242.      this.elementData[cx].prev = ne;
  243.     }
  244.  else
  245.     {
  246.      if (this.first == null)
  247.         {
  248.          this.first = ne;
  249.          this.last  = ne;
  250.         }
  251.      else
  252.         {
  253.          cx = this.last; this.last = ne;
  254.          this.elementData[cx].next = ne; this.elementData[ne].prev = cx;
  255.         }
  256.     }
  257.        
  258.  
  259.  this.elementCount++;
  260.  return true;
  261. }
  262. //**********************************************************************************
  263. function getLastIndexOfElement()
  264. {
  265.  var data = arguments[0];
  266.  var cx   = this.last;
  267.  var ox   = this.elementCount - 1;
  268.  var k    = this.elementCount;
  269.  
  270.  if (arguments.length == 2) ox = arguments[1];
  271.  
  272.  if (ox >= this.elementCount || ox < 0) {return -1;}
  273.  
  274.  while (--k > ox) {cx = this.elementData[cx].prev;}
  275.  
  276.  while (cx != null)
  277.     {
  278.      if (this.elementData[cx].data == data) {break;}
  279.  
  280.      cx = this.elementData[cx].prev; k--;
  281.     }
  282.  
  283.  if (cx == null) {k = -1;}
  284.  
  285.  return k;
  286. }
  287. //**********************************************************************************
  288. function updateElement()
  289. {
  290.  var data = arguments[0];
  291.  var ox   = arguments[1];
  292.  
  293.  if (ox > this.elementCount) {return false;}
  294.  
  295.  var cx = this.first;
  296.  while (ox-- > 0) {cx = this.elementData[cx].next;}
  297.  
  298.  if (cx == null) {return false;}
  299.  
  300.  this.elementData[cx].data = data;
  301.  return true;
  302. }
  303. //**********************************************************************************
  304. function updateElement12()
  305. {
  306.  var temp;
  307.  
  308.  if (arguments.length == 2)
  309.     {
  310.      temp = this.elementAt(arguments[0]);
  311.      this.setElementAt(arguments[1],arguments[0]);
  312.      return temp;
  313.     }
  314.  else return null;
  315. }
  316. //**********************************************************************************
  317. function deleteAll()
  318. {
  319.  this.capacity();
  320.  
  321.  this.length       = 0;
  322.  this.elementCount = 0;
  323.  this.first        = null;
  324.  this.last         = null;
  325.  this.avail        = null;
  326.  this.elementData  = new Array();
  327.  return true;
  328. }
  329. //**********************************************************************************
  330. function deleteElement()
  331. {
  332.  var data = arguments[0];
  333.  var cx   = this.first;
  334.  
  335.  while (cx != null)
  336.     {
  337.      if (this.elementData[cx].data == data) {break;};
  338.  
  339.      cx = this.elementData[cx].next;
  340.     }
  341.  
  342.  if (cx == null) {return false;}
  343.  
  344.  var px = this.elementData[cx].prev;
  345.  var nx = this.elementData[cx].next;
  346.  
  347.  if (px == null) {this.first = nx;}
  348.  else
  349.     {this.elementData[px].next = nx;}
  350.  
  351.  if (nx == null) {this.last = px;}
  352.  else
  353.     {this.elementData[nx].prev = px;}
  354.  
  355.  this.elementData[cx].next = this.avail;
  356.  this.avail = cx;
  357.  
  358.  this.elementCount--;
  359.  return true;
  360. }
  361. //**********************************************************************************
  362. function deleteThisElement()
  363. {
  364.  var cx = this.first;
  365.  var ox = arguments[0];
  366.  
  367.  if (ox > this.elementCount) {return false;}
  368.  
  369.  while (ox-- > 0)
  370.     {cx = this.elementData[cx].next;}
  371.  
  372.  var px = this.elementData[cx].prev;
  373.  var nx = this.elementData[cx].next;
  374.  
  375.  if (px == null) {this.first = nx;}
  376.  else
  377.     {this.elementData[px].next = nx;}
  378.  
  379.  if (nx == null) {this.last = px;}
  380.  else
  381.     {this.elementData[nx].prev = px;}
  382.  
  383.  this.elementData[cx].next = this.avail;
  384.  this.avail = cx;
  385.  
  386.  this.elementCount--;
  387.  return true;
  388. }
  389. //**********************************************************************************
  390. function deleteVectorElement()
  391. {
  392.  var temp;
  393.  
  394.  if (arguments.length == 1)
  395.     {
  396.      if (typeof argument[0] == "number")
  397.         {
  398.          temp = elementAt(arguments[0]);
  399.          this.removeElementAt(arguments[0]);
  400.          return temp;
  401.         }
  402.      else
  403.         {
  404.          if (typeof argument[0] == "object")
  405.             {
  406.              this.removeElement(arguments[0]);
  407.              return true;
  408.             }
  409.         }
  410.     }
  411.  else return false;
  412. }
  413. //**********************************************************************************
  414. function deleteCollection(collection) {}
  415. //**********************************************************************************
  416. function deleteElementRange(fromindex,toindex) {}
  417. //**********************************************************************************
  418. function getCapacity()
  419. {
  420.  if (this.length > this.capacityRequest)
  421.     {if (this.capacityIncrement == 0)
  422.         {
  423.          while (this.capacityRequest < this.length) {this.capacityRequest *= 2;}
  424.         }
  425.      else
  426.         {    
  427.          while (this.capacityRequest < this.length)
  428.             {this.capacityRequest += this.capacityIncrement;}
  429.         }
  430.     }
  431.  
  432.  return this.capacityRequest;  
  433. }
  434. //**********************************************************************************
  435. function cloneVector()
  436. {
  437.  var nv = new Vector();
  438.  
  439.  nv.length            = this.length;
  440.  nv.capacityIncrement = this.capacityIncrement;
  441.  nv.elementCount      = this.elementCount;
  442.  nv.first             = this.first;
  443.  nv.last              = this.last;
  444.  nv.avail             = this.avail;
  445.  nv.elementData       = new Array(nv.elementData.length);
  446.  
  447.  for (var cx = 0; cx < nv.length; cx++)
  448.      {
  449.       nv.elementData[cx]      = new vectorEntry(this.elementData[cx].data);
  450.       nv.elementData[cx].prev = this.elementData[cx].prev;
  451.       nv.elementData[cx].next = this.elementData[cx].next;
  452.      }
  453.  
  454.  return nv;
  455. }
  456. //**********************************************************************************
  457. function enumVector()
  458. {
  459.  return new vectorEnum(this.first,this.elementData);
  460. }
  461. //**********************************************************************************
  462. function testElementCount()
  463. {
  464.  return this.elementCount == 0;
  465. }
  466. //**********************************************************************************
  467. function findElement()
  468. {
  469.  var data = arguments[0];
  470.  var cx   = this.first;
  471.  
  472.  while (cx != null && this.elementData[cx].data != data)
  473.     {
  474.      cx = this.elementData[cx].next;
  475.     }
  476.  
  477.  return cx != null;
  478. }
  479. //**********************************************************************************
  480. function findAllElements(collection) {}
  481. //**********************************************************************************
  482. function extractData()
  483. {
  484.  var na = arguments[0];
  485.  var cx = this.first;
  486.  var k  = 0;
  487.  
  488.  while (cx != null)
  489.     {
  490.      na[k++] = this.elementData[cx].data;
  491.      cx      = this.elementData[cx].next;
  492.     }
  493.  
  494.  return true;
  495. }
  496. //**********************************************************************************
  497. function getArrayLength()
  498. {
  499.  this.capacity(); return this.length;
  500. }
  501. //**********************************************************************************
  502. function buildArray(object) {}
  503. //**********************************************************************************
  504. function clearVector() {}
  505. //**********************************************************************************
  506. function setArrayLength(capacity) {}
  507. //**********************************************************************************
  508. function ensureAllocation(minCapacity) {}
  509. //**********************************************************************************
  510. function equalsVector(object) {return false;}
  511. //**********************************************************************************
  512. function trimCapacity(capacity) {}
  513. //**********************************************************************************
  514. function itarateVector() {}
  515. //**********************************************************************************
  516. function listVectorIterator(index) {}
  517. //**********************************************************************************
  518. function retainCollection(collection) {}
  519. //**********************************************************************************
  520. function VectorString()
  521. {
  522.  var out = "[";
  523.  var cx  = this.first;
  524.  
  525.  while (cx != null)
  526.     {
  527.      out += this.elementData[cx].data;
  528.      cx   = this.elementData[ cx].next;
  529.  
  530.      if (cx == null) {break;}
  531.  
  532.      out  += ",";
  533.     }
  534.  
  535.  out += "]";
  536.  return out;
  537. }
  538. //******************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement