valuemetic.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * @file valuemetic.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. * Comparison functions for values.
  32. *
  33. * Synopsis:
  34. * val_lesser(v1, v2)
  35. * val_greater(v1, v2)
  36. * val_lesser_equal(v1, v2)
  37. * val_greater_equal(v1, v2)
  38. * val_equal(v1, v2)
  39. * val_different(v1, v2)
  40. *
  41. * Variables:
  42. * (empty) - "true" or "false", reflecting the value of the relation in question
  43. *
  44. * Description:
  45. * These statements perform comparisons of values. Order of values is defined by the
  46. * following rules:
  47. * 1. Values of different types have the following order: strings, lists, maps.
  48. * 2. String values are ordered lexicographically, with respect to the numeric values
  49. * of their bytes.
  50. * 3. List values are ordered lexicographically, where the order of the elements is
  51. * defined by recursive application of these rules.
  52. * 4. Map values are ordered lexicographically, as if a map was a list of (key, value)
  53. * pairs ordered by key, where the order of both keys and values is defined by
  54. * recursive application of these rules.
  55. */
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <ncd/module_common.h>
  59. #include <generated/blog_channel_ncd_valuemetic.h>
  60. struct instance {
  61. NCDModuleInst *i;
  62. int result;
  63. };
  64. typedef int (*compute_func) (NCDValRef v1, NCDValRef v2);
  65. static int compute_lesser (NCDValRef v1, NCDValRef v2)
  66. {
  67. return NCDVal_Compare(v1, v2) < 0;
  68. }
  69. static int compute_greater (NCDValRef v1, NCDValRef v2)
  70. {
  71. return NCDVal_Compare(v1, v2) > 0;
  72. }
  73. static int compute_lesser_equal (NCDValRef v1, NCDValRef v2)
  74. {
  75. return NCDVal_Compare(v1, v2) <= 0;
  76. }
  77. static int compute_greater_equal (NCDValRef v1, NCDValRef v2)
  78. {
  79. return NCDVal_Compare(v1, v2) >= 0;
  80. }
  81. static int compute_equal (NCDValRef v1, NCDValRef v2)
  82. {
  83. return NCDVal_Compare(v1, v2) == 0;
  84. }
  85. static int compute_different (NCDValRef v1, NCDValRef v2)
  86. {
  87. return NCDVal_Compare(v1, v2) != 0;
  88. }
  89. static void new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, compute_func cfunc)
  90. {
  91. struct instance *o = vo;
  92. o->i = i;
  93. NCDValRef v1_arg;
  94. NCDValRef v2_arg;
  95. if (!NCDVal_ListRead(params->args, 2, &v1_arg, &v2_arg)) {
  96. ModuleLog(i, BLOG_ERROR, "wrong arity");
  97. goto fail0;
  98. }
  99. o->result = cfunc(v1_arg, v2_arg);
  100. NCDModuleInst_Backend_Up(i);
  101. return;
  102. fail0:
  103. NCDModuleInst_Backend_DeadError(i);
  104. }
  105. static void func_die (void *vo)
  106. {
  107. struct instance *o = vo;
  108. NCDModuleInst_Backend_Dead(o->i);
  109. }
  110. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  111. {
  112. struct instance *o = vo;
  113. if (name == NCD_STRING_EMPTY) {
  114. *out = ncd_make_boolean(mem, o->result, o->i->params->iparams->string_index);
  115. return 1;
  116. }
  117. return 0;
  118. }
  119. static void func_new_lesser (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  120. {
  121. new_templ(vo, i, params, compute_lesser);
  122. }
  123. static void func_new_greater (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  124. {
  125. new_templ(vo, i, params, compute_greater);
  126. }
  127. static void func_new_lesser_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  128. {
  129. new_templ(vo, i, params, compute_lesser_equal);
  130. }
  131. static void func_new_greater_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  132. {
  133. new_templ(vo, i, params, compute_greater_equal);
  134. }
  135. static void func_new_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  136. {
  137. new_templ(vo, i, params, compute_equal);
  138. }
  139. static void func_new_different (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  140. {
  141. new_templ(vo, i, params, compute_different);
  142. }
  143. static struct NCDModule modules[] = {
  144. {
  145. .type = "val_lesser",
  146. .func_new2 = func_new_lesser,
  147. .func_die = func_die,
  148. .func_getvar2 = func_getvar2,
  149. .alloc_size = sizeof(struct instance)
  150. }, {
  151. .type = "val_greater",
  152. .func_new2 = func_new_greater,
  153. .func_die = func_die,
  154. .func_getvar2 = func_getvar2,
  155. .alloc_size = sizeof(struct instance)
  156. }, {
  157. .type = "val_lesser_equal",
  158. .func_new2 = func_new_lesser_equal,
  159. .func_die = func_die,
  160. .func_getvar2 = func_getvar2,
  161. .alloc_size = sizeof(struct instance)
  162. }, {
  163. .type = "val_greater_equal",
  164. .func_new2 = func_new_greater_equal,
  165. .func_die = func_die,
  166. .func_getvar2 = func_getvar2,
  167. .alloc_size = sizeof(struct instance)
  168. }, {
  169. .type = "val_equal",
  170. .func_new2 = func_new_equal,
  171. .func_die = func_die,
  172. .func_getvar2 = func_getvar2,
  173. .alloc_size = sizeof(struct instance)
  174. }, {
  175. .type = "val_different",
  176. .func_new2 = func_new_different,
  177. .func_die = func_die,
  178. .func_getvar2 = func_getvar2,
  179. .alloc_size = sizeof(struct instance)
  180. }, {
  181. .type = NULL
  182. }
  183. };
  184. const struct NCDModuleGroup ncdmodule_valuemetic = {
  185. .modules = modules
  186. };