pd-code-pre-nxt 0.0.1__tar.gz → 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,64 @@
1
+ Metadata-Version: 2.4
2
+ Name: pd-code-pre-nxt
3
+ Version: 0.1.1
4
+ Summary: get `pre` and `next` node for every number in pd_code.
5
+ License: MIT
6
+ License-File: LICENSE
7
+ Author: GGN_2015
8
+ Author-email: neko@jlulug.org
9
+ Requires-Python: >=3.10
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Requires-Dist: pd-code-sanity (>=0.1.0)
18
+ Description-Content-Type: text/markdown
19
+
20
+ # pd-code-pre-nxt
21
+
22
+ Build deterministic predecessor and successor maps for every PD-code arc label.
23
+
24
+ ## Installation
25
+
26
+ ```bash
27
+ pip install pd-code-pre-nxt
28
+ ```
29
+
30
+ ## Usage example
31
+
32
+ ```python
33
+ from pd_code_pre_nxt import get_pre_nxt
34
+
35
+ pd = [[1, 5, 2, 4], [3, 1, 4, 6], [5, 3, 6, 2]]
36
+ pre, nxt = get_pre_nxt(pd)
37
+ print(nxt[1], pre[1])
38
+ ```
39
+
40
+ ## Algorithm
41
+
42
+ Opposite crossing slots define the component graph. Each component must be a two-regular cycle, with a special deterministic treatment for two-label cycles. The smallest label is chosen as the cycle start; both traversal directions are compared lexicographically and the canonical one defines `pre` and `nxt`. Numeric suffixes are supported for temporary prefixed labels used during connected sums.
43
+
44
+ ## Input conventions
45
+
46
+ A PD code is represented as a list of four-entry crossings. Arc labels normally occur exactly twice. Public functions validate inputs and return new values rather than mutating caller-owned data unless their API explicitly says otherwise.
47
+
48
+ ## External software
49
+
50
+ No external software is required.
51
+
52
+ ## Development
53
+
54
+ Run examples and package checks before release. Python packages require Python 3.10 or newer. Build PyPI artifacts with:
55
+
56
+ ```bash
57
+ poetry check
58
+ poetry build
59
+ ```
60
+
61
+ ## License
62
+
63
+ MIT. See `LICENSE`.
64
+
@@ -0,0 +1,44 @@
1
+ # pd-code-pre-nxt
2
+
3
+ Build deterministic predecessor and successor maps for every PD-code arc label.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install pd-code-pre-nxt
9
+ ```
10
+
11
+ ## Usage example
12
+
13
+ ```python
14
+ from pd_code_pre_nxt import get_pre_nxt
15
+
16
+ pd = [[1, 5, 2, 4], [3, 1, 4, 6], [5, 3, 6, 2]]
17
+ pre, nxt = get_pre_nxt(pd)
18
+ print(nxt[1], pre[1])
19
+ ```
20
+
21
+ ## Algorithm
22
+
23
+ Opposite crossing slots define the component graph. Each component must be a two-regular cycle, with a special deterministic treatment for two-label cycles. The smallest label is chosen as the cycle start; both traversal directions are compared lexicographically and the canonical one defines `pre` and `nxt`. Numeric suffixes are supported for temporary prefixed labels used during connected sums.
24
+
25
+ ## Input conventions
26
+
27
+ A PD code is represented as a list of four-entry crossings. Arc labels normally occur exactly twice. Public functions validate inputs and return new values rather than mutating caller-owned data unless their API explicitly says otherwise.
28
+
29
+ ## External software
30
+
31
+ No external software is required.
32
+
33
+ ## Development
34
+
35
+ Run examples and package checks before release. Python packages require Python 3.10 or newer. Build PyPI artifacts with:
36
+
37
+ ```bash
38
+ poetry check
39
+ poetry build
40
+ ```
41
+
42
+ ## License
43
+
44
+ MIT. See `LICENSE`.
@@ -0,0 +1,74 @@
1
+ import re
2
+
3
+ import pd_code_sanity
4
+
5
+
6
+ def get_raw_val(value: int | str) -> int:
7
+ if isinstance(value, bool):
8
+ raise TypeError("boolean labels are not valid")
9
+ if isinstance(value, int):
10
+ return value
11
+ match = re.search(r"(-?\d+)$", value)
12
+ if not match:
13
+ raise ValueError(f"label has no numeric suffix: {value!r}")
14
+ return int(match.group(1))
15
+
16
+
17
+ def get_num_set(pd_code: list[list]) -> list:
18
+ return sorted({label for crossing in pd_code for label in crossing}, key=get_raw_val)
19
+
20
+
21
+ def _canonical_cycle(component: set, adjacency: dict) -> list:
22
+ start = min(component, key=get_raw_val)
23
+ neighbors = list(adjacency[start])
24
+ if len(component) == 2:
25
+ return [start, neighbors[0]]
26
+ if len(neighbors) != 2:
27
+ raise ValueError("PD component graph is not a cycle")
28
+
29
+ candidates = []
30
+ for first in neighbors:
31
+ cycle = [start]
32
+ previous, current = start, first
33
+ while current != start:
34
+ if current in cycle:
35
+ raise ValueError("PD component graph contains a short cycle")
36
+ cycle.append(current)
37
+ next_nodes = adjacency[current] - {previous}
38
+ if len(next_nodes) != 1:
39
+ raise ValueError("PD component graph is not 2-regular")
40
+ previous, current = current, next(iter(next_nodes))
41
+ if len(cycle) != len(component):
42
+ raise ValueError("PD component graph is disconnected")
43
+ candidates.append(cycle)
44
+ return min(candidates, key=lambda cycle: [get_raw_val(item) for item in cycle])
45
+
46
+
47
+ def get_pre_nxt(pd_code: list[list]) -> tuple[dict, dict]:
48
+ """Build deterministic predecessor/successor maps from PD topology."""
49
+ if not pd_code_sanity.sanity(pd_code):
50
+ raise ValueError("invalid PD code")
51
+
52
+ adjacency: dict = {}
53
+ for crossing in pd_code:
54
+ for a, b in ((crossing[0], crossing[2]), (crossing[1], crossing[3])):
55
+ adjacency.setdefault(a, set()).add(b)
56
+ adjacency.setdefault(b, set()).add(a)
57
+
58
+ pre, nxt, seen = {}, {}, set()
59
+ for start in get_num_set(pd_code):
60
+ if start in seen:
61
+ continue
62
+ stack, component = [start], set()
63
+ while stack:
64
+ node = stack.pop()
65
+ if node in component:
66
+ continue
67
+ component.add(node)
68
+ stack.extend(adjacency[node] - component)
69
+ cycle = _canonical_cycle(component, adjacency)
70
+ seen.update(component)
71
+ for index, node in enumerate(cycle):
72
+ pre[node] = cycle[index - 1]
73
+ nxt[node] = cycle[(index + 1) % len(cycle)]
74
+ return pre, nxt
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pd-code-pre-nxt"
3
- version = "0.0.1"
3
+ version = "0.1.1"
4
4
  description = "get `pre` and `next` node for every number in pd_code."
5
5
  authors = [
6
6
  {name = "GGN_2015",email = "neko@jlulug.org"}
@@ -8,9 +8,9 @@ authors = [
8
8
  license = {text = "MIT"}
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
11
- dependencies = [
12
-
13
- ]
11
+ dependencies = [
12
+ "pd-code-sanity>=0.1.0"
13
+ ]
14
14
 
15
15
 
16
16
  [build-system]
@@ -1,44 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: pd-code-pre-nxt
3
- Version: 0.0.1
4
- Summary: get `pre` and `next` node for every number in pd_code.
5
- License: MIT
6
- License-File: LICENSE
7
- Author: GGN_2015
8
- Author-email: neko@jlulug.org
9
- Requires-Python: >=3.10
10
- Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Programming Language :: Python :: 3
12
- Classifier: Programming Language :: Python :: 3.10
13
- Classifier: Programming Language :: Python :: 3.11
14
- Classifier: Programming Language :: Python :: 3.12
15
- Classifier: Programming Language :: Python :: 3.13
16
- Classifier: Programming Language :: Python :: 3.14
17
- Description-Content-Type: text/markdown
18
-
19
- # pd_code_pre_nxt
20
- get `pre` and `next` node for every number in pd_code.
21
-
22
- ## Install
23
-
24
- ```bash
25
- pip install pd-code-pre-nxt
26
- ```
27
-
28
- ## Usage
29
-
30
- ```python
31
- import pd_code_pre_nxt
32
-
33
- # link pd_code
34
- pd_code = [[1, 5, 2, 4], [3, 1, 4, 6], [5, 3, 6, 2]]
35
-
36
- # output number set (ascending order)
37
- print(pd_code_pre_nxt.get_num_set(pd_code))
38
-
39
- # get pre, nxt dict for pd_code
40
- pre, nxt = pd_code_pre_nxt.get_pre_nxt(pd_code)
41
- print(pre)
42
- print(nxt)
43
- ```
44
-
@@ -1,25 +0,0 @@
1
- # pd_code_pre_nxt
2
- get `pre` and `next` node for every number in pd_code.
3
-
4
- ## Install
5
-
6
- ```bash
7
- pip install pd-code-pre-nxt
8
- ```
9
-
10
- ## Usage
11
-
12
- ```python
13
- import pd_code_pre_nxt
14
-
15
- # link pd_code
16
- pd_code = [[1, 5, 2, 4], [3, 1, 4, 6], [5, 3, 6, 2]]
17
-
18
- # output number set (ascending order)
19
- print(pd_code_pre_nxt.get_num_set(pd_code))
20
-
21
- # get pre, nxt dict for pd_code
22
- pre, nxt = pd_code_pre_nxt.get_pre_nxt(pd_code)
23
- print(pre)
24
- print(nxt)
25
- ```
@@ -1,77 +0,0 @@
1
- import pd_code_sanity
2
-
3
- # 提取原始数值
4
- # 如果里面有下划线,则去掉下划线转 int
5
- def get_raw_val(val_in_crs:int|str) -> int:
6
- if isinstance(val_in_crs, str):
7
- return int(val_in_crs.split("_")[-1])
8
- return int(val_in_crs)
9
-
10
- def add_edge(pre:dict, nxt:dict, val1, val2):
11
- raw_val1 = get_raw_val(val1)
12
- raw_val2 = get_raw_val(val2)
13
-
14
- # 记录前后关系
15
- pre_val = -1
16
- nxt_val = -1
17
-
18
- # 确定前后关系
19
- if abs(raw_val1 - raw_val2) == 1:
20
- if raw_val1 < raw_val2:
21
- pre_val = val1
22
- nxt_val = val2
23
- else:
24
- pre_val = val2
25
- nxt_val = val1
26
- else:
27
- if raw_val1 < raw_val2:
28
- pre_val = val2
29
- nxt_val = val1
30
- else:
31
- pre_val = val1
32
- nxt_val = val2
33
-
34
- # 设置 dict 值
35
- pre[nxt_val] = pre_val
36
- nxt[pre_val] = nxt_val
37
-
38
- # 获得 pd_code 中的数值集合
39
- # 按照字典序从小到大排序
40
- def get_num_set(pd_code:list[list]):
41
- val_set = set()
42
- for crossing in pd_code:
43
- for term in crossing:
44
- if term not in val_set:
45
- val_set.add(term)
46
- return sorted(list(val_set), key=lambda x:get_raw_val(x))
47
-
48
- # 计算 pre 和 nxt
49
- # 没有 r1 crossing 就不会出现混乱
50
- def get_pre_nxt(pd_code:list[list]) -> tuple[dict, dict]:
51
-
52
- # 检查类型是否正确
53
- if not pd_code_sanity.sanity(pd_code):
54
- raise TypeError()
55
-
56
- pre = dict()
57
- nxt = dict()
58
- for crossing in pd_code:
59
- if len(set(crossing)) > 2: # 至少有三个不同的数
60
- add_edge(pre, nxt, crossing[0], crossing[2])
61
- add_edge(pre, nxt, crossing[1], crossing[3])
62
- else:
63
- two_values = list(set(crossing)) # 只有两个不同的数
64
- if len(two_values) != 2:
65
- raise AssertionError()
66
- for i in range(2):
67
- pre[two_values[i]] = two_values[1-i] # 互为前驱后继
68
- nxt[two_values[i]] = two_values[1-i]
69
-
70
- # 获取数值合集
71
- num_set = get_num_set(pd_code)
72
- for num in num_set:
73
- if pre.get(num) is None:
74
- pre[num] = nxt[num]
75
- if nxt.get(num) is None:
76
- nxt[num] = pre[num]
77
- return pre, nxt
File without changes