ValueStack.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2001, 2015 Per M.A. Bothner and Brainfood Inc.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.mapping;
  4. import gnu.lists.*;
  5. class ValueStack extends TreeList {
  6. int gapStartOnPush;
  7. Consumer consumerOnPush = this;
  8. int oindexOnPush;
  9. /** Most recent argument to writeObject, assumming no intervining writes,
  10. * If writeObject is the most recent write, lastObject==this.
  11. */
  12. Object lastObject = this;
  13. public void clear() {
  14. super.clear();
  15. lastObject = this;
  16. }
  17. void push() {
  18. if (lastObject != this) {
  19. super.writeObject(lastObject);
  20. lastObject = this;
  21. }
  22. int oindex = find(consumerOnPush);
  23. writeIntForce32(oindex);
  24. writeIntForce32(gapStartOnPush);
  25. }
  26. void pop(int saved) {
  27. gapStartOnPush = getIntN(saved-2);
  28. int oindex = getIntN(saved-5);
  29. consumerOnPush = (Consumer) objects[oindex];
  30. objects[oindex] = null;
  31. oindexOnPush = oindex;
  32. gapStart = saved-6;
  33. }
  34. public void pushArgState(CallContext ctx) {
  35. int saveGap = gapStart;
  36. writeIntForce32(ctx.consumerOnPushArgState);
  37. writeIntForce32(ctx.next);
  38. writeIntForce32(ctx.count);
  39. writeIntForce32(ctx.firstKeyword);
  40. writeIntForce32(ctx.numKeywords);
  41. writeIntForce32(ctx.nextKeyword);
  42. writeIntForce32(ctx.matchState);
  43. ctx.consumerOnPushArgState = saveGap;
  44. int ogrow = ctx.count+2;
  45. reserveObjects(ogrow);
  46. System.arraycopy(ctx.values, 0, objects, oindex,
  47. ctx.count);
  48. // Should we save applyMethod and/or proc fields?
  49. objects[oindex+ctx.count] = ctx.keywords;
  50. objects[oindex+ctx.count+1] = ctx.sortedKeywords;
  51. oindex += ogrow;
  52. ctx.next = 0;
  53. ctx.count = 0;
  54. ctx.firstKeyword = 0;
  55. ctx.numKeywords = 0;
  56. ctx.nextKeyword = 0;
  57. ctx.matchState = 0; // ???
  58. ctx.keywords = null;
  59. ctx.sortedKeywords = null;
  60. }
  61. public void popArgState(CallContext ctx) {
  62. int start = ctx.consumerOnPushArgState;
  63. ctx.consumerOnPushArgState = getIntN(start+1);
  64. ctx.next = getIntN(start+4);
  65. ctx.count = getIntN(start+7);
  66. ctx.firstKeyword = getIntN(start+10);
  67. ctx.numKeywords = getIntN(start+13);
  68. ctx.nextKeyword = getIntN(start+16);
  69. ctx.matchState = getIntN(start+19);
  70. gapStart = start;
  71. int ogrow = ctx.count+2;
  72. oindex -= ogrow;
  73. System.arraycopy(objects, oindex,
  74. ctx.values, 0, ctx.count);
  75. ctx.keywords = (String[]) objects[oindex+ctx.count];
  76. ctx.sortedKeywords = (short[]) objects[oindex+ctx.count+1];
  77. for (int i = 0; i < ogrow; i++)
  78. objects[oindex+1] = null;
  79. }
  80. Object getValue() {
  81. Object last = lastObject;
  82. if (gapStart == gapStartOnPush) {
  83. return last == this ? Values.empty : last;
  84. }
  85. int next = nextDataIndex(gapStartOnPush);
  86. if (next == gapStart && last == this)
  87. return getPosNext(gapStartOnPush << 1); // Singleton value
  88. Values.FromTreeList vals = new Values.FromTreeList();
  89. super.consumeIRange(gapStartOnPush, gapStart, vals);
  90. if (lastObject != this)
  91. vals.writeObject(lastObject);
  92. return vals;
  93. }
  94. @Override
  95. public void ensureSpace(int needed) {
  96. super.ensureSpace(needed+3);
  97. if (lastObject != this) {
  98. super.writeObject(lastObject);
  99. lastObject = this;
  100. }
  101. }
  102. @Override
  103. public void writeObject(Object v) {
  104. if (lastObject != this)
  105. super.writeObject(lastObject);
  106. lastObject = v;
  107. }
  108. }