bool-hybrid-array 9.11.14__py3-none-any.whl → 9.11.16__py3-none-any.whl

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.
bool_hybrid_array/core.py CHANGED
@@ -15,7 +15,6 @@ from functools import lru_cache
15
15
  from typing import _GenericAlias
16
16
  from typing import Callable, Union, Sequence, MutableSequence, Any, overload, Sized
17
17
  hybrid_array_cache:list[tuple[Any]] = []
18
- index_range=lambda BHA:(BHA[i] for i in itertools.count(0) if i < BHA.size)
19
18
  try:
20
19
  msvcrt = ctypes.CDLL('msvcrt.dll')
21
20
  memcpy = msvcrt.memcpy
@@ -359,7 +358,7 @@ class BoolHybridArray(MutableSequence,Exception,metaclass=ResurrectMeta):# type:
359
358
  return int(self.size)
360
359
  def __iter__(self):
361
360
  if not self:return BHA_Iterator([])
362
- return BHA_Iterator(map(self.__getitem__,index_range(size)))
361
+ return BHA_Iterator(map(self.__getitem__,itertools.takewhile(lambda x: x < self.size, itertools.count(0))))
363
362
  def __next__(self):
364
363
  return next(self.generator)
365
364
  def __contains__(self, value:Any) -> bool:
@@ -449,13 +448,13 @@ class BoolHybridArray(MutableSequence,Exception,metaclass=ResurrectMeta):# type:
449
448
  raise ValueError(f"异或运算要求数组长度相同({len(self)} vs {len(other)})")
450
449
  return BoolHybridArr(map(operator.xor, self, other),hash_ = self.hash_)
451
450
  def __gt__(self,other):
452
- if self.size!=len(other)
451
+ if self.size!=len(other):
453
452
  return self.size>len(other)
454
- return any(map(operator.gt,self,other)
453
+ return any(map(operator.gt,self,other))
455
454
  def __lt__(self,other):
456
- if self.size!=len(other)
455
+ if self.size!=len(other):
457
456
  return self.size<len(other)
458
- return any(map(operator.lt,self,other)
457
+ return any(map(operator.lt,self,other))
459
458
  def __rxor__(self, other) -> BoolHybridArray:
460
459
  return self^other
461
460
  def __invert__(self) -> BoolHybridArray:
@@ -947,6 +946,13 @@ def numba_opt():
947
946
  bisect.bisect_left = numba.njit(sig, cache=True)(bisect.bisect_left)
948
947
  bisect.bisect_right = numba.njit(sig, cache=True)(bisect.bisect_right)
949
948
  from ._cppiostream import cin,cout,endl
949
+ def namespace(name,bases,namespace_):
950
+ tmp = {}
951
+ for base in bases:tmp.update(base.__namespace__)
952
+ self = ProtectedBuiltinsDict({**tmp,**namespace_},name = name,protected_names = namespace_.get("protected_names",()))
953
+ self["__namespace__"] = self
954
+ return self
955
+
950
956
  builtins.np = np
951
957
  builtins.T = BHA_bool(1)
952
958
  builtins.F = BHA_bool(0)
@@ -0,0 +1,3 @@
1
+ try:from .compile_core import *
2
+ except:from .core import*
3
+ __all__ = tuple(globals())
@@ -0,0 +1,158 @@
1
+ from __future__ import annotations
2
+ from collections.abc import Sequence
3
+ from ..int_array import IntHybridArray
4
+ from ..core import BHA_Iterator
5
+ import operator,math,itertools
6
+ class BHA_Float:
7
+ def __init__(self, data: int | str | float | BHA_Float | tuple):
8
+ if isinstance(data, tuple):
9
+ self.a,self.b,self.length = data
10
+ return
11
+ if isinstance(data, BHA_Float):
12
+ self.a = data.a
13
+ self.b = data.b
14
+ self.length = data.length
15
+ return
16
+ if isinstance(data, int):
17
+ data = float(data)
18
+ if isinstance(data, float):
19
+ data = repr(data)
20
+ a, b = data.split('.')
21
+ b = b.rstrip('0')
22
+ self.length = len(b) if b else 1
23
+ self.a, self.b = int(a) if a else 0, int(b) if b else 0
24
+
25
+ def _align_decimal(self, other):
26
+ max_len = max(self.length, other.length)
27
+ self_num = self.a * (10 ** max_len) + self.b * (10 ** (max_len - self.length))
28
+ other_num = other.a * (10 ** max_len) + other.b * (10 ** (max_len - other.length))
29
+ return self_num, other_num, max_len
30
+
31
+ def __add__(self, other):
32
+ other = BHA_Float(other)
33
+ self_num, other_num, max_len = self._align_decimal(other)
34
+ sum_num = self_num + other_num
35
+ string = repr(sum_num)
36
+ integer_part = string[:-max_len] if len(string) > max_len else '0'
37
+ decimal_part = string[-max_len:] if len(string) >= max_len else string.zfill(max_len)
38
+ return BHA_Float(f"{integer_part}.{decimal_part}")
39
+
40
+ def __sub__(self, other):
41
+ other = BHA_Float(other)
42
+ self_num, other_num, max_len = self._align_decimal(other)
43
+ sub_num = self_num - other_num
44
+ if sub_num < 0:
45
+ string = repr(-sub_num)
46
+ sign = '-'
47
+ else:
48
+ string = repr(sub_num)
49
+ sign = ''
50
+ integer_part = string[:-max_len] if len(string) > max_len else '0'
51
+ decimal_part = string[-max_len:] if len(string) >= max_len else string.zfill(max_len)
52
+ return BHA_Float(f"{sign}{integer_part}.{decimal_part}")
53
+ def __mul__(self, other):
54
+ other = BHA_Float(other)
55
+ self_num = self.a * (10 ** self.length) + self.b
56
+ other_num = other.a * (10 ** other.length) + other.b
57
+ product = self_num * other_num
58
+ string = repr(product)
59
+ total_decimal = self.length + other.length
60
+ string = string.zfill(total_decimal + 1)
61
+ integer_part = string[:-total_decimal]
62
+ decimal_part = string[-total_decimal:]
63
+ return BHA_Float(f"{integer_part}.{decimal_part}")
64
+
65
+ def __truediv__(self, other, total_decimal = 22):
66
+ other = BHA_Float(other)
67
+ self_num = self.a * (10 ** self.length) + self.b
68
+ other_num = other.a * (10 ** other.length) + other.b
69
+ if other_num == 0:
70
+ raise ZeroDivisionError("Cannot divide by zero")
71
+ div_num = (self_num * (10 ** total_decimal)) // other_num
72
+ string = repr(div_num)
73
+ if len(string) <= total_decimal:
74
+ string = '0' * (total_decimal - len(string) + 1) + string
75
+ integer_part = string[:-total_decimal]
76
+ decimal_part = string[-total_decimal:]
77
+ return BHA_Float(f"{integer_part}.{decimal_part}")
78
+ def __repr__(self):
79
+ return f"BHA_Float({self.a}.{str(self.b).zfill(self.length)})"
80
+ def __str__(self):
81
+ return f"{self.a}.{str(self.b).zfill(self.length)}"
82
+ __radd__ = __add__
83
+ __rmul__ = __mul__
84
+ __rsub__ = lambda self,other:BHA_Float(other)-self
85
+ __rtruediv__ = lambda self,other:BHA_Float(other)/self
86
+ def __float__(self):
87
+ return float(str(self))
88
+ def __int__(self):
89
+ return self.a
90
+ is_integer = lambda self:not self.b
91
+ __bool__ = lambda self:self.a or self.b
92
+ def as_integer_ratio(self) -> tuple[int, int]:
93
+ denominator = 10 ** self.length
94
+ numerator = self.a * denominator + self.b
95
+ if numerator < 0:
96
+ numerator = -numerator
97
+ sign = -1
98
+ else:
99
+ sign = 1
100
+ gcd_val = math.gcd(numerator, denominator)
101
+ if gcd_val == 0:
102
+ gcd_val = 1
103
+ simplified_num = sign * (numerator // gcd_val)
104
+ simplified_den = denominator // gcd_val
105
+ return (simplified_num, simplified_den)
106
+ def __eq__(self,other):
107
+ try:
108
+ other = BHA_Float(other)
109
+ self_num, other_num, _ = self._align_decimal(other)
110
+ return self_num == other_num
111
+ except (ValueError, AttributeError):
112
+ return False
113
+ def __floordiv__(self, other):
114
+ return BHA_Float(int(float(self / other)))
115
+ def __mod__(self, other):
116
+ return self - (self // other) * other
117
+ def __pow__(self, power, m = None):
118
+ return BHA_Float(pow(float(self),float(BHA_Float(power)),m))
119
+ def __neg__(self):
120
+ return BHA_Float((-self.a, self.b, self.length))
121
+ def __pos__(self):
122
+ return self
123
+ def __abs__(self):
124
+ return BHA_Float((abs(self.a), self.b, self.length))
125
+ def __lt__(self,other):
126
+ a, b, _ = self._align_decimal(other)
127
+ return a<b
128
+ def __gt__(self,other):
129
+ a, b, _ = self._align_decimal(other)
130
+ return a>b
131
+ def __round__(self,n = None):
132
+ return round(float(self),n)
133
+ __rmod__ = lambda self,other:BHA_Float(other)%self
134
+ __rfloordiv__ = lambda self,other:BHA_Float(other)//self
135
+ __rpow__ = lambda self,other,m=None:pow(BHA_Float(other),self,m)
136
+ class FloatHybridArray(Sequence):
137
+ def __init__(self,data,Type = BHA_Float):
138
+ self.Type = Type
139
+ data = BHA_Iterator(map(BHA_Float,data))
140
+ self.lengths = IntHybridArray(map(operator.attrgetter('length'),data))
141
+ self.a = IntHybridArray(map(operator.attrgetter('a'),data))
142
+ self.b = IntHybridArray(map(operator.attrgetter('b'),data))
143
+ def __getitem__(self,index):
144
+ return self.Type(BHA_Float((self.a[index],self.b[index],self.lengths[index])))
145
+ def __setitem__(self,index,value):
146
+ value = BHA_Float(value)
147
+ self.lengths[index] = value.length
148
+ self.a[index] = value.a
149
+ self.b[index] = value.b
150
+ def append(self,value):
151
+ value = BHA_Float(value)
152
+ self.lengths.append(value.length)
153
+ self.a.append(value.a)
154
+ self.b.append(value.b)
155
+ def __len__(self):
156
+ return len(self.a)
157
+ def __iter__(self):
158
+ return BHA_Iterator(value for value in itertools.takewhile(lambda x: x < len(self), itertools.count(0)))
@@ -129,7 +129,8 @@ class IntHybridArray(BoolHybridArray,metaclass=ResurrectMeta):
129
129
  tmp1 = IntHybridArray([value],bit_length = self.bit_length)
130
130
  tmp = tmp1.view()
131
131
  if tmp1[0] == value:
132
- super().__setitem__(slice(key*self.bit_length,(key+1)*self.bit_length),tmp)
132
+ for i,v in zip(range(key*self.bit_length,(key+1)*self.bit_length),tmp):
133
+ super().__setitem__(i,v)
133
134
  else:
134
135
  lst = list(self)
135
136
  lst[key] = value
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bool-hybrid-array
3
- Version: 9.11.14
3
+ Version: 9.11.16
4
4
  Summary: 一个高效的布尔数组(密集+稀疏混合存储,节省内存)
5
5
  Home-page: https://github.com/BKsell/bool-hybrid-array
6
6
  Author: 蔡靖杰
@@ -449,7 +449,44 @@ print(f"第二次出队: {q.dequeue()}") # 输出:F(直接从 self.b 弹出
449
449
 
450
450
  print(f"出队2个元素后: {q}") # 输出:BHA_Queue([T,T,F,T,F])
451
451
 
452
+ #float_array子包(9.11.15版本新增)
452
453
 
454
+ # 1. 初始化数组
455
+ f_arr = float_array.FloatHybridArray([1.1, 2.2, 3.3, 4.4, 5.5])
456
+
457
+ # 2. 获取元素
458
+ first_elem = f_arr[0] # 获取第一个元素:BHA_Float(1.1)
459
+ print(f"第一个元素:{first_elem}")
460
+
461
+ # 3. 修改元素
462
+ f_arr[1] = 9.9 # 修改第二个元素为 9.9
463
+ print(f"修改后第二个元素:{f_arr[1]}")
464
+
465
+ # 4. 添加元素
466
+ f_arr.append(6.6) # 追加元素 6.6
467
+ print(f"数组长度:{len(f_arr)}") # 输出:6
468
+
469
+ # 5. 遍历数组
470
+ for elem in f_arr:
471
+ print(f"数组元素:{elem}")
472
+
473
+ #namespace元类(9.11.15版本新增)
474
+ class BaseMathTools(metaclass = namespace):
475
+ pi = 3.141
476
+ def add(a,b):
477
+ return a+b
478
+ protected_names = ("pi","add")
479
+
480
+ class MathTools(BaseMathTools,metaclass = namespace):
481
+ pi = 3.1415926535897932
482
+ def mul(a,b):
483
+ return a*b
484
+ def sub(a,b):
485
+ return a-b
486
+ e = 2.718281828
487
+ protected_names = (*BaseMathTools.protected_names,"e","sub","mul")
488
+
489
+ print(MathTools.mul(10,23))#输出:230
453
490
  ```
454
491
 
455
492
  ## 性能优势
@@ -626,6 +663,8 @@ print(f"出队2个元素后: {q}") # 输出:BHA_Queue([T,T,F,T,F])
626
663
  * **9.11.12**:优化了IntHybridArray的__setitem__,新增了IntHybridArray的sort
627
664
  * **9.11.13**:修复IndexError的错误
628
665
  * **9.11.14**:新增BoolHybridArray数组的大小比较
666
+ * **9.11.15**:新增float_array子包和namespace元类
667
+ * **9.11.16**:修复SyntaxError错误
629
668
 
630
669
 
631
670
 
@@ -0,0 +1,15 @@
1
+ bool_hybrid_array/__init__.py,sha256=Zluk7xAYl3eL8dcMj2lFmMQVAkvWTNoWFELjDBrkq6Y,1112
2
+ bool_hybrid_array/__main__.py,sha256=vJd9O_raREHIRCU0hkYzXM-lzRhgybhJqsKze6YcSDg,10058
3
+ bool_hybrid_array/_cppiostream.py,sha256=1i973ag_8XgivQkeIbqzHivflcQkVFtX77R_UwChSAg,9205
4
+ bool_hybrid_array/core.c,sha256=32k6ov1QiLS4OhMZoshe3Xie8ttBu3RANsOkP-UfL9w,3306682
5
+ bool_hybrid_array/core.py,sha256=LaOW0HXEms6FbNQYfkmY3m40m9lX4wxbVGWjU4xXDcI,42864
6
+ bool_hybrid_array/秘密.md,sha256=Ii2NvXmv-Ktu04zJsGLcQZvlzT4gOatByE4B2wTK1Ks,48
7
+ bool_hybrid_array/float_array/__init__.py,sha256=tnOiqeZ4AIvNgPV16tt0ydz0NWzSa6e4arInS6F8Y-0,86
8
+ bool_hybrid_array/float_array/core.py,sha256=LVC0YOINBoHVB5sGZcuzlsVYs_cUgfxC4idCgIzTZ78,6639
9
+ bool_hybrid_array/int_array/__init__.py,sha256=CCKDUL5GBAXOprc9FhtLNL_rP4IDhvGVWJ-Dporp6YI,112
10
+ bool_hybrid_array/int_array/core.py,sha256=ZH_UzPduamvqoYDB2tobg3zQTfZzIjrQ1qc8xi4QvP0,7974
11
+ bool_hybrid_array-9.11.16.dist-info/licenses/LICENSE,sha256=Sg4rnGXkBDYkwJCWyxdWp5H60rhVAxpNvFh_l3JWZdY,1070
12
+ bool_hybrid_array-9.11.16.dist-info/METADATA,sha256=BdR82YpDjCkqOHgY2TLF-pweTC1IzsbtNeEMh3u3OIs,27789
13
+ bool_hybrid_array-9.11.16.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
14
+ bool_hybrid_array-9.11.16.dist-info/top_level.txt,sha256=vk-TD77wuVQsN1rJ6uVWZX4sC_wya_WplRDwQKJoBZM,18
15
+ bool_hybrid_array-9.11.16.dist-info/RECORD,,
@@ -1,14 +0,0 @@
1
- bool_hybrid_array/__init__.py,sha256=UYkGgU0Ux77DN1qfEW37ba2mjDAhqCIrqCgxGpY3h_g,1086
2
- bool_hybrid_array/__main__.py,sha256=uFyebfsZl1tW54AVcc6IDhk36BglTGb_Vw3Djx8pq00,8987
3
- bool_hybrid_array/_cppiostream.py,sha256=1i973ag_8XgivQkeIbqzHivflcQkVFtX77R_UwChSAg,9205
4
- bool_hybrid_array/compile_core.cp314-win_amd64.pyd,sha256=3vFAZ6vSMqoP_fWRkXd_KVPVUw4oo2Nv7xi0sBQu2fQ,516096
5
- bool_hybrid_array/core.c,sha256=iG3px_6ZJzYhlSnZUpZxJP4CRYfMGk39dlQveU26ZFc,79
6
- bool_hybrid_array/core.py,sha256=6BLUmXX-5fBnjamspmfaZAuVetBvDH-eAn6_F5QPaxM,42607
7
- bool_hybrid_array/秘密.md,sha256=Ii2NvXmv-Ktu04zJsGLcQZvlzT4gOatByE4B2wTK1Ks,48
8
- bool_hybrid_array/int_array/__init__.py,sha256=CCKDUL5GBAXOprc9FhtLNL_rP4IDhvGVWJ-Dporp6YI,112
9
- bool_hybrid_array/int_array/core.py,sha256=DZ6mC-qLQahV25b_1RHGPK959XYtILYP_Pbwwg7nFLA,7936
10
- bool_hybrid_array-9.11.14.dist-info/licenses/LICENSE,sha256=Sg4rnGXkBDYkwJCWyxdWp5H60rhVAxpNvFh_l3JWZdY,1070
11
- bool_hybrid_array-9.11.14.dist-info/METADATA,sha256=mZs2z7RnExO3ywqfEdGXdfb1SRgWXsOx3oTewbn_moI,26618
12
- bool_hybrid_array-9.11.14.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
13
- bool_hybrid_array-9.11.14.dist-info/top_level.txt,sha256=vk-TD77wuVQsN1rJ6uVWZX4sC_wya_WplRDwQKJoBZM,18
14
- bool_hybrid_array-9.11.14.dist-info/RECORD,,