You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

foundation.py 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. class AlgoDatValue:
  2. """A class representing a value with statistics."""
  3. memory = 0
  4. read = 0
  5. write = 0
  6. compare = 0
  7. add_operation = 0
  8. sub_operation = 0
  9. mul_operation = 0
  10. div_operation = 0
  11. bit_operation = 0
  12. @staticmethod
  13. def summary():
  14. print(f"Memory: {AlgoDatValue.memory}")
  15. print(f"Read: {AlgoDatValue.read}")
  16. print(f"Write: {AlgoDatValue.write}")
  17. print(f"Compare: {AlgoDatValue.compare}")
  18. print(f"Add: {AlgoDatValue.add_operation}")
  19. print(f"Sub: {AlgoDatValue.sub_operation}")
  20. print(f"Mul: {AlgoDatValue.mul_operation}")
  21. print(f"Div: {AlgoDatValue.div_operation}")
  22. print(f"Bit: {AlgoDatValue.bit_operation}")
  23. @staticmethod
  24. def reset():
  25. AlgoDatValue.memory = 0
  26. AlgoDatValue.read = 0
  27. AlgoDatValue.write = 0
  28. AlgoDatValue.compare = 0
  29. AlgoDatValue.add_operation = 0
  30. AlgoDatValue.sub_operation = 0
  31. AlgoDatValue.mul_operation = 0
  32. AlgoDatValue.div_operation = 0
  33. AlgoDatValue.bit_operation = 0
  34. def __init__(self, value):
  35. AlgoDatValue.memory += 1
  36. AlgoDatValue.write += 1
  37. self.value = value
  38. def __str__(self):
  39. AlgoDatValue.read += 1
  40. return f"{self.value}"
  41. def __repr__(self):
  42. AlgoDatValue.read += 1
  43. return f"{self.value}"
  44. def __eq__(self, other):
  45. AlgoDatValue.compare += 1
  46. AlgoDatValue.read += 2
  47. if not isinstance(other, AlgoDatValue):
  48. return self.value == other
  49. return self.value == other.value
  50. def __lt__(self, other):
  51. AlgoDatValue.compare += 1
  52. AlgoDatValue.read += 2
  53. if not isinstance(other, AlgoDatValue):
  54. return self.value < other
  55. return self.value < other.value
  56. def __le__(self, other):
  57. AlgoDatValue.compare += 1
  58. AlgoDatValue.read += 2
  59. if not isinstance(other, AlgoDatValue):
  60. return self.value <= other
  61. return self.value <= other.value
  62. def __gt__(self, other):
  63. AlgoDatValue.compare += 1
  64. AlgoDatValue.read += 2
  65. if not isinstance(other, AlgoDatValue):
  66. return self.value > other
  67. return self.value > other.value
  68. def __ge__(self, other):
  69. AlgoDatValue.compare += 1
  70. AlgoDatValue.read += 2
  71. if not isinstance(other, AlgoDatValue):
  72. return self.value >= other
  73. return self.value >= other.value
  74. def __add__(self, other):
  75. AlgoDatValue.add_operation += 1
  76. AlgoDatValue.read += 2
  77. return self.value + other.value
  78. def __sub__(self, other):
  79. AlgoDatValue.sub_operation += 1
  80. AlgoDatValue.read += 2
  81. return self.value - other.value
  82. def __mul__(self, other):
  83. AlgoDatValue.mul_operation += 1
  84. AlgoDatValue.read += 2
  85. return self.value * other.value
  86. def __truediv__(self, other):
  87. AlgoDatValue.div_operation += 1
  88. AlgoDatValue.read += 2
  89. return self.value / other.value
  90. def __floordiv__(self, other):
  91. AlgoDatValue.div_operation += 1
  92. AlgoDatValue.read += 2
  93. return self.value // other.value
  94. def __mod__(self, other):
  95. AlgoDatValue.div_operation += 1
  96. AlgoDatValue.read += 2
  97. return self.value % other.value
  98. def __iadd__(self, other):
  99. AlgoDatValue.add_operation += 1
  100. AlgoDatValue.read += 1
  101. AlgoDatValue.write += 1
  102. self.value += other.value
  103. return self
  104. def __isub__(self, other):
  105. AlgoDatValue.sub_operation += 1
  106. AlgoDatValue.read += 1
  107. AlgoDatValue.write += 1
  108. self.value -= other.value
  109. return self
  110. def __imul__(self, other):
  111. AlgoDatValue.mul_operation += 1
  112. AlgoDatValue.read += 1
  113. AlgoDatValue.write += 1
  114. self.value *= other.value
  115. return self
  116. def __itruediv__(self, other):
  117. AlgoDatValue.div_operation += 1
  118. AlgoDatValue.read += 1
  119. AlgoDatValue.write += 1
  120. self.value /= other.value
  121. return self
  122. def __ifloordiv__(self, other):
  123. AlgoDatValue.div_operation += 1
  124. AlgoDatValue.read += 1
  125. AlgoDatValue.write += 1
  126. self.value //= other.value
  127. return self
  128. def __imod__(self, other):
  129. AlgoDatValue.div_operation += 1
  130. AlgoDatValue.read += 1
  131. AlgoDatValue.write += 1
  132. self.value %= other.value
  133. return self
  134. def __neg__(self):
  135. return -self.value
  136. def __pos__(self):
  137. return +self.value
  138. def __abs__(self):
  139. return abs(self.value)
  140. def __invert__(self):
  141. return ~self.value
  142. def __lshift__(self, other):
  143. AlgoDatValue.bit_operation += 1
  144. AlgoDatValue.read += 2
  145. return self.value << other.value
  146. def __rshift__(self, other):
  147. AlgoDatValue.bit_operation += 1
  148. AlgoDatValue.read += 2
  149. return self.value >> other.value
  150. def __and__(self, other):
  151. AlgoDatValue.bit_operation += 1
  152. AlgoDatValue.read += 2
  153. return self.value & other.value
  154. def __xor__(self, other):
  155. AlgoDatValue.bit_operation += 1
  156. AlgoDatValue.read += 2
  157. return self.value ^ other.value
  158. def __or__(self, other):
  159. AlgoDatValue.bit_operation += 1
  160. AlgoDatValue.read += 2
  161. return self.value | other.value
  162. def __ilshift__(self, other):
  163. AlgoDatValue.bit_operation += 1
  164. AlgoDatValue.read += 2
  165. AlgoDatValue.write += 1
  166. self.value <<= other.value
  167. return self
  168. def __irshift__(self, other):
  169. AlgoDatValue.bit_operation += 1
  170. AlgoDatValue.read += 2
  171. AlgoDatValue.write += 1
  172. self.value >>= other.value
  173. return self
  174. def __iand__(self, other):
  175. AlgoDatValue.bit_operation += 1
  176. AlgoDatValue.read += 2
  177. AlgoDatValue.write += 1
  178. self.value &= other.value
  179. return self
  180. def __ixor__(self, other):
  181. AlgoDatValue.bit_operation += 1
  182. AlgoDatValue.read += 2
  183. AlgoDatValue.write += 1
  184. self.value ^= other.value
  185. return self
  186. def __ior__(self, other):
  187. AlgoDatValue.bit_operation += 1
  188. AlgoDatValue.read += 2
  189. AlgoDatValue.write += 1
  190. self.value |= other.value
  191. return self
  192. def __int__(self):
  193. return int(self.value)
  194. def __float__(self):
  195. return float(self.value)
  196. def __complex__(self):
  197. return complex(self.value)
  198. def __round__(self, n=0):
  199. return round(self.value, n)
  200. def __setattr__(self, name, value):
  201. if name == "value":
  202. AlgoDatValue.write += 1
  203. self.__dict__[name] = value
  204. class AlgoDatArray:
  205. """A class representing an array of AlgoDatValue objects."""
  206. def __init__(self, size):
  207. self.size = size
  208. self.array = []
  209. for i in range(size):
  210. self.array.append(AlgoDatValue(None))
  211. def set(self, index, value):
  212. assert isinstance(value, AlgoDatValue)
  213. assert isinstance(index, int)
  214. self.array[index] = value
  215. def get(self, index):
  216. assert isinstance(index, int)
  217. return self.array[index]
  218. def __str__(self):
  219. return str(self.array)
  220. def __len__(self):
  221. return len(self.array)
  222. def __iter__(self):
  223. return iter(self.array)
  224. def __getitem__(self, index):
  225. assert isinstance(index, int)
  226. return self.array[index]
  227. def __setitem__(self, index, value):
  228. assert isinstance(index, int)
  229. assert isinstance(value, AlgoDatValue)
  230. self.array[index] = value
  231. class MinusInf:
  232. """A class representing negative infinity."""
  233. def __gt__(self, other):
  234. return False
  235. def __ge__(self):
  236. return False
  237. def __lt__(self, other):
  238. return True
  239. def __le__(self, other):
  240. return True
  241. def __eq__(self, other):
  242. return False
  243. class Inf:
  244. """A class representing positive infinity."""
  245. def __gt__(self, other):
  246. return True
  247. def __ge__(self):
  248. return True
  249. def __lt__(self, other):
  250. return False
  251. def __le__(self, other):
  252. return False
  253. def __eq__(self, other):
  254. return False
  255. def read_int_sequence(filename: str) -> AlgoDatArray:
  256. """Reads a sequence of integers from a file and returns an AlgoDatArray object."""
  257. with open(filename, "r") as file:
  258. l = list(map(int, file.read().split()))
  259. a = AlgoDatArray(len(l))
  260. AlgoDatValue.memory -= len(l)
  261. for i in range(len(l)):
  262. a.set(i, AlgoDatValue(l[i]))
  263. return a