当前位置:   article > 正文

更新版的双向链接_DList,主要更新了迭代器,(与vector兼容)

更新版的双向链接_DList,主要更新了迭代器,(与vector兼容)

使用例子1:使用通用算法排序

  1. int main()
  2. {
  3. vector<int> v = {1,2,3,4,5,9,8,7,0,6};
  4. _DList<int> d = {1,2,3,4,5,9,8,7,0,6};
  5. //输出
  6. _pcn( (_DList<int>) v );
  7. _pcn(d);
  8. //选择排序
  9. lf::sort_selection(v.begin(), v.end());
  10. lf::sort_selection(d.begin(), d.end());
  11. //输出
  12. _pcn((_DList<int>) v);
  13. _pcn(d);
  14. //选择排序
  15. lf::sort_selection(v.rbegin(), v.rend());
  16. lf::sort_selection(d.rbegin(), d.rend());
  17. //输出
  18. _pcn((_DList<int>) v);
  19. _pcn(d);
  20. vector<_string> v2 = { "int","double","if","for", "template","class"};
  21. _DList<_string> d2 = { "int","double","if","for", "template","class" };
  22. //输出
  23. _pcn((_DList<_string>) v2);
  24. _pcn(d2);
  25. lf::sort_selection(v2.begin(), v2.end());
  26. lf::sort_selection(d2.begin(), d2.end());
  27. //输出
  28. _pcn((_DList<_string>) v2);
  29. _pcn(d2);
  30. }

输出:

用例子2:

  1. int main()
  2. {
  3. vector<int> v = {1,2,3,4,5,9,8,7,0,6};
  4. _DList<int> d = {1,2,3,4,5,9,8,7,0,6};
  5. _pin(*v.begin());
  6. _pin(* (v.begin() + 1) );
  7. _pin(* (v.end() - 1) );
  8. _pin(*d.begin());
  9. _pin(*(d.begin() + 1));
  10. _pin(*(d.end() - 1));
  11. }

输出:

其中函数void sort_selection:

template<typename IteratorClass>
void sort_selection(IteratorClass itBegin, IteratorClass itEnd, const bool& bMinMax = true)
{

    /*
        7 4 5 9 8 2 1
        1 4 5 9 8 2 7
          2 5 9 8 4 7
            4 9 8 5 7
              5 8 9 7
                7 9 8
                  8 9

        7 2 2 2 2 2 2
        2 7 2 2 2 2 2
          2 7 2 2 2 2
            2 7 2 2 2
              2 7 2 2
                2 7 2
                  2 7

        在 [0 , n-1] 中找出最小的放在第一位
        在 [1 , n-1] 中找出最小的放在第二位
        ...


    */

    if (bMinMax) {

        while (itBegin != itEnd) {
            //在[itBegin + 1, itEnd] 中找出最小值,与itBegin交换         
            Swap(itBegin, Min(itBegin, itEnd));
            ++itBegin;
        }
    }
    else {
        while (itBegin != itEnd) {
            //在[itBegin + 1, itEnd] 中找出最小值,与itBegin交换
            Swap(itBegin, Max(itBegin, itEnd));
            ++itBegin;
        }
    }
}

  1. /*******************************************************************************************
  2. 文件名 : _AlgorithmLibrary.h
  3. 功能 : 算法库
  4. 作者 : 李锋
  5. 手机 : 13828778863
  6. Email : ruizhilf@139.com
  7. 创建时间 : 2024年07月02日
  8. 最后一次修改时间 : 2024年07月02日
  9. *********************************************************************************************/
  10. #pragma once
  11. //Algorithm library 算法库
  12. #include "_Macro.h"
  13. /*****************************************************************************
  14. 排序
  15. ****************************************************************************/
  16. //排序
  17. //参考:https://blog.csdn.net/qq_45615577/article/details/115257685
  18. //排序的概念
  19. /*
  20. 排序:所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作。
  21. 稳定性:假定在待排序的记录序列中,存在多个具有相同的关键字的记录,若经过排序,这些记录的相对次 序保持不变,即在原序列中,r[i] = r[j],且r[i]在r[j]之前,而在排序后的序列中,r[i]仍在r[j]之前,则称这种排 序算法是稳定的;否则称为不稳定的。
  22. 内部排序:数据元素全部放在内存中的排序。
  23. 外部排序:数据元素太多不能同时放在内存中,根据排序过程的要求不能在内外存之间移动数据的排序。
  24. ————————————————
  25. 版权声明:本文为博主原创文章,遵循 CC 4.0 BY - SA 版权协议,转载请附上原文出处链接和本声明。
  26. 原文链接:https ://blog.csdn.net/qq_45615577/article/details/115257685
  27. */
  28. _LF_BEGIN_
  29. /// <summary>
  30. /// 选择排序---直接选择排序
  31. /// 每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,
  32. /// 直到全部待排序的 数据元素排完 。
  33. /// 找出序列中的最小关键字,然后将这个元素与序列首端元素交换位置。例如,序列前i个
  34. /// 元素已经有序,从第i + 1到第n个元素中选择关键字最小的元素,假设第j个元素为最小
  35. /// 元素,则交换第j个元素与第i + 1个元素的位置。依次执行此操作,直到第n - 1个元素
  36. /// 也被确定。
  37. /// </summary>
  38. /// <typeparam name="T">类型</typeparam>
  39. /// <param name="pData">开始位置</param>
  40. /// <param name="nCount">元素个数</param>
  41. /// <param name="so">排序顺序,默认从小到大</param>
  42. /// 创建时间: 2024-07-01 最后一修改时间:2024-07-01
  43. /// 参考网址:https://blog.csdn.net/qq_45615577/article/details/115257685
  44. template<class T>
  45. void sort_selection(T* pData, const size_t& nCount, const bool& bMinMax = true)
  46. {
  47. /*
  48. 7 4 5 9 8 2 1
  49. 1 4 5 9 8 2 7
  50. 2 5 9 8 4 7
  51. 4 9 8 5 7
  52. 5 8 9 7
  53. 7 9 8
  54. 8 9
  55. 在 [0 , n-1] 中找出最小的放在第一位
  56. 在 [1 , n-1] 中找出最小的放在第二位
  57. ...
  58. */
  59. if (pData == null || nCount == 0) return;
  60. int nSortedCount = 0; //已排序好的个数
  61. if (bMinMax) {
  62. while (nSortedCount < nCount) {
  63. int minIndex = nSortedCount;
  64. //在[nStart, nCount-1] 中找出最小值
  65. for (int n = nSortedCount + 1; n < nCount; ++n) {
  66. if (*(pData + n) < *(pData + minIndex)) {
  67. minIndex = n;
  68. }
  69. }
  70. if (minIndex != nSortedCount) {
  71. T tmp = *(pData + minIndex);
  72. *(pData + minIndex) = *(pData + nSortedCount);
  73. *(pData + nSortedCount) = tmp;
  74. }
  75. ++nSortedCount;
  76. }
  77. }
  78. else {
  79. while (nSortedCount < nCount) {
  80. int maxIndex = nSortedCount;
  81. //在[nStart, nCount-1] 中找出最大值
  82. for (int n = nSortedCount + 1; n < nCount; ++n) {
  83. if (*(pData + n) > *(pData + maxIndex)) {
  84. maxIndex = n;
  85. }
  86. }
  87. if (maxIndex != nSortedCount) {
  88. T tmp = *(pData + maxIndex);
  89. *(pData + maxIndex) = *(pData + nSortedCount);
  90. *(pData + nSortedCount) = tmp;
  91. }
  92. ++nSortedCount;
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// 返回最小值的位置
  98. /// lf::_DList<int> d1 = { 1,3,5,8,2,0 };
  99. /// lf::_DList<int> d2 = { 1 };
  100. /// lf::_DList<int> d3 = { };
  101. /// vector<int> v = { 1,3,5,8,2,0 };
  102. ///
  103. /// _pin(*lf::Min(d1.begin(), d1.end())); //输出: 0
  104. /// _pin(*lf::Min(d2.begin(), d2.end())); //输出: 1
  105. /// _pin(*lf::Min(d3.begin(), d3.end())); //报错,最少一个元素
  106. ///
  107. /// _pin(*lf::Min(v.begin(), v.end())); //输出: 0
  108. ///
  109. /// _string s = _t("sdwsffa");
  110. /// _pin(*lf::Min(s.begin(), s.end())); //输出: a
  111. /// </summary>
  112. /// <typeparam name="IteratorClass"></typeparam>
  113. /// <param name="itBegin"></param>
  114. /// <param name="itEnd"></param>
  115. /// <returns></returns>
  116. /// 创建时间: 2024-07-02 最后一修改时间:2024-07-02
  117. template<typename IteratorClass>
  118. IteratorClass Min(IteratorClass itBegin, IteratorClass itEnd) {
  119. assert(itBegin != itEnd);
  120. IteratorClass result = itBegin;
  121. while (itBegin != itEnd) {
  122. if (*result > *itBegin)
  123. result = itBegin;
  124. ++itBegin;
  125. }
  126. return result;
  127. }
  128. /// <summary>
  129. ///
  130. /// </summary>
  131. /// <typeparam name="IteratorClass"></typeparam>
  132. /// <param name="itBegin"></param>
  133. /// <param name="itEnd"></param>
  134. /// <returns></returns>
  135. /// 创建时间: 2024-07-02 最后一修改时间:2024-07-02
  136. template<typename IteratorClass>
  137. IteratorClass Max(IteratorClass itBegin, IteratorClass itEnd) {
  138. assert(itBegin != itEnd);
  139. IteratorClass result = itBegin;
  140. while (itBegin != itEnd) {
  141. if (*result < *itBegin)
  142. result = itBegin;
  143. ++itBegin;
  144. }
  145. return result;
  146. }
  147. /// <summary>
  148. ///
  149. /// </summary>
  150. /// <typeparam name="IteratorClass"></typeparam>
  151. /// <param name="it1"></param>
  152. /// <param name="it2"></param>
  153. /// 创建时间: 2024-07-02 最后一修改时间:2024-07-02
  154. template<typename IteratorClass>
  155. void Swap(IteratorClass it1, IteratorClass it2) {
  156. //std::cout << "===================================\n";
  157. //_pin(*it1);
  158. //_pin(*it2);
  159. auto tmp = *it1; //如果*it2是 int& 则,tmp 的类型是int, 并不是int&。
  160. *it1 = *it2;
  161. *it2 = tmp;
  162. //_pin(*it1);
  163. //_pin(*it2);
  164. //std::cout << "===================================\n";
  165. }
  166. /// <summary>
  167. /// lf::_DList<int> d4 = {1,2,3,6,5,4};
  168. /// lf::sort_selection(d4.begin(), d4.end(), _SortOrder::s_Minmax);
  169. /// _pcn(d4); //输出:d4={1,2,3,4,5,6}
  170. /// lf::sort_selection(d4.begin(), d4.end(), _SortOrder::s_Maxmin);
  171. /// _pcn(d4); //输出:d4={6,5,4,3,2,1}
  172. ///
  173. /// _string s2 = _t("_DListNodeIterator,abcd,efg");
  174. /// _pcn(s2); //s2=_DListNodeIterator,abcd,efg
  175. /// lf::sort_selection(s2.begin(), s2.end(), _SortOrder::s_Minmax);
  176. /// _pcn(s2); //s2=,,DILN_aabcddeeefgioorrsttt
  177. /// lf::sort_selection(s2.begin(), s2.end(), _SortOrder::s_Maxmin);
  178. /// _pcn(s2); //s2=tttsrrooigfeeeddcbaa_NLID,,
  179. /// </summary>
  180. /// <typeparam name="IteratorClass"></typeparam>
  181. /// <param name="itBegin"></param>
  182. /// <param name="itEnd"></param>
  183. /// <param name="so"></param>
  184. /// 创建时间: 2024-07-01 最后一修改时间:2024-07-02(已测试)
  185. template<typename IteratorClass>
  186. void sort_selection(IteratorClass itBegin, IteratorClass itEnd, const bool& bMinMax = true)
  187. {
  188. /*
  189. 7 4 5 9 8 2 1
  190. 1 4 5 9 8 2 7
  191. 2 5 9 8 4 7
  192. 4 9 8 5 7
  193. 5 8 9 7
  194. 7 9 8
  195. 8 9
  196. 7 2 2 2 2 2 2
  197. 2 7 2 2 2 2 2
  198. 2 7 2 2 2 2
  199. 2 7 2 2 2
  200. 2 7 2 2
  201. 2 7 2
  202. 2 7
  203. 在 [0 , n-1] 中找出最小的放在第一位
  204. 在 [1 , n-1] 中找出最小的放在第二位
  205. ...
  206. */
  207. if (bMinMax) {
  208. while (itBegin != itEnd) {
  209. //在[itBegin + 1, itEnd] 中找出最小值,与itBegin交换
  210. Swap(itBegin, Min(itBegin, itEnd));
  211. ++itBegin;
  212. }
  213. }
  214. else {
  215. while (itBegin != itEnd) {
  216. //在[itBegin + 1, itEnd] 中找出最小值,与itBegin交换
  217. Swap(itBegin, Max(itBegin, itEnd));
  218. ++itBegin;
  219. }
  220. }
  221. }
  222. /*****************************************************************************
  223. 查找
  224. 顺序查找
  225. 二分查找
  226. 插值查找、
  227. 斐波那契查找
  228. 分块查找
  229. 哈希查找
  230. 树表查找
  231. ****************************************************************************/
  232. template<typename IteratorType>
  233. struct FindResult
  234. {
  235. /// <summary>
  236. /// 如果没找到 Index == -1
  237. /// </summary>
  238. int Index;
  239. IteratorType Iter;
  240. public:
  241. inline FindResult()
  242. {
  243. Index = -1;
  244. }
  245. inline FindResult(const FindResult& r)
  246. {
  247. Index = r.Index;
  248. Iter = r.Iter;
  249. }
  250. };
  251. /// <summary>
  252. /// 顺序查找
  253. ///
  254. /// 注意:
  255. /// 如果是向后查找,则返回的索引是以itFirst开始算来,第一个为 0,即itFirst为0。
  256. /// 如果是向前揸找,则返回的索引是以itLast开始算起,第一个,即itLast为0。
  257. ///
  258. /// </summary>
  259. /// <typeparam name="IteratorType"></typeparam>
  260. /// <typeparam name="DataType"></typeparam>
  261. /// <param name="itBegin"></param>
  262. /// <param name="itEnd"></param>
  263. /// <param name="tValue"></param>
  264. /// <param name="bBackward"></param>
  265. /// <returns></returns>
  266. /// 创建时间: 2024-07-03 最后一修改时间:2024-07-03
  267. template<typename IteratorType, typename DataType>
  268. FindResult<IteratorType> find_sequential(IteratorType itFirst, IteratorType itLast,
  269. const DataType& tFindValue, const bool& bBackward = true)
  270. {
  271. FindResult<IteratorType> fr;
  272. fr.Index = -1;
  273. int n = 0;
  274. if (bBackward) { //向后
  275. while (true)
  276. {
  277. if (*itFirst == tFindValue) {
  278. fr.Index = n;
  279. fr.Iter = itFirst;
  280. break;
  281. }
  282. ++n;
  283. ++itFirst;
  284. if (itFirst == itLast) break;
  285. }
  286. }
  287. else {
  288. while (true)
  289. {
  290. if (*itLast == tFindValue) {
  291. fr.Index = n;
  292. fr.Iter = itLast;
  293. break;
  294. }
  295. ++n;
  296. --itLast;
  297. if (itFirst == itLast) break;
  298. }
  299. }
  300. return fr;
  301. }
  302. /// <summary>
  303. /// 集合类必须带有 begin() 和 end() 函数
  304. /// 例子:
  305. /// std::vector<int> v = { 1,2,3,23,435,4646,34 };
  306. /// lf::_DList<int> d = { 1,2,3,23,435,4646,34 };
  307. /// if (lf::IsExists(d, 23))
  308. /// {
  309. /// cout << "存在23.\n";
  310. /// }
  311. /// if (lf::IsExists(v, 55))
  312. /// {
  313. /// cout << "存在55.\n";
  314. /// }
  315. /// </summary>
  316. /// <typeparam name="value_type"></typeparam>
  317. /// <typeparam name="CollectionClass"></typeparam>
  318. /// <param name="col"></param>
  319. /// <param name="value"></param>
  320. /// <returns></returns>
  321. /// 创建时间: 2024-07-03 最后一修改时间:2024-07-03
  322. template<typename CollectionClass, typename value_type = CollectionClass::value_type>
  323. bool IsExists(const CollectionClass& col, const value_type& value)
  324. {
  325. auto itbegin = col.begin();
  326. auto itend = col.end();
  327. while (itbegin != itend)
  328. {
  329. if (*itbegin == value)
  330. return true;
  331. ++itbegin;
  332. }
  333. return false;
  334. }
  335. /// <summary>
  336. /// 二分法查找有序表的通用算法(可查链表,数组,字符串...等等)
  337. /// 注意事项:
  338. /// (1)你设计的迭代器模板中必须有using value_type = T,且有加减运算功能,
  339. /// 其本上能与C++标准库std中一样。
  340. /// (2)集合必须是有序的。
  341. /// 例子:
  342. /// vector<int> v;
  343. /// find_binary(b.begin(),v.end(),3); // Inde返回 (-1, *Iterator == ?)
  344. ///
  345. /// vector<int> v = {3};
  346. /// find_binary(b.begin(),v.end(),3); // 返回 (Index == 0, *Iterator == 3)
  347. ///
  348. /// const char* sz = "abcdefgb";
  349. /// auto f3 = lf::find_binary(sz, sz + 8, 'c'); //返回 (Index == 2, *Iterator == c)
  350. /// </summary>
  351. /// <typeparam name="IteratorType">迭代器类型</typeparam>
  352. /// <typeparam name="value_type">值类型</typeparam>
  353. /// <param name="itBegin">开始位置</param>
  354. /// <param name="itEnd">结束位置</param>
  355. /// <param name="vtFindValue">查找值</param>
  356. /// <returns>返回索引与指向vtFindValue的迭代器</returns>
  357. /// 创建时间: 2024-07-03 最后一修改时间:2024-07-04 (初步测试)
  358. template<typename IteratorType,typename value_type = IteratorType::value_type>
  359. FindResult<IteratorType> find_binary(const IteratorType& itBegin,
  360. const IteratorType& itEnd, const value_type& vtFindValue)
  361. {
  362. FindResult<IteratorType> fr;
  363. auto beg = itBegin;
  364. auto end = itEnd;
  365. int nCount = end - beg;
  366. if (nCount == 0) return fr;
  367. if (*(itEnd-1) > *itBegin) {//从小到大排列
  368. auto mid = beg + nCount / 2;
  369. while (mid != itEnd){
  370. if(*mid == vtFindValue){
  371. fr.Iter = mid;
  372. fr.Index = mid - itBegin;
  373. return fr;
  374. }
  375. if (vtFindValue < *mid)
  376. end = mid;
  377. else
  378. beg = mid + 1; //在mid之后查找
  379. mid = beg + (end - beg) / 2; //新的中间点
  380. }
  381. }else{ //从大到小排列
  382. auto mid = beg + nCount / 2;
  383. while (mid != itEnd) {
  384. if (*mid == vtFindValue) {
  385. fr.Iter = mid;
  386. fr.Index = mid - itBegin;
  387. return fr;
  388. }
  389. if (vtFindValue > *mid)
  390. end = mid;
  391. else
  392. beg = mid + 1; //在mid之后查找
  393. mid = beg + (end - beg) / 2; //新的中间点
  394. }
  395. }
  396. return fr;
  397. }
  398. _LF_END_

更新的_DList类 _List.h

  1. /*******************************************************************************************
  2. 文件名 : _List.h
  3. 作者 : 李锋
  4. 功能 : 链表
  5. 手机 : 13828778863
  6. Email : ruizhilf@139.com
  7. 创建时间 : 2016年07月31日
  8. 最后一次修改时间 : 2024年07月25日
  9. /// 链表是一种用来存储数据集合的数据结构。链表具有如下属性
  10. ///(1)元素通过指针依次相连
  11. ///(2)最后一个元素的指针为空(null)。
  12. ///(3)在程序的执行过程中,链表的长度可以自由伸缩。
  13. ///(4)链表的长度可以要求的任意长度(除非系统内存耗尽)。
  14. ///(5)它不会浪费内存空间(但会需要额外的内存空间存储指针)。
  15. 记住: Virtual C++ Bug: 模板继承用到父类成员访问时,要用 this->
  16. ********************************************************************************************/
  17. #ifndef __LIST_H_
  18. #define __LIST_H_
  19. #include "_Macro.h"
  20. #include "_Memory.h"
  21. #include "_ByteArray.h"
  22. #include "_Pair.h"
  23. #include "_IteratorBase.h"
  24. _LF_BEGIN_
  25. template<class T> class _DList; //前置声明
  26. /// <summary>
  27. /// 排序顺序
  28. /// </summary>
  29. enum class _SortOrder
  30. {
  31. s_Minmax = 0, //从小到大
  32. s_Maxmin = 1, //从大到小
  33. s_null = 2 //无排序顺序
  34. };
  35. /// <summary>
  36. /// 单链表节点
  37. /// </summary>
  38. /// <typeparam name="T"></typeparam>
  39. /// 创建时间 2021年10月23日 最后一次修改时间 : 2021年10月23日
  40. template<class T>
  41. class _SListNode
  42. {
  43. public:
  44. /// <summary>
  45. /// 节点数据
  46. /// </summary>
  47. T Data;
  48. /// <summary>
  49. /// 下一个节点
  50. /// </summary>
  51. _SListNode<T>* Next;
  52. /// <summary>
  53. /// 构造函数
  54. /// </summary>
  55. /// <param name="aData"></param>
  56. _SListNode(T aData)
  57. {
  58. Data = aData;
  59. }
  60. };
  61. /// <summary>
  62. /// C#语句:public class _DListNode<T>
  63. /// </summary>
  64. /// <typeparam name="T">默认数据</typeparam>
  65. template<class T>
  66. class _DListNode
  67. {
  68. public:
  69. /// <summary>
  70. /// 节点数据
  71. /// </summary>
  72. T Data;
  73. /// <summary>
  74. /// 前一个节点
  75. /// </summary>
  76. _DListNode<T>* Prev;
  77. /// <summary>
  78. /// 下一个节点
  79. /// </summary>
  80. _DListNode<T>* Next;
  81. /// <summary>
  82. /// 构造函数
  83. /// </summary>
  84. /// <param name="aData">默认数据</param>
  85. _DListNode(const T& aData)
  86. {
  87. Data = aData;
  88. Prev = null;
  89. Next = null;
  90. }
  91. _DListNode()
  92. {
  93. Prev = null;
  94. Next = null;
  95. Data = T();
  96. }
  97. };
  98. /// <summary>
  99. /// 单链表:
  100. /// 通常我们说的链表指的是单链表(singly linked list)。单链表包括一组结点,每个结
  101. /// 点有一个 next 指针域,用来存储指向(逻辑上)下一个元素对应结点的指针。最后一个结点的
  102. /// next指针域的值为null,这预示着已经到达链表的尾部。
  103. ///
  104. /// </summary>
  105. /// <typeparam name="T"></typeparam>
  106. template<class T>
  107. class _SList : public _Object
  108. {
  109. private:
  110. /// <summary>
  111. /// 第一个结点
  112. /// </summary>
  113. _SListNode<T>* _First;
  114. /// <summary>
  115. /// 结点数
  116. /// </summary>
  117. int _Count;
  118. public:
  119. inline const int& Count() { return _Count; }
  120. inline const _SListNode<T>* First() { return _First; }
  121. };
  122. //------------------------------------------LDIterator
  123. //参考网址: https://blog.csdn.net/qq_28398301/article/details/106321525 C++用for遍历自定义类
  124. /// <summary>
  125. ///
  126. /// </summary>
  127. /// <typeparam name="T"></typeparam>
  128. template<class T>
  129. class _DListNodeIterator
  130. {
  131. public:
  132. using value_type = T;
  133. private:
  134. /// <summary>
  135. /// 当前节点
  136. /// </summary>
  137. _DListNode<T>* _pCurrentNode;
  138. /// <summary>
  139. /// 当前链表
  140. /// </summary>
  141. const _DList<T>* _pCurrentList;
  142. public:
  143. inline _DListNodeIterator()
  144. {
  145. _pCurrentList = null;
  146. _pCurrentNode = null;
  147. }
  148. inline _DListNodeIterator(const _DList<T>* pCurrentList)
  149. {
  150. assert(pCurrentList != null);
  151. _pCurrentList = (_DList<T>*)pCurrentList;
  152. _pCurrentNode = null;
  153. }
  154. /// <summary>
  155. /// 构造函数,传值迭代器管理的值
  156. /// </summary>
  157. /// <param name="pNode"></param>
  158. inline _DListNodeIterator(const _DListNode<T>* pCurrentNode, const _DList<T>* pCurrentList)
  159. {
  160. _pCurrentNode = (_DListNode<T>*)pCurrentNode;
  161. _pCurrentList = (_DList<T>*)pCurrentList;
  162. }
  163. inline _DListNodeIterator(const _DListNodeIterator& it)
  164. {
  165. _pCurrentNode = it._pCurrentNode;
  166. _pCurrentList = it._pCurrentList;
  167. }
  168. /*
  169. /// <summary>
  170. /// 比较实现
  171. /// </summary>
  172. /// <param name="that"></param>
  173. /// <returns></returns>
  174. bool operator != (const _DListNodeIterator& that) { return _pNode != that._pNode; }
  175. bool operator > (const _DListNodeIterator& right) { return _pNode->Data > right._pNode->Data; }
  176. bool operator < (const _DListNodeIterator& right) { return _pNode->Data < right._pNode->Data; }
  177. /// <summary>
  178. /// 自增实现
  179. /// </summary>
  180. /// <returns></returns>
  181. inline _DListNodeIterator& operator ++ () { _pNode = _pNode->Next; return *this; }
  182. /// <summary>
  183. /// lf::_DList<int> d = { 1,3,5,8,2 };
  184. /// auto it1 = d.begin();
  185. /// cout << *it1 << "\n"; // 输出:1
  186. /// cout << *(it1 + 2) << "\n"; // 输出:5
  187. /// </summary>
  188. /// <param name="nDiff"></param>
  189. /// <returns></returns>
  190. /// 创建时间: 2024-07-01 最后一修改时间:2024-07-01
  191. inline _DListNodeIterator operator+(const size_t nDiff) {
  192. _DListNodeIterator result(_pNode);
  193. for (int n = 0; n < nDiff; ++n)
  194. {
  195. result._pNode = result._pNode->Next;
  196. }
  197. return result;
  198. }
  199. /// <summary>
  200. /// 解引用,取值
  201. /// </summary>
  202. /// <typeparam name="T"></typeparam>
  203. T& operator * () { return _pNode->Data; }
  204. //LDIterator(const LDIterator&) = delete;
  205. //LDIterator& operator=(const LDIterator&) = delete;
  206. //~LDIterator() = default;
  207. */
  208. public: //----------------------------重写
  209. /// <summary>
  210. /// 解引用,取值
  211. /// </summary>
  212. /// <typeparam name="T"></typeparam>
  213. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-03
  214. inline T& operator * () const { return _pCurrentNode->Data; }
  215. /// <summary>
  216. /// 向前移动为正,向后移动为负
  217. /// </summary>
  218. /// <param name="iDiff"></param>
  219. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  220. inline virtual void Move(const int& iDiff)
  221. {
  222. if (iDiff == 0) return;
  223. if (iDiff > 0){
  224. if (_pCurrentNode == null) //已经是最后了,不能向后移了
  225. {
  226. return;
  227. }
  228. int n = 0;
  229. while (true){
  230. _pCurrentNode = _pCurrentNode->Next;
  231. ++n;
  232. if (n == iDiff){ return; }
  233. }
  234. }else{
  235. int n = 0;
  236. if (_pCurrentNode == null) {//向前移,减-1进入未尾元素
  237. _pCurrentNode = _pCurrentList->Last();
  238. n = -1; //已经向前移了一位
  239. if (n == iDiff) { return; }
  240. }
  241. while (true){
  242. _pCurrentNode = _pCurrentNode->Prev;
  243. --n;
  244. if (n == iDiff){ return; }
  245. }
  246. }
  247. throw "未重写代码?";
  248. }
  249. /// <summary>
  250. ///
  251. /// </summary>
  252. /// <param name="right"></param>
  253. /// <returns></returns>
  254. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  255. inline _DListNodeIterator operator+(const int& iDiff)const {
  256. _DListNodeIterator itResult(*this);
  257. itResult.Move(iDiff);
  258. return itResult;
  259. }
  260. /// <summary>
  261. ///
  262. /// </summary>
  263. /// <param name="right"></param>
  264. /// <returns></returns>
  265. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  266. inline _DListNodeIterator operator-(const int& iDiff)const {
  267. _DListNodeIterator itResult(*this);
  268. itResult.Move(-iDiff);
  269. return itResult;
  270. }
  271. /// <summary>
  272. ///
  273. /// </summary>
  274. /// <param name="r"></param>
  275. /// <returns></returns>
  276. /// 创建时间:2024-07-03 最后一次修改时间:2024-07-25
  277. inline int operator-(const _DListNodeIterator& r)const {
  278. int n1 = _pCurrentList->FindNodeIndex(_pCurrentNode);
  279. int n2 = _pCurrentList->FindNodeIndex(r._pCurrentNode);
  280. //assert(n1 != -1 && n2 != -1);
  281. //确保 _pCurrentList->end() - _pCurrentList->begin() == _pCurrentList->Count
  282. //超过边界,指针都设为指向最后一位元素的下一位
  283. if (n1 == -1) n1 = _pCurrentList->Count;
  284. if (n2 == -1) n2 = _pCurrentList->Count;
  285. return n1 - n2;
  286. }
  287. /// <summary>
  288. /// 前置加加
  289. /// </summary>
  290. /// <returns></returns>
  291. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  292. inline _DListNodeIterator& operator++() { Move(1); return *this; }
  293. /// <summary>
  294. /// 前置减减
  295. /// </summary>
  296. /// <returns></returns>
  297. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  298. inline _DListNodeIterator& operator--() { Move(-1); return *this; }
  299. /// <summary>
  300. /// 后置加加
  301. /// </summary>
  302. /// <param name=""></param>
  303. /// <returns></returns>
  304. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  305. inline _DListNodeIterator operator++(int) {
  306. _DListNodeIterator sResult(*this);
  307. Move(1);
  308. return sResult;
  309. }
  310. /// <summary>
  311. /// 后置减减
  312. /// </summary>
  313. /// <param name=""></param>
  314. /// <returns></returns>
  315. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  316. inline _DListNodeIterator operator--(int) {
  317. _DListNodeIterator sResult(*this);
  318. Move(-1);
  319. return sResult;
  320. }
  321. /// <summary>
  322. ///
  323. /// </summary>
  324. /// <param name="r"></param>
  325. /// <returns></returns>
  326. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  327. inline _DListNodeIterator& operator+=(const int& iDiff) {
  328. Move(iDiff);
  329. return *this;
  330. }
  331. /// <summary>
  332. ///
  333. /// </summary>
  334. /// <param name="r"></param>
  335. /// <returns></returns>
  336. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  337. inline _DListNodeIterator& operator-=(const int& iDiff) {
  338. Move(iDiff);
  339. return *this;
  340. }
  341. /// <summary>
  342. /// 关联代码:
  343. /// _DList<int> d = { 1,3,53,55,35,97,35,10 };
  344. /// d.end() - d.begin(); //8
  345. /// </summary>
  346. /// <param name="r"></param>
  347. /// <returns></returns>
  348. /// 创建时间:2024-07-03 最后一次修改时间:2024-07-03
  349. inline int operator-(const _DListNodeIterator& r)
  350. {
  351. //note1 到最未尾的索引
  352. //note2 到最未尾的索引
  353. int i1 = 0, i2 = 0;
  354. auto pNode1 = _pCurrentNode;
  355. auto pNode2 = r._pCurrentNode;
  356. //pNote1到结点未尾的距离
  357. while (pNode1 != null)
  358. {
  359. pNode1 = pNode1->Next;
  360. ++i1;
  361. }
  362. //pNote2到结点未尾的距离
  363. while (pNode2 != null)
  364. {
  365. pNode2 = pNode2->Next;
  366. ++i2;
  367. }
  368. // - (i1-i2) 值越小,离结点未越近,例如 null 结点,就是 0。
  369. return i2 - i1;
  370. }
  371. //-------------------------------如果是非线性表,下面的运算符重载也要重写
  372. inline bool operator!=(const _DListNodeIterator& r) { return this->_pCurrentNode != r._pCurrentNode; }
  373. inline bool operator==(const _DListNodeIterator& r) { return this->_pCurrentNode == r._pCurrentNode; }
  374. inline bool operator>(const _DListNodeIterator& r) { return this->_pCurrentNode > r._pCurrentNode; }
  375. inline bool operator<(const _DListNodeIterator& r) { return this->_pCurrentNode < r._pCurrentNode; }
  376. };
  377. /// <summary>
  378. ///
  379. /// </summary>
  380. /// <typeparam name="T"></typeparam>
  381. /// 创建时间: 2024-07-03 最后一修改时间:2024-07-03
  382. template<class T>
  383. class _DListNodeReverseIterator
  384. {
  385. public:
  386. using value_type = T;
  387. private:
  388. /// <summary>
  389. /// 当前节点
  390. /// </summary>
  391. _DListNode<T>* _pCurrentNode;
  392. /// <summary>
  393. /// 当前链表
  394. /// </summary>
  395. _DList<T>* _pCurrentList;
  396. public:
  397. inline _DListNodeReverseIterator()
  398. {
  399. _pCurrentList = null;
  400. _pCurrentNode = null;
  401. }
  402. inline _DListNodeReverseIterator(const _DList<T>* pCurrentList)
  403. {
  404. assert(pCurrentList != null);
  405. _pCurrentList = (_DList<T>*)pCurrentList;
  406. _pCurrentNode = null;
  407. }
  408. /// <summary>
  409. /// 构造函数,传值迭代器管理的值
  410. /// </summary>
  411. /// <param name="pNode"></param>
  412. inline _DListNodeReverseIterator(const _DListNode<T>* pCurrentNode, const _DList<T>* pCurrentList)
  413. {
  414. _pCurrentNode = (_DListNode<T>*)pCurrentNode;
  415. _pCurrentList = (_DList<T>*)pCurrentList;
  416. }
  417. inline _DListNodeReverseIterator(const _DListNodeReverseIterator& it)
  418. {
  419. _pCurrentNode = it._pCurrentNode;
  420. _pCurrentList = it._pCurrentList;
  421. }
  422. /// <summary>
  423. /// 解引用,取值
  424. /// </summary>
  425. /// <typeparam name="T"></typeparam>
  426. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-03
  427. inline T& operator * ()const { return _pCurrentNode->Data; }
  428. /// <summary>
  429. /// 前置加加
  430. /// </summary>
  431. /// <returns></returns>
  432. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  433. inline _DListNodeReverseIterator& operator++() { Move(1); return *this; }
  434. /// <summary>
  435. /// 前置减减
  436. /// </summary>
  437. /// <returns></returns>
  438. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  439. inline _DListNodeReverseIterator& operator--() { Move(-1); return *this; }
  440. /// <summary>
  441. /// 后置加加
  442. /// </summary>
  443. /// <param name=""></param>
  444. /// <returns></returns>
  445. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  446. inline _DListNodeReverseIterator operator++(int) {
  447. _DListNodeIterator sResult(*this);
  448. Move(1);
  449. return sResult;
  450. }
  451. /// <summary>
  452. /// 后置减减
  453. /// </summary>
  454. /// <param name=""></param>
  455. /// <returns></returns>
  456. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  457. inline _DListNodeReverseIterator operator--(int) {
  458. _DListNodeIterator sResult(*this);
  459. Move(-1);
  460. return sResult;
  461. }
  462. /// <summary>
  463. /// 向后移动为正,向前移动为负
  464. /// </summary>
  465. /// <param name="iDiff"></param>
  466. /// 创建时间:2024-07-02 最后一次修改时间:2024-07-02
  467. inline void Move(const int& iDiff)
  468. {
  469. assert(_pCurrentList != null);
  470. if (iDiff == 0) return;
  471. if (iDiff > 0) { //向后移动
  472. int n = 0;
  473. if (_pCurrentNode == null) { //不能向前移动
  474. _pCurrentNode = _pCurrentList->Last();
  475. n = 1; //已经向后移动了一位
  476. if (n == iDiff) { return; }
  477. }
  478. while (true) {
  479. _pCurrentNode = _pCurrentNode->Prev;
  480. ++n;
  481. if (n == iDiff) { return; }
  482. }
  483. }
  484. else {
  485. if (_pCurrentNode == null) { //不能向后移动
  486. return;
  487. }
  488. int n = 0;
  489. while (true) {
  490. _pCurrentNode = _pCurrentNode->Next;
  491. --n;
  492. if (n == iDiff) { return;}
  493. }
  494. }
  495. throw "未重写代码?";
  496. }
  497. inline bool operator!=(const _DListNodeReverseIterator& r) { return this->_pCurrentNode != r._pCurrentNode; }
  498. inline bool operator==(const _DListNodeReverseIterator& r) { return this->_pCurrentNode == r._pCurrentNode; }
  499. inline bool operator>(const _DListNodeReverseIterator& r) { return this->_pCurrentNode > r._pCurrentNode; }
  500. inline bool operator<(const _DListNodeReverseIterator& r) { return this->_pCurrentNode < r._pCurrentNode; }
  501. };
  502. /// <summary>
  503. /// 双向链表, 数据 T 要求能够比较大小,否则编译会出错。
  504. /// </summary>
  505. /// <typeparam name="T"></typeparam>
  506. template<class T>
  507. class _DList : public _Object
  508. {
  509. public:
  510. using value_type = T;
  511. using iterator_type = _DListNodeIterator<T>;
  512. protected:
  513. _DListNode<T>* _First; //第一个节点
  514. _DListNode<T>* _Last; //最后一个节点
  515. int _Count; //节点个数
  516. int _MaxBuffer; //双链表最大可以存储的元素个数
  517. _SortOrder _so; //排序顺序
  518. protected:
  519. /// <summary>
  520. /// 初台化数据
  521. /// </summary>
  522. /// <returns></returns>
  523. inline void InitData()
  524. {
  525. _Count = 0; _First = null; _Last = null; _MaxBuffer = 100000;
  526. _so = _SortOrder::s_null;
  527. }
  528. public: //---------------------------------------------------------------------------属性
  529. __declspec(property(get = GetSortOrder, put = SetSortOrder) ) _SortOrder SortOrder;
  530. const _SortOrder& GetSortOrder() const { return _so; }
  531. virtual void SetSortOrder(const _SortOrder so) { _so = so; }
  532. __declspec(property(get = GetCount)) const int Count;
  533. inline int GetCount() const { return _Count; }
  534. public:
  535. //------------------------------------------------------------构造与析构
  536. /// <summary>
  537. /// 默认构造函数
  538. /// </summary>
  539. /// <returns></returns>
  540. inline _DList()
  541. {
  542. InitData();
  543. }
  544. inline _DList(const _DList& dl)
  545. {
  546. //_cout << _t("inline _DList<T>::_DList(const _DList& dl)\n");
  547. InitData();
  548. _DListNode<T>* dn = dl.First();
  549. while (dn != null)
  550. {
  551. Add(dn->Data);
  552. dn = dn->Next;
  553. }
  554. }
  555. inline _DList(const std::vector<T>& v) {
  556. InitData();
  557. for (const T& t : v)
  558. {
  559. Add(t);
  560. }
  561. }
  562. inline _DList(const T& item)
  563. {
  564. InitData();
  565. Add(item);
  566. }
  567. /// <summary>
  568. /// 列表初始化 dList<int> idl = {1,2,3,4};
  569. /// </summary>
  570. /// <typeparam name="T"></typeparam>
  571. /// <param name="tList"></param>
  572. inline _DList(std::initializer_list<T> tList)
  573. {
  574. InitData();
  575. for (T t : tList) { Add(t); }
  576. }
  577. /// <summary>
  578. /// 析构函数
  579. /// </summary>
  580. inline virtual ~_DList()
  581. {
  582. //_cout << _t("inline _DList<T>::~_DList()\n");
  583. ClearData();
  584. }
  585. //------------------------------------------------------------属性
  586. /// <summary>
  587. /// 双链表最大可以存储的元素个数
  588. /// </summary>
  589. int MaxBuffer()const { return _MaxBuffer; }
  590. /// <summary>
  591. ///设定双链表最大可以存储的元素个数
  592. /// </summary>
  593. /// <param name="nCaptionty"></param>
  594. inline void MaxBuffer(int nCaptionty) { _MaxBuffer = nCaptionty; }
  595. inline int CSharp_Count() const { return _Count; }
  596. inline _DListNode<T>* First()const { return _First; }
  597. inline _DListNode<T>* Last()const { return _Last; }
  598. inline _DListNodeIterator<T> begin()const { return _DListNodeIterator<T>(_First,this); }
  599. inline _DListNodeReverseIterator<T> rbegin()const { return _DListNodeReverseIterator<T>(_Last,this); }
  600. /// <summary>
  601. ///
  602. /// </summary>
  603. /// <returns></returns>
  604. inline _DListNodeIterator<T> end()const
  605. {
  606. //迭代器使用的语句
  607. //for (_DListNodeIterator<int> f = dl.begin(); f != dl.end(); f++) { }
  608. return _DListNodeIterator<T>(null,this);
  609. }
  610. /// <summary>
  611. ///
  612. /// </summary>
  613. /// <returns></returns>
  614. inline _DListNodeReverseIterator<T> rend()const
  615. {
  616. //迭代器使用的语句
  617. //for (_DListNodeIterator<int> f = dl.begin(); f != dl.end(); f++) { }
  618. return _DListNodeReverseIterator<T>(null, this);
  619. }
  620. //-----------------------------------------------------------运算符重载
  621. /// <summary>
  622. /// 重载的下标操作符 []
  623. /// </summary>
  624. T& operator[](const int& nIndex) const { return IndexOfNode(nIndex)->Data; }
  625. /// <summary>
  626. /// 重载的下标操作符 =
  627. /// </summary>
  628. /// 创建时间: ????-??-?? 最后一次修改时间:2024-04-19
  629. inline _DList<T>& operator = (const _DList<T>& other)
  630. {
  631. if (this != &other)
  632. {
  633. ClearData();
  634. Add(other);
  635. }
  636. return *this;
  637. }
  638. /// <summary>
  639. /// 类型转换
  640. /// </summary>
  641. inline operator _string() const{ return ToString(); }
  642. /// <summary>
  643. /// 类型转换
  644. /// </summary>
  645. inline operator _stdstr() const { return ToString().Data; }
  646. //---------------------------------------------------------虚函数重写
  647. /// <summary>
  648. /// 是否存在 item
  649. /// </summary>
  650. inline virtual bool Contains(const T& item){ return BinarySearch(item) != -1; }
  651. /// <summary>
  652. /// 交换两个节点的数据
  653. /// </summary>
  654. /// <typeparam name="T"></typeparam>
  655. /// <param name="iIndex1"></param>
  656. /// <param name="iIndex2"></param>
  657. /// <returns></returns>
  658. inline bool SwapNodeData(const int& iIndex1, const int& iIndex2)
  659. {
  660. _DListNode<T>* pNode1, * pNode2;
  661. pNode1 = IndexOfNode(iIndex1);
  662. pNode2 = IndexOfNode(iIndex2);
  663. if (!(pNode1 != null && pNode2 != null))
  664. {
  665. return false;
  666. }
  667. T ptmp = pNode1->Data;
  668. pNode1->Data = pNode2->Data;
  669. pNode2->Data = ptmp;
  670. return true;
  671. }
  672. /// <summary>
  673. /// 选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是:第一次从待排序的数据元素中选出最小
  674. /// 或最大)的一个元素,存放在序列的起始位置,然后再从剩余的未排序元素中寻找到最小(大)元素,然后放到已排序
  675. /// 的序列的末尾。以此类推,直到全部待排序的数据元素的个数为零。选择排序是不稳定的排序方法。
  676. /// </summary>
  677. /// <typeparam name="T"></typeparam>
  678. /// <param name="sortord"></param>
  679. /// 创建时间: ???-??-?? 最后一次修改时间:???-??-?? 已测试
  680. inline virtual void Sort_Selection(const _SortOrder& sortord = _SortOrder::s_Minmax)
  681. {
  682. if (_Count == 0 || _Count == 1) return;
  683. _DListNode<T>* min = _First, * tmp = _First->Next;
  684. if (sortord == _SortOrder::s_Minmax) //从小到大
  685. {
  686. while (min->Next != null)
  687. {
  688. while (tmp != null)
  689. {
  690. if (tmp->Data < min->Data) //交换数据
  691. {
  692. T pt = tmp->Data;
  693. tmp->Data = min->Data;
  694. min->Data = pt;
  695. }
  696. tmp = tmp->Next;
  697. }
  698. min = min->Next;
  699. tmp = min->Next;
  700. }
  701. }
  702. else
  703. {
  704. while (min->Next != null)
  705. {
  706. while (tmp != null)
  707. {
  708. if (tmp->Data > min->Data)
  709. {
  710. T pt = tmp->Data;
  711. tmp->Data = min->Data;
  712. min->Data = pt;
  713. }
  714. tmp = tmp->Next;
  715. }
  716. min = min->Next;
  717. tmp = min->Next;
  718. }
  719. }
  720. this->_so = sortord; //已排序
  721. }
  722. /// <summary>
  723. /// 返回索引的节点
  724. /// </summary>
  725. inline virtual _DListNode<T>* IndexOfNode(const int& iPos)const
  726. {
  727. if (iPos < 0 || iPos >= _Count) //错误索引:
  728. {
  729. return NULL;
  730. }
  731. unsigned int nindex = 0;
  732. if (iPos > _Count / 2)
  733. {
  734. _DListNode<T>* pNode = _Last;
  735. while (pNode != null)
  736. {
  737. if (nindex++ == _Count - iPos - 1) { return pNode; }
  738. pNode = pNode->Prev;
  739. }
  740. }
  741. else
  742. {
  743. _DListNode<T>* pNode = _First;
  744. while (pNode != null)
  745. {
  746. if (nindex++ == iPos)
  747. {
  748. return pNode;
  749. }
  750. pNode = pNode->Next;
  751. }
  752. }
  753. return null;
  754. }
  755. /// <summary>
  756. /// 把索引为nIndex的节点移到最后
  757. /// </summary>
  758. /// <typeparam name="T"></typeparam>
  759. /// <param name="iIndex"></param>
  760. /// <returns></returns>
  761. inline virtual bool MoveLast(const int& iIndex)
  762. {
  763. _DListNode<T>* pNode = IndexOfNode(iIndex);
  764. if (pNode != null)
  765. {
  766. if (pNode == _Last)
  767. return true;
  768. if (pNode == _First) //此时最少两个节点
  769. {
  770. _First = _First->Next;
  771. _First->Prev = null;
  772. _Last->Next = pNode;
  773. pNode->Prev = _Last;
  774. _Last = pNode;
  775. }
  776. else
  777. {
  778. pNode->Prev->Next = pNode->Next;
  779. pNode->Next->Prev = pNode->Prev;
  780. _Last->Next = pNode;
  781. pNode->Prev = _Last;
  782. _Last = pNode;
  783. }
  784. return true;
  785. }
  786. return false;
  787. }
  788. /// <summary>
  789. /// 把节点移到最后
  790. /// </summary>
  791. /// 创建时间: 2022-02-18 最后一次修改时间:2022-02-18
  792. inline virtual bool MoveLast(_DListNode<T>* dnCurrent)
  793. {
  794. if (dnCurrent == null) return false;
  795. this->_so = _SortOrder::s_null;
  796. if (_Count == 0)
  797. {
  798. return false;
  799. }
  800. else if (_Count == 1)
  801. {
  802. return true;
  803. }
  804. else if (_Count == 2)
  805. {
  806. if (dnCurrent == _First) //交换_First 与 _Last 数据
  807. {
  808. T tmp = _First->Data;
  809. _First->Data = _Last->Data;
  810. _Last->Data = tmp;
  811. }
  812. return true;
  813. }
  814. else
  815. {
  816. if (dnCurrent == _First)
  817. {
  818. _First = _First->Next;
  819. _First->Prev = null;
  820. _Last->Next = dnCurrent;
  821. dnCurrent->Next = null;
  822. dnCurrent->Prev = _Last;
  823. _Last = dnCurrent;
  824. }
  825. else if (dnCurrent != _Last)
  826. {
  827. dnCurrent->Prev->Next = dnCurrent->Next;
  828. dnCurrent->Next->Prev = dnCurrent->Prev;
  829. _Last->Next = dnCurrent;
  830. dnCurrent->Next = null;
  831. dnCurrent->Prev = _Last;
  832. _Last = dnCurrent;
  833. }
  834. return true;
  835. }
  836. }
  837. /// <summary>
  838. /// 把索引为nIndex的节点移到最前
  839. /// </summary>
  840. /// <param name="iIndex"></param>
  841. inline virtual bool MoveFirst(const int& iIndex)
  842. {
  843. _DListNode<T>* pNode = IndexOfNode(iIndex);
  844. if (pNode != null)
  845. {
  846. if (pNode == _First)
  847. return true;
  848. if (pNode == _Last) //此时最少两个节点
  849. {
  850. _Last->Prev->Next = null;
  851. _Last = _Last->Prev;
  852. pNode->Prev = null;
  853. pNode->Next = _First;
  854. _First->Prev = pNode;
  855. _First = pNode;
  856. }
  857. else
  858. {
  859. pNode->Prev->Next = pNode->Next;
  860. pNode->Next->Prev = pNode->Prev;
  861. pNode->Next = _First;
  862. _First->Prev = pNode;
  863. pNode->Prev = null;
  864. _First = pNode;
  865. }
  866. return true;
  867. }
  868. return false;
  869. }
  870. /// <summary>
  871. /// 将指定集合的元素添加到末尾
  872. /// </summary>
  873. /// <typeparam name="T"></typeparam>
  874. /// <param name="item"></param>
  875. /// <returns></returns>
  876. inline virtual bool Add(const T& item)
  877. {
  878. if (_Count == 0)
  879. {
  880. //_First = new _DListNode<T>(item);
  881. _First = _Memory::New< _DListNode<T> >(1);
  882. _First->Data = item;
  883. _First->Next = null;
  884. _First->Prev = null;
  885. _Last = _First;
  886. }
  887. else
  888. {
  889. _DListNode<T>* pNew = _Memory::New< _DListNode<T> >(1);
  890. pNew->Data = item;
  891. pNew->Next = null;
  892. pNew->Prev = _Last;
  893. _Last->Next = pNew;
  894. _Last = pNew;
  895. }
  896. ++_Count;
  897. this->_so = _SortOrder::s_null; //要重新排序
  898. return true;
  899. }
  900. /// <summary>
  901. /// 添加一个链表
  902. /// </summary>
  903. inline void Add(const _DList<T>& dList)
  904. {
  905. assert(&dList != null);
  906. _DListNode<T>* pNode = dList._First;
  907. while (pNode != null)
  908. {
  909. Add(pNode->Data);
  910. pNode = pNode->Next;
  911. }
  912. }
  913. protected:
  914. /// <summary>
  915. /// 在结点pListItem前面插入一个结点,成功,返回新的结点,否则返回null
  916. /// </summary>
  917. /// <typeparam name="T"></typeparam>
  918. /// <param name="pListItem"></param>
  919. /// <param name="rData"></param>
  920. /// <returns></returns>
  921. inline virtual _DListNode<T>* InserNodeFront(_DListNode<T>* pListItem, const T& rData)
  922. {
  923. assert(pListItem != null);
  924. _DListNode<T>* pNode = _Memory::New<_DListNode<T>>(1);
  925. pNode->Data = rData;
  926. //pNode
  927. pNode->Next = pListItem;
  928. pNode->Prev = pListItem->Prev;
  929. //--pListItem->Prev
  930. if (pListItem->Prev != null)
  931. {
  932. pListItem->Prev->Next = pNode;
  933. }
  934. else
  935. {
  936. _First = pNode;
  937. }
  938. //pListItem
  939. pListItem->Prev = pNode;
  940. ++_Count;
  941. return pNode;
  942. }
  943. /// <summary>
  944. /// 在结点pListItem后面插入一个结点,成功,返回新的结点,否则返回0;
  945. /// </summary>
  946. /// <typeparam name="T"></typeparam>
  947. /// <param name="pListItem"></param>
  948. /// <param name="rData"></param>
  949. /// <returns></returns>
  950. inline virtual _DListNode<T>* InserNodeBack(_DListNode<T>* pListItem, const T& rData)
  951. {
  952. if (pListItem == null) return null;
  953. _DListNode<T>* pNode = _Memory::New<_DListNode<T>>(1);
  954. pNode->Data = rData;
  955. //pNode
  956. pNode->Prev = pListItem;
  957. pNode->Next = pListItem->Next;
  958. //pListItem->Next
  959. if (pListItem->Next != null)
  960. {
  961. pListItem->Next->Prev = pNode;
  962. }
  963. else
  964. {
  965. _Last = pNode;
  966. }
  967. //--pListItem
  968. pListItem->Next = pNode;
  969. ++_Count;
  970. return pNode;
  971. }
  972. //------------------------------------------------------------操作
  973. public:
  974. /// <summary>
  975. /// 如果已排好序,它会按二分法查找,否则它会普通查找。
  976. /// </summary>
  977. /// <param name="item"></param>
  978. /// <returns></returns>
  979. /// 创建时间: ????-??-?? 最后一次修改时间:????_??_?? 已测试
  980. inline int BinarySearch(const T& item)const {
  981. switch (this->_so)
  982. {
  983. case _SortOrder::s_Maxmin:
  984. {
  985. if (_Count == 0)
  986. {
  987. return -1;
  988. }
  989. if (_Count == 1)
  990. {
  991. //return (_First.Data as IComparable).CompareTo(item) == 0 ? 0 : -1; //C#
  992. return _First->Data == item ? 0 : -1;
  993. }
  994. if (_Count == 2)
  995. {
  996. //if ((_First.Data as IComparable).CompareTo(item) == 0) return 0;
  997. //if ((_First.Next.Data as IComparable).CompareTo(item) == 0) return 1;
  998. if (_First->Data == item) return 0;
  999. if (_First->Next->Data == item) return 1;
  1000. return -1;
  1001. }
  1002. int nPos = (int)_Count / 2; //nPos在中间,所以无素一定要大于等于3才行
  1003. int nLeft = 0; //左边远素
  1004. int nRight = (int)_Count - 1; //右边远素
  1005. _DListNode<T>* pNode;
  1006. while (nRight >= 0 && nLeft >= 0)
  1007. {
  1008. pNode = IndexOfNode(nPos);
  1009. //int iCom = (item as IComparable).CompareTo(ld.Data);
  1010. if (item > pNode->Data)
  1011. {
  1012. if (nRight == nLeft || nPos == nLeft)
  1013. {
  1014. return -1;
  1015. }
  1016. nRight = nPos - 1;
  1017. }
  1018. else if (item < pNode->Data)
  1019. {
  1020. if (nRight == nLeft || nPos == nRight)
  1021. {
  1022. return -1;
  1023. }
  1024. nLeft = nPos + 1;
  1025. }
  1026. else
  1027. {
  1028. return nPos;
  1029. }
  1030. nPos = nLeft + (nRight - nLeft + 1) / 2;
  1031. }
  1032. break;
  1033. }
  1034. case _SortOrder::s_Minmax:
  1035. {
  1036. if (_Count == 0)
  1037. {
  1038. return -1;
  1039. }
  1040. if (_Count == 1)
  1041. {
  1042. //return (_First.Data as IComparable).CompareTo(item) == 0 ? 0 : -1;
  1043. return _First->Data == item ? 0 : -1;
  1044. }
  1045. if (_Count == 2)
  1046. {
  1047. //if ((_First.Data as IComparable).CompareTo(item) == 0) return 0;
  1048. //if ((_First.Next.Data as IComparable).CompareTo(item) == 0) return 1;
  1049. if (_First->Data == item) return 0;
  1050. if (_First->Next->Data == item) return 1;
  1051. return -1;
  1052. }
  1053. int nPos = (int)_Count / 2; //nPos在中间,所以无素一定要大于等于3才行
  1054. int nLeft = 0; //左边远素
  1055. int nRight = (int)_Count - 1; //右边远素
  1056. _DListNode<T>* pNode = null;
  1057. while (nRight >= 0 && nLeft >= 0)
  1058. {
  1059. pNode = IndexOfNode(nPos);
  1060. //int iCom = (item as IComparable).CompareTo(ld.Data);
  1061. if (item < pNode->Data)
  1062. {
  1063. if (nRight == nLeft || nPos == nLeft)
  1064. {
  1065. return -1;
  1066. }
  1067. nRight = nPos - 1;
  1068. }
  1069. else if (item > pNode->Data)
  1070. {
  1071. if (nRight == nLeft || nPos == nRight)
  1072. {
  1073. return -1;
  1074. }
  1075. nLeft = nPos + 1;
  1076. }
  1077. else
  1078. {
  1079. return nPos;
  1080. }
  1081. nPos = nLeft + (nRight - nLeft + 1) / 2;
  1082. }
  1083. break;
  1084. }
  1085. case _SortOrder::s_null:
  1086. {
  1087. _DListNode<T>* pNode = _First;
  1088. int iCount = 0;
  1089. while (pNode != null)
  1090. {
  1091. if (pNode->Data == item)
  1092. {
  1093. return iCount;
  1094. }
  1095. pNode = pNode->Next;
  1096. ++iCount;
  1097. }
  1098. break;
  1099. }
  1100. default:
  1101. {
  1102. return -1;
  1103. }
  1104. }
  1105. return -1;
  1106. }
  1107. /// <summary>
  1108. /// 清除节点,并释放内存
  1109. /// </summary>
  1110. /// <typeparam name="T"></typeparam>
  1111. /// 创建时间: ????-??-?? 最后一次修改时间:2022-12-24 (已测试)
  1112. inline void ClearData() override { ClearMemory(); }
  1113. /// <summary>
  1114. /// 清除节点,并释放内存
  1115. /// </summary>
  1116. inline void ClearMemory() override
  1117. {
  1118. _Count = 0;
  1119. _DListNode<T>* dn = _First;
  1120. while (dn != null)
  1121. {
  1122. if (dn->Prev != null)
  1123. _Memory::Delete< _DListNode<T> >(dn->Prev, 1);
  1124. dn = dn->Next;
  1125. }
  1126. if (_Last != null)
  1127. _Memory::Delete< _DListNode<T> >(_Last, 1);
  1128. _First = null;
  1129. _Last = null;
  1130. this->_so = _SortOrder::s_null;
  1131. }
  1132. /// <summary>
  1133. /// 复制一个链表,清除原来链表的数据
  1134. /// </summary>
  1135. /// <typeparam name="T"></typeparam>
  1136. /// <param name="ld"></param>
  1137. inline void CopyFrom(const _DList<T>& ld)
  1138. {
  1139. if (this != &ld)
  1140. {
  1141. ClearData();
  1142. _DListNode<T>* pNode = ld._First;
  1143. while (pNode != null)
  1144. {
  1145. Add(pNode->Data);
  1146. pNode = pNode->Next;
  1147. }
  1148. }
  1149. }
  1150. /// <summary>
  1151. /// 移除指定索引处的元素
  1152. /// </summary>
  1153. /// <typeparam name="T">类型</typeparam>
  1154. /// <param name="iIndex">要移除的元素的从零开始的索引。</param>
  1155. /// <returns>成功返回true,否则返回false</returns>
  1156. /// 创建时间:????-??-?? 最后一次修改时间:2023-04-08
  1157. inline bool RemoveAt(const int& iIndex)
  1158. {
  1159. if (iIndex < 0 || iIndex >= _Count) return false;
  1160. _DListNode<T>* pNode = _First;
  1161. int iCount = 0;
  1162. while (pNode != null)
  1163. {
  1164. if (iCount == iIndex)
  1165. {
  1166. if (pNode == _First)
  1167. {
  1168. if (_First->Next == null) //只有一个
  1169. {
  1170. ClearData();
  1171. return true;
  1172. }
  1173. else
  1174. {
  1175. _First = _First->Next;
  1176. _First->Prev = null;
  1177. --_Count;
  1178. _Memory::Delete< _DListNode<T> >(pNode, 1);
  1179. return true;
  1180. }
  1181. }
  1182. else if (pNode == _Last)
  1183. {
  1184. if (_Last->Prev == null)
  1185. {
  1186. ClearData();
  1187. return true;
  1188. }
  1189. else
  1190. {
  1191. _Last = _Last->Prev;
  1192. _Last->Next = null;
  1193. --_Count;
  1194. _Memory::Delete< _DListNode<T>>(pNode, 1);
  1195. return true;
  1196. }
  1197. }
  1198. else
  1199. {
  1200. pNode->Prev->Next = pNode->Next;
  1201. pNode->Next->Prev = pNode->Prev;
  1202. --_Count;
  1203. _Memory::Delete< _DListNode<T>>(pNode, 1);
  1204. return true;
  1205. }
  1206. }
  1207. pNode = pNode->Next;
  1208. ++iCount;
  1209. }
  1210. return false;
  1211. }
  1212. /// <summary>
  1213. /// 查找一项数据
  1214. /// </summary>
  1215. inline _DListNode<T>* FindNodeItem(const T& item)const
  1216. {
  1217. switch (this->_so)
  1218. {
  1219. case _SortOrder::s_Maxmin:
  1220. {
  1221. if (_Count == 0)
  1222. {
  1223. return null;
  1224. }
  1225. if (_Count == 1)
  1226. {
  1227. //return (_First.Data as IComparable).CompareTo(item) == 0 ? _First : null;
  1228. return _First->Data == item ? _First : null;
  1229. }
  1230. if (_Count == 2)
  1231. {
  1232. //if ((_First.Data as IComparable).CompareTo(item) == 0) return _First;
  1233. //if ((_First.Next.Data as IComparable).CompareTo(item) == 0) return _First.Next;
  1234. if (_First->Data == item) return _First;
  1235. if (_First->Next->Data == item) return _First->Next;
  1236. return null;
  1237. }
  1238. int nPos = (int)(_Count / 2); //nPos在中间,所以无素一定要大于等于3才行
  1239. int nLeft = 0; //左边远素
  1240. int nRight = (int)(_Count - 1); //右边远素
  1241. _DListNode<T>* pNode = null;
  1242. while (nRight >= 0 && nLeft >= 0)
  1243. {
  1244. pNode = IndexOfNode(nPos);
  1245. //int iCom = (item as IComparable).CompareTo(ld.Data);
  1246. if (item > pNode->Data)
  1247. {
  1248. if (nRight == nLeft || nPos == nLeft)
  1249. {
  1250. return null;
  1251. }
  1252. nRight = nPos - 1;
  1253. }
  1254. else if (item < pNode->Data)
  1255. {
  1256. if (nRight == nLeft || nPos == nRight)
  1257. {
  1258. return null;
  1259. }
  1260. nLeft = nPos + 1;
  1261. }
  1262. else
  1263. {
  1264. return pNode;
  1265. }
  1266. nPos = nLeft + (nRight - nLeft + 1) / 2;
  1267. }
  1268. break;
  1269. }
  1270. case _SortOrder::s_Minmax:
  1271. {
  1272. if (_Count == 0)
  1273. {
  1274. return null;
  1275. }
  1276. if (_Count == 1)
  1277. {
  1278. //return (_First.Data as IComparable).CompareTo(item) == 0 ? _First : null;
  1279. return _First->Data == item ? _First : null;
  1280. }
  1281. if (_Count == 2)
  1282. {
  1283. //if ((_First.Data as IComparable).CompareTo(item) == 0) return _First;
  1284. //if ((_First.Next.Data as IComparable).CompareTo(item) == 0) return _First.Next;
  1285. if (_First->Data == item) return _First;
  1286. if (_First->Next->Data == item) return _First->Next;
  1287. return null;
  1288. }
  1289. int nPos = (int)(_Count / 2); //nPos在中间,所以无素一定要大于等于3才行
  1290. int nLeft = 0; //左边远素
  1291. int nRight = (int)(_Count - 1); //右边远素
  1292. _DListNode<T>* pNode = null;
  1293. while (nRight >= 0 && nLeft >= 0)
  1294. {
  1295. pNode = IndexOfNode(nPos);
  1296. //int iCom = (item as IComparable).CompareTo(ld.Data);
  1297. if (item < pNode->Data)
  1298. {
  1299. if (nRight == nLeft || nPos == nLeft)
  1300. {
  1301. return null;
  1302. }
  1303. nRight = nPos - 1;
  1304. }
  1305. else if (item > pNode->Data)
  1306. {
  1307. if (nRight == nLeft || nPos == nRight)
  1308. {
  1309. return null;
  1310. }
  1311. nLeft = nPos + 1;
  1312. }
  1313. else
  1314. {
  1315. return pNode;
  1316. }
  1317. nPos = nLeft + (nRight - nLeft + 1) / 2;
  1318. }
  1319. break;
  1320. }
  1321. case _SortOrder::s_null:
  1322. {
  1323. _DListNode<T>* pNode = _First;
  1324. while (pNode != null)
  1325. {
  1326. //总结:Equals比较的永远是变量的内容是否相同,而= =比较的则是引用地址是否相同(前提:此种类型内部没有对Equals 或= = 进行重写操作,
  1327. //否则输出可能会有不同)。string 类型是个特例,因为他的内部对这两个都进行了重写。
  1328. if (item == pNode->Data)
  1329. {
  1330. return pNode;
  1331. }
  1332. /*
  1333. if (pNode.Data.Equals(item))
  1334. {
  1335. return pNode;
  1336. }
  1337. */
  1338. pNode = pNode->Next;
  1339. }
  1340. break;
  1341. }
  1342. default:
  1343. {
  1344. return null;
  1345. }
  1346. }
  1347. return null;
  1348. }
  1349. /// <summary>
  1350. /// 在链接中查找节点,返回节点的索引
  1351. /// </summary>
  1352. /// <param name="pFind"></param>
  1353. /// <returns></returns>
  1354. /// 创建时间:2024-07-03 最后一次修改时间:2024-07-03
  1355. inline int FindNodeIndex(const _DListNode<T>* pFind)const
  1356. {
  1357. if (pFind != null){
  1358. _DListNode<T>* pNode = _First;
  1359. int nIndex = 0;
  1360. while (pNode != null){
  1361. if (pNode == pFind)
  1362. return nIndex;
  1363. pNode = pNode->Next;
  1364. ++nIndex;
  1365. }
  1366. }
  1367. return -1;
  1368. }
  1369. /// <summary>
  1370. /// 删除链表中的数据
  1371. /// </summary>
  1372. /// <typeparam name="T"></typeparam>
  1373. /// <param name="pData"></param>
  1374. /// <returns></returns>
  1375. inline bool RemoveItem(const T& pData)
  1376. {
  1377. if (this->_so == _SortOrder::s_null)
  1378. {
  1379. _DListNode<T>* pNode = _First;
  1380. while (pNode != null)
  1381. {
  1382. if (pNode->Data == pData) //找到一项
  1383. {
  1384. if (pNode == _First) //删除的是第一个节点
  1385. {
  1386. if (_First->Next != null)
  1387. {
  1388. _First->Next->Prev = null;
  1389. _First = _First->Next;
  1390. }
  1391. else
  1392. {
  1393. _First = null;
  1394. _Last = null;
  1395. }
  1396. }
  1397. else if (pNode == _Last) //删除的是最后一个节点
  1398. {
  1399. if (_Last->Prev != null)
  1400. {
  1401. _Last->Prev->Next = null;
  1402. _Last = _Last->Prev;
  1403. }
  1404. else
  1405. {
  1406. _Last = null;
  1407. _Last = null;
  1408. }
  1409. }
  1410. else //删除的是中间的一个节点
  1411. {
  1412. pNode->Next->Prev = pNode->Prev;
  1413. pNode->Prev->Next = pNode->Next;
  1414. }
  1415. --_Count;
  1416. return true;
  1417. }
  1418. pNode = pNode->Next;
  1419. }
  1420. return false;
  1421. }
  1422. else
  1423. {
  1424. return RemoveAt(BinarySearch(pData));
  1425. }
  1426. }
  1427. /// <summary>
  1428. /// 删除最后一个节点
  1429. /// </summary>
  1430. /// 创建时间:2020-09-12 最后一次修改时间: 2023-01-18
  1431. inline bool DeleteLast()
  1432. {
  1433. auto pDelete = Last();
  1434. if (_Count <= 0)
  1435. {
  1436. return false;
  1437. }
  1438. else if (_Count == 1)
  1439. {
  1440. _First = null;
  1441. _Last = null;
  1442. _Count = 0;
  1443. }
  1444. else if (_Count == 2)
  1445. {
  1446. _Last = _First;
  1447. _Last->Prev = null;
  1448. _Last->Next = null;
  1449. _First->Next = null;
  1450. _First->Prev = null;
  1451. --_Count;
  1452. }
  1453. else
  1454. {
  1455. _Last->Prev->Next = null;
  1456. _Last = _Last->Prev;
  1457. --_Count;
  1458. }
  1459. _Memory::Delete<_DListNode<T>>(pDelete, 1);
  1460. return true;
  1461. }
  1462. /// <summary>
  1463. /// 添加一个元素,并把它放在首位,其它元素后移,如果后面的元素删除,总无素个数不变,如果元素个数为零,则添加一个无素。
  1464. /// </summary>
  1465. /// <param name="Item"></param>
  1466. /// <param name="bRemoveLast"></param>
  1467. /// 创建时间: 2022-04-19 最后一次修改时间:2022-04-19
  1468. inline void HistoryAdd(const T& Item, bool bRemoveLast)
  1469. {
  1470. if (_Count == 0) {
  1471. Add(Item);
  1472. }
  1473. else if (_Count == 1) {
  1474. if (bRemoveLast) {
  1475. _First->Data = Item;
  1476. }
  1477. else {
  1478. //把无素放在最前
  1479. _DListNode<T>* dnNew = _Memory::New<_DListNode<T>>(1);
  1480. dnNew->Data = Item;
  1481. _First->Prev = dnNew;
  1482. dnNew->Next = _First;
  1483. _First = dnNew;
  1484. ++_Count;
  1485. }
  1486. }
  1487. else {
  1488. if (bRemoveLast) {
  1489. _DListNode<T>* dnDelete = _Last;
  1490. //删除最后一个元素
  1491. _Last = _Last->Prev;
  1492. _Last->Next = null;
  1493. _DListNode<T>* dnNew = _Memory::New<_DListNode<T>>(1);
  1494. dnNew->Data = Item;
  1495. _First->Prev = dnNew;
  1496. dnNew->Next = _First;
  1497. dnNew->Prev = null;
  1498. _First = dnNew;
  1499. _Memory::Delete< _DListNode<T>>(dnDelete, 1);
  1500. }
  1501. else {
  1502. //把无素放在最前
  1503. _DListNode<T>* dnNew = _Memory::New<_DListNode<T>>(1);
  1504. dnNew->Data = Item;
  1505. _First->Prev = dnNew;
  1506. dnNew->Next = _First;
  1507. dnNew->Prev = null;
  1508. _First = dnNew;
  1509. ++_Count;
  1510. }
  1511. }
  1512. }
  1513. /// <summary>
  1514. /// 出栈(先进后出),删除最后一个元素,
  1515. /// </summary>
  1516. /// 创建时间: 2022-04-19 最后一次修改时间:2023-04-06
  1517. inline T StackPop()
  1518. {
  1519. assert(_Count > 0);
  1520. T tResult = _Last->Data;
  1521. this->RemoveAt(_Count - 1);
  1522. return tResult;
  1523. }
  1524. //----------------------------------------------------------------------------------------------------Python List方法
  1525. /// <summary>
  1526. /// 将元素tItem添加到列表末尾。
  1527. /// </summary>
  1528. /// 创建时间: 2023-04-08 最后一次修改时间:2023-04-08
  1529. inline void Python_append(const T& tItem)
  1530. {
  1531. Add(tItem);
  1532. }
  1533. /// <summary>
  1534. /// 在位置nIndex后面插入一个元素tItem
  1535. /// </summary>
  1536. inline void Python_insert(const int nIndex, const T& tItem)
  1537. {
  1538. InserNodeBack(IndexOfNode(nIndex), tItem);
  1539. }
  1540. /// <summary>
  1541. /// 在最前面插入一项
  1542. /// </summary>
  1543. /// <param name="tItem"></param>
  1544. /// 创建时间: 2024-04-16 最后一次修改时间:2024-04-16
  1545. inline void Inseart_front(const T& tItem)
  1546. {
  1547. _DListNode<T>* pNode = _Memory::New<_DListNode<T>>(1);
  1548. pNode->Data = tItem;
  1549. if (_First != null)
  1550. {
  1551. _First->Prev = pNode;
  1552. pNode->Next = _First;
  1553. pNode->Prev = null;
  1554. _First = pNode;
  1555. }
  1556. else
  1557. {
  1558. pNode->Next = null;
  1559. pNode->Prev = null;
  1560. _First = pNode;
  1561. _Last = pNode;
  1562. }
  1563. ++_Count;
  1564. }
  1565. /// <summary>
  1566. /// 删除索引位置为nIndex的元素,并返回删除的元素值的拷贝,如果索引值nIndex = -1,则默认删除为最后一个元素。
  1567. /// </summary>
  1568. /// <param name="nIndex"></param>
  1569. /// <returns></returns>
  1570. /// 创建时间: 2023-04-08 最后一次修改时间:2023-04-09
  1571. inline T Python_pop(const int nIndex = -1)
  1572. {
  1573. if (nIndex == -1)
  1574. {
  1575. return StackPop();
  1576. }
  1577. else
  1578. {
  1579. auto dn = this->IndexOfNode(nIndex);
  1580. T tmp = T();
  1581. if (dn != null)
  1582. {
  1583. tmp = dn->Data;
  1584. DeleteNode(dn);
  1585. }
  1586. return tmp;
  1587. }
  1588. }
  1589. /// <summary>
  1590. /// 删除值为tItem的一项。
  1591. /// 注意,方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,
  1592. /// 就需要使用循环来确保每个值都删除。
  1593. /// </summary>
  1594. inline bool Python_remove(const T& tItem)
  1595. {
  1596. int n = _Count;
  1597. DeleteNode(FindNodeItem(tItem));
  1598. return n == _Count;
  1599. }
  1600. protected:
  1601. /// <summary>
  1602. /// 删除链表中的节点
  1603. /// </summary>
  1604. /// <typeparam name="T"></typeparam>
  1605. /// <param name="pListItem"></param>
  1606. /// <returns></returns>
  1607. //inline bool DeleteNode(const _DListNode<T>* pNodeDelete)
  1608. //{
  1609. // if (_Count == 0 || pNodeDelete == null)
  1610. // {
  1611. // return false;
  1612. // }
  1613. // _DListNode<T>* pNode = _First.Next;
  1614. // while (pNode != null)
  1615. // {
  1616. // if (pNode == pNodeDelete) //找到了
  1617. // {
  1618. // //pListItem->Prev
  1619. // if (pNodeDelete->Prev != null)
  1620. // {
  1621. // pNodeDelete->Prev->Next = pNodeDelete->Next;
  1622. // }
  1623. // else //删除的是第一个节点
  1624. // {
  1625. // _First = pNodeDelete.Next;
  1626. // }
  1627. // //pListItem->Next
  1628. // if (pNodeDelete->Next != null)
  1629. // {
  1630. // pNodeDelete->Next->Prev = pNodeDelete->Prev;
  1631. // }
  1632. // else //删除的是最后一个节点
  1633. // {
  1634. // _Last = pNodeDelete->Prev;
  1635. // }
  1636. // break;
  1637. // }
  1638. // pNode = pNode->Next;
  1639. // }
  1640. // if (pNode != null)
  1641. // {
  1642. // --_Count;
  1643. // _Memory::Delete< _DListNode<T> >(pNode,1); //C#不用清除内存
  1644. // return true;
  1645. // }
  1646. // return false;
  1647. //}
  1648. /// <summary>
  1649. /// 删除链表中的某一节点,注意,这个节点一定要是在链表中的。
  1650. /// </summary>
  1651. /// 创建时间: 2023-04-09 最后一次修改时间:2023-04-09
  1652. inline void DeleteNode(_DListNode<T>* pNodeDelete)
  1653. {
  1654. if (_Count == 0 || pNodeDelete == null) return;
  1655. if (_Count == 1)
  1656. {
  1657. ClearMemory();
  1658. }
  1659. else
  1660. {
  1661. if (pNodeDelete == _First) //删除的是第一个节点
  1662. {
  1663. _First = _First->Next;
  1664. _First->Prev = null;
  1665. }
  1666. else if (pNodeDelete == _Last)
  1667. {
  1668. _Last = _Last->Prev;
  1669. _Last->Next = null;
  1670. }
  1671. else
  1672. {
  1673. pNodeDelete->Prev->Next = pNodeDelete->Next;
  1674. pNodeDelete->Next->Prev = pNodeDelete->Prev;
  1675. }
  1676. --_Count;
  1677. _Memory::Delete< _DListNode<T> >(pNodeDelete, 1);
  1678. }
  1679. }
  1680. public:
  1681. //---------------------------------------------------------- C++
  1682. inline void Push_back(const T& TypeObject) { Add(TypeObject); }
  1683. public: //------------------------------------------------------------------重写
  1684. /// <summary>
  1685. /// 转换为字符串
  1686. /// </summary>
  1687. /// <returns></returns>
  1688. /// 创建时间: 2023-05-16 最后一次修改时间:2024-07-23
  1689. inline virtual _string ToSplitString(const _string& sSplitString) const override
  1690. {
  1691. //不可以这样: const _DList<_Object>* dp = (_DList<_Object> *)pList;
  1692. //就算 T 类型数据是从 _Object中继承也不可以。
  1693. _string sResult;
  1694. sResult.Add(_t("{"));
  1695. _string sp = sSplitString.Length == 0 ? _string(_t(",")) : sSplitString;
  1696. if(_Count > 0){
  1697. //是否继承处_Object
  1698. if (std::is_base_of<_Object, T>::value)
  1699. {
  1700. _Object* po;
  1701. if (_Count == 1)
  1702. {
  1703. po = (_Object*)(&_First->Data);
  1704. sResult.Add((_string)(*po));
  1705. }
  1706. _DListNode<T>* dn = this->_First;
  1707. while (dn != _Last)
  1708. {
  1709. po = (_Object*)(&dn->Data);
  1710. sResult.Add((_string)(*po));
  1711. sResult.Add(sp);
  1712. dn = dn->Next;
  1713. }
  1714. po = (_Object*)(&_Last->Data);
  1715. sResult.Add((_string)(*po));
  1716. }
  1717. else
  1718. {
  1719. if (typeid(T) == typeid(int))
  1720. {
  1721. _DListNode<T>* dn = this->_First;
  1722. while (dn != _Last)
  1723. {
  1724. int* pInt = (int*)&(dn->Data);
  1725. sResult.Add(_string::Java_valueOf(*pInt));
  1726. sResult.Add(sp);
  1727. dn = dn->Next;
  1728. }
  1729. if (this->Count > 0)
  1730. sResult.Add(_string::Java_valueOf(*((int*)(&(_Last->Data)))));
  1731. }
  1732. else if(typeid(T) == typeid(size_t))
  1733. {
  1734. _DListNode<T>* dn = this->_First;
  1735. while (dn != _Last)
  1736. {
  1737. size_t* pInt = (size_t*)&(dn->Data);
  1738. sResult.Add(_string::Java_valueOf(*pInt));
  1739. sResult.Add(sp);
  1740. dn = dn->Next;
  1741. }
  1742. if (this->Count > 0)
  1743. sResult.Add(_string::Java_valueOf(*((size_t*)(&(_Last->Data)))));
  1744. }
  1745. else if (typeid(T) == typeid(double))
  1746. {
  1747. if (_Count == 0) return sResult;
  1748. double* pd;
  1749. if (_Count == 1)
  1750. {
  1751. pd = (double*)(&(_First->Data));
  1752. sResult.Add(_Convert::DoubleToString(*pd));
  1753. }
  1754. _DListNode<T>* dn = this->_First;
  1755. while (dn != _Last)
  1756. {
  1757. pd = (double*)&(dn->Data);
  1758. sResult.Add(_Convert::DoubleToString(*pd));
  1759. sResult.Add(sp);
  1760. dn = dn->Next;
  1761. }
  1762. pd = (double*)&(_Last->Data);
  1763. sResult.Add(_Convert::DoubleToString(*pd));
  1764. }
  1765. else if (typeid(T) == typeid(_string))
  1766. {
  1767. _string* ps;
  1768. _DListNode<T>* dn = this->_First;
  1769. while (dn != _Last)
  1770. {
  1771. ps = (_string*)(&dn->Data);
  1772. sResult.Add(_t("\""));
  1773. sResult.Add(*ps);
  1774. sResult.Add(_t("\""));
  1775. sResult.Add(sp);
  1776. dn = dn->Next;
  1777. }
  1778. if (_Count > 0)
  1779. {
  1780. ps = (_string*)(&_Last->Data);
  1781. sResult.Add(_t("\""));
  1782. sResult.Add(*ps);
  1783. sResult.Add(_t("\""));
  1784. }
  1785. }
  1786. else if (typeid(T) == typeid(_StrA))
  1787. {
  1788. _StrA* ps;
  1789. if (_Count <= 0)
  1790. {
  1791. }
  1792. else if (_Count == 1)
  1793. {
  1794. ps = (_StrA*)(&_First->Data);
  1795. sResult.Add(_t("\""));
  1796. sResult.Add(*ps);
  1797. sResult.Add(_t("\""));
  1798. }
  1799. else
  1800. {
  1801. _DListNode<T>* dn = this->_First;
  1802. while (dn != _Last)
  1803. {
  1804. ps = (_StrA*)(&dn->Data);
  1805. sResult.Add(_t("\""));
  1806. sResult.Add(*ps);
  1807. sResult.Add(_t("\""));
  1808. sResult.Add(_t(','));
  1809. dn = dn->Next;
  1810. }
  1811. ps = (_StrA*)(&_Last->Data);
  1812. sResult.Add(_t("\""));
  1813. sResult.Add(*ps);
  1814. sResult.Add(_t("\""));
  1815. }
  1816. }
  1817. else //所有继承自 _Object 类的数据类型都可以
  1818. {
  1819. sResult.Add(_t("_LDList::ToSplitString重写,数据类型为:"));
  1820. sResult.Add(_string(typeid(T).name()));
  1821. }
  1822. }
  1823. }
  1824. sResult.Add(_t("}\n"));
  1825. return sResult;
  1826. }
  1827. inline _string ToString()const
  1828. {
  1829. return ToSplitString(_t(""));
  1830. }
  1831. }; //--------------------------------------------------------------------------DList
  1832. //-----------------------------------------------------------------------SortedDList
  1833. /// <summary>
  1834. /// C#语句:public class SortList<T> : _DList<T>
  1835. /// </summary>
  1836. /// <typeparam name="T"></typeparam>
  1837. template<class T>
  1838. class SortedDList : public _DList<T>
  1839. {
  1840. };
  1841. //----------------------------------------------------------------------StringList
  1842. template<class T>
  1843. class _StrList : public _DList<T>
  1844. {
  1845. public:
  1846. //------------------------------------------------------------------构造与析构
  1847. _StrList() : _DList<T>() {};
  1848. _StrList(const _StrList<T>& sl) : _DList<T>(sl) {};
  1849. explicit _StrList(const _char* pStr, const _char* pSplit, bool bIgnoreEmptyString = false)
  1850. {
  1851. this->InitData();
  1852. SplitForSeparator(pStr, pSplit, bIgnoreEmptyString);
  1853. }
  1854. /// <summary>
  1855. /// 要加上 explicit阻止自动转换, 否则执行语名会自动调用这个构造函数 _StrList ls = { L"AA",L"BB"};
  1856. /// </summary>
  1857. /// <param name="sText"></param>
  1858. /// <param name="sSplit"></param>
  1859. /// <param name="bIgnoreEmptyString"></param>
  1860. explicit _StrList(const T& sText, const T& sSplit, bool bIgnoreEmptyString = false)
  1861. {
  1862. //assert(sText != null && sSplit != null);
  1863. //SplitForSeparator(sText.Data, sSplit.Data, bIgnoreEmptyString);
  1864. if (sText.Length == 0) { return; }
  1865. if (sSplit.Length == 0) { Add(sText); return; }
  1866. int iStart = 0;
  1867. int iIndex = sText.IndexOf(sSplit, iStart);
  1868. if (iIndex == -1)
  1869. {
  1870. Add(sText);
  1871. return;
  1872. }
  1873. while (iIndex != -1 && iStart + 1 <= sText.Length)
  1874. {
  1875. if (iIndex != iStart)
  1876. Add(sText.SubStr(iStart, iIndex - iStart));
  1877. else
  1878. {
  1879. if (!bIgnoreEmptyString) Add(T());
  1880. }
  1881. iStart = iIndex + sSplit.Length;
  1882. iIndex = sText.IndexOf(sSplit, iStart);
  1883. if (iIndex == -1 && sText.Length != iStart)
  1884. Add(sText.SubStr(iStart, sText.Length - iStart)); //拷贝最后一个
  1885. }
  1886. }
  1887. _StrList(std::initializer_list<T> aList)
  1888. {
  1889. for (T s : aList)
  1890. {
  1891. Add(s);
  1892. }
  1893. }
  1894. public:
  1895. //------------------------------------------------------------------操作
  1896. void writeToFile(const T& sFullPathName)
  1897. {
  1898. }
  1899. bool readToFile(const T& sFullPathName)
  1900. {
  1901. return false;
  1902. }
  1903. bool readToUnicodeFile(const T& sFileName, const T& sSplit)
  1904. {
  1905. return false;
  1906. }
  1907. /// <summary>
  1908. /// 获取所有字符串的总共长度
  1909. /// </summary>
  1910. /// <returns></returns>
  1911. /// 创建时间: 2022-11-05 最后一次修改时间: 2022-11-05
  1912. int GetStringLength() const
  1913. {
  1914. int iSum = 0;
  1915. auto dn = this->_First;
  1916. while (dn != null) {
  1917. iSum += (int)dn->Data.Length;
  1918. dn = dn->Next;
  1919. }
  1920. return iSum;
  1921. }
  1922. T connectForSeparator(const T& sConnector)const
  1923. {
  1924. if (this->_Count == 0) return _t("");
  1925. if (this->_Count == 1) return this->_First->Data;
  1926. T tmp(_t(""), GetStringLength() + sConnector.Length * this->_Count + 100);
  1927. _DListNode<T>* ldNode = this->_First;
  1928. while (ldNode != this->_Last)
  1929. {
  1930. tmp += ldNode->Data;
  1931. tmp += sConnector;
  1932. ldNode = ldNode->Next;
  1933. }
  1934. tmp += this->_Last->Data; //加入最后一项
  1935. return tmp;
  1936. }
  1937. T connectForSeparator(const _char& cConnector) const
  1938. {
  1939. return connectForSeparator(&cConnector);
  1940. }
  1941. /// <summary>
  1942. /// 返回分隔后的字符串列表
  1943. /// </summary>
  1944. /// <param name="pStr">原文本</param>
  1945. /// <param name="pSplit">分隔字符串</param>
  1946. /// <param name="bIgnoreEmptyString">是否忽略空字符串</param>
  1947. /// <returns>返回一个字符串列表</returns>
  1948. /// 创建时间: 2022-10-04 最后一修改时间:2022-10-05 已测试
  1949. _StrList<T>& SplitForSeparator(const _char* pStr, const _char* pSplit, bool bIgnoreEmptyString)
  1950. {
  1951. if (pStr == null || pSplit == null)
  1952. {
  1953. _cout << "在中_StrList::SplitForSeparator中" << L"pStr == null || pSplit == null" << "\n";
  1954. throw "pStr == null || pSplit == null";
  1955. }
  1956. this->ClearData();
  1957. if (pStr[0] == 0 || pSplit[0] == 0)
  1958. {
  1959. Add(_t(""), 0, 0);
  1960. return *this;
  1961. }
  1962. int i = 0;
  1963. int j = 0;
  1964. int nStart = 0, nEnd = 0;
  1965. while (pStr[i] != 0) {
  1966. // _cout << _t("pStr[i] = ") << pStr[i] << _t("\n"); //此句出错,无任何提示
  1967. bool bFind = true;
  1968. j = 0;
  1969. while (pSplit[j] != 0) {
  1970. if (pStr[i + j] != pSplit[j]) {
  1971. bFind = false;
  1972. break;
  1973. }
  1974. ++j;
  1975. }
  1976. if (bFind) {
  1977. nEnd = i - 1; //此处如果 nEnd 是 int ,则 nEnd = i - 1 => nEnd = 18446744073709551615 => 溢出错误
  1978. if (bIgnoreEmptyString) {
  1979. if (nStart <= nEnd) {
  1980. Add(pStr, nStart, nEnd); //这里应nStart <= nEnd,而不是 nStart < nEnd,因为当 nStart = nEnd 还是有一个字符的
  1981. }
  1982. }
  1983. else {
  1984. Add(pStr, nStart, nEnd);
  1985. }
  1986. nStart = i + j;
  1987. i = nStart - 1;
  1988. }
  1989. ++i;
  1990. }
  1991. //拷贝右边最后一项
  1992. if (bIgnoreEmptyString) {
  1993. if (nStart <= i - 1) {
  1994. Add(pStr, nStart, (int)i - 1);
  1995. }
  1996. }
  1997. else {
  1998. Add(pStr, nStart, (int)i - 1);
  1999. }
  2000. return *this;
  2001. }
  2002. //int SplitForSeparator(const T& sText, const _char& cSplit);
  2003. int IndexOf(const T& s)
  2004. {
  2005. int nIndex = 0;
  2006. auto dn = this->_First;
  2007. while (dn)
  2008. {
  2009. if (dn->Data == s) { return nIndex; }
  2010. ++nIndex;
  2011. dn = dn->Next;
  2012. }
  2013. return -1;
  2014. }
  2015. int GetMaxSpaceLength(int nTabCount = 9, int nChineseCharactersCount = 3)const
  2016. {
  2017. int nMax = 0;
  2018. auto dn = this->_First;
  2019. while (dn)
  2020. {
  2021. int n = gs.s_length_space(dn->Data.Data, nTabCount, nChineseCharactersCount);
  2022. if (n > nMax) nMax = n;
  2023. dn = dn->Next;
  2024. }
  2025. return nMax;
  2026. }
  2027. /// <summary>
  2028. /// 用tab键使每行等长
  2029. /// </summary>
  2030. /// <param name="nTabCount"></param>
  2031. /// <returns></returns>
  2032. /// 创建时间: 2022-10-30 最后一次修改时间: 2022-10-30
  2033. T equilongTabLine(int nTabCount = 9)
  2034. {
  2035. this->RemoveHeadTab();
  2036. int iMax = GetMaxSpaceLength(nTabCount);
  2037. for (T& s : *this) {
  2038. int nSapceLength = gs.s_length_space(s.Data);
  2039. int n = (iMax - nSapceLength) / 9;
  2040. if (iMax - nSapceLength - n * 9 >= 5)
  2041. ++n;
  2042. else
  2043. --n;
  2044. for (int j = 0; j < n; j++)
  2045. {
  2046. s.Add(_t("\t"));
  2047. }
  2048. }
  2049. return connectForSeparator(_t('\n')) + _t('\n');
  2050. }
  2051. /// <summary>
  2052. /// 计算所有行,如果每行都有开始处都有 \t ,则每行除去 \t , 除去个数以最少 \t 行为准。
  2053. /// 例:
  2054. /// \t\t abc
  2055. /// \t bcd
  2056. /// \t\t\t dd
  2057. /// 运行函数后变成
  2058. /// \t abc
  2059. /// bcd
  2060. /// \t\t dd
  2061. /// </summary>
  2062. /// 创建时间: 2022-11-05 最后一次修改时间: 2022-11-05
  2063. void RemoveHeadTab()
  2064. {
  2065. int iMin = 100000000;
  2066. for (T& s : *this) {
  2067. if (s.Length > 0)
  2068. {
  2069. int iCount = gs.s_headTabCount(s.Data);
  2070. if (iCount < iMin) { iMin = iCount; }
  2071. //log::d("iCount=" + iCount.ToString(), s);
  2072. }
  2073. }
  2074. //log::d("iMin=" + iMin.ToString());
  2075. if (iMin > 0)
  2076. {
  2077. for (T& s : *this) {
  2078. s = s.SubStr(iMin, s.Length - iMin);
  2079. }
  2080. }
  2081. }
  2082. //-----------------------------------------------------------------------虚函数
  2083. virtual bool Add(const T& item) override
  2084. {
  2085. //_cout << item << "\n";
  2086. return _DList<T>::Add(item);
  2087. }
  2088. void Add(const _StrList<T>& ls) //覆盖 父类重载函数 virtual bool Add(const T& item);
  2089. {
  2090. _DListNode<T>* pNode = ls._First;
  2091. while (pNode != null)
  2092. {
  2093. Add(pNode->Data);
  2094. pNode = pNode->Next;
  2095. }
  2096. }
  2097. bool Add(const T& str, int nStartPos, int nEndPos)
  2098. {
  2099. if (str.Length == 0 || nEndPos - nStartPos < 0)
  2100. {
  2101. Add(T());
  2102. }
  2103. else {
  2104. int nLength = nEndPos - nStartPos + 1;
  2105. /*
  2106. _Mem<_char> m(nLength + 1);
  2107. for (int i = 0; i < nLength; ++i)
  2108. {
  2109. m.Data[i] = pStr[nStartPos + i];
  2110. }
  2111. m.Data[nLength] = 0;
  2112. */
  2113. Add(str.SubStr(nStartPos, nLength));
  2114. }
  2115. return false;
  2116. }
  2117. _string ToSplitString(const _string& sSplitString) const override
  2118. {
  2119. /*
  2120. * auto dn = this->_First;
  2121. T sResult;
  2122. while (dn != this->_Last)
  2123. {
  2124. sResult.std_append(dn->Data);
  2125. sResult.std_append(sConnector);
  2126. dn = dn->Next;
  2127. }
  2128. if (this->_Last != null)
  2129. {
  2130. sResult.std_append(dn->Data);
  2131. }
  2132. char c = '\n';
  2133. sResult.std_append(c );
  2134. return sResult;
  2135. */
  2136. return _DList<T>::ToSplitString(sSplitString);
  2137. }
  2138. };
  2139. /// <summary>
  2140. /// 不重复的字符串列表
  2141. /// </summary>
  2142. /// <typeparam name="T"></typeparam>
  2143. /// 创建时间: 2023-05-11 最后一次修改时间:2023-05-11
  2144. template<class T>
  2145. class _UStrList : public _StrList<T>
  2146. {
  2147. public:
  2148. /// <summary>
  2149. /// 加入一个串,如果这个串存在,则把这项移到最后。
  2150. /// </summary>
  2151. /// <param name="item"></param>
  2152. /// 创建时间: ????-??-?? 最后一次修改时间:2023-05-11 (已测试)
  2153. bool Add(const T& item)override
  2154. {
  2155. bool bFind = false;
  2156. _DListNode<T>* dnTemp = this->_First;
  2157. while (dnTemp != null)
  2158. {
  2159. if (dnTemp->Data == item)
  2160. {
  2161. this->MoveLast(dnTemp);
  2162. return false;
  2163. }
  2164. dnTemp = dnTemp->Next;
  2165. }
  2166. _StrList<T>::Add(item);
  2167. return true;
  2168. }
  2169. };
  2170. /// <summary>
  2171. /// 值都是唯一的字符串列表, Case Insensitive(不区分大小写)
  2172. /// </summary>
  2173. template<class T>
  2174. class _UStrListCI : public _StrList<T>
  2175. {
  2176. public:
  2177. /// <summary>
  2178. /// 加入一个串,如果这个串存在,则把这项移到最后。
  2179. /// </summary>
  2180. /// <param name="item"></param>
  2181. /// 创建时间: ????-??-?? 最后一次修改时间:2022-02-18
  2182. bool Add(const T& item)override
  2183. {
  2184. bool bFind = false;
  2185. _DListNode<T>* dnTemp = this->_First;
  2186. while (dnTemp != null)
  2187. {
  2188. if (dnTemp->Data.CSharp_ToLower() == item.CSharp_ToLower())
  2189. {
  2190. this->MoveLast(dnTemp);
  2191. return false;
  2192. }
  2193. dnTemp = dnTemp->Next;
  2194. }
  2195. _StrList<T>::Add(item);
  2196. return true;
  2197. }
  2198. /*
  2199. /// <summary>
  2200. /// 返回前面一个值
  2201. /// </summary>
  2202. /// <param name="tCurrValue"></param>
  2203. /// <returns></returns>
  2204. T& GetForward(T& sCurr)
  2205. {
  2206. int iIndex = BinarySearch(sCurr);
  2207. if (iIndex != -1)
  2208. {
  2209. if (iIndex + 1 < _Count)
  2210. return this[iIndex + 1];
  2211. }
  2212. return null;
  2213. }
  2214. /// <summary>
  2215. /// 返回后面的一个值
  2216. /// </summary>
  2217. /// <param name="sCurr"></param>
  2218. /// <returns></returns>
  2219. T& GetBack(T& sCurr)
  2220. {
  2221. int iIndex = BinarySearch(sCurr);
  2222. if (iIndex != -1)
  2223. {
  2224. if (iIndex - 1 < _Count && iIndex - 1 >= 0)
  2225. return this[iIndex - 1];
  2226. }
  2227. return null;
  2228. }
  2229. */
  2230. };
  2231. /// <summary>
  2232. /// 已排序好的字符串列表
  2233. /// </summary>
  2234. template<class T>
  2235. class _SStrList : public _StrList<T>
  2236. {
  2237. public:
  2238. _SStrList(const _SortOrder so = _SortOrder::s_Minmax)
  2239. {
  2240. this->_so = so;
  2241. if (this->_so == _SortOrder::s_null)
  2242. {
  2243. throw _t("排序不能为空!");
  2244. }
  2245. }
  2246. //-------------------------------------------------------------------------------重写
  2247. /// <summary>
  2248. /// 快速添加字符串,差不多用了8个小时(两天),才写成。
  2249. /// </summary>
  2250. /// <param name="s"></param>
  2251. /// <returns></returns>
  2252. bool Add(const T& item) override
  2253. {
  2254. if (this->_Count < 3)
  2255. {
  2256. bool bInsert = false;
  2257. _DListNode<T>* ld = this->_First;
  2258. while (ld != null)
  2259. {
  2260. if (this->_so == _SortOrder::s_Maxmin)
  2261. {
  2262. if (item >= ld->Data)
  2263. {
  2264. _StrList<T>::InserNodeFront(ld, item); bInsert = true;
  2265. break;
  2266. }
  2267. }
  2268. else
  2269. {
  2270. if (item <= ld->Data)
  2271. {
  2272. _StrList<T>::InserNodeFront(ld, item); bInsert = true; break;
  2273. }
  2274. }
  2275. ld = ld->Next;
  2276. }
  2277. if (!bInsert) _StrList<T>::Add(item);
  2278. return true;
  2279. }
  2280. int nPos = this->_Count / 2; //nPos在中间,所以无素一定要大于等于3才行
  2281. int nLeft = 0; //左边远素
  2282. int nRight = this->_Count - 1; //右边远素
  2283. if (this->_so == _SortOrder::s_Maxmin)
  2284. {
  2285. _DListNode<T>* ld = null;
  2286. while (nRight >= 0 && nLeft >= 0)
  2287. {
  2288. ld = this->IndexOfNode(nPos);
  2289. if (item >= ld->Data)
  2290. {
  2291. if (nRight == nLeft || nPos == nLeft)
  2292. {
  2293. this->InserNodeFront(ld, item);
  2294. return true;
  2295. }
  2296. nRight = nPos - 1;
  2297. }
  2298. else
  2299. {
  2300. if (nRight == nLeft || nPos == nRight)
  2301. {
  2302. this->InserNodeBack(ld, item);
  2303. return true;
  2304. }
  2305. nLeft = nPos + 1;
  2306. }
  2307. nPos = nLeft + (nRight - nLeft + 1) / 2;
  2308. }
  2309. }
  2310. else
  2311. {
  2312. _DListNode<T>* ld = null;
  2313. while (nRight >= 0 && nLeft >= 0)
  2314. {
  2315. ld = this->IndexOfNode(nPos);
  2316. if (item <= ld->Data)
  2317. {
  2318. if (nRight == nLeft || nPos == nLeft)
  2319. {
  2320. this->InserNodeFront(ld, item);
  2321. return true;
  2322. }
  2323. nRight = nPos - 1;
  2324. }
  2325. else
  2326. {
  2327. if (nRight == nLeft || nPos == nRight)
  2328. {
  2329. this->InserNodeBack(ld, item);
  2330. return true;
  2331. }
  2332. nLeft = nPos + 1;
  2333. }
  2334. nPos = nLeft + (nRight - nLeft + 1) / 2;
  2335. }
  2336. }
  2337. return true;
  2338. }
  2339. /// <summary>
  2340. /// 确定某元素是否在列表中
  2341. /// </summary>
  2342. /// <param name="item">查找的对象。对于引用类型,该值可以为 null。</param>
  2343. /// <returns>如果在列表中找到 item,则为 true,否则为 false。</returns>
  2344. bool Contains(const T& item) override
  2345. {
  2346. if (this->_Count == 0) return false;
  2347. if (this->_Count == 1) return this->_First->Data == item;
  2348. if (this->_Count == 2) return this->_First->Data == item || this->_First->Next->Data == item;
  2349. int nPos = (int)this->_Count / 2; //nPos在中间,所以无素一定要大于等于3才行
  2350. int nLeft = 0; //左边远素
  2351. int nRight = (int)this->_Count - 1; //右边远素
  2352. _DListNode<T>* ld = null;
  2353. if (this->_so == _SortOrder::s_Maxmin)
  2354. {
  2355. while (nRight >= 0 && nLeft >= 0)
  2356. {
  2357. ld = this->IndexOfNode(nPos);
  2358. int iCom = item.CompareTo(ld->Data);
  2359. if (iCom > 0)
  2360. {
  2361. if (nRight == nLeft || nPos == nLeft)
  2362. {
  2363. return false;
  2364. }
  2365. nRight = nPos - 1;
  2366. }
  2367. else if (iCom < 0)
  2368. {
  2369. if (nRight == nLeft || nPos == nRight)
  2370. {
  2371. return false;
  2372. }
  2373. nLeft = nPos + 1;
  2374. }
  2375. else
  2376. {
  2377. return true;
  2378. }
  2379. nPos = nLeft + (nRight - nLeft + 1) / 2;
  2380. }
  2381. }
  2382. else
  2383. {
  2384. while (nRight >= 0 && nLeft >= 0)
  2385. {
  2386. ld = this->IndexOfNode(nPos);
  2387. int iCom = item.CompareTo(ld->Data);
  2388. if (iCom < 0)
  2389. {
  2390. if (nRight == nLeft || nPos == nLeft)
  2391. {
  2392. return false;
  2393. }
  2394. nRight = nPos - 1;
  2395. }
  2396. else if (iCom > 0)
  2397. {
  2398. if (nRight == nLeft || nPos == nRight)
  2399. {
  2400. return false;
  2401. }
  2402. nLeft = nPos + 1;
  2403. }
  2404. else
  2405. {
  2406. return true;
  2407. }
  2408. nPos = nLeft + (nRight - nLeft + 1) / 2;
  2409. }
  2410. }
  2411. return false;
  2412. }
  2413. };
  2414. /// <summary>
  2415. /// 值都是唯一的字符串列表,且已排好序,区分大小写
  2416. /// </summary>
  2417. template<class T>
  2418. class _SUStrList : public _SStrList<T>
  2419. {
  2420. public:
  2421. bool Add(const T& item)override
  2422. {
  2423. if (this->Contains(item))
  2424. return false;
  2425. return _SStrList<T>::Add(item);
  2426. }
  2427. _SUStrList(_SortOrder so = _SortOrder::s_Minmax) : _SStrList<T>(so)
  2428. {
  2429. }
  2430. };
  2431. /// <summary>
  2432. /// UStringListCI_值都是唯一,且已排序的字符串列表,不区分大小写
  2433. /// </summary>
  2434. template<class T>
  2435. class _SUStrListUI : public _SUStrList<T>
  2436. {
  2437. public:
  2438. _SUStrListUI(_SortOrder st) : _SUStrList<T>(st)
  2439. {
  2440. }
  2441. /// <summary>
  2442. /// 确定某元素是否在列表中
  2443. /// </summary>
  2444. /// <param name="item">查找的对象。对于引用类型,该值可以为 null。</param>
  2445. /// <returns>如果在列表中找到 item,则为 true,否则为 false。</returns>
  2446. bool Contains(const T& item) override
  2447. {
  2448. if (this->_Count == 0) return false;
  2449. if (this->_Count == 1) return this->_First->Data.ToLower().CompareTo(item.ToLower()) == 0;
  2450. if (this->_Count == 2) return this->_First->Data.ToLower().CompareTo(item.ToLower()) == 0 || this->_First->Next->Data.ToLower().CompareTo(item.ToLower()) == 0;
  2451. int nPos = (int)this->_Count / 2; //nPos在中间,所以无素一定要大于等于3才行
  2452. int nLeft = 0; //左边远素
  2453. int nRight = (int)this->_Count - 1; //右边远素
  2454. _DListNode<T>* ld = null;
  2455. if (this->_so == _SortOrder::s_Maxmin)
  2456. {
  2457. while (nRight >= 0 && nLeft >= 0)
  2458. {
  2459. ld = this->IndexOfNode(nPos);
  2460. int iCom = item.ToLower().CompareTo(ld->Data.ToLower());
  2461. if (iCom > 0)
  2462. {
  2463. if (nRight == nLeft || nPos == nLeft)
  2464. {
  2465. return false;
  2466. }
  2467. nRight = nPos - 1;
  2468. }
  2469. else if (iCom < 0)
  2470. {
  2471. if (nRight == nLeft || nPos == nRight)
  2472. {
  2473. return false;
  2474. }
  2475. nLeft = nPos + 1;
  2476. }
  2477. else
  2478. {
  2479. return true;
  2480. }
  2481. nPos = nLeft + (nRight - nLeft + 1) / 2;
  2482. }
  2483. }
  2484. else
  2485. {
  2486. while (nRight >= 0 && nLeft >= 0)
  2487. {
  2488. ld = this->IndexOfNode(nPos);
  2489. int iCom = item.ToLower().CompareTo(ld->Data.ToLower());
  2490. if (iCom < 0)
  2491. {
  2492. if (nRight == nLeft || nPos == nLeft)
  2493. {
  2494. return false;
  2495. }
  2496. nRight = nPos - 1;
  2497. }
  2498. else if (iCom > 0)
  2499. {
  2500. if (nRight == nLeft || nPos == nRight)
  2501. {
  2502. return false;
  2503. }
  2504. nLeft = nPos + 1;
  2505. }
  2506. else
  2507. {
  2508. return true;
  2509. }
  2510. nPos = nLeft + (nRight - nLeft + 1) / 2;
  2511. }
  2512. }
  2513. return false;
  2514. }
  2515. };
  2516. _LF_END_
  2517. #endif

2014-07-25更新

 /// <summary>
 /// 
 /// </summary>
 /// <param name="r"></param>
 /// <returns></returns>
 /// 创建时间:2024-07-03    最后一次修改时间:2024-07-25
 inline int operator-(const _DListNodeIterator& r)const {

     int n1 = _pCurrentList->FindNodeIndex(_pCurrentNode);
     int n2 = _pCurrentList->FindNodeIndex(r._pCurrentNode);

     //assert(n1 != -1 && n2 != -1);

     //确保 _pCurrentList->end() - _pCurrentList->begin() == _pCurrentList->Count
     //超过边界,指针都设为指向最后一位元素的下一位
     if (n1 == -1)  n1 = _pCurrentList->Count;
     if (n2 == -1)  n2 = _pCurrentList->Count;

 
     return n1 - n2; 
 }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/948491
推荐阅读
相关标签
  

闽ICP备14008679号