omlish-cext 0.0.0.dev505__tar.gz → 0.0.0.dev507__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: omlish-cext
3
- Version: 0.0.0.dev505
3
+ Version: 0.0.0.dev507
4
4
  Summary: omlish
5
5
  Author: wrmsr
6
6
  License-Expression: BSD-3-Clause
@@ -14,7 +14,7 @@ Classifier: Programming Language :: Python :: 3.13
14
14
  Requires-Python: >=3.13
15
15
  Description-Content-Type: text/markdown
16
16
  License-File: LICENSE
17
- Requires-Dist: omlish==0.0.0.dev505
17
+ Requires-Dist: omlish==0.0.0.dev507
18
18
  Dynamic: license-file
19
19
 
20
20
  # Overview
@@ -4,8 +4,10 @@
4
4
  #include "Python.h"
5
5
  #include "structmember.h"
6
6
 
7
- #include <vector>
7
+ #include <memory>
8
8
  #include <unordered_map>
9
+ #include <variant>
10
+ #include <vector>
9
11
 
10
12
  //
11
13
 
@@ -30,56 +32,12 @@ static inline collection_state * get_collection_state(PyObject *module)
30
32
 
31
33
  // Helper struct to track unique typed values during processing
32
34
  struct UniqueInfo {
33
- PyObject *unique_tv_cls; // The unique class
34
- PyObject *tv; // The typed value
35
- std::vector<PyObject*> *unique_lst; // Pointer to the list in unique_dct
36
- size_t idx; // Index in unique_lst when added
35
+ PyObject *unique_tv_cls; // The unique class (borrowed from map key)
36
+ PyObject *tv; // The typed value (borrowed from args)
37
+ size_t idx; // Index in unique_lst when added
37
38
  };
38
39
 
39
- // Temporary list item - either a TypedValue or a UniqueInfo
40
- struct TmpItem {
41
- bool is_unique;
42
- union {
43
- PyObject *tv; // Non-unique typed value
44
- UniqueInfo *unique_info; // Unique typed value info
45
- };
46
-
47
- TmpItem() : is_unique(false), tv(nullptr) {}
48
- ~TmpItem() {
49
- if (is_unique && unique_info != nullptr) {
50
- delete unique_info;
51
- }
52
- }
53
-
54
- // Disable copy, enable move
55
- TmpItem(const TmpItem&) = delete;
56
- TmpItem& operator=(const TmpItem&) = delete;
57
-
58
- TmpItem(TmpItem&& other) noexcept : is_unique(other.is_unique) {
59
- if (is_unique) {
60
- unique_info = other.unique_info;
61
- other.unique_info = nullptr;
62
- } else {
63
- tv = other.tv;
64
- }
65
- }
66
-
67
- TmpItem& operator=(TmpItem&& other) noexcept {
68
- if (this != &other) {
69
- if (is_unique && unique_info != nullptr) {
70
- delete unique_info;
71
- }
72
- is_unique = other.is_unique;
73
- if (is_unique) {
74
- unique_info = other.unique_info;
75
- other.unique_info = nullptr;
76
- } else {
77
- tv = other.tv;
78
- }
79
- }
80
- return *this;
81
- }
82
- };
40
+ using TmpItem = std::variant<PyObject*, std::unique_ptr<UniqueInfo>>;
83
41
 
84
42
  PyDoc_STRVAR(init_typed_values_collection_doc,
85
43
  "init_typed_values_collection(*tvs, override=False, check_type=None)\n"
@@ -231,19 +189,13 @@ static PyObject * init_typed_values_collection(PyObject *module, PyObject *const
231
189
  std::vector<PyObject*> &unique_lst = insertion.first->second;
232
190
  unique_lst.push_back(tv);
233
191
 
234
- // Create UniqueInfo
235
- UniqueInfo *info = new UniqueInfo{
236
- unique_tv_cls, // Borrowed reference (will be decreffed at end)
192
+ // Add to tmp_lst using make_unique
193
+ // unique_ptr automatically handles deletion, variant handles moves
194
+ tmp_lst.emplace_back(std::make_unique<UniqueInfo>(UniqueInfo{
195
+ unique_tv_cls, // Borrowed from map
237
196
  tv,
238
- &unique_lst,
239
197
  unique_lst.size()
240
- };
241
-
242
- // Add to tmp_lst
243
- TmpItem item;
244
- item.is_unique = true;
245
- item.unique_info = info;
246
- tmp_lst.push_back(std::move(item));
198
+ }));
247
199
 
248
200
  } else {
249
201
  // Check if it's a TypedValue
@@ -258,10 +210,7 @@ static PyObject * init_typed_values_collection(PyObject *module, PyObject *const
258
210
  }
259
211
 
260
212
  // Non-unique typed value
261
- TmpItem item;
262
- item.is_unique = false;
263
- item.tv = tv;
264
- tmp_lst.push_back(std::move(item));
213
+ tmp_lst.emplace_back(tv);
265
214
  }
266
215
  }
267
216
 
@@ -279,11 +228,16 @@ static PyObject * init_typed_values_collection(PyObject *module, PyObject *const
279
228
 
280
229
  // Process tmp_lst to build output list and tmp_dct
281
230
  for (auto &item : tmp_lst) {
282
- if (item.is_unique) {
283
- UniqueInfo *info = item.unique_info;
231
+ // Check if item holds unique_ptr<UniqueInfo>
232
+ if (auto *uniq_ptr_wrapper = std::get_if<std::unique_ptr<UniqueInfo>>(&item)) {
233
+ UniqueInfo *info = uniq_ptr_wrapper->get();
234
+
235
+ // Look up the vector now (after all insertions are done, no more rehashing)
236
+ auto it = unique_dct.find(info->unique_tv_cls);
237
+ assert(it != unique_dct.end());
284
238
 
285
239
  // Last-in-wins: only include if this is the last one
286
- if (info->idx == info->unique_lst->size()) {
240
+ if (info->idx == it->second.size()) {
287
241
  // Add to output list
288
242
  if (PyList_Append(lst, info->tv) == -1) {
289
243
  Py_DECREF(lst);
@@ -299,7 +253,8 @@ static PyObject * init_typed_values_collection(PyObject *module, PyObject *const
299
253
  }
300
254
  }
301
255
  } else {
302
- PyObject *tv = item.tv;
256
+ // Must be PyObject*
257
+ PyObject *tv = std::get<PyObject*>(item);
303
258
 
304
259
  // Add to output list
305
260
  if (PyList_Append(lst, tv) == -1) {
@@ -542,4 +497,4 @@ PyMODINIT_FUNC PyInit__collection(void)
542
497
  return PyModuleDef_Init(&collection_module);
543
498
  }
544
499
 
545
- }
500
+ }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: omlish-cext
3
- Version: 0.0.0.dev505
3
+ Version: 0.0.0.dev507
4
4
  Summary: omlish
5
5
  Author: wrmsr
6
6
  License-Expression: BSD-3-Clause
@@ -14,7 +14,7 @@ Classifier: Programming Language :: Python :: 3.13
14
14
  Requires-Python: >=3.13
15
15
  Description-Content-Type: text/markdown
16
16
  License-File: LICENSE
17
- Requires-Dist: omlish==0.0.0.dev505
17
+ Requires-Dist: omlish==0.0.0.dev507
18
18
  Dynamic: license-file
19
19
 
20
20
  # Overview
@@ -0,0 +1 @@
1
+ omlish==0.0.0.dev507
@@ -13,7 +13,7 @@ urls = {source = 'https://github.com/wrmsr/omlish'}
13
13
  license = 'BSD-3-Clause'
14
14
  readme = 'README.md'
15
15
  requires-python = '>=3.13'
16
- version = '0.0.0.dev505'
16
+ version = '0.0.0.dev507'
17
17
  classifiers = [
18
18
  'Development Status :: 2 - Pre-Alpha',
19
19
  'Intended Audience :: Developers',
@@ -24,7 +24,7 @@ classifiers = [
24
24
  ]
25
25
  description = 'omlish'
26
26
  dependencies = [
27
- 'omlish == 0.0.0.dev505',
27
+ 'omlish == 0.0.0.dev507',
28
28
  ]
29
29
 
30
30
  [tool.setuptools]
@@ -1 +0,0 @@
1
- omlish==0.0.0.dev505