list.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /**
  2. * @file list.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * List construction module.
  32. *
  33. * Synopsis:
  34. * list(elem1, ..., elemN)
  35. * list listfrom(list l1, ..., list lN)
  36. *
  37. * Description:
  38. * The first form creates a list with the given elements.
  39. * The second form creates a list by concatenating the given
  40. * lists.
  41. *
  42. * Variables:
  43. * (empty) - list containing elem1, ..., elemN
  44. * length - number of elements in list
  45. *
  46. * Synopsis: list::append(arg)
  47. *
  48. * Synopsis: list::appendv(list arg)
  49. * Description: Appends the elements of arg to the list.
  50. *
  51. * Synopsis: list::length()
  52. * Variables:
  53. * (empty) - number of elements in list at the time of initialization
  54. * of this method
  55. *
  56. * Synopsis: list::get(string index)
  57. * Variables:
  58. * (empty) - element of list at position index (starting from zero) at the time of initialization
  59. *
  60. * Synopsis: list::shift()
  61. *
  62. * Synopsis: list::contains(value)
  63. * Variables:
  64. * (empty) - "true" if list contains value, "false" if not
  65. *
  66. * Synopsis:
  67. * list::find(start_pos, value)
  68. * Description:
  69. * finds the first occurrence of 'value' in the list at position >='start_pos'.
  70. * Variables:
  71. * pos - position of element, or "none" if not found
  72. * found - "true" if found, "false" if not
  73. *
  74. * Sysnopsis:
  75. * list::remove_at(remove_pos)
  76. * Description:
  77. * Removes the element at position 'remove_pos', which must refer to an existing element.
  78. *
  79. * Synopsis:
  80. * list::remove(value)
  81. * Description:
  82. * Removes the first occurrence of value in the list, which must be in the list.
  83. *
  84. * Synopsis:
  85. * list::set(list l1, ..., list lN)
  86. * Description:
  87. * Replaces the list with the concatenation of given lists.
  88. */
  89. #include <stdlib.h>
  90. #include <string.h>
  91. #include <stdio.h>
  92. #include <inttypes.h>
  93. #include <misc/offset.h>
  94. #include <misc/parse_number.h>
  95. #include <structure/IndexedList.h>
  96. #include <ncd/module_common.h>
  97. #include <generated/blog_channel_ncd_list.h>
  98. struct elem {
  99. IndexedListNode il_node;
  100. NCDValMem mem;
  101. NCDValRef val;
  102. };
  103. struct instance {
  104. NCDModuleInst *i;
  105. IndexedList il;
  106. };
  107. struct length_instance {
  108. NCDModuleInst *i;
  109. uint64_t length;
  110. };
  111. struct get_instance {
  112. NCDModuleInst *i;
  113. NCDValMem mem;
  114. NCDValRef val;
  115. };
  116. struct contains_instance {
  117. NCDModuleInst *i;
  118. int contains;
  119. };
  120. struct find_instance {
  121. NCDModuleInst *i;
  122. int is_found;
  123. uint64_t found_pos;
  124. };
  125. static uint64_t list_count (struct instance *o)
  126. {
  127. return IndexedList_Count(&o->il);
  128. }
  129. static struct elem * insert_value (NCDModuleInst *i, struct instance *o, NCDValRef val, uint64_t idx)
  130. {
  131. ASSERT(idx <= list_count(o))
  132. ASSERT(!NCDVal_IsInvalid(val))
  133. struct elem *e = malloc(sizeof(*e));
  134. if (!e) {
  135. ModuleLog(i, BLOG_ERROR, "malloc failed");
  136. goto fail0;
  137. }
  138. NCDValMem_Init(&e->mem);
  139. e->val = NCDVal_NewCopy(&e->mem, val);
  140. if (NCDVal_IsInvalid(e->val)) {
  141. goto fail1;
  142. }
  143. IndexedList_InsertAt(&o->il, &e->il_node, idx);
  144. return e;
  145. fail1:
  146. NCDValMem_Free(&e->mem);
  147. free(e);
  148. fail0:
  149. return NULL;
  150. }
  151. static void remove_elem (struct instance *o, struct elem *e)
  152. {
  153. IndexedList_Remove(&o->il, &e->il_node);
  154. NCDValMem_Free(&e->mem);
  155. free(e);
  156. }
  157. static struct elem * get_elem_at (struct instance *o, uint64_t idx)
  158. {
  159. ASSERT(idx < list_count(o))
  160. IndexedListNode *iln = IndexedList_GetAt(&o->il, idx);
  161. struct elem *e = UPPER_OBJECT(iln, struct elem, il_node);
  162. return e;
  163. }
  164. static struct elem * get_first_elem (struct instance *o)
  165. {
  166. ASSERT(list_count(o) > 0)
  167. IndexedListNode *iln = IndexedList_GetFirst(&o->il);
  168. struct elem *e = UPPER_OBJECT(iln, struct elem, il_node);
  169. return e;
  170. }
  171. static struct elem * get_last_elem (struct instance *o)
  172. {
  173. ASSERT(list_count(o) > 0)
  174. IndexedListNode *iln = IndexedList_GetLast(&o->il);
  175. struct elem *e = UPPER_OBJECT(iln, struct elem, il_node);
  176. return e;
  177. }
  178. static void cut_list_front (struct instance *o, uint64_t count)
  179. {
  180. while (list_count(o) > count) {
  181. remove_elem(o, get_first_elem(o));
  182. }
  183. }
  184. static void cut_list_back (struct instance *o, uint64_t count)
  185. {
  186. while (list_count(o) > count) {
  187. remove_elem(o, get_last_elem(o));
  188. }
  189. }
  190. static int append_list_contents (NCDModuleInst *i, struct instance *o, NCDValRef args)
  191. {
  192. ASSERT(NCDVal_IsList(args))
  193. uint64_t orig_count = list_count(o);
  194. size_t append_count = NCDVal_ListCount(args);
  195. for (size_t j = 0; j < append_count; j++) {
  196. NCDValRef elem = NCDVal_ListGet(args, j);
  197. if (!insert_value(i, o, elem, list_count(o))) {
  198. goto fail;
  199. }
  200. }
  201. return 1;
  202. fail:
  203. cut_list_back(o, orig_count);
  204. return 0;
  205. }
  206. static int append_list_contents_contents (NCDModuleInst *i, struct instance *o, NCDValRef args)
  207. {
  208. ASSERT(NCDVal_IsList(args))
  209. uint64_t orig_count = list_count(o);
  210. size_t append_count = NCDVal_ListCount(args);
  211. for (size_t j = 0; j < append_count; j++) {
  212. NCDValRef elem = NCDVal_ListGet(args, j);
  213. if (!NCDVal_IsList(elem)) {
  214. ModuleLog(i, BLOG_ERROR, "wrong type");
  215. goto fail;
  216. }
  217. if (!append_list_contents(i, o, elem)) {
  218. goto fail;
  219. }
  220. }
  221. return 1;
  222. fail:
  223. cut_list_back(o, orig_count);
  224. return 0;
  225. }
  226. static struct elem * find_elem (struct instance *o, NCDValRef val, uint64_t start_idx, uint64_t *out_idx)
  227. {
  228. if (start_idx >= list_count(o)) {
  229. return NULL;
  230. }
  231. for (IndexedListNode *iln = IndexedList_GetAt(&o->il, start_idx); iln; iln = IndexedList_GetNext(&o->il, iln)) {
  232. struct elem *e = UPPER_OBJECT(iln, struct elem, il_node);
  233. if (NCDVal_Compare(e->val, val) == 0) {
  234. if (out_idx) {
  235. *out_idx = start_idx;
  236. }
  237. return e;
  238. }
  239. start_idx++;
  240. }
  241. return NULL;
  242. }
  243. static int list_to_value (NCDModuleInst *i, struct instance *o, NCDValMem *mem, NCDValRef *out_val)
  244. {
  245. *out_val = NCDVal_NewList(mem, IndexedList_Count(&o->il));
  246. if (NCDVal_IsInvalid(*out_val)) {
  247. goto fail;
  248. }
  249. for (IndexedListNode *iln = IndexedList_GetFirst(&o->il); iln; iln = IndexedList_GetNext(&o->il, iln)) {
  250. struct elem *e = UPPER_OBJECT(iln, struct elem, il_node);
  251. NCDValRef copy = NCDVal_NewCopy(mem, e->val);
  252. if (NCDVal_IsInvalid(copy)) {
  253. goto fail;
  254. }
  255. if (!NCDVal_ListAppend(*out_val, copy)) {
  256. goto fail;
  257. }
  258. }
  259. return 1;
  260. fail:
  261. return 0;
  262. }
  263. static void func_new_list (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  264. {
  265. struct instance *o = vo;
  266. o->i = i;
  267. // init list
  268. IndexedList_Init(&o->il);
  269. // append contents
  270. if (!append_list_contents(i, o, params->args)) {
  271. goto fail1;
  272. }
  273. // signal up
  274. NCDModuleInst_Backend_Up(o->i);
  275. return;
  276. fail1:
  277. cut_list_front(o, 0);
  278. NCDModuleInst_Backend_DeadError(i);
  279. }
  280. static void func_new_listfrom (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  281. {
  282. struct instance *o = vo;
  283. o->i = i;
  284. // init list
  285. IndexedList_Init(&o->il);
  286. // append contents contents
  287. if (!append_list_contents_contents(i, o, params->args)) {
  288. goto fail1;
  289. }
  290. // signal up
  291. NCDModuleInst_Backend_Up(o->i);
  292. return;
  293. fail1:
  294. cut_list_front(o, 0);
  295. NCDModuleInst_Backend_DeadError(i);
  296. }
  297. static void func_die (void *vo)
  298. {
  299. struct instance *o = vo;
  300. // free list elements
  301. cut_list_front(o, 0);
  302. NCDModuleInst_Backend_Dead(o->i);
  303. }
  304. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  305. {
  306. struct instance *o = vo;
  307. if (!strcmp(name, "")) {
  308. if (!list_to_value(o->i, o, mem, out)) {
  309. return 0;
  310. }
  311. return 1;
  312. }
  313. if (!strcmp(name, "length")) {
  314. *out = ncd_make_uintmax(mem, list_count(o));
  315. return 1;
  316. }
  317. return 0;
  318. }
  319. static void append_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  320. {
  321. // check arguments
  322. NCDValRef arg;
  323. if (!NCDVal_ListRead(params->args, 1, &arg)) {
  324. ModuleLog(i, BLOG_ERROR, "wrong arity");
  325. goto fail0;
  326. }
  327. // get method object
  328. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  329. // append
  330. if (!insert_value(i, mo, arg, list_count(mo))) {
  331. goto fail0;
  332. }
  333. // signal up
  334. NCDModuleInst_Backend_Up(i);
  335. return;
  336. fail0:
  337. NCDModuleInst_Backend_DeadError(i);
  338. }
  339. static void appendv_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  340. {
  341. // check arguments
  342. NCDValRef arg;
  343. if (!NCDVal_ListRead(params->args, 1, &arg)) {
  344. ModuleLog(i, BLOG_ERROR, "wrong arity");
  345. goto fail0;
  346. }
  347. if (!NCDVal_IsList(arg)) {
  348. ModuleLog(i, BLOG_ERROR, "wrong type");
  349. goto fail0;
  350. }
  351. // get method object
  352. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  353. // append
  354. if (!append_list_contents(i, mo, arg)) {
  355. goto fail0;
  356. }
  357. // signal up
  358. NCDModuleInst_Backend_Up(i);
  359. return;
  360. fail0:
  361. NCDModuleInst_Backend_DeadError(i);
  362. }
  363. static void length_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  364. {
  365. struct length_instance *o = vo;
  366. o->i = i;
  367. // check arguments
  368. if (!NCDVal_ListRead(params->args, 0)) {
  369. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  370. goto fail0;
  371. }
  372. // get method object
  373. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  374. // remember length
  375. o->length = list_count(mo);
  376. // signal up
  377. NCDModuleInst_Backend_Up(o->i);
  378. return;
  379. fail0:
  380. NCDModuleInst_Backend_DeadError(i);
  381. }
  382. static void length_func_die (void *vo)
  383. {
  384. struct length_instance *o = vo;
  385. NCDModuleInst_Backend_Dead(o->i);
  386. }
  387. static int length_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  388. {
  389. struct length_instance *o = vo;
  390. if (!strcmp(name, "")) {
  391. *out = ncd_make_uintmax(mem, o->length);
  392. return 1;
  393. }
  394. return 0;
  395. }
  396. static void get_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  397. {
  398. struct get_instance *o = vo;
  399. o->i = i;
  400. // check arguments
  401. NCDValRef index_arg;
  402. if (!NCDVal_ListRead(params->args, 1, &index_arg)) {
  403. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  404. goto fail0;
  405. }
  406. uintmax_t index;
  407. if (!ncd_read_uintmax(index_arg, &index)) {
  408. ModuleLog(o->i, BLOG_ERROR, "wrong value");
  409. goto fail0;
  410. }
  411. // get method object
  412. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  413. // check index
  414. if (index >= list_count(mo)) {
  415. ModuleLog(o->i, BLOG_ERROR, "no element at index %"PRIuMAX, index);
  416. goto fail0;
  417. }
  418. // get element
  419. struct elem *e = get_elem_at(mo, index);
  420. // init mem
  421. NCDValMem_Init(&o->mem);
  422. // copy value
  423. o->val = NCDVal_NewCopy(&o->mem, e->val);
  424. if (NCDVal_IsInvalid(o->val)) {
  425. goto fail1;
  426. }
  427. // signal up
  428. NCDModuleInst_Backend_Up(o->i);
  429. return;
  430. fail1:
  431. NCDValMem_Free(&o->mem);
  432. fail0:
  433. NCDModuleInst_Backend_DeadError(i);
  434. }
  435. static void get_func_die (void *vo)
  436. {
  437. struct get_instance *o = vo;
  438. // free mem
  439. NCDValMem_Free(&o->mem);
  440. NCDModuleInst_Backend_Dead(o->i);
  441. }
  442. static int get_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  443. {
  444. struct get_instance *o = vo;
  445. if (!strcmp(name, "")) {
  446. *out = NCDVal_NewCopy(mem, o->val);
  447. return 1;
  448. }
  449. return 0;
  450. }
  451. static void shift_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  452. {
  453. // check arguments
  454. if (!NCDVal_ListRead(params->args, 0)) {
  455. ModuleLog(i, BLOG_ERROR, "wrong arity");
  456. goto fail0;
  457. }
  458. // get method object
  459. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  460. // check first
  461. if (list_count(mo) == 0) {
  462. ModuleLog(i, BLOG_ERROR, "list has no elements");
  463. goto fail0;
  464. }
  465. // remove first
  466. remove_elem(mo, get_first_elem(mo));
  467. // signal up
  468. NCDModuleInst_Backend_Up(i);
  469. return;
  470. fail0:
  471. NCDModuleInst_Backend_DeadError(i);
  472. }
  473. static void contains_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  474. {
  475. struct contains_instance *o = vo;
  476. o->i = i;
  477. // read arguments
  478. NCDValRef value_arg;
  479. if (!NCDVal_ListRead(params->args, 1, &value_arg)) {
  480. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  481. goto fail0;
  482. }
  483. // get method object
  484. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  485. // search
  486. o->contains = !!find_elem(mo, value_arg, 0, NULL);
  487. // signal up
  488. NCDModuleInst_Backend_Up(o->i);
  489. return;
  490. fail0:
  491. NCDModuleInst_Backend_DeadError(i);
  492. }
  493. static void contains_func_die (void *vo)
  494. {
  495. struct contains_instance *o = vo;
  496. NCDModuleInst_Backend_Dead(o->i);
  497. }
  498. static int contains_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  499. {
  500. struct contains_instance *o = vo;
  501. if (!strcmp(name, "")) {
  502. *out = ncd_make_boolean(mem, o->contains, o->i->params->iparams->string_index);
  503. return 1;
  504. }
  505. return 0;
  506. }
  507. static void find_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  508. {
  509. struct find_instance *o = vo;
  510. o->i = i;
  511. // read arguments
  512. NCDValRef start_pos_arg;
  513. NCDValRef value_arg;
  514. if (!NCDVal_ListRead(params->args, 2, &start_pos_arg, &value_arg)) {
  515. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  516. goto fail0;
  517. }
  518. // read start position
  519. uintmax_t start_pos;
  520. if (!ncd_read_uintmax(start_pos_arg, &start_pos) || start_pos > UINT64_MAX) {
  521. ModuleLog(o->i, BLOG_ERROR, "wrong start pos");
  522. goto fail0;
  523. }
  524. // get method object
  525. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  526. // find
  527. o->is_found = !!find_elem(mo, value_arg, start_pos, &o->found_pos);
  528. // signal up
  529. NCDModuleInst_Backend_Up(o->i);
  530. return;
  531. fail0:
  532. NCDModuleInst_Backend_DeadError(i);
  533. }
  534. static void find_func_die (void *vo)
  535. {
  536. struct find_instance *o = vo;
  537. NCDModuleInst_Backend_Dead(o->i);
  538. }
  539. static int find_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  540. {
  541. struct find_instance *o = vo;
  542. if (!strcmp(name, "pos")) {
  543. char value[64] = "none";
  544. if (o->is_found) {
  545. generate_decimal_repr_string(o->found_pos, value);
  546. }
  547. *out = NCDVal_NewString(mem, value);
  548. return 1;
  549. }
  550. if (!strcmp(name, "found")) {
  551. *out = ncd_make_boolean(mem, o->is_found, o->i->params->iparams->string_index);
  552. return 1;
  553. }
  554. return 0;
  555. }
  556. static void removeat_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  557. {
  558. // read arguments
  559. NCDValRef remove_pos_arg;
  560. if (!NCDVal_ListRead(params->args, 1, &remove_pos_arg)) {
  561. ModuleLog(i, BLOG_ERROR, "wrong arity");
  562. goto fail0;
  563. }
  564. // read position
  565. uintmax_t remove_pos;
  566. if (!ncd_read_uintmax(remove_pos_arg, &remove_pos)) {
  567. ModuleLog(i, BLOG_ERROR, "wrong pos");
  568. goto fail0;
  569. }
  570. // get method object
  571. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  572. // check position
  573. if (remove_pos >= list_count(mo)) {
  574. ModuleLog(i, BLOG_ERROR, "pos out of range");
  575. goto fail0;
  576. }
  577. // remove
  578. remove_elem(mo, get_elem_at(mo, remove_pos));
  579. // signal up
  580. NCDModuleInst_Backend_Up(i);
  581. return;
  582. fail0:
  583. NCDModuleInst_Backend_DeadError(i);
  584. }
  585. static void remove_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  586. {
  587. // read arguments
  588. NCDValRef value_arg;
  589. if (!NCDVal_ListRead(params->args, 1, &value_arg)) {
  590. ModuleLog(i, BLOG_ERROR, "wrong arity");
  591. goto fail0;
  592. }
  593. // get method object
  594. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  595. // find element
  596. struct elem *e = find_elem(mo, value_arg, 0, NULL);
  597. if (!e) {
  598. ModuleLog(i, BLOG_ERROR, "value does not exist");
  599. goto fail0;
  600. }
  601. // remove element
  602. remove_elem(mo, e);
  603. // signal up
  604. NCDModuleInst_Backend_Up(i);
  605. return;
  606. fail0:
  607. NCDModuleInst_Backend_DeadError(i);
  608. }
  609. static void set_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  610. {
  611. // get method object
  612. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  613. // remember old count
  614. uint64_t old_count = list_count(mo);
  615. // append contents of our lists
  616. if (!append_list_contents_contents(i, mo, params->args)) {
  617. goto fail0;
  618. }
  619. // remove old elements
  620. cut_list_front(mo, list_count(mo) - old_count);
  621. // signal up
  622. NCDModuleInst_Backend_Up(i);
  623. return;
  624. fail0:
  625. NCDModuleInst_Backend_DeadError(i);
  626. }
  627. static struct NCDModule modules[] = {
  628. {
  629. .type = "list",
  630. .func_new2 = func_new_list,
  631. .func_die = func_die,
  632. .func_getvar = func_getvar,
  633. .alloc_size = sizeof(struct instance)
  634. }, {
  635. .type = "listfrom",
  636. .base_type = "list",
  637. .func_new2 = func_new_listfrom,
  638. .func_die = func_die,
  639. .func_getvar = func_getvar,
  640. .alloc_size = sizeof(struct instance)
  641. }, {
  642. .type = "concatlist", // alias for listfrom
  643. .base_type = "list",
  644. .func_new2 = func_new_listfrom,
  645. .func_die = func_die,
  646. .func_getvar = func_getvar,
  647. .alloc_size = sizeof(struct instance)
  648. }, {
  649. .type = "list::append",
  650. .func_new2 = append_func_new
  651. }, {
  652. .type = "list::appendv",
  653. .func_new2 = appendv_func_new
  654. }, {
  655. .type = "list::length",
  656. .func_new2 = length_func_new,
  657. .func_die = length_func_die,
  658. .func_getvar = length_func_getvar,
  659. .alloc_size = sizeof(struct length_instance)
  660. }, {
  661. .type = "list::get",
  662. .func_new2 = get_func_new,
  663. .func_die = get_func_die,
  664. .func_getvar = get_func_getvar,
  665. .alloc_size = sizeof(struct get_instance)
  666. }, {
  667. .type = "list::shift",
  668. .func_new2 = shift_func_new
  669. }, {
  670. .type = "list::contains",
  671. .func_new2 = contains_func_new,
  672. .func_die = contains_func_die,
  673. .func_getvar = contains_func_getvar,
  674. .alloc_size = sizeof(struct contains_instance)
  675. }, {
  676. .type = "list::find",
  677. .func_new2 = find_func_new,
  678. .func_die = find_func_die,
  679. .func_getvar = find_func_getvar,
  680. .alloc_size = sizeof(struct find_instance)
  681. }, {
  682. .type = "list::remove_at",
  683. .func_new2 = removeat_func_new
  684. }, {
  685. .type = "list::remove",
  686. .func_new2 = remove_func_new
  687. }, {
  688. .type = "list::set",
  689. .func_new2 = set_func_new
  690. }, {
  691. .type = NULL
  692. }
  693. };
  694. const struct NCDModuleGroup ncdmodule_list = {
  695. .modules = modules
  696. };