multiless.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package multiless
  2. type (
  3. // A helper for long chains of "less-than" comparisons, where later comparisons are only
  4. // required if earlier ones haven't resolved the comparison.
  5. Computation struct {
  6. ok bool
  7. less bool
  8. }
  9. )
  10. func New() Computation {
  11. return Computation{}
  12. }
  13. func (me Computation) EagerSameLess(same, less bool) Computation {
  14. if me.ok || same {
  15. return me
  16. }
  17. return Computation{
  18. ok: true,
  19. less: less,
  20. }
  21. }
  22. func (me Computation) LazySameLess(lazy func() (same, less bool)) Computation {
  23. if me.ok {
  24. return me
  25. }
  26. same, less := lazy()
  27. if !same {
  28. me.less = less
  29. }
  30. return me
  31. }
  32. // Sorts so that false comes before true.
  33. func (me Computation) Bool(l, r bool) Computation {
  34. return me.EagerSameLess(l == r, r)
  35. }
  36. func (me Computation) Uint32(l, r uint32) Computation {
  37. return me.EagerSameLess(l == r, l < r)
  38. }
  39. func (me Computation) Int64(l, r int64) Computation {
  40. return me.EagerSameLess(l == r, l < r)
  41. }
  42. func (me Computation) Uint64(l, r uint64) Computation {
  43. return me.EagerSameLess(l == r, l < r)
  44. }
  45. func (me Computation) Int(l, r int) Computation {
  46. return me.EagerSameLess(l == r, l < r)
  47. }
  48. func (me Computation) CmpInt64(i int64) Computation {
  49. return me.EagerSameLess(i == 0, i < 0)
  50. }
  51. func (me Computation) Cmp(i int) Computation {
  52. return me.EagerSameLess(i == 0, i < 0)
  53. }
  54. func (me Computation) Uintptr(l, r uintptr) Computation {
  55. return me.EagerSameLess(l == r, l < r)
  56. }
  57. func (me Computation) Less() bool {
  58. return me.less
  59. }
  60. func (me Computation) Ok() bool {
  61. return me.ok
  62. }
  63. func (me Computation) LessOk() (less, ok bool) {
  64. return me.less, me.ok
  65. }
  66. func (me Computation) MustLess() bool {
  67. less, ok := me.LessOk()
  68. if !ok {
  69. panic("computation has not differentiated yet")
  70. }
  71. return less
  72. }
  73. func (me Computation) Float64(l, r float64) Computation {
  74. return me.EagerSameLess(l == r, l < r)
  75. }
  76. func (me Computation) Lazy(f func() Computation) Computation {
  77. if me.ok {
  78. return me
  79. }
  80. return f()
  81. }
  82. func (me Computation) AndThen(then Computation) Computation {
  83. if me.ok {
  84. return me
  85. } else {
  86. return then
  87. }
  88. }
  89. // Returns -1 if less, 0 if equal, and 1 if greater than
  90. func (me Computation) OrderingInt() int {
  91. if me.ok {
  92. if me.less {
  93. return -1
  94. } else {
  95. return 1
  96. }
  97. } else {
  98. return 0
  99. }
  100. }