pd-code-pre-nxt 0.0.1__tar.gz → 0.1.0__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,43 @@
1
+ Metadata-Version: 2.4
2
+ Name: pd-code-pre-nxt
3
+ Version: 0.1.0
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 from PD-code topology.
23
+
24
+ ## Installation
25
+
26
+ ```bash
27
+ pip install pd-code-pre-nxt
28
+ ```
29
+
30
+ ## Quick start
31
+
32
+ `from pd_code_pre_nxt import get_pre_nxt`.
33
+
34
+ PD codes are lists of four-entry crossings. Each arc label must occur exactly twice. Functions validate their inputs and do not mutate caller-owned PD-code lists unless explicitly documented.
35
+
36
+ ## Development
37
+
38
+ Use Python 3.10 or newer for Python packages. Build distributions with `poetry build`. Run the package's tests or examples before publishing. C++ projects require a modern standards-compliant compiler.
39
+
40
+ ## License
41
+
42
+ MIT. See `LICENSE`.
43
+
@@ -0,0 +1,23 @@
1
+ # pd-code-pre-nxt
2
+
3
+ Build deterministic predecessor and successor maps from PD-code topology.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install pd-code-pre-nxt
9
+ ```
10
+
11
+ ## Quick start
12
+
13
+ `from pd_code_pre_nxt import get_pre_nxt`.
14
+
15
+ PD codes are lists of four-entry crossings. Each arc label must occur exactly twice. Functions validate their inputs and do not mutate caller-owned PD-code lists unless explicitly documented.
16
+
17
+ ## Development
18
+
19
+ Use Python 3.10 or newer for Python packages. Build distributions with `poetry build`. Run the package's tests or examples before publishing. C++ projects require a modern standards-compliant compiler.
20
+
21
+ ## License
22
+
23
+ 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.0"
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