-
Notifications
You must be signed in to change notification settings - Fork 0
/
Range.lua
174 lines (164 loc) · 5.33 KB
/
Range.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
return function (Version)
local Range = class("Range")
local SEPARATOR = " "
Range.NOTHING = -1
Range.ANY = 0
Range.EQUALS = 1
Range.UNDER = 2
Range.ABOVE = 3
Range.BETWEEN = 4
local operatorMap = {
["*"] = Range.ANY,
["#"] = Range.NOTHING,
["="] = Range.EQUALS,
["<"] = Range.UNDER,
[">"] = Range.ABOVE,
["~"] = Range.BETWEEN
}
function Range:_init(type, a, b)
self.type = type
self.a = a
self.b = b
end
function Range.Parse(source)
local operator = string.sub(source, 1, 1)
local type = operatorMap[operator]
if not type then
error ("Unknown range operator!")
end
local a
local b
if type == Range.BETWEEN then
local pos = string.find(source, SEPARATOR)
a = Version(string.sub(source, 2, pos - 1))
b = Version(string.sub(source, pos + 1))
elseif type ~= Range.NOTHING then
a = Version(string.sub(source, 2))
end
return Range(type, a, b)
end
function Range:ToString()
local operator
for ch, v in pairs(operatorMap) do
if v == self.type then
operator = ch
break
end
end
if self.type == Range.NOTHING or self.type == Range.ANY then
return operator
elseif self.type == Range.BETWEEN then
return table.concat{operator, self.a.ToString(), " ", self.b.ToString()}
else
return operator .. self.a.ToString()
end
end
local testMap = {
[Range.ANY] = function(r, v) return true end,
[Range.NOTHING] = function(r, v) return false end,
[Range.EQUALS] = function(r, v) return r.a == v end,
[Range.UNDER] = function(r, v) return v < r.a end,
[Range.ABOVE] = function(r, v) return v > r.a end,
[Range.BETWEEN] = function(r, v) return r.a < v and v < r.b end
}
function Range:Test(version)
return testMap[self.type](self, version)
end
local intersectMap = {
[Range.EQUALS] = {
[Range.EQUALS] = function(r1, r2)
if r1.a == r2.a then
return Range(Range.EQUALS, r1.a)
else
return Range(Range.NOTHING)
end
end,
[Range.UNDER] = function(r1, r2)
if r1.a < r2.a then
return Range(Range.EQUALS, r1.a)
else
return Range(Range.NOTHING)
end
end,
[Range.ABOVE] = function(r1, r2)
if r1.a > r2.a then
return Range(Range.EQUALS, r1.a)
else
return Range(Range.NOTHING)
end
end,
[Range.BETWEEN] = function(r1, r2)
if r2.a < r1.a and r1.a < r2.b then
return Range(Range.EQUALS, r1.a)
else
return Range(Range.NOTHING)
end
end
},
[Range.UNDER] = {
[Range.UNDER] = function(r1, r2)
return Range(Range.EQUALS, r1.a * r2.a)
end,
[Range.ABOVE] = function(r1, r2)
if r2.a == r1.a then
return Range(Range.EQUALS, r1.a)
elseif r2.a < r1.a then
return Range(Range.BETWEEN, r2.a, r1.a)
else
return Range(Range.NOTHING)
end
end,
[Range.BETWEEN] = function(r1, r2)
if r1.a == r2.a then
return Range(Range.EQUALS, r1.a)
elseif r1.a > r2.a then
return Range(Range.BETWEEN, r2.a, r1.a * r2.b)
else
return Range(Range.NOTHING)
end
end
},
[Range.ABOVE] = {
[Range.ABOVE] = function(r1, r2)
return Range(Range.EQUALS, r1.a + r2.a)
end,
[Range.BETWEEN] = function(r1, r2)
if r1.a == r2.b then
return Range(Range.EQUALS, r1.a)
elseif r1.a < r2.b then
return Range(Range.BETWEEN, r1.a + r2.a, r2.b)
else
return Range(Range.NOTHING)
end
end
},
[Range.BETWEEN] = {
[Range.BETWEEN] = function(r1, r2)
if r1.b == r2.a then
return Range(Range.EQUALS, r1.b)
elseif r1.a == r2.b then
return Range(Range.EQUALS, r1.a)
elseif r2.a < r1.b and r2.b > r1.a then
return Range(Range.BETWEEN, r1.a + r2.a, r1.b * r2.b)
else
return Range(Range.NOTHING)
end
end
}
}
function Range:__mult(other)
local r1 = self
local r2 = other
if r1.type > r2.type then
r1, r2 = r2, r1
end
if r1.type == Range.NOTHING then
return Range(Range.NOTHING)
elseif r1.type == Range.ANY then
return Range(Range.ANY)
else
return intersectMap[r1.type][r2.type](r1, r2)
end
end
return Range
end