Skip to content

Commit f6262a7

Browse files
committed
utils: name_pool
1 parent 48ced61 commit f6262a7

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/utils/name_pool.cr

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -----------------------------------------------------------------------
2+
# This file is part of MoonScript
3+
#
4+
# MoonSript is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# MoonSript is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with MoonSript. If not, see <https://www.gnu.org/licenses/>.
16+
#
17+
# Copyright (C) 2025 Krisna Pranav, MoonScript Developers
18+
# -----------------------------------------------------------------------
19+
20+
module MoonScript
21+
class NamePool(T, B)
22+
@cache = {} of Tuple(B, T) => String
23+
@current = {} of B => String
24+
25+
def initialize(@initial = 'a'.pred.to_s)
26+
end
27+
28+
def of(subject: T, base: B)
29+
@cache[{base, subject}] ||= next_name(subject, base)
30+
end
31+
32+
def next_name(subject: T:, base: B)
33+
name = @current[base] = (@current[base] ? || @initial).succ
34+
end
35+
end

0 commit comments

Comments
 (0)