-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_hook.lua
66 lines (49 loc) · 1.22 KB
/
stack_hook.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
local error = error
local NotLimitedStack = {}
NotLimitedStack.__index = NotLimitedStack
function NotLimitedStack:new()
-- да, типа синглетон
return self
end
function NotLimitedStack:beforePush(stack)
end
function NotLimitedStack:afterPush(stack)
end
function NotLimitedStack:afterPop(stack)
end
local LimitedStack = {}
LimitedStack.__index = LimitedStack
function LimitedStack:new(max_deep)
local t = {}
setmetatable(t, LimitedStack)
t:reset(max_deep)
return t
end
function LimitedStack:beforePush(stack)
if self.cur_count >= self.max then
error('Stack overflow', 1)
return nil
end
end
function LimitedStack:afterPush(stack)
self.cur_count = self.cur_count + 1
end
function LimitedStack:afterPop(stack)
local cur_count = self.cur_count
if cur_count > 0 then
self.cur_count = cur_count - 1
end
end
function LimitedStack:reset(new_deep)
local max_to_set = new_deep or self.max
max_to_set = tonumber(max_to_set)
if not max_to_set or max_to_set < 1 then
error('Incorrect deep')
end
self.max = max_to_set
self.cur_count = 0
end
return {
NotLimitedStack = NotLimitedStack,
LimitedStack = LimitedStack
}