Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fork-context.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * @fileoverview A class to operate forking.
  3. *
  4. * This is state of forking.
  5. * This has a fork list and manages it.
  6. *
  7. * @author Toru Nagashima
  8. */
  9. "use strict";
  10. //------------------------------------------------------------------------------
  11. // Requirements
  12. //------------------------------------------------------------------------------
  13. const assert = require("assert"),
  14. CodePathSegment = require("./code-path-segment");
  15. //------------------------------------------------------------------------------
  16. // Helpers
  17. //------------------------------------------------------------------------------
  18. /**
  19. * Gets whether or not a given segment is reachable.
  20. *
  21. * @param {CodePathSegment} segment - A segment to get.
  22. * @returns {boolean} `true` if the segment is reachable.
  23. */
  24. function isReachable(segment) {
  25. return segment.reachable;
  26. }
  27. /**
  28. * Creates new segments from the specific range of `context.segmentsList`.
  29. *
  30. * When `context.segmentsList` is `[[a, b], [c, d], [e, f]]`, `begin` is `0`, and
  31. * `end` is `-1`, this creates `[g, h]`. This `g` is from `a`, `c`, and `e`.
  32. * This `h` is from `b`, `d`, and `f`.
  33. *
  34. * @param {ForkContext} context - An instance.
  35. * @param {number} begin - The first index of the previous segments.
  36. * @param {number} end - The last index of the previous segments.
  37. * @param {Function} create - A factory function of new segments.
  38. * @returns {CodePathSegment[]} New segments.
  39. */
  40. function makeSegments(context, begin, end, create) {
  41. const list = context.segmentsList;
  42. const normalizedBegin = begin >= 0 ? begin : list.length + begin;
  43. const normalizedEnd = end >= 0 ? end : list.length + end;
  44. const segments = [];
  45. for (let i = 0; i < context.count; ++i) {
  46. const allPrevSegments = [];
  47. for (let j = normalizedBegin; j <= normalizedEnd; ++j) {
  48. allPrevSegments.push(list[j][i]);
  49. }
  50. segments.push(create(context.idGenerator.next(), allPrevSegments));
  51. }
  52. return segments;
  53. }
  54. /**
  55. * `segments` becomes doubly in a `finally` block. Then if a code path exits by a
  56. * control statement (such as `break`, `continue`) from the `finally` block, the
  57. * destination's segments may be half of the source segments. In that case, this
  58. * merges segments.
  59. *
  60. * @param {ForkContext} context - An instance.
  61. * @param {CodePathSegment[]} segments - Segments to merge.
  62. * @returns {CodePathSegment[]} The merged segments.
  63. */
  64. function mergeExtraSegments(context, segments) {
  65. let currentSegments = segments;
  66. while (currentSegments.length > context.count) {
  67. const merged = [];
  68. for (let i = 0, length = currentSegments.length / 2 | 0; i < length; ++i) {
  69. merged.push(CodePathSegment.newNext(
  70. context.idGenerator.next(),
  71. [currentSegments[i], currentSegments[i + length]]
  72. ));
  73. }
  74. currentSegments = merged;
  75. }
  76. return currentSegments;
  77. }
  78. //------------------------------------------------------------------------------
  79. // Public Interface
  80. //------------------------------------------------------------------------------
  81. /**
  82. * A class to manage forking.
  83. */
  84. class ForkContext {
  85. /**
  86. * @param {IdGenerator} idGenerator - An identifier generator for segments.
  87. * @param {ForkContext|null} upper - An upper fork context.
  88. * @param {number} count - A number of parallel segments.
  89. */
  90. constructor(idGenerator, upper, count) {
  91. this.idGenerator = idGenerator;
  92. this.upper = upper;
  93. this.count = count;
  94. this.segmentsList = [];
  95. }
  96. /**
  97. * The head segments.
  98. * @type {CodePathSegment[]}
  99. */
  100. get head() {
  101. const list = this.segmentsList;
  102. return list.length === 0 ? [] : list[list.length - 1];
  103. }
  104. /**
  105. * A flag which shows empty.
  106. * @type {boolean}
  107. */
  108. get empty() {
  109. return this.segmentsList.length === 0;
  110. }
  111. /**
  112. * A flag which shows reachable.
  113. * @type {boolean}
  114. */
  115. get reachable() {
  116. const segments = this.head;
  117. return segments.length > 0 && segments.some(isReachable);
  118. }
  119. /**
  120. * Creates new segments from this context.
  121. *
  122. * @param {number} begin - The first index of previous segments.
  123. * @param {number} end - The last index of previous segments.
  124. * @returns {CodePathSegment[]} New segments.
  125. */
  126. makeNext(begin, end) {
  127. return makeSegments(this, begin, end, CodePathSegment.newNext);
  128. }
  129. /**
  130. * Creates new segments from this context.
  131. * The new segments is always unreachable.
  132. *
  133. * @param {number} begin - The first index of previous segments.
  134. * @param {number} end - The last index of previous segments.
  135. * @returns {CodePathSegment[]} New segments.
  136. */
  137. makeUnreachable(begin, end) {
  138. return makeSegments(this, begin, end, CodePathSegment.newUnreachable);
  139. }
  140. /**
  141. * Creates new segments from this context.
  142. * The new segments don't have connections for previous segments.
  143. * But these inherit the reachable flag from this context.
  144. *
  145. * @param {number} begin - The first index of previous segments.
  146. * @param {number} end - The last index of previous segments.
  147. * @returns {CodePathSegment[]} New segments.
  148. */
  149. makeDisconnected(begin, end) {
  150. return makeSegments(this, begin, end, CodePathSegment.newDisconnected);
  151. }
  152. /**
  153. * Adds segments into this context.
  154. * The added segments become the head.
  155. *
  156. * @param {CodePathSegment[]} segments - Segments to add.
  157. * @returns {void}
  158. */
  159. add(segments) {
  160. assert(segments.length >= this.count, `${segments.length} >= ${this.count}`);
  161. this.segmentsList.push(mergeExtraSegments(this, segments));
  162. }
  163. /**
  164. * Replaces the head segments with given segments.
  165. * The current head segments are removed.
  166. *
  167. * @param {CodePathSegment[]} segments - Segments to add.
  168. * @returns {void}
  169. */
  170. replaceHead(segments) {
  171. assert(segments.length >= this.count, `${segments.length} >= ${this.count}`);
  172. this.segmentsList.splice(-1, 1, mergeExtraSegments(this, segments));
  173. }
  174. /**
  175. * Adds all segments of a given fork context into this context.
  176. *
  177. * @param {ForkContext} context - A fork context to add.
  178. * @returns {void}
  179. */
  180. addAll(context) {
  181. assert(context.count === this.count);
  182. const source = context.segmentsList;
  183. for (let i = 0; i < source.length; ++i) {
  184. this.segmentsList.push(source[i]);
  185. }
  186. }
  187. /**
  188. * Clears all secments in this context.
  189. *
  190. * @returns {void}
  191. */
  192. clear() {
  193. this.segmentsList = [];
  194. }
  195. /**
  196. * Creates the root fork context.
  197. *
  198. * @param {IdGenerator} idGenerator - An identifier generator for segments.
  199. * @returns {ForkContext} New fork context.
  200. */
  201. static newRoot(idGenerator) {
  202. const context = new ForkContext(idGenerator, null, 1);
  203. context.add([CodePathSegment.newRoot(idGenerator.next())]);
  204. return context;
  205. }
  206. /**
  207. * Creates an empty fork context preceded by a given context.
  208. *
  209. * @param {ForkContext} parentContext - The parent fork context.
  210. * @param {boolean} forkLeavingPath - A flag which shows inside of `finally` block.
  211. * @returns {ForkContext} New fork context.
  212. */
  213. static newEmpty(parentContext, forkLeavingPath) {
  214. return new ForkContext(
  215. parentContext.idGenerator,
  216. parentContext,
  217. (forkLeavingPath ? 2 : 1) * parentContext.count
  218. );
  219. }
  220. }
  221. module.exports = ForkContext;