Advertisement
xiahanlu

list

May 15th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.68 KB | None | 0 0
  1. /* simple list set - moecmks .*.**/
  2. #if !defined (LIST_C_H)
  3. # define LIST_C_H
  4.  
  5. # define __NULLPTR (void *) 0
  6. # define list_bool int
  7. # define list_true 1
  8. # define list_false 0
  9.  
  10. # include <malloc.h>
  11. # include <assert.h>
  12.  
  13. struct list_v {
  14. struct list_v *level_n;
  15. struct list_v *level_p;
  16. void *obj;
  17. };
  18. struct list_ {
  19. struct list_v *set;
  20. int nums;
  21. };
  22. static
  23. void list_init (struct list_ **s) {
  24.  
  25. struct list_ *u = (struct list_ *) malloc (sizeof (struct list_));
  26.  
  27. u->nums =0;
  28. u->set = __NULLPTR;
  29. *s = u;
  30. }
  31. static
  32. struct list_v *list_get (struct list_ *s, int pos) {
  33.  
  34. int id;
  35. struct list_v *u = __NULLPTR;
  36. assert (s != __NULLPTR);
  37.  
  38. if (pos == -1)
  39. pos = s->nums - 1;
  40. if (pos <= (s->nums - 1))
  41. for (id =0, u = s->set; id != pos; id++)
  42. u =u->level_n;
  43. else ;
  44. return u;
  45. }
  46. static
  47. void *list_getnode (struct list_ *s, int pos) {
  48.  
  49. struct list_v *p= list_get (s, pos);
  50. if (p) return p->obj;
  51. else return __NULLPTR;
  52. }
  53. static
  54. int list_insert (struct list_ *s, int pos, void *obj) {
  55.  
  56. struct list_v *u = __NULLPTR;
  57. struct list_v *v;
  58. assert (s != __NULLPTR);
  59.  
  60. if (pos == -1) pos = s->nums;
  61. if (pos > (s->nums)) return -1;
  62.  
  63. u = (struct list_v *) malloc (sizeof (struct list_v));
  64. u->obj = obj;
  65.  
  66. if (s->nums == 0) {
  67. u->level_n = u;
  68. u->level_p = u;
  69. s->set = u;
  70. s->nums++;
  71. return 0;
  72. }
  73.  
  74. v = list_get (s, pos == s->nums ?0: pos);
  75. u->level_n = v;
  76. u->level_p = v->level_p;
  77. v->level_p->level_n = u;
  78. v->level_p = u;
  79.  
  80. if (pos ==0)
  81. s->set = u;
  82. s->nums++;
  83. return 0;
  84. }
  85. static
  86. int list_insert_tail (struct list_ *s, void *obj) {
  87.  
  88. struct list_v *u;
  89. assert (s != __NULLPTR);
  90.  
  91. u = (struct list_v *) malloc (sizeof (struct list_v));
  92. u->obj = obj;
  93.  
  94. if (s->nums == 0) {
  95. u->level_n = u;
  96. u->level_p = u;
  97. s->set = u;
  98. } else {
  99. u->level_n = s->set;
  100. u->level_p = s->set->level_p;
  101. s->set->level_p->level_n = u;
  102. s->set->level_p = u;
  103. }
  104. s->nums++;
  105. return 0;
  106. }
  107. static
  108. int list_insert_tail_L (struct list_ *s, struct list_v *u) {
  109.  
  110. // struct list_v *u;
  111. assert (s != __NULLPTR);
  112. if (s->nums == 0) {
  113. u->level_n = u;
  114. u->level_p = u;
  115. s->set = u;
  116. } else {
  117. u->level_n = s->set;
  118. u->level_p = s->set->level_p;
  119. s->set->level_p->level_n = u;
  120. s->set->level_p = u;
  121. }
  122. s->nums++;
  123. return 0;
  124. }
  125. static
  126. int list_insert_L (struct list_ *s, int pos, struct list_v *liobj) {
  127.  
  128. struct list_v *u = liobj;
  129. struct list_v *v;
  130. assert (s != __NULLPTR);
  131.  
  132. if (pos == -1)
  133. pos = s->nums;
  134. if (pos > (s->nums))
  135. return -1;
  136. if (s->nums == 0) {
  137. u->level_n = u;
  138. u->level_p = u;
  139. s->set = u;
  140. s->nums++;
  141. return 0;
  142. }
  143.  
  144. v = list_get (s, pos == s->nums ?0: pos);
  145. u->level_n = v;
  146. u->level_p = v->level_p;
  147. v->level_p->level_n = u;
  148. v->level_p = u;
  149.  
  150. if (pos ==0)
  151. s->set = u;
  152. s->nums++;
  153. return 0;
  154. }
  155. static
  156. void list_single_free (struct list_v *sv) {
  157.  
  158. if (sv != __NULLPTR) {
  159. free (sv);
  160. }
  161. }
  162. static
  163. struct list_v *list_pop (struct list_ *s, int pos) {
  164.  
  165. struct list_v *u = __NULLPTR;
  166. struct list_v *v;
  167. assert (s != __NULLPTR);
  168.  
  169. if (pos == -1)
  170. pos = s->nums - 1;
  171. if (pos >= (s->nums) || (s->nums <= 0))
  172. return __NULLPTR;
  173. v = list_get (s, pos);
  174. u = v;
  175.  
  176. if (pos ==0)
  177. s->set = v->level_n;
  178. v->level_p->level_n = v->level_n;
  179. v->level_n->level_p = v->level_p;
  180.  
  181. if ( (-- s->nums) == 0)
  182. s->set = __NULLPTR;
  183. return v;
  184. }
  185. static
  186. void *list_pop2 (struct list_ *s, int pos) {
  187.  
  188. struct list_v *u = __NULLPTR;
  189. struct list_v *v;
  190. void *obj;
  191. assert (s != __NULLPTR);
  192.  
  193. if (pos == -1)
  194. pos = s->nums - 1;
  195. if (pos >= (s->nums) || (s->nums <= 0))
  196. return __NULLPTR;
  197. v = list_get (s, pos);
  198. u = v;
  199.  
  200. if (pos ==0)
  201. s->set = v->level_n;
  202. v->level_p->level_n = v->level_n;
  203. v->level_n->level_p = v->level_p;
  204.  
  205. if ( (-- s->nums) == 0)
  206. s->set = __NULLPTR;
  207. obj = v->obj;
  208. free (v);
  209. return obj;
  210. }
  211. static
  212. void list_remove_ISQ (struct list_ *s, int pos, void (*pernode_)(void *node)) {
  213.  
  214. struct list_v *u = __NULLPTR;
  215. struct list_v *v;
  216. assert (s != __NULLPTR);
  217.  
  218. if (pos == -1)
  219. pos = s->nums - 1;
  220. if (pos >= (s->nums) || (s->nums <= 0))
  221. return ;
  222. v = list_get (s, pos);
  223. u = v;
  224.  
  225. if (pos ==0)
  226. s->set = v->level_n;
  227. v->level_p->level_n = v->level_n;
  228. v->level_n->level_p = v->level_p;
  229.  
  230. if (pernode_ != __NULLPTR)
  231. pernode_ (& v->obj);
  232. free (v);
  233. if ( (-- s->nums) == 0)
  234. s->set = __NULLPTR;
  235. }
  236. static
  237. void list_remove_ISQ_L (struct list_ *s, struct list_v *c, void (*pernode_)(void *node)) {
  238.  
  239. struct list_v *u = __NULLPTR;
  240. assert (s != __NULLPTR);
  241.  
  242. if ((s->nums <= 0))
  243. return ;
  244. if (s->set ==c)
  245. s->set = c->level_n;
  246. c->level_p->level_n = c->level_n;
  247. c->level_n->level_p = c->level_p;
  248.  
  249. if (pernode_ != __NULLPTR)
  250. pernode_ (& c->obj);
  251. free (c);
  252. if ( (-- s->nums) == 0)
  253. s->set = __NULLPTR;
  254. }
  255. static
  256. void list_pop_L (struct list_ *s, struct list_v *c) {
  257.  
  258. struct list_v *u = __NULLPTR;
  259. assert (s != __NULLPTR);
  260.  
  261. if ((s->nums <= 0))
  262. return ;
  263. if (s->set ==c)
  264. s->set = c->level_n;
  265.  
  266. c->level_p->level_n = c->level_n;
  267. c->level_n->level_p = c->level_p;
  268.  
  269. if ( (-- s->nums) == 0)
  270. s->set = __NULLPTR;
  271. }
  272. static
  273. void *list_pop_L2 (struct list_ *s, struct list_v *c) {
  274.  
  275. struct list_v *u = __NULLPTR;
  276. void *no;
  277. assert (s != __NULLPTR);
  278.  
  279. if ((s->nums <= 0))
  280. return __NULLPTR;
  281. if (s->set ==c)
  282. s->set = c->level_n;
  283.  
  284. c->level_p->level_n = c->level_n;
  285. c->level_n->level_p = c->level_p;
  286.  
  287. if ( (-- s->nums) == 0)
  288. s->set = __NULLPTR;
  289. no = c->obj;
  290. free (c);
  291. return no;
  292. }
  293. static int
  294. list_retidx (struct list_ *s, struct list_v *litemv) {
  295.  
  296. if (s && litemv) {
  297. int id;
  298. struct list_v *c = s->set;
  299. for ( id= 0; id != s->nums; id++) {
  300. if ( c == litemv)
  301. return id;
  302. c= c->level_n;
  303. }
  304. }
  305. return -1;
  306. }
  307. static int
  308. list_retidx2 (struct list_ *s, void *node) {
  309.  
  310. if (s && node) {
  311. int id;
  312. struct list_v *c = s->set;
  313. for ( id= 0; id != s->nums; id++) {
  314. if ( c->obj == node)
  315. return id;
  316. c= c->level_n;
  317. }
  318. }
  319. return -1;
  320. }
  321. static
  322. void list_remove (struct list_ *s, int pos) {
  323.  
  324. list_remove_ISQ (s, pos, __NULLPTR);
  325. }
  326. static
  327. void list_remove_ISQ2 (struct list_ *s, void (*pernode_)(void *base), struct list_v *base, int delnums) {
  328.  
  329. if (s && base && (delnums > 0 || (delnums == -1))) {
  330. struct list_v *c = s->set;
  331. int pos = list_retidx (s, base);
  332. if (delnums == -1)
  333. delnums = s->nums - pos;
  334. if (pos != -1) {
  335. __retry:
  336. list_remove_ISQ(s, pos, pernode_);
  337. if (-- delnums > 0) {
  338. goto __retry;
  339. }
  340. }
  341. }
  342. }
  343. static
  344. void list_remove_ISQ3 (struct list_ *s, void (*pernode_)(void *node), void *node, int delnums) {
  345.  
  346. if (s && node && (delnums > 0 || (delnums == -1))) {
  347. struct list_v *c = s->set;
  348. int pos = list_retidx2 (s, node);
  349. if (delnums == -1)
  350. delnums = s->nums - pos;
  351. if (pos != -1) {
  352. __retry:
  353. list_remove_ISQ(s, pos, pernode_);
  354. if (-- delnums > 0) {
  355. goto __retry;
  356. }
  357. }
  358. }
  359. }
  360. static
  361. void list_remove_ISQ3a (struct list_ *s, void (*pernode_)(void **node)) {
  362.  
  363. if (s ) {
  364. while (s->nums > 0)
  365. list_remove_ISQ(s, 0, pernode_);
  366. }
  367. }
  368. static
  369. void list_uninit_ISQ (struct list_ **s, void (*pernode_)(void *node)) {
  370.  
  371. if (s != __NULLPTR && (*s != __NULLPTR)) {
  372. while ( (*s)->nums > 0)
  373. list_remove_ISQ (* s, 0, pernode_);
  374. free (*s);
  375. * s = __NULLPTR;
  376. }
  377. }
  378. static
  379. void list_removeALL_ISQ (struct list_ *s, void (*pernode_)(void *node)) {
  380.  
  381. if (s != __NULLPTR) {
  382. while ( s->nums > 0)
  383. list_remove_ISQ (s, 0, pernode_);
  384. s->set = __NULLPTR;
  385. }
  386. }
  387. static
  388. void list_uninit (struct list_ **s) {
  389.  
  390. list_uninit_ISQ (s, __NULLPTR);
  391. }
  392. static
  393. struct list_v *list_get2 (struct list_v *base, int pos) {
  394.  
  395. if (base && (pos >= 0)) {
  396. int id;
  397. struct list_v *u = base;
  398. for ( id= 0; id != pos; id++) {
  399. u =u->level_n;
  400. }
  401. return u;
  402. }
  403. return __NULLPTR;
  404. }
  405. static
  406. void *list_getnode2 (struct list_v *base, int pos) {
  407.  
  408. if (base && (pos >= 0)) {
  409. int id;
  410. struct list_v *u = base;
  411. for ( id= 0; id != pos; id++)
  412. u =u->level_n;
  413. return u->obj;
  414. }
  415. return __NULLPTR;
  416. }
  417. static
  418. list_bool list_getnode3 (struct list_v *base, int pos, int lens, void **accepta) {
  419.  
  420. if (base && (pos >= 0) && (lens > 0) && accepta) {
  421. int id;
  422. for ( id =0; id != lens; id++) {
  423. void *apts = list_getnode2 (base, pos+ id);
  424. if (apts != __NULLPTR) {
  425. accepta[id] = apts;
  426. } else {
  427. return list_false;
  428. }
  429. }
  430. return list_true;
  431. }
  432. return list_false;
  433. }
  434. static
  435. list_bool list_getlist3 (struct list_v *base, int pos, int lens, struct list_v **accepta) {
  436.  
  437. if (base && (pos >= 0) && (lens > 0) && accepta) {
  438. int id;
  439. for ( id =0; id != lens; id++) {
  440. struct list_v *apts = list_get2 (base, pos+ id);
  441. if (apts != __NULLPTR)
  442. accepta[id] = apts;
  443. else return list_false;
  444. }
  445. return list_true;
  446. }
  447. return list_false;
  448. }
  449.  
  450. /*
  451. not mine, from linux kernel but it looks pretty bad in this version....
  452. */
  453. #define LIST_FOREACH(/* list_ * */ head, /* int*/ id_, /* list_v */ node) \
  454. for ( (id_) = 0, (node) = (head)->set; \
  455. (id_) != (head)->nums; \
  456. (node) = (node)->level_n, (id_)++)
  457.  
  458. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement