forked from olivierkes/manuskript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique_id.py
43 lines (31 loc) · 978 Bytes
/
unique_id.py
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
#!/usr/bin/env python
# --!-- coding: utf8 --!--
class UniqueID:
def __init__(self, host, value: int):
self.host = host
self.value = value
def __str__(self):
return str(self.value)
class UniqueIDHost:
def __init__(self):
self.counter = 0
self.uids = dict()
def reset(self):
self.counter = 0
self.uids.clear()
def newID(self):
uid = UniqueID(self, self.counter)
self.counter = self.counter + 1
self.uids[uid.value] = uid
return uid
def loadID(self, value: int):
if value in self.uids:
raise ValueError("ID not unique: " + str(value))
uid = UniqueID(self, value)
self.counter = max(self.counter, uid.value + 1)
self.uids[uid.value] = uid
return uid
def removeID(self, uid: UniqueID):
if uid.host != self:
raise ValueError("ID not bound to host!")
self.uids.pop(uid.value)