Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. Boundary = function (value, type) {
  2. this.value = value;
  3. this.type = type;
  4. }
  5.  
  6. Boundary.weightTable = {
  7. ")": 1,
  8. "]": 2,
  9. "[": 2,
  10. "(": 3
  11. }
  12.  
  13. Boundary.prototype.lessThan = function (boundary) {
  14. if (this.value < boundary.value) {
  15. return 1;
  16. } else if (this.value > boundary.value) {
  17. return -1;
  18. } else { // this.value === interval.value
  19. var w1 = Boundary.weightTable[this.type];
  20. var w2 = Boundary.weightTable[boundary.type];
  21. if (w1 === w2) {
  22. return 0;
  23. } else if (w1 < w2) {
  24. return 1;
  25. } else {
  26. return -1;
  27. }
  28. }
  29. }
  30.  
  31. Interval = function (left, right) {
  32. if (left.lessThan(right) < 0) {
  33. throw left + " > " + right;
  34. }
  35. this.left = left;
  36. this.right = right;
  37. }
  38.  
  39. Interval.prototype.intersects = function (interval) {
  40. // Sort
  41. var first;
  42. var second;
  43. if (this.left.lessThan(interval.left) >= 0) {
  44. first = this;
  45. second = interval;
  46. } else {
  47. first = interval;
  48. second = this;
  49. }
  50. if (first.right.lessThan(second.left) > 0) {
  51. return false;
  52. } else {
  53. return true;
  54. }
  55. }
  56.  
  57. Interval.makeOpen = function (left, right) {
  58. return new Interval(new Boundary(left, "("),
  59. new Boundary(right, ")"));
  60. }
  61.  
  62. Interval.makeClosed = function (left, right) {
  63. return new Interval(new Boundary(left, "["),
  64. new Boundary(right, "]"));
  65. }
  66.  
  67. Interval.makeOpenLeft = function (left, right) {
  68. return new Interval(new Boundary(left, "("),
  69. new Boundary(right, "]"));
  70. }
  71.  
  72. Interval.makeOpenRight = function (left, right) {
  73. return new Interval(new Boundary(left, "["),
  74. new Boundary(right, ")"));
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement