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

SafeValueInt.cs 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using System;
  2. using System.Threading;
  3. namespace Preparation.Utility
  4. {
  5. //其对应属性不应当有set访问器,避免不安全的=赋值
  6. public class AtomicInt
  7. {
  8. private int v;
  9. public AtomicInt(int x)
  10. {
  11. v = x;
  12. }
  13. public override string ToString() => Interlocked.CompareExchange(ref v, -1, -1).ToString();
  14. public int Get() => Interlocked.CompareExchange(ref v, -1, -1);
  15. public static implicit operator int(AtomicInt aint) => Interlocked.CompareExchange(ref aint.v, -1, -1);
  16. /// <returns>返回操作前的值</returns>
  17. public int SetReturnOri(int value) => Interlocked.Exchange(ref v, value);
  18. public int Add(int x) => Interlocked.Add(ref v, x);
  19. public int Sub(int x) => Interlocked.Add(ref v, -x);
  20. public int Inc() => Interlocked.Increment(ref v);
  21. public int Dec() => Interlocked.Decrement(ref v);
  22. /// <returns>返回操作前的值</returns>
  23. public int CompareExReturnOri(int newV, int compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
  24. }
  25. /*
  26. /// <summary>
  27. /// 记录(不是根据时间)完成多少进度的进度条(long)。
  28. /// </summary>
  29. public struct IntProgressByAdding
  30. {
  31. private int completedProgress = -1;
  32. private int requiredProgress;
  33. public IntProgressByAdding(int completedProgress, int requiredProgress)
  34. {
  35. this.completedProgress = completedProgress;
  36. this.requiredProgress = requiredProgress;
  37. }
  38. public IntProgressByAdding(int requiredProgress)
  39. {
  40. this.requiredProgress = requiredProgress;
  41. }
  42. public IntProgressByAdding()
  43. {
  44. this.requiredProgress=int.MaxValue;
  45. }
  46. }
  47. */
  48. /// <summary>
  49. /// 一个保证在[0,maxValue]的可变int,支持可变的maxValue(请确保大于0)
  50. /// </summary>
  51. public class IntWithVariableRange
  52. {
  53. private int v;
  54. private int maxV;
  55. private readonly object vLock = new();
  56. public IntWithVariableRange(int value, int maxValue)
  57. {
  58. if (maxValue < 0)
  59. {
  60. Debugger.Output("Warning:Try to set IntWithVariableRange.maxValue to " + maxValue.ToString() + ".");
  61. maxValue = 0;
  62. }
  63. v = value < maxValue ? value : maxValue;
  64. this.maxV = maxValue;
  65. }
  66. /// <summary>
  67. /// 默认使Value=maxValue
  68. /// </summary>
  69. public IntWithVariableRange(int maxValue)
  70. {
  71. if (maxValue < 0)
  72. {
  73. Debugger.Output("Warning:Try to set IntWithVariableRange.maxValue to " + maxValue.ToString() + ".");
  74. maxValue = 0;
  75. }
  76. v = this.maxV = maxValue;
  77. }
  78. public IntWithVariableRange()
  79. {
  80. v = this.maxV = int.MaxValue;
  81. }
  82. public override string ToString()
  83. {
  84. lock (vLock)
  85. {
  86. return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString();
  87. }
  88. }
  89. public int GetValue() { lock (vLock) return v; }
  90. public static implicit operator int(IntWithVariableRange aint) => aint.GetValue();
  91. public int GetMaxV() { lock (vLock) return maxV; }
  92. /// <summary>
  93. /// 若maxValue<=0则maxValue设为0并返回False
  94. /// </summary>
  95. public bool SetMaxV(int maxValue)
  96. {
  97. if (maxValue < 0) maxValue = 0;
  98. lock (vLock)
  99. {
  100. maxV = maxValue;
  101. if (v > maxValue) v = maxValue;
  102. }
  103. return maxValue > 0;
  104. }
  105. /// <summary>
  106. /// 应当保证该maxValue>=0
  107. /// </summary>
  108. public void SetPositiveMaxV(int maxValue)
  109. {
  110. lock (vLock)
  111. {
  112. maxV = maxValue;
  113. if (v > maxValue) v = maxValue;
  114. }
  115. }
  116. /// <summary>
  117. /// 应当保证该value>=0
  118. /// </summary>
  119. public int SetPositiveV(int value)
  120. {
  121. lock (vLock)
  122. {
  123. return v = (value > maxV) ? maxV : value;
  124. }
  125. }
  126. public int SetV(int value)
  127. {
  128. if (value < 0) value = 0;
  129. lock (vLock)
  130. {
  131. return v = (value > maxV) ? maxV : value;
  132. }
  133. }
  134. /// <returns>返回实际改变量</returns>
  135. public int AddV(int addV)
  136. {
  137. lock (vLock)
  138. {
  139. int previousV = v;
  140. v += addV;
  141. if (v < 0) v = 0;
  142. if (v > maxV) v = maxV;
  143. return v - previousV;
  144. }
  145. }
  146. /// <summary>
  147. /// 应当保证增加值大于0
  148. /// </summary>
  149. /// <returns>返回实际改变量</returns>
  150. public int AddPositiveV(int addPositiveV)
  151. {
  152. lock (vLock)
  153. {
  154. addPositiveV = Math.Min(addPositiveV, maxV - v);
  155. v += addPositiveV;
  156. }
  157. return addPositiveV;
  158. }
  159. /// <summary>
  160. /// 应当保证该减少值大于0,返回实际改变量
  161. /// </summary>
  162. public int SubPositiveV(int subPositiveV)
  163. {
  164. lock (vLock)
  165. {
  166. subPositiveV = Math.Min(subPositiveV, v);
  167. v -= subPositiveV;
  168. }
  169. return subPositiveV;
  170. }
  171. /// <summary>
  172. /// 试图加到满,如果无法加到maxValue则不加并返回-1
  173. /// </summary>
  174. public int TryAddAll(int addV)
  175. {
  176. lock (vLock)
  177. {
  178. if (maxV - v <= addV)
  179. {
  180. addV = maxV - v;
  181. v = maxV;
  182. return addV;
  183. }
  184. return 0;
  185. }
  186. }
  187. }
  188. /// <summary>
  189. /// 一个保证在[0,maxNum],每CDms自动+1的int,支持可变的CD、maxNum(请确保大于0)
  190. /// </summary>
  191. public class IntNumUpdateByCD
  192. {
  193. private int num;
  194. private int maxNum;
  195. private int cd;
  196. private long updateTime = 0;
  197. private readonly object numLock = new();
  198. public IntNumUpdateByCD(int num, int maxNum, int cd)
  199. {
  200. if (num < 0) Debugger.Output("Bug:IntNumUpdateByCD.num (" + num.ToString() + ") is less than 0.");
  201. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateByCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  202. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 0.");
  203. this.num = (num < maxNum) ? num : maxNum;
  204. this.maxNum = maxNum;
  205. this.cd = cd;
  206. this.updateTime = Environment.TickCount64;
  207. }
  208. /// <summary>
  209. /// 默认使num=maxNum
  210. /// </summary>
  211. public IntNumUpdateByCD(int maxNum, int cd)
  212. {
  213. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateByCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  214. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 0.");
  215. this.num = this.maxNum = maxNum;
  216. this.cd = cd;
  217. }
  218. public IntNumUpdateByCD()
  219. {
  220. this.num = this.maxNum = 0;
  221. this.cd = int.MaxValue;
  222. }
  223. public int GetMaxNum() { lock (numLock) return maxNum; }
  224. public int GetCD() { lock (numLock) return cd; }
  225. public int GetNum(long time)
  226. {
  227. lock (numLock)
  228. {
  229. if (num < maxNum && time - updateTime >= cd)
  230. {
  231. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  232. updateTime += add * cd;
  233. return (num += add);
  234. }
  235. return num;
  236. }
  237. }
  238. public static implicit operator int(IntNumUpdateByCD aint) => aint.GetNum(Environment.TickCount64);
  239. /// <summary>
  240. /// 应当保证该subV>=0
  241. /// </summary>
  242. public int TrySub(int subV)
  243. {
  244. if (subV < 0) Debugger.Output("Bug:IntNumUpdateByCD Try to sub " + subV.ToString() + ", which is less than 0.");
  245. long time = Environment.TickCount64;
  246. lock (numLock)
  247. {
  248. if (num < maxNum && time - updateTime >= cd)
  249. {
  250. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  251. updateTime += add * cd;
  252. num += add;
  253. }
  254. if (num == maxNum) updateTime = time;
  255. num -= subV = Math.Min(subV, num);
  256. }
  257. return subV;
  258. }
  259. /// <summary>
  260. /// 应当保证该addV>=0
  261. /// </summary>
  262. public void TryAdd(int addV)
  263. {
  264. if (addV < 0) Debugger.Output("Bug:IntNumUpdateByCD Try to add " + addV.ToString() + ", which is less than 0.");
  265. lock (numLock)
  266. {
  267. num += Math.Min(addV, maxNum - num);
  268. }
  269. }
  270. /// <summary>
  271. /// 若maxNum<=0则maxNum及Num设为0并返回False
  272. /// </summary>
  273. public bool SetMaxNumAndNum(int maxNum)
  274. {
  275. if (maxNum < 0) maxNum = 0;
  276. lock (numLock)
  277. {
  278. this.num = this.maxNum = maxNum;
  279. }
  280. return maxNum > 0;
  281. }
  282. /// <summary>
  283. /// 应当保证该maxnum>=0
  284. /// </summary>
  285. public void SetPositiveMaxNumAndNum(int maxNum)
  286. {
  287. lock (numLock)
  288. {
  289. this.num = this.maxNum = maxNum;
  290. }
  291. }
  292. /// <summary>
  293. /// 应当保证该maxnum>=0
  294. /// </summary>
  295. public void SetPositiveMaxNum(int maxNum)
  296. {
  297. lock (numLock)
  298. {
  299. if ((this.maxNum = maxNum) < num)
  300. num = maxNum;
  301. }
  302. }
  303. /// <summary>
  304. /// 若maxNum<=0则maxNum及Num设为0并返回False
  305. /// </summary>
  306. public bool SetMaxNum(int maxNum)
  307. {
  308. if (maxNum < 0) maxNum = 0;
  309. lock (numLock)
  310. {
  311. if ((this.maxNum = maxNum) < num)
  312. num = maxNum;
  313. }
  314. return maxNum > 0;
  315. }
  316. /// <summary>
  317. /// 若num<0则num设为0并返回False
  318. /// </summary>
  319. public bool SetNum(int num)
  320. {
  321. lock (numLock)
  322. {
  323. if (num < 0)
  324. {
  325. this.num = 0;
  326. updateTime = Environment.TickCount64;
  327. return false;
  328. }
  329. if (num < maxNum)
  330. {
  331. if (this.num == maxNum) updateTime = Environment.TickCount64;
  332. this.num = num;
  333. }
  334. else this.num = maxNum;
  335. return true;
  336. }
  337. }
  338. /// <summary>
  339. /// 应当保证该num>=0
  340. /// </summary>
  341. public void SetPositiveNum(int num)
  342. {
  343. lock (numLock)
  344. {
  345. if (num < maxNum)
  346. {
  347. if (this.num == maxNum) updateTime = Environment.TickCount64;
  348. this.num = num;
  349. }
  350. else this.num = maxNum;
  351. }
  352. }
  353. public void SetCD(int cd)
  354. {
  355. lock (numLock)
  356. {
  357. if (cd <= 0) Debugger.Output("Bug:SetReturnOri IntNumUpdateByCD.cd to " + cd.ToString() + ".");
  358. this.cd = cd;
  359. }
  360. }
  361. }
  362. }