linq.hpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. /**
  2. *
  3. * @file linq.hpp
  4. * @author zxw
  5. * Copyright 2024, yjrobotics. All rights reserved.
  6. *
  7. * robotics
  8. *
  9. */
  10. #pragma once
  11. //stl
  12. #include <iostream>
  13. #include <functional>
  14. #include <algorithm>
  15. #include <vector>
  16. #include <mutex>
  17. #include <map>
  18. namespace robotics {
  19. namespace v3 {
  20. /**
  21. * @brief 状态
  22. */
  23. enum class linq_change_type {
  24. Insert,
  25. Delete,
  26. Update
  27. };
  28. /**
  29. * @brief linq
  30. * @tparam _Type
  31. */
  32. template<typename _Type>
  33. class linq {
  34. public:
  35. /**
  36. * @brief 构造
  37. * @param value
  38. */
  39. linq(std::vector<_Type>& value) :
  40. source_(value) {
  41. source_ok_.assign(source_.size(), true);
  42. }
  43. /**
  44. * @brief 析构
  45. */
  46. ~linq() {
  47. submit();
  48. }
  49. /**
  50. * @brief 重置结果
  51. * @return
  52. */
  53. linq& reset() {
  54. std::lock_guard<std::mutex> locker(mutex_);
  55. source_ok_.assign(source_.size(), true);
  56. return *this;
  57. }
  58. /**
  59. * @brief 重置回调
  60. * @return
  61. */
  62. linq& reset_changed() {
  63. std::lock_guard<std::mutex> locker(mutex_);
  64. changed_list_.clear();
  65. return *this;
  66. }
  67. /**
  68. * @brief 提交
  69. * @return
  70. */
  71. linq& submit() {
  72. std::lock_guard<std::mutex> locker(mutex_);
  73. for (size_t i = 0; i < changed_list_.size(); ++i) {
  74. for (auto const& it : changed_event) {
  75. it(std::get<0>(changed_list_[i]), std::get<1>(changed_list_[i]), std::get<2>(changed_list_[i]), changed_list_.size() == (i + 1));
  76. }
  77. }
  78. changed_list_.clear();
  79. return *this;
  80. }
  81. /**
  82. * @brief 绑定改变事件
  83. * @tparam _Fn
  84. * @tparam _Obj
  85. * @param fn
  86. * @param obj
  87. * @return
  88. */
  89. template<typename _Fn, typename _Obj>
  90. linq& changed_bind(_Fn&& fn, _Obj&& obj) {
  91. std::lock_guard<std::mutex> locker(mutex_);
  92. changed_event.push_back(std::bind(std::forward<_Fn>(fn), std::forward<_Obj>(obj), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
  93. return *this;
  94. }
  95. /**
  96. * @brief 绑定改变事件
  97. * @tparam _Fn
  98. * @param fn
  99. * @return
  100. */
  101. template<typename _Fn>
  102. linq& changed_bind(_Fn&& fn) {
  103. std::lock_guard<std::mutex> locker(mutex_);
  104. changed_event.push_back(std::forward<_Fn>(fn));
  105. return *this;
  106. }
  107. /**
  108. * @brief 查询
  109. * @tparam _Fn
  110. * @param fn
  111. * @return
  112. */
  113. template<typename _Fn>
  114. linq& where(_Fn&& fn) {
  115. std::lock_guard<std::mutex> locker(mutex_);
  116. typename std::vector<_Type>::const_iterator it;
  117. for (size_t i = 0; i < source_ok_.size(); ++i) {
  118. if (!source_ok_[i]) {
  119. continue;
  120. }
  121. it = source_.begin() + i;
  122. source_ok_[i] = fn(it);
  123. }
  124. return *this;
  125. }
  126. /**
  127. * @brief 修改
  128. * @tparam _Fn
  129. * @param fn
  130. * @return
  131. */
  132. template<typename _Fn>
  133. linq& update(_Fn&& fn) {
  134. std::lock_guard<std::mutex> locker(mutex_);
  135. typename std::vector<_Type>::iterator it;
  136. for (size_t i = 0; i < source_ok_.size(); ++i) {
  137. if (!source_ok_[i]) {
  138. continue;
  139. }
  140. it = source_.begin() + i;
  141. if (changed_event.empty()) {
  142. fn(it);
  143. }
  144. else {
  145. _Type tmp = *it;
  146. if (fn(it)) {
  147. changed_list_.push_back(std::make_tuple(*it, tmp, linq_change_type::Update));
  148. }
  149. }
  150. }
  151. return *this;
  152. }
  153. /**
  154. * @brief 删除
  155. * @tparam _Fn
  156. * @param fn
  157. * @return
  158. */
  159. template<typename _Fn>
  160. linq& remove(_Fn&& fn) {
  161. std::lock_guard<std::mutex> locker(mutex_);
  162. typename std::vector<_Type>::const_iterator it;
  163. for (std::int64_t i = 0; i < source_ok_.size(); ++i) {
  164. if (!source_ok_[i]) {
  165. continue;
  166. }
  167. it = source_.begin() + i;
  168. if (fn(it)) {
  169. if (!changed_event.empty()) {
  170. changed_list_.push_back(std::make_tuple(*it, *it, linq_change_type::Delete));
  171. }
  172. if (i + 1 < source_ok_.size() && !source_ok_.empty()) {
  173. source_ok_[i] = std::move(source_ok_.back());
  174. source_[i] = std::move(source_.back());
  175. source_ok_.pop_back();
  176. source_.pop_back();
  177. }
  178. else {
  179. source_ok_.pop_back();
  180. source_.pop_back();
  181. }
  182. --i;
  183. }
  184. }
  185. return *this;
  186. }
  187. /**
  188. * @brief 删除
  189. * @return
  190. */
  191. linq& remove() {
  192. std::lock_guard<std::mutex> locker(mutex_);
  193. typename std::vector<_Type>::const_iterator it;
  194. for (std::int64_t i = 0; i < source_ok_.size(); ++i) {
  195. if (!source_ok_[i]) {
  196. continue;
  197. }
  198. it = source_.begin() + i;
  199. if (!changed_event.empty()) {
  200. changed_list_.push_back(std::make_tuple(*it, *it, linq_change_type::Delete));
  201. }
  202. if (i + 1 < source_ok_.size() && !source_ok_.empty()) {
  203. source_ok_[i] = std::move(source_ok_.back());
  204. source_[i] = std::move(source_.back());
  205. source_ok_.pop_back();
  206. source_.pop_back();
  207. }
  208. else {
  209. source_ok_.pop_back();
  210. source_.pop_back();
  211. }
  212. --i;
  213. }
  214. return *this;
  215. }
  216. /**
  217. * @brief 添加
  218. * @param values
  219. * @return
  220. */
  221. linq& append(std::vector<_Type> const& values) {
  222. for (auto const& it : values) {
  223. append(it);
  224. }
  225. return *this;
  226. }
  227. /**
  228. * @brief 添加
  229. * @param value
  230. * @return
  231. */
  232. linq& append(_Type const& value) {
  233. std::lock_guard<std::mutex> locker(mutex_);
  234. source_.push_back(value);
  235. source_ok_.push_back(true);
  236. if (!changed_event.empty()) {
  237. changed_list_.push_back(std::make_tuple(value, value, linq_change_type::Insert));
  238. }
  239. return *this;
  240. }
  241. /**
  242. * @brief 排序
  243. * @tparam _Fn
  244. * @param fn
  245. * @return
  246. */
  247. template<typename _Fn>
  248. linq& order_by(_Fn&& fn) {
  249. std::lock_guard<std::mutex> locker(mutex_);
  250. std::vector<std::pair<_Type, bool>> alllist;
  251. for (std::int64_t i = 0; i < source_.size(); ++i) {
  252. alllist.push_back(std::pair(source_[i], source_ok_[i]));
  253. }
  254. std::sort(alllist.begin(), alllist.end(), [&fn](std::pair<_Type, bool> const& val1, std::pair<_Type, bool> const& val2) {
  255. return fn(&val1.first, &val2.first); });
  256. source_.clear();
  257. source_ok_.clear();
  258. for (std::int64_t i = 0; i < alllist.size(); ++i) {
  259. source_.push_back(alllist[i].first);
  260. source_ok_.push_back(alllist[i].second);
  261. }
  262. return *this;
  263. }
  264. /**
  265. * @brief 分页
  266. * @param page
  267. * @param count
  268. * @return
  269. */
  270. linq& limit(unsigned int page, unsigned int count) {
  271. std::lock_guard<std::mutex> locker(mutex_);
  272. size_t begin_index = (page - 1) * count;
  273. size_t current_index = 0;
  274. size_t current_count = 0;
  275. size_t i = 0;
  276. for (size_t i = 0; i < source_ok_.size(); ++i) {
  277. if (!source_ok_[i]) {
  278. continue;
  279. }
  280. if (begin_index <= current_index && count > current_count) {
  281. ++current_count;
  282. }
  283. else if (count <= current_count) {
  284. std::fill(source_ok_.begin() + i, source_ok_.end(), false);
  285. break;
  286. }
  287. else {
  288. source_ok_[i] = false;
  289. }
  290. current_index++;
  291. }
  292. return *this;
  293. }
  294. /**
  295. * @brief 数量
  296. * @param result
  297. * @return
  298. */
  299. linq& count(size_t& result) {
  300. result = count();
  301. return *this;
  302. }
  303. /**
  304. * @brief 数量
  305. * @param result
  306. * @return
  307. */
  308. linq& count(int& result) {
  309. result = count();
  310. return *this;
  311. }
  312. /**
  313. * @brief 数量
  314. * @param result
  315. * @return
  316. */
  317. linq& size(size_t& result) {
  318. result = count();
  319. return *this;
  320. }
  321. /**
  322. * @brief 数量
  323. * @param result
  324. * @return
  325. */
  326. linq& size(int& result) {
  327. result = count();
  328. return *this;
  329. }
  330. /**
  331. * @brief 平均值
  332. * @tparam _Ret
  333. * @param result
  334. * @return
  335. */
  336. template<typename _Ret>
  337. linq& avg(_Ret& result) {
  338. result = avg<_Ret>();
  339. return *this;
  340. }
  341. /**
  342. * @brief 平均值
  343. * @tparam _Ret
  344. * @tparam _Fn
  345. * @param result
  346. * @param fn
  347. * @return
  348. */
  349. template<typename _Ret, typename _Fn>
  350. linq& avg(_Ret& result, _Fn&& fn) {
  351. result = avg<_Ret>(std::forward<_Fn>(fn));
  352. return *this;
  353. }
  354. /**
  355. * @brief 最大值
  356. * @tparam _Ret
  357. * @param result
  358. * @return
  359. */
  360. template<typename _Ret>
  361. linq& max(_Ret& result) {
  362. result = max<_Ret>();
  363. return *this;
  364. }
  365. /**
  366. * @brief 最大值
  367. * @tparam _Ret
  368. * @tparam _Fn
  369. * @param result
  370. * @param fn
  371. * @return
  372. */
  373. template<typename _Ret, typename _Fn>
  374. linq& max(_Ret& result, _Fn&& fn) {
  375. result = max<_Ret>(std::forward<_Fn>(fn));
  376. return *this;
  377. }
  378. /**
  379. * @brief 最小值
  380. * @tparam _Ret
  381. * @param result
  382. * @return
  383. */
  384. template<typename _Ret>
  385. linq& min(_Ret& result) {
  386. result = min<_Ret>();
  387. return *this;
  388. }
  389. /**
  390. * @brief 最小值
  391. * @tparam _Ret
  392. * @tparam _Fn
  393. * @param result
  394. * @param fn
  395. * @return
  396. */
  397. template<typename _Ret, typename _Fn>
  398. linq& min(_Ret& result, _Fn&& fn) {
  399. result = min<_Ret>(std::forward<_Fn>(fn));
  400. return *this;
  401. }
  402. /**
  403. * @brief 求和
  404. * @tparam _Ret
  405. * @param result
  406. * @return
  407. */
  408. template<typename _Ret>
  409. linq& sum(_Ret& result) {
  410. result = sum<_Ret>();
  411. return *this;
  412. }
  413. /**
  414. * @brief 求和
  415. * @tparam _Ret
  416. * @tparam _Fn
  417. * @param result
  418. * @param fn
  419. * @return
  420. */
  421. template<typename _Ret, typename _Fn>
  422. linq& sum(_Ret& result, _Fn&& fn) {
  423. result = sum<_Ret>(std::forward<_Fn>(fn));
  424. return *this;
  425. }
  426. /**
  427. * @brief 是否包含
  428. * @param result
  429. * @param value
  430. * @return
  431. */
  432. linq& contains(bool& result, _Type const& value) {
  433. result = contains(value);
  434. return *this;
  435. }
  436. /**
  437. * @brief 是否包含
  438. * @param result
  439. * @param fn
  440. * @return
  441. */
  442. linq& contains(bool& result, std::function<bool(typename std::vector<_Type>::const_iterator)> const& fn) {
  443. result = contains(fn);
  444. return *this;
  445. }
  446. /**
  447. * @brief 选择
  448. * @tparam _Ret
  449. * @tparam _Fn
  450. * @param result
  451. * @param fn
  452. * @return
  453. */
  454. template<typename _Ret, typename _Fn>
  455. linq& select(std::vector<_Ret>& result, _Fn&& fn) {
  456. result = select<_Ret>(std::forward<_Fn>(fn));
  457. return *this;
  458. }
  459. /**
  460. * @brief 分组
  461. * @tparam _Ret
  462. * @tparam _Fn
  463. * @param result
  464. * @param fn
  465. * @return
  466. */
  467. template<typename _Ret, typename _Fn>
  468. linq& group_by(_Ret& result, _Fn&& fn) {
  469. result = group_by<typename _Ret::key_type>(std::forward<_Fn>(fn));
  470. return *this;
  471. }
  472. /**
  473. * @brief 自定义操作
  474. * @tparam _Fn
  475. * @param fn
  476. * @return
  477. */
  478. template<typename _Fn>
  479. linq& function(_Fn&& fn) {
  480. fn();
  481. return *this;
  482. }
  483. /**
  484. * @brief 自增
  485. * @tparam _Param
  486. * @param value
  487. * @return
  488. */
  489. template<typename _Param>
  490. linq& inc(_Param& value) {
  491. ++value;
  492. return *this;
  493. }
  494. /**
  495. * @brief 数量
  496. * @return
  497. */
  498. size_t count() {
  499. std::lock_guard<std::mutex> locker(mutex_);
  500. size_t result = 0;
  501. for (size_t i = 0; i < source_ok_.size(); ++i) {
  502. if (!source_ok_[i]) {
  503. continue;
  504. }
  505. result++;
  506. }
  507. return result;
  508. }
  509. /**
  510. * @brief 数量
  511. * @return
  512. */
  513. size_t size() {
  514. return count();
  515. }
  516. /**
  517. * @brief 计算平均值
  518. * @tparam _Ret
  519. * @return
  520. */
  521. template<typename _Ret>
  522. _Ret avg() {
  523. std::lock_guard<std::mutex> locker(mutex_);
  524. _Ret all = _Ret();
  525. size_t count = 0;
  526. typename std::vector<_Type>::const_iterator it;
  527. for (size_t i = 0; i < source_ok_.size(); ++i) {
  528. if (!source_ok_[i]) {
  529. continue;
  530. }
  531. it = source_.begin() + i;
  532. all += *it;
  533. count++;
  534. }
  535. return all / _Ret(count);
  536. }
  537. /**
  538. * @brief 计算平均值
  539. * @tparam _Ret
  540. * @tparam _Fn
  541. * @param fn
  542. * @return
  543. */
  544. template<typename _Ret, typename _Fn>
  545. _Ret avg(_Fn&& fn) {
  546. std::lock_guard<std::mutex> locker(mutex_);
  547. _Ret all = _Ret();
  548. size_t count = 0;
  549. typename std::vector<_Type>::const_iterator it;
  550. for (size_t i = 0; i < source_ok_.size(); ++i) {
  551. if (!source_ok_[i]) {
  552. continue;
  553. }
  554. it = source_.begin() + i;
  555. all += fn(it);
  556. count++;
  557. }
  558. return all / _Ret(count);
  559. }
  560. /**
  561. * @brief 最大值
  562. * @tparam _Ret
  563. * @return
  564. */
  565. template<typename _Ret>
  566. _Ret max() {
  567. std::lock_guard<std::mutex> locker(mutex_);
  568. _Ret result = _Ret();
  569. bool ok = true;
  570. typename std::vector<_Type>::const_iterator it;
  571. for (size_t i = 0; i < source_ok_.size(); ++i) {
  572. if (!source_ok_[i]) {
  573. continue;
  574. }
  575. it = source_.begin() + i;
  576. if (ok) {
  577. ok = false;
  578. result = *it;
  579. }
  580. else {
  581. if (result < *it) {
  582. result = *it;
  583. }
  584. }
  585. }
  586. return result;
  587. }
  588. /**
  589. * @brief 最大值
  590. * @tparam _Ret
  591. * @tparam _Fn
  592. * @param fn
  593. * @return
  594. */
  595. template<typename _Ret, typename _Fn>
  596. _Ret max(_Fn&& fn) {
  597. std::lock_guard<std::mutex> locker(mutex_);
  598. _Ret result = _Ret();
  599. bool ok = true;
  600. typename std::vector<_Type>::const_iterator it;
  601. for (size_t i = 0; i < source_ok_.size(); ++i) {
  602. if (!source_ok_[i]) {
  603. continue;
  604. }
  605. it = source_.begin() + i;
  606. if (ok) {
  607. ok = false;
  608. result = fn(it);
  609. }
  610. else {
  611. _Ret tmp_value = fn(it);
  612. if (result < tmp_value) {
  613. result = tmp_value;
  614. }
  615. }
  616. }
  617. return result;
  618. }
  619. /**
  620. * @brief 最小值
  621. * @tparam _Ret
  622. * @return
  623. */
  624. template<typename _Ret>
  625. _Ret min() {
  626. std::lock_guard<std::mutex> locker(mutex_);
  627. _Ret result = _Ret();
  628. bool ok = true;
  629. typename std::vector<_Type>::const_iterator it;
  630. for (size_t i = 0; i < source_ok_.size(); ++i) {
  631. if (!source_ok_[i]) {
  632. continue;
  633. }
  634. it = source_.begin() + i;
  635. if (ok) {
  636. ok = false;
  637. result = *it;
  638. }
  639. else {
  640. if (result > *it) {
  641. result = *it;
  642. }
  643. }
  644. }
  645. return result;
  646. }
  647. /**
  648. * @brief 最小值
  649. * @tparam _Ret
  650. * @tparam _Fn
  651. * @param fn
  652. * @return
  653. */
  654. template<typename _Ret, typename _Fn>
  655. _Ret min(_Fn&& fn) {
  656. std::lock_guard<std::mutex> locker(mutex_);
  657. _Ret result = _Ret();
  658. bool ok = true;
  659. typename std::vector<_Type>::const_iterator it;
  660. for (size_t i = 0; i < source_ok_.size(); ++i) {
  661. if (!source_ok_[i]) {
  662. continue;
  663. }
  664. it = source_.begin() + i;
  665. if (ok) {
  666. ok = false;
  667. result = fn(it);
  668. }
  669. else {
  670. _Ret tmp_value = fn(it);
  671. if (result > tmp_value) {
  672. result = tmp_value;
  673. }
  674. }
  675. }
  676. return result;
  677. }
  678. /**
  679. * @brief 求和
  680. * @tparam _Ret
  681. * @return
  682. */
  683. template<typename _Ret>
  684. _Ret sum() {
  685. std::lock_guard<std::mutex> locker(mutex_);
  686. _Ret result = _Ret();
  687. typename std::vector<_Type>::const_iterator it;
  688. for (size_t i = 0; i < source_ok_.size(); ++i) {
  689. if (!source_ok_[i]) {
  690. continue;
  691. }
  692. it = source_.begin() + i;
  693. result += *it;
  694. }
  695. return result;
  696. }
  697. /**
  698. * @brief 求和
  699. * @tparam _Ret
  700. * @tparam _Fn
  701. * @param fn
  702. * @return
  703. */
  704. template<typename _Ret, typename _Fn>
  705. _Ret sum(_Fn&& fn) {
  706. std::lock_guard<std::mutex> locker(mutex_);
  707. _Ret result = _Ret();
  708. typename std::vector<_Type>::const_iterator it;
  709. for (size_t i = 0; i < source_ok_.size(); ++i) {
  710. if (!source_ok_[i]) {
  711. continue;
  712. }
  713. it = source_.begin() + i;
  714. result += fn(it);
  715. }
  716. return result;
  717. }
  718. /**
  719. * @brief 是否包含
  720. * @param value
  721. * @return
  722. */
  723. bool contains(_Type const& value) {
  724. std::lock_guard<std::mutex> locker(mutex_);
  725. typename std::vector<_Type>::const_iterator it;
  726. for (size_t i = 0; i < source_ok_.size(); ++i) {
  727. if (!source_ok_[i]) {
  728. continue;
  729. }
  730. it = source_.begin() + i;
  731. if (*it == value) {
  732. return true;
  733. }
  734. }
  735. return false;
  736. }
  737. /**
  738. * @brief 是否包含
  739. * @param fn
  740. * @return
  741. */
  742. bool contains(std::function<bool(typename std::vector<_Type>::const_iterator)>const& fn) {
  743. std::lock_guard<std::mutex> locker(mutex_);
  744. typename std::vector<_Type>::const_iterator it;
  745. for (size_t i = 0; i < source_ok_.size(); ++i) {
  746. if (!source_ok_[i]) {
  747. continue;
  748. }
  749. it = source_.begin() + i;
  750. if (fn(it)) {
  751. return true;
  752. }
  753. }
  754. return false;
  755. }
  756. /**
  757. * @brief 选择
  758. * @tparam _Ret
  759. * @tparam _Fn
  760. * @param fn
  761. * @return
  762. */
  763. template<typename _Ret, typename _Fn>
  764. std::vector<_Ret> select(_Fn&& fn) {
  765. std::lock_guard<std::mutex> locker(mutex_);
  766. std::vector<_Ret> result;
  767. typename std::vector<_Type>::const_iterator it;
  768. for (size_t i = 0; i < source_ok_.size(); ++i) {
  769. if (!source_ok_[i]) {
  770. continue;
  771. }
  772. it = source_.begin() + i;
  773. result.push_back(fn(it));
  774. }
  775. return result;
  776. }
  777. /**
  778. * @brief 分组
  779. * @tparam _Key
  780. * @tparam _Fn
  781. * @param fn
  782. * @return
  783. */
  784. template<typename _Key, typename _Fn>
  785. std::map<_Key, std::vector<_Type>> group_by(_Fn&& fn) {
  786. std::lock_guard<std::mutex> locker(mutex_);
  787. std::map<_Key, std::vector<_Type>> result;
  788. typename std::vector<_Type>::const_iterator it;
  789. for (size_t i = 0; i < source_ok_.size(); ++i) {
  790. if (!source_ok_[i]) {
  791. continue;
  792. }
  793. it = source_.begin() + i;
  794. result[fn(it)].push_back(*it);
  795. }
  796. return result;
  797. }
  798. /**
  799. * @brief 获取值
  800. * @return
  801. */
  802. std::vector<_Type> value() {
  803. std::lock_guard<std::mutex> locker(mutex_);
  804. std::vector<_Type> result;
  805. typename std::vector<_Type>::const_iterator it;
  806. for (size_t i = 0; i < source_ok_.size(); ++i) {
  807. if (!source_ok_[i]) {
  808. continue;
  809. }
  810. it = source_.begin() + i;
  811. result.push_back(*it);
  812. }
  813. return result;
  814. }
  815. /**
  816. * @brief 原始资源
  817. * @return
  818. */
  819. std::vector<_Type> source() {
  820. return source_;
  821. }
  822. /**
  823. * @brief 获取值
  824. */
  825. operator std::vector<_Type>() {
  826. return value();
  827. }
  828. /**
  829. * @brief 获取原始资源
  830. * @return
  831. */
  832. std::vector<std::shared_ptr<_Type>> source_ptr() {
  833. std::lock_guard<std::mutex> locker(mutex_);
  834. std::vector<std::shared_ptr<_Type>> result;
  835. for (auto& it : source_) {
  836. result.push_back(std::make_shared<_Type>(it));
  837. }
  838. return result;
  839. }
  840. /**
  841. * @brief 获取数据
  842. * @return
  843. */
  844. std::vector<std::shared_ptr<_Type>> value_ptr() {
  845. std::lock_guard<std::mutex> locker(mutex_);
  846. std::vector<std::shared_ptr<_Type>> result;
  847. typename std::vector<_Type>::const_iterator it;
  848. for (size_t i = 0; i < source_ok_.size(); ++i) {
  849. if (!source_ok_[i]) {
  850. continue;
  851. }
  852. it = source_.begin() + i;
  853. result.push_back(std::make_shared<_Type>(*it));
  854. }
  855. return result;
  856. }
  857. /**
  858. * @brief 获取数据
  859. */
  860. operator std::vector<std::shared_ptr<_Type>>() {
  861. return value_ptr();
  862. }
  863. /**
  864. * @brief 获取一条数据
  865. * @return
  866. */
  867. std::optional<_Type> first() {
  868. std::lock_guard<std::mutex> locker(mutex_);
  869. typename std::vector<_Type>::const_iterator it;
  870. for (size_t i = 0; i < source_ok_.size(); ++i) {
  871. if (!source_ok_[i]) {
  872. continue;
  873. }
  874. it = source_.begin() + i;
  875. return *it;
  876. }
  877. return std::nullopt;
  878. }
  879. /**
  880. * @brief 获取一条数据
  881. */
  882. operator std::optional<_Type>() {
  883. return first();
  884. }
  885. /**
  886. * @brief
  887. * @return
  888. */
  889. bool empty() {
  890. for (bool it : source_ok_) {
  891. if (it) {
  892. return false;
  893. }
  894. }
  895. return true;
  896. }
  897. /**
  898. * @brief 改变事件
  899. */
  900. std::vector<std::function<void(_Type const&, _Type const&, linq_change_type, bool)>> changed_event;
  901. protected:
  902. std::mutex mutex_;
  903. std::vector<_Type>& source_;
  904. std::vector<bool> source_ok_;
  905. std::vector<std::tuple<_Type, _Type, linq_change_type>> changed_list_;
  906. };
  907. /**
  908. * @brief linq_ptr
  909. * @tparam _Type
  910. */
  911. template<typename _Type>
  912. class linq_ptr {
  913. public:
  914. /**
  915. * @brief 构造
  916. * @param value
  917. */
  918. linq_ptr(std::vector<std::shared_ptr<_Type>>& value) :
  919. source_(value) {
  920. source_ok_.assign(source_.size(), true);
  921. }
  922. /**
  923. * @brief 析构
  924. */
  925. ~linq_ptr() {
  926. submit();
  927. }
  928. /**
  929. * @brief 重置结果
  930. * @return
  931. */
  932. linq_ptr& reset() {
  933. std::lock_guard<std::mutex> locker(mutex_);
  934. source_ok_.assign(source_.size(), true);
  935. return *this;
  936. }
  937. /**
  938. * @brief 重置回调
  939. * @return
  940. */
  941. linq_ptr& reset_changed() {
  942. std::lock_guard<std::mutex> locker(mutex_);
  943. changed_list_.clear();
  944. return *this;
  945. }
  946. /**
  947. * @brief 提交
  948. * @return
  949. */
  950. linq_ptr& submit() {
  951. std::lock_guard<std::mutex> locker(mutex_);
  952. for (size_t i = 0; i < changed_list_.size(); ++i) {
  953. for (auto const& it : changed_event) {
  954. it(std::get<0>(changed_list_[i]), std::get<1>(changed_list_[i]), std::get<2>(changed_list_[i]), changed_list_.size() == (i + 1));
  955. }
  956. }
  957. changed_list_.clear();
  958. return *this;
  959. }
  960. /**
  961. * @brief 绑定改变事件
  962. * @tparam _Fn
  963. * @tparam _Obj
  964. * @param fn
  965. * @param obj
  966. * @return
  967. */
  968. template<typename _Fn, typename _Obj>
  969. linq_ptr& changed_bind(_Fn&& fn, _Obj&& obj) {
  970. std::lock_guard<std::mutex> locker(mutex_);
  971. changed_event.push_back(std::bind(std::forward<_Fn>(fn), std::forward<_Obj>(obj), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
  972. return *this;
  973. }
  974. /**
  975. * @brief 绑定改变事件
  976. * @tparam _Fn
  977. * @param fn
  978. * @return
  979. */
  980. template<typename _Fn>
  981. linq_ptr& changed_bind(_Fn&& fn) {
  982. std::lock_guard<std::mutex> locker(mutex_);
  983. changed_event.push_back(std::forward<_Fn>(fn));
  984. return *this;
  985. }
  986. /**
  987. * @brief 查询
  988. * @param fn
  989. * @return
  990. */
  991. linq_ptr& where(std::function<bool(std::shared_ptr<const _Type> const&)> const& fn) {
  992. std::lock_guard<std::mutex> locker(mutex_);
  993. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  994. for (size_t i = 0; i < source_ok_.size(); ++i) {
  995. if (!source_ok_[i]) {
  996. continue;
  997. }
  998. it = source_.begin() + i;
  999. source_ok_[i] = fn(*it);
  1000. }
  1001. return *this;
  1002. }
  1003. /**
  1004. * @brief 修改
  1005. * @param fn
  1006. * @return
  1007. */
  1008. linq_ptr& update(std::function<bool(std::shared_ptr<_Type> const&)> const& fn) {
  1009. std::lock_guard<std::mutex> locker(mutex_);
  1010. typename std::vector<std::shared_ptr<_Type>>::iterator it;
  1011. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1012. if (!source_ok_[i]) {
  1013. continue;
  1014. }
  1015. it = source_.begin() + i;
  1016. if (changed_event.empty()) {
  1017. fn(*it);
  1018. }
  1019. else {
  1020. _Type tmp = **it;
  1021. if (fn(*it)) {
  1022. changed_list_.push_back(std::make_tuple(**it, tmp, linq_change_type::Update));
  1023. }
  1024. }
  1025. }
  1026. return *this;
  1027. }
  1028. /**
  1029. * @brief 删除
  1030. * @param fn
  1031. * @return
  1032. */
  1033. linq_ptr& remove(std::function<bool(std::shared_ptr<const _Type> const&)> const& fn) {
  1034. std::lock_guard<std::mutex> locker(mutex_);
  1035. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1036. for (std::int64_t i = 0; i < source_ok_.size(); ++i) {
  1037. if (!source_ok_[i]) {
  1038. continue;
  1039. }
  1040. it = source_.begin() + i;
  1041. if (fn(*it)) {
  1042. if (!changed_event.empty()) {
  1043. changed_list_.push_back(std::make_tuple(**it, **it, linq_change_type::Delete));
  1044. }
  1045. if (i + 1 < source_ok_.size() && !source_ok_.empty()) {
  1046. source_ok_[i] = std::move(source_ok_.back());
  1047. source_[i] = std::move(source_.back());
  1048. source_ok_.pop_back();
  1049. source_.pop_back();
  1050. }
  1051. else {
  1052. source_ok_.pop_back();
  1053. source_.pop_back();
  1054. }
  1055. --i;
  1056. }
  1057. }
  1058. return *this;
  1059. }
  1060. /**
  1061. * @brief 删除
  1062. * @return
  1063. */
  1064. linq_ptr& remove() {
  1065. std::lock_guard<std::mutex> locker(mutex_);
  1066. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1067. for (std::int64_t i = 0; i < source_ok_.size(); ++i) {
  1068. if (!source_ok_[i]) {
  1069. continue;
  1070. }
  1071. it = source_.begin() + i;
  1072. if (!changed_event.empty()) {
  1073. changed_list_.push_back(std::make_tuple(**it, **it, linq_change_type::Delete));
  1074. }
  1075. if (i + 1 < source_ok_.size() && !source_ok_.empty()) {
  1076. source_ok_[i] = std::move(source_ok_.back());
  1077. source_[i] = std::move(source_.back());
  1078. source_ok_.pop_back();
  1079. source_.pop_back();
  1080. }
  1081. else {
  1082. source_ok_.pop_back();
  1083. source_.pop_back();
  1084. }
  1085. --i;
  1086. }
  1087. return *this;
  1088. }
  1089. /**
  1090. * @brief 添加
  1091. * @param values
  1092. * @return
  1093. */
  1094. linq_ptr& append(std::vector<_Type> const& values) {
  1095. for (auto const& it : values) {
  1096. append(it);
  1097. }
  1098. return *this;
  1099. }
  1100. /**
  1101. * @brief 添加
  1102. * @param value
  1103. * @return
  1104. */
  1105. linq_ptr& append(_Type const& value) {
  1106. append(std::make_shared<_Type>(value));
  1107. return *this;
  1108. }
  1109. /**
  1110. * @brief 添加
  1111. * @param values
  1112. * @return
  1113. */
  1114. linq_ptr& append(std::vector<std::shared_ptr<_Type>> const& values) {
  1115. for (auto const& it : values) {
  1116. append(it);
  1117. }
  1118. return *this;
  1119. }
  1120. /**
  1121. * @brief 添加
  1122. * @param value
  1123. * @return
  1124. */
  1125. linq_ptr& append(std::shared_ptr<_Type> const& value) {
  1126. std::lock_guard<std::mutex> locker(mutex_);
  1127. source_.push_back(value);
  1128. source_ok_.push_back(true);
  1129. if (!changed_event.empty()) {
  1130. changed_list_.push_back(std::make_tuple(*value, *value, linq_change_type::Insert));
  1131. }
  1132. return *this;
  1133. }
  1134. /**
  1135. * @brief 排序
  1136. * @tparam _Fn
  1137. * @param fn
  1138. * @return
  1139. */
  1140. template<typename _Fn>
  1141. linq_ptr& order_by(_Fn&& fn) {
  1142. std::lock_guard<std::mutex> locker(mutex_);
  1143. std::vector<std::pair<std::shared_ptr<_Type>, bool>> alllist;
  1144. for (std::int64_t i = 0; i < source_.size(); ++i) {
  1145. alllist.push_back(std::pair(source_[i], source_ok_[i]));
  1146. }
  1147. std::sort(alllist.begin(), alllist.end(), [&fn](std::pair<std::shared_ptr<_Type>, bool> val1, std::pair<std::shared_ptr<_Type>, bool> val2) {
  1148. return fn(&val1.first, &val2.first); });
  1149. source_.clear();
  1150. source_ok_.clear();
  1151. for (std::int64_t i = 0; i < alllist.size(); ++i) {
  1152. source_.push_back(alllist[i].first);
  1153. source_ok_.push_back(alllist[i].second);
  1154. }
  1155. return *this;
  1156. }
  1157. /**
  1158. * @brief 分页
  1159. * @param page
  1160. * @param count
  1161. * @return
  1162. */
  1163. linq_ptr& limit(unsigned int page, unsigned int count) {
  1164. std::lock_guard<std::mutex> locker(mutex_);
  1165. size_t begin_index = (page - 1) * count;
  1166. size_t current_index = 0;
  1167. size_t current_count = 0;
  1168. size_t i = 0;
  1169. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1170. if (!source_ok_[i]) {
  1171. continue;
  1172. }
  1173. if (begin_index <= current_index && count > current_count) {
  1174. ++current_count;
  1175. }
  1176. else if (count <= current_count) {
  1177. std::fill(source_ok_.begin() + i, source_ok_.end(), false);
  1178. break;
  1179. }
  1180. else {
  1181. source_ok_[i] = false;
  1182. }
  1183. current_index++;
  1184. }
  1185. return *this;
  1186. }
  1187. /**
  1188. * @brief 数量
  1189. * @param result
  1190. * @return
  1191. */
  1192. linq_ptr& count(size_t& result) {
  1193. result = count();
  1194. return *this;
  1195. }
  1196. /**
  1197. * @brief 数量
  1198. * @param result
  1199. * @return
  1200. */
  1201. linq_ptr& count(int& result) {
  1202. result = count();
  1203. return *this;
  1204. }
  1205. /**
  1206. * @brief 数量
  1207. * @param result
  1208. * @return
  1209. */
  1210. linq_ptr& size(size_t& result) {
  1211. result = count();
  1212. return *this;
  1213. }
  1214. /**
  1215. * @brief 数量
  1216. * @param result
  1217. * @return
  1218. */
  1219. linq_ptr& size(int& result) {
  1220. result = count();
  1221. return *this;
  1222. }
  1223. /**
  1224. * @brief 平均值
  1225. * @tparam _Ret
  1226. * @param result
  1227. * @return
  1228. */
  1229. template<typename _Ret>
  1230. linq_ptr& avg(_Ret& result) {
  1231. result = avg<_Ret>();
  1232. return *this;
  1233. }
  1234. /**
  1235. * @brief 平均值
  1236. * @tparam _Ret
  1237. * @tparam _Fn
  1238. * @param result
  1239. * @param fn
  1240. * @return
  1241. */
  1242. template<typename _Ret, typename _Fn>
  1243. linq_ptr& avg(_Ret& result, _Fn&& fn) {
  1244. result = avg<_Ret>(std::forward<_Fn>(fn));
  1245. return *this;
  1246. }
  1247. /**
  1248. * @brief 最大值
  1249. * @tparam _Ret
  1250. * @param result
  1251. * @return
  1252. */
  1253. template<typename _Ret>
  1254. linq_ptr& max(_Ret& result) {
  1255. result = max<_Ret>();
  1256. return *this;
  1257. }
  1258. /**
  1259. * @brief 最大值
  1260. * @tparam _Ret
  1261. * @tparam _Fn
  1262. * @param result
  1263. * @param fn
  1264. * @return
  1265. */
  1266. template<typename _Ret, typename _Fn>
  1267. linq_ptr& max(_Ret& result, _Fn&& fn) {
  1268. result = max<_Ret>(std::forward<_Fn>(fn));
  1269. return *this;
  1270. }
  1271. /**
  1272. * @brief 最小值
  1273. * @tparam _Ret
  1274. * @param result
  1275. * @return
  1276. */
  1277. template<typename _Ret>
  1278. linq_ptr& min(_Ret& result) {
  1279. result = min<_Ret>();
  1280. return *this;
  1281. }
  1282. /**
  1283. * @brief 最小值
  1284. * @tparam _Ret
  1285. * @tparam _Fn
  1286. * @param result
  1287. * @param fn
  1288. * @return
  1289. */
  1290. template<typename _Ret, typename _Fn>
  1291. linq_ptr& min(_Ret& result, _Fn&& fn) {
  1292. result = min<_Ret>(std::forward<_Fn>(fn));
  1293. return *this;
  1294. }
  1295. /**
  1296. * @brief 求和
  1297. * @tparam _Ret
  1298. * @param result
  1299. * @return
  1300. */
  1301. template<typename _Ret>
  1302. linq_ptr& sum(_Ret& result) {
  1303. result = sum<_Ret>();
  1304. return *this;
  1305. }
  1306. /**
  1307. * @brief 求和
  1308. * @tparam _Ret
  1309. * @tparam _Fn
  1310. * @param result
  1311. * @param fn
  1312. * @return
  1313. */
  1314. template<typename _Ret, typename _Fn>
  1315. linq_ptr& sum(_Ret& result, _Fn&& fn) {
  1316. result = sum<_Ret>(std::forward<_Fn>(fn));
  1317. return *this;
  1318. }
  1319. /**
  1320. * @brief 是否包含
  1321. * @param result
  1322. * @param value
  1323. * @return
  1324. */
  1325. linq_ptr& contains(bool& result, std::shared_ptr<_Type> const& value) {
  1326. result = contains(value);
  1327. return *this;
  1328. }
  1329. /**
  1330. * @brief 是否包含
  1331. * @param result
  1332. * @param fn
  1333. * @return
  1334. */
  1335. linq_ptr& contains(bool& result, std::function<bool(std::shared_ptr<const _Type> const&)> const& fn) {
  1336. result = contains(fn);
  1337. return *this;
  1338. }
  1339. /**
  1340. * @brief 选择
  1341. * @tparam _Ret
  1342. * @tparam _Fn
  1343. * @param result
  1344. * @param fn
  1345. * @return
  1346. */
  1347. template<typename _Ret, typename _Fn>
  1348. linq_ptr& select(std::vector<_Ret>& result, _Fn&& fn) {
  1349. result = select<_Ret>(std::forward<_Fn>(fn));
  1350. return *this;
  1351. }
  1352. /**
  1353. * @brief 分组
  1354. * @tparam _Ret
  1355. * @tparam _Fn
  1356. * @param result
  1357. * @param fn
  1358. * @return
  1359. */
  1360. template<typename _Ret, typename _Fn>
  1361. linq_ptr& group_by(_Ret& result, _Fn&& fn) {
  1362. result = group_by<typename _Ret::key_type>(std::forward<_Fn>(fn));
  1363. return *this;
  1364. }
  1365. /**
  1366. * @brief 自定义操作
  1367. * @tparam _Fn
  1368. * @param fn
  1369. * @return
  1370. */
  1371. template<typename _Fn>
  1372. linq_ptr& function(_Fn&& fn) {
  1373. fn();
  1374. return *this;
  1375. }
  1376. /**
  1377. * @brief 自增
  1378. * @tparam _Param
  1379. * @param value
  1380. * @return
  1381. */
  1382. template<typename _Param>
  1383. linq_ptr& inc(_Param& value) {
  1384. ++value;
  1385. return *this;
  1386. }
  1387. /**
  1388. * @brief 数量
  1389. * @return
  1390. */
  1391. size_t count() {
  1392. std::lock_guard<std::mutex> locker(mutex_);
  1393. size_t result = 0;
  1394. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1395. if (!source_ok_[i]) {
  1396. continue;
  1397. }
  1398. result++;
  1399. }
  1400. return result;
  1401. }
  1402. /**
  1403. * @brief 数量
  1404. * @return
  1405. */
  1406. size_t size() {
  1407. return count();
  1408. }
  1409. /**
  1410. * @brief 计算平均值
  1411. * @tparam _Ret
  1412. * @return
  1413. */
  1414. template<typename _Ret>
  1415. _Ret avg() {
  1416. std::lock_guard<std::mutex> locker(mutex_);
  1417. _Ret all = _Ret();
  1418. size_t count = 0;
  1419. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1420. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1421. if (!source_ok_[i]) {
  1422. continue;
  1423. }
  1424. it = source_.begin() + i;
  1425. all += **it;
  1426. count++;
  1427. }
  1428. return all / _Ret(count);
  1429. }
  1430. /**
  1431. * @brief 计算平均值
  1432. * @tparam _Ret
  1433. * @param fn
  1434. * @return
  1435. */
  1436. template<typename _Ret>
  1437. _Ret avg(std::function<_Ret(std::shared_ptr<const _Type> const&)> const& fn) {
  1438. std::lock_guard<std::mutex> locker(mutex_);
  1439. _Ret all = _Ret();
  1440. size_t count = 0;
  1441. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1442. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1443. if (!source_ok_[i]) {
  1444. continue;
  1445. }
  1446. it = source_.begin() + i;
  1447. all += fn(*it);
  1448. count++;
  1449. }
  1450. return all / _Ret(count);
  1451. }
  1452. /**
  1453. * @brief 最大值
  1454. * @tparam _Ret
  1455. * @return
  1456. */
  1457. template<typename _Ret>
  1458. _Ret max() {
  1459. std::lock_guard<std::mutex> locker(mutex_);
  1460. _Ret result = _Ret();
  1461. bool ok = true;
  1462. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1463. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1464. if (!source_ok_[i]) {
  1465. continue;
  1466. }
  1467. it = source_.begin() + i;
  1468. if (ok) {
  1469. ok = false;
  1470. result = **it;
  1471. }
  1472. else {
  1473. if (result < **it) {
  1474. result = **it;
  1475. }
  1476. }
  1477. }
  1478. return result;
  1479. }
  1480. /**
  1481. * @brief
  1482. * @tparam _Ret
  1483. * @param fn
  1484. * @return
  1485. */
  1486. template<typename _Ret>
  1487. _Ret max(std::function<_Ret(std::shared_ptr<const _Type> const&)> const& fn) {
  1488. std::lock_guard<std::mutex> locker(mutex_);
  1489. _Ret result = _Ret();
  1490. bool ok = true;
  1491. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1492. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1493. if (!source_ok_[i]) {
  1494. continue;
  1495. }
  1496. it = source_.begin() + i;
  1497. if (ok) {
  1498. ok = false;
  1499. result = fn(*it);
  1500. }
  1501. else {
  1502. _Ret tmp_value = fn(*it);
  1503. if (result < tmp_value) {
  1504. result = tmp_value;
  1505. }
  1506. }
  1507. }
  1508. return result;
  1509. }
  1510. /**
  1511. * @brief 最小值
  1512. * @tparam _Ret
  1513. * @return
  1514. */
  1515. template<typename _Ret>
  1516. _Ret min() {
  1517. std::lock_guard<std::mutex> locker(mutex_);
  1518. _Ret result = _Ret();
  1519. bool ok = true;
  1520. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1521. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1522. if (!source_ok_[i]) {
  1523. continue;
  1524. }
  1525. it = source_.begin() + i;
  1526. if (ok) {
  1527. ok = false;
  1528. result = *it;
  1529. }
  1530. else {
  1531. if (result > *it) {
  1532. result = *it;
  1533. }
  1534. }
  1535. }
  1536. return result;
  1537. }
  1538. /**
  1539. * @brief 最小值
  1540. * @tparam _Ret
  1541. * @param fn
  1542. * @return
  1543. */
  1544. template<typename _Ret>
  1545. _Ret min(std::function<_Ret(std::shared_ptr<const _Type> const&)> const& fn) {
  1546. std::lock_guard<std::mutex> locker(mutex_);
  1547. _Ret result = _Ret();
  1548. bool ok = true;
  1549. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1550. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1551. if (!source_ok_[i]) {
  1552. continue;
  1553. }
  1554. it = source_.begin() + i;
  1555. if (ok) {
  1556. ok = false;
  1557. result = fn(*it);
  1558. }
  1559. else {
  1560. _Ret tmp_value = fn(*it);
  1561. if (result > tmp_value) {
  1562. result = tmp_value;
  1563. }
  1564. }
  1565. }
  1566. return result;
  1567. }
  1568. /**
  1569. * @brief 求和
  1570. * @tparam _Ret
  1571. * @return
  1572. */
  1573. template<typename _Ret>
  1574. _Ret sum() {
  1575. std::lock_guard<std::mutex> locker(mutex_);
  1576. _Ret result = _Ret();
  1577. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1578. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1579. if (!source_ok_[i]) {
  1580. continue;
  1581. }
  1582. it = source_.begin() + i;
  1583. result += **it;
  1584. }
  1585. return result;
  1586. }
  1587. /**
  1588. * @brief 求和
  1589. * @tparam _Ret
  1590. * @param fn
  1591. * @return
  1592. */
  1593. template<typename _Ret>
  1594. _Ret sum(std::function<_Ret(std::shared_ptr<const _Type> const&)> const& fn) {
  1595. std::lock_guard<std::mutex> locker(mutex_);
  1596. _Ret result = _Ret();
  1597. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1598. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1599. if (!source_ok_[i]) {
  1600. continue;
  1601. }
  1602. it = source_.begin() + i;
  1603. result += fn(*it);
  1604. }
  1605. return result;
  1606. }
  1607. /**
  1608. * @brief 是否包含
  1609. * @param value
  1610. * @return
  1611. */
  1612. bool contains(std::shared_ptr<_Type> const& value) {
  1613. std::lock_guard<std::mutex> locker(mutex_);
  1614. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1615. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1616. if (!source_ok_[i]) {
  1617. continue;
  1618. }
  1619. it = source_.begin() + i;
  1620. if (**it == *value) {
  1621. return true;
  1622. }
  1623. }
  1624. return false;
  1625. }
  1626. /**
  1627. * @brief 是否存在
  1628. * @param fn
  1629. * @return
  1630. */
  1631. bool contains(std::function<bool(std::shared_ptr<const _Type> const&)>const& fn) {
  1632. std::lock_guard<std::mutex> locker(mutex_);
  1633. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1634. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1635. if (!source_ok_[i]) {
  1636. continue;
  1637. }
  1638. it = source_.begin() + i;
  1639. if (fn(*it)) {
  1640. return true;
  1641. }
  1642. }
  1643. return false;
  1644. }
  1645. /**
  1646. * @brief 选择
  1647. * @tparam _Ret
  1648. * @param fn
  1649. * @return
  1650. */
  1651. template<typename _Ret>
  1652. std::vector<_Ret> select(std::function<_Ret(std::shared_ptr<const _Type> const&)> const& fn) {
  1653. std::lock_guard<std::mutex> locker(mutex_);
  1654. std::vector<_Ret> result;
  1655. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1656. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1657. if (!source_ok_[i]) {
  1658. continue;
  1659. }
  1660. it = source_.begin() + i;
  1661. result.push_back(fn(*it));
  1662. }
  1663. return result;
  1664. }
  1665. /**
  1666. * @brief 分组
  1667. * @tparam _Key
  1668. * @param function
  1669. * @return
  1670. */
  1671. template<typename _Key>
  1672. std::map<_Key, std::vector<std::shared_ptr<_Type>>> group_by(std::function<_Key(std::shared_ptr<const _Type> const&)> const& fn) {
  1673. std::lock_guard<std::mutex> locker(mutex_);
  1674. std::map<_Key, std::vector<std::shared_ptr<_Type>>> result;
  1675. typename std::vector<std::shared_ptr<_Type>>::const_iterator it;
  1676. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1677. if (!source_ok_[i]) {
  1678. continue;
  1679. }
  1680. it = source_.begin() + i;
  1681. result[fn(*it)].push_back(*it);
  1682. }
  1683. return result;
  1684. }
  1685. /**
  1686. * @brief 获取值
  1687. * @return
  1688. */
  1689. std::vector<std::shared_ptr<_Type>> value_ptr() {
  1690. std::lock_guard<std::mutex> locker(mutex_);
  1691. std::vector<std::shared_ptr<_Type>> result;
  1692. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1693. if (!source_ok_[i]) {
  1694. continue;
  1695. }
  1696. result.push_back(*(source_.begin() + i));
  1697. }
  1698. return result;
  1699. }
  1700. /**
  1701. * @brief 获取一个值
  1702. * @return
  1703. */
  1704. std::shared_ptr<_Type> first() {
  1705. std::lock_guard<std::mutex> locker(mutex_);
  1706. std::shared_ptr<_Type> result = nullptr;
  1707. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1708. if (!source_ok_[i]) {
  1709. continue;
  1710. }
  1711. result = *(source_.begin() + i);
  1712. break;
  1713. }
  1714. return result;
  1715. }
  1716. /**
  1717. * @brief 获取值
  1718. */
  1719. operator std::shared_ptr<_Type>() {
  1720. return first();
  1721. }
  1722. /**
  1723. * @brief 原始资源
  1724. * @return
  1725. */
  1726. std::vector<std::shared_ptr<_Type>> source_ptr() {
  1727. return source_;
  1728. }
  1729. /**
  1730. * @brief 获取值
  1731. */
  1732. operator std::vector<std::shared_ptr<_Type>>() {
  1733. return value_ptr();
  1734. }
  1735. /**
  1736. * @brief 获取值
  1737. * @return
  1738. */
  1739. std::vector<_Type> value() {
  1740. std::lock_guard<std::mutex> locker(mutex_);
  1741. std::vector<_Type> result;
  1742. for (size_t i = 0; i < source_ok_.size(); ++i) {
  1743. if (!source_ok_[i]) {
  1744. continue;
  1745. }
  1746. result.push_back(**(source_.begin() + i));
  1747. }
  1748. return result;
  1749. }
  1750. /**
  1751. * @brief 原始资源
  1752. * @return
  1753. */
  1754. std::vector<_Type> source() {
  1755. std::lock_guard<std::mutex> locker(mutex_);
  1756. std::vector<_Type> result;
  1757. for (auto& it : source_) {
  1758. result.push_back(*it);
  1759. }
  1760. return result;
  1761. }
  1762. /**
  1763. * @brief
  1764. * @return
  1765. */
  1766. bool empty() {
  1767. for (bool it : source_ok_) {
  1768. if (it) {
  1769. return false;
  1770. }
  1771. }
  1772. return true;
  1773. }
  1774. /**
  1775. * @brief 获取值
  1776. */
  1777. operator std::vector<_Type>() {
  1778. return value();
  1779. }
  1780. /**
  1781. * @brief 改变事件
  1782. */
  1783. std::vector<std::function<void(_Type const&, _Type const&, linq_change_type, bool)>> changed_event;
  1784. protected:
  1785. std::mutex mutex_;
  1786. std::vector<bool> source_ok_;
  1787. std::vector<std::shared_ptr<_Type>>& source_;
  1788. std::vector<std::tuple<_Type, _Type, linq_change_type>> changed_list_;
  1789. };
  1790. /**
  1791. * @brief from
  1792. * @tparam _Type
  1793. * @param value
  1794. * @return
  1795. */
  1796. template<typename _Type>
  1797. static inline linq<_Type> from(std::vector<_Type>& value) {
  1798. return linq<_Type>(value);
  1799. }
  1800. /**
  1801. * @brief from
  1802. * @tparam _Type
  1803. * @param value
  1804. * @return
  1805. */
  1806. template<typename _Type>
  1807. static inline linq_ptr<_Type> from(std::vector<std::shared_ptr<_Type>>& value) {
  1808. return linq_ptr<_Type>(value);
  1809. }
  1810. }
  1811. }