clipsruby 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (346) hide show
  1. checksums.yaml +7 -0
  2. data/ext/clipsruby/agenda.c +1373 -0
  3. data/ext/clipsruby/agenda.h +169 -0
  4. data/ext/clipsruby/analysis.c +1142 -0
  5. data/ext/clipsruby/analysis.h +61 -0
  6. data/ext/clipsruby/argacces.c +526 -0
  7. data/ext/clipsruby/argacces.h +77 -0
  8. data/ext/clipsruby/bload.c +884 -0
  9. data/ext/clipsruby/bload.h +94 -0
  10. data/ext/clipsruby/bmathfun.c +557 -0
  11. data/ext/clipsruby/bmathfun.h +66 -0
  12. data/ext/clipsruby/bsave.c +634 -0
  13. data/ext/clipsruby/bsave.h +130 -0
  14. data/ext/clipsruby/classcom.c +976 -0
  15. data/ext/clipsruby/classcom.h +115 -0
  16. data/ext/clipsruby/classexm.c +1376 -0
  17. data/ext/clipsruby/classexm.h +97 -0
  18. data/ext/clipsruby/classfun.c +1392 -0
  19. data/ext/clipsruby/classfun.h +164 -0
  20. data/ext/clipsruby/classinf.c +1245 -0
  21. data/ext/clipsruby/classinf.h +94 -0
  22. data/ext/clipsruby/classini.c +843 -0
  23. data/ext/clipsruby/classini.h +75 -0
  24. data/ext/clipsruby/classpsr.c +957 -0
  25. data/ext/clipsruby/classpsr.h +73 -0
  26. data/ext/clipsruby/clips.h +133 -0
  27. data/ext/clipsruby/clipsruby.c +619 -0
  28. data/ext/clipsruby/clsltpsr.c +931 -0
  29. data/ext/clipsruby/clsltpsr.h +72 -0
  30. data/ext/clipsruby/commline.c +1217 -0
  31. data/ext/clipsruby/commline.h +131 -0
  32. data/ext/clipsruby/conscomp.c +1593 -0
  33. data/ext/clipsruby/conscomp.h +150 -0
  34. data/ext/clipsruby/constant.h +264 -0
  35. data/ext/clipsruby/constrct.c +1090 -0
  36. data/ext/clipsruby/constrct.h +216 -0
  37. data/ext/clipsruby/constrnt.c +554 -0
  38. data/ext/clipsruby/constrnt.h +132 -0
  39. data/ext/clipsruby/crstrtgy.c +1088 -0
  40. data/ext/clipsruby/crstrtgy.h +85 -0
  41. data/ext/clipsruby/cstrcbin.c +185 -0
  42. data/ext/clipsruby/cstrcbin.h +61 -0
  43. data/ext/clipsruby/cstrccmp.h +43 -0
  44. data/ext/clipsruby/cstrccom.c +1791 -0
  45. data/ext/clipsruby/cstrccom.h +115 -0
  46. data/ext/clipsruby/cstrcpsr.c +835 -0
  47. data/ext/clipsruby/cstrcpsr.h +97 -0
  48. data/ext/clipsruby/cstrnbin.c +282 -0
  49. data/ext/clipsruby/cstrnbin.h +55 -0
  50. data/ext/clipsruby/cstrnchk.c +826 -0
  51. data/ext/clipsruby/cstrnchk.h +91 -0
  52. data/ext/clipsruby/cstrncmp.c +238 -0
  53. data/ext/clipsruby/cstrncmp.h +57 -0
  54. data/ext/clipsruby/cstrnops.c +1176 -0
  55. data/ext/clipsruby/cstrnops.h +47 -0
  56. data/ext/clipsruby/cstrnpsr.c +1394 -0
  57. data/ext/clipsruby/cstrnpsr.h +88 -0
  58. data/ext/clipsruby/cstrnutl.c +564 -0
  59. data/ext/clipsruby/cstrnutl.h +54 -0
  60. data/ext/clipsruby/default.c +454 -0
  61. data/ext/clipsruby/default.h +57 -0
  62. data/ext/clipsruby/defins.c +971 -0
  63. data/ext/clipsruby/defins.h +127 -0
  64. data/ext/clipsruby/developr.c +677 -0
  65. data/ext/clipsruby/developr.h +69 -0
  66. data/ext/clipsruby/dffctbin.c +477 -0
  67. data/ext/clipsruby/dffctbin.h +76 -0
  68. data/ext/clipsruby/dffctbsc.c +308 -0
  69. data/ext/clipsruby/dffctbsc.h +72 -0
  70. data/ext/clipsruby/dffctcmp.c +297 -0
  71. data/ext/clipsruby/dffctcmp.h +44 -0
  72. data/ext/clipsruby/dffctdef.c +364 -0
  73. data/ext/clipsruby/dffctdef.h +104 -0
  74. data/ext/clipsruby/dffctpsr.c +179 -0
  75. data/ext/clipsruby/dffctpsr.h +49 -0
  76. data/ext/clipsruby/dffnxbin.c +520 -0
  77. data/ext/clipsruby/dffnxbin.h +67 -0
  78. data/ext/clipsruby/dffnxcmp.c +378 -0
  79. data/ext/clipsruby/dffnxcmp.h +54 -0
  80. data/ext/clipsruby/dffnxexe.c +241 -0
  81. data/ext/clipsruby/dffnxexe.h +58 -0
  82. data/ext/clipsruby/dffnxfun.c +1192 -0
  83. data/ext/clipsruby/dffnxfun.h +155 -0
  84. data/ext/clipsruby/dffnxpsr.c +514 -0
  85. data/ext/clipsruby/dffnxpsr.h +57 -0
  86. data/ext/clipsruby/dfinsbin.c +509 -0
  87. data/ext/clipsruby/dfinsbin.h +66 -0
  88. data/ext/clipsruby/dfinscmp.c +345 -0
  89. data/ext/clipsruby/dfinscmp.h +48 -0
  90. data/ext/clipsruby/drive.c +1191 -0
  91. data/ext/clipsruby/drive.h +65 -0
  92. data/ext/clipsruby/emathfun.c +1213 -0
  93. data/ext/clipsruby/emathfun.h +99 -0
  94. data/ext/clipsruby/engine.c +1568 -0
  95. data/ext/clipsruby/engine.h +203 -0
  96. data/ext/clipsruby/entities.h +276 -0
  97. data/ext/clipsruby/envrnbld.c +514 -0
  98. data/ext/clipsruby/envrnbld.h +40 -0
  99. data/ext/clipsruby/envrnmnt.c +257 -0
  100. data/ext/clipsruby/envrnmnt.h +112 -0
  101. data/ext/clipsruby/evaluatn.c +1736 -0
  102. data/ext/clipsruby/evaluatn.h +211 -0
  103. data/ext/clipsruby/expressn.c +494 -0
  104. data/ext/clipsruby/expressn.h +154 -0
  105. data/ext/clipsruby/exprnbin.c +538 -0
  106. data/ext/clipsruby/exprnbin.h +60 -0
  107. data/ext/clipsruby/exprnops.c +564 -0
  108. data/ext/clipsruby/exprnops.h +67 -0
  109. data/ext/clipsruby/exprnpsr.c +1112 -0
  110. data/ext/clipsruby/exprnpsr.h +98 -0
  111. data/ext/clipsruby/extconf.rb +2 -0
  112. data/ext/clipsruby/extnfunc.c +1015 -0
  113. data/ext/clipsruby/extnfunc.h +157 -0
  114. data/ext/clipsruby/factbin.c +447 -0
  115. data/ext/clipsruby/factbin.h +56 -0
  116. data/ext/clipsruby/factbld.c +1035 -0
  117. data/ext/clipsruby/factbld.h +63 -0
  118. data/ext/clipsruby/factcmp.c +386 -0
  119. data/ext/clipsruby/factcmp.h +46 -0
  120. data/ext/clipsruby/factcom.c +759 -0
  121. data/ext/clipsruby/factcom.h +80 -0
  122. data/ext/clipsruby/factfile.c +1761 -0
  123. data/ext/clipsruby/factfile.h +54 -0
  124. data/ext/clipsruby/factfun.c +682 -0
  125. data/ext/clipsruby/factfun.h +77 -0
  126. data/ext/clipsruby/factgen.c +1305 -0
  127. data/ext/clipsruby/factgen.h +229 -0
  128. data/ext/clipsruby/facthsh.c +438 -0
  129. data/ext/clipsruby/facthsh.h +81 -0
  130. data/ext/clipsruby/factlhs.c +250 -0
  131. data/ext/clipsruby/factlhs.h +54 -0
  132. data/ext/clipsruby/factmch.c +905 -0
  133. data/ext/clipsruby/factmch.h +68 -0
  134. data/ext/clipsruby/factmngr.c +3373 -0
  135. data/ext/clipsruby/factmngr.h +325 -0
  136. data/ext/clipsruby/factprt.c +498 -0
  137. data/ext/clipsruby/factprt.h +60 -0
  138. data/ext/clipsruby/factqpsr.c +796 -0
  139. data/ext/clipsruby/factqpsr.h +61 -0
  140. data/ext/clipsruby/factqury.c +1267 -0
  141. data/ext/clipsruby/factqury.h +112 -0
  142. data/ext/clipsruby/factrete.c +978 -0
  143. data/ext/clipsruby/factrete.h +70 -0
  144. data/ext/clipsruby/factrhs.c +667 -0
  145. data/ext/clipsruby/factrhs.h +55 -0
  146. data/ext/clipsruby/filecom.c +353 -0
  147. data/ext/clipsruby/filecom.h +137 -0
  148. data/ext/clipsruby/filertr.c +481 -0
  149. data/ext/clipsruby/filertr.h +94 -0
  150. data/ext/clipsruby/fileutil.c +1020 -0
  151. data/ext/clipsruby/fileutil.h +50 -0
  152. data/ext/clipsruby/generate.c +1079 -0
  153. data/ext/clipsruby/generate.h +57 -0
  154. data/ext/clipsruby/genrcbin.c +902 -0
  155. data/ext/clipsruby/genrcbin.h +69 -0
  156. data/ext/clipsruby/genrccmp.c +640 -0
  157. data/ext/clipsruby/genrccmp.h +59 -0
  158. data/ext/clipsruby/genrccom.c +2017 -0
  159. data/ext/clipsruby/genrccom.h +119 -0
  160. data/ext/clipsruby/genrcexe.c +737 -0
  161. data/ext/clipsruby/genrcexe.h +73 -0
  162. data/ext/clipsruby/genrcfun.c +890 -0
  163. data/ext/clipsruby/genrcfun.h +185 -0
  164. data/ext/clipsruby/genrcpsr.c +1618 -0
  165. data/ext/clipsruby/genrcpsr.h +80 -0
  166. data/ext/clipsruby/globlbin.c +458 -0
  167. data/ext/clipsruby/globlbin.h +71 -0
  168. data/ext/clipsruby/globlbsc.c +361 -0
  169. data/ext/clipsruby/globlbsc.h +83 -0
  170. data/ext/clipsruby/globlcmp.c +330 -0
  171. data/ext/clipsruby/globlcmp.h +52 -0
  172. data/ext/clipsruby/globlcom.c +289 -0
  173. data/ext/clipsruby/globlcom.h +63 -0
  174. data/ext/clipsruby/globldef.c +1087 -0
  175. data/ext/clipsruby/globldef.h +151 -0
  176. data/ext/clipsruby/globlpsr.c +530 -0
  177. data/ext/clipsruby/globlpsr.h +59 -0
  178. data/ext/clipsruby/immthpsr.c +431 -0
  179. data/ext/clipsruby/immthpsr.h +55 -0
  180. data/ext/clipsruby/incrrset.c +530 -0
  181. data/ext/clipsruby/incrrset.h +73 -0
  182. data/ext/clipsruby/inherpsr.c +850 -0
  183. data/ext/clipsruby/inherpsr.h +52 -0
  184. data/ext/clipsruby/inscom.c +2076 -0
  185. data/ext/clipsruby/inscom.h +182 -0
  186. data/ext/clipsruby/insfile.c +1764 -0
  187. data/ext/clipsruby/insfile.h +96 -0
  188. data/ext/clipsruby/insfun.c +1451 -0
  189. data/ext/clipsruby/insfun.h +134 -0
  190. data/ext/clipsruby/insmngr.c +2550 -0
  191. data/ext/clipsruby/insmngr.h +125 -0
  192. data/ext/clipsruby/insmoddp.c +1041 -0
  193. data/ext/clipsruby/insmoddp.h +91 -0
  194. data/ext/clipsruby/insmult.c +804 -0
  195. data/ext/clipsruby/insmult.h +62 -0
  196. data/ext/clipsruby/inspsr.c +602 -0
  197. data/ext/clipsruby/inspsr.h +60 -0
  198. data/ext/clipsruby/insquery.c +1278 -0
  199. data/ext/clipsruby/insquery.h +115 -0
  200. data/ext/clipsruby/insqypsr.c +729 -0
  201. data/ext/clipsruby/insqypsr.h +63 -0
  202. data/ext/clipsruby/iofun.c +2045 -0
  203. data/ext/clipsruby/iofun.h +116 -0
  204. data/ext/clipsruby/lgcldpnd.c +644 -0
  205. data/ext/clipsruby/lgcldpnd.h +75 -0
  206. data/ext/clipsruby/main.c +112 -0
  207. data/ext/clipsruby/match.h +142 -0
  208. data/ext/clipsruby/memalloc.c +481 -0
  209. data/ext/clipsruby/memalloc.h +197 -0
  210. data/ext/clipsruby/miscfun.c +1801 -0
  211. data/ext/clipsruby/miscfun.h +132 -0
  212. data/ext/clipsruby/modulbin.c +607 -0
  213. data/ext/clipsruby/modulbin.h +84 -0
  214. data/ext/clipsruby/modulbsc.c +347 -0
  215. data/ext/clipsruby/modulbsc.h +67 -0
  216. data/ext/clipsruby/modulcmp.c +499 -0
  217. data/ext/clipsruby/modulcmp.h +54 -0
  218. data/ext/clipsruby/moduldef.c +817 -0
  219. data/ext/clipsruby/moduldef.h +271 -0
  220. data/ext/clipsruby/modulpsr.c +1150 -0
  221. data/ext/clipsruby/modulpsr.h +69 -0
  222. data/ext/clipsruby/modulutl.c +1036 -0
  223. data/ext/clipsruby/modulutl.h +84 -0
  224. data/ext/clipsruby/msgcom.c +1221 -0
  225. data/ext/clipsruby/msgcom.h +125 -0
  226. data/ext/clipsruby/msgfun.c +1076 -0
  227. data/ext/clipsruby/msgfun.h +118 -0
  228. data/ext/clipsruby/msgpass.c +1441 -0
  229. data/ext/clipsruby/msgpass.h +103 -0
  230. data/ext/clipsruby/msgpsr.c +698 -0
  231. data/ext/clipsruby/msgpsr.h +73 -0
  232. data/ext/clipsruby/multifld.c +1404 -0
  233. data/ext/clipsruby/multifld.h +130 -0
  234. data/ext/clipsruby/multifun.c +2182 -0
  235. data/ext/clipsruby/multifun.h +102 -0
  236. data/ext/clipsruby/network.h +142 -0
  237. data/ext/clipsruby/objbin.c +1522 -0
  238. data/ext/clipsruby/objbin.h +79 -0
  239. data/ext/clipsruby/objcmp.c +1507 -0
  240. data/ext/clipsruby/objcmp.h +71 -0
  241. data/ext/clipsruby/object.h +260 -0
  242. data/ext/clipsruby/objrtbin.c +701 -0
  243. data/ext/clipsruby/objrtbin.h +79 -0
  244. data/ext/clipsruby/objrtbld.c +2393 -0
  245. data/ext/clipsruby/objrtbld.h +66 -0
  246. data/ext/clipsruby/objrtcmp.c +734 -0
  247. data/ext/clipsruby/objrtcmp.h +66 -0
  248. data/ext/clipsruby/objrtfnx.c +1330 -0
  249. data/ext/clipsruby/objrtfnx.h +222 -0
  250. data/ext/clipsruby/objrtgen.c +736 -0
  251. data/ext/clipsruby/objrtgen.h +63 -0
  252. data/ext/clipsruby/objrtmch.c +1524 -0
  253. data/ext/clipsruby/objrtmch.h +160 -0
  254. data/ext/clipsruby/parsefun.c +415 -0
  255. data/ext/clipsruby/parsefun.h +67 -0
  256. data/ext/clipsruby/pattern.c +1265 -0
  257. data/ext/clipsruby/pattern.h +163 -0
  258. data/ext/clipsruby/pprint.c +328 -0
  259. data/ext/clipsruby/pprint.h +79 -0
  260. data/ext/clipsruby/prccode.c +1478 -0
  261. data/ext/clipsruby/prccode.h +145 -0
  262. data/ext/clipsruby/prcdrfun.c +640 -0
  263. data/ext/clipsruby/prcdrfun.h +95 -0
  264. data/ext/clipsruby/prcdrpsr.c +1068 -0
  265. data/ext/clipsruby/prcdrpsr.h +79 -0
  266. data/ext/clipsruby/prdctfun.c +869 -0
  267. data/ext/clipsruby/prdctfun.h +77 -0
  268. data/ext/clipsruby/prntutil.c +878 -0
  269. data/ext/clipsruby/prntutil.h +125 -0
  270. data/ext/clipsruby/proflfun.c +827 -0
  271. data/ext/clipsruby/proflfun.h +118 -0
  272. data/ext/clipsruby/reorder.c +2082 -0
  273. data/ext/clipsruby/reorder.h +172 -0
  274. data/ext/clipsruby/reteutil.c +1732 -0
  275. data/ext/clipsruby/reteutil.h +111 -0
  276. data/ext/clipsruby/retract.c +710 -0
  277. data/ext/clipsruby/retract.h +74 -0
  278. data/ext/clipsruby/router.c +737 -0
  279. data/ext/clipsruby/router.h +147 -0
  280. data/ext/clipsruby/rulebin.c +1136 -0
  281. data/ext/clipsruby/rulebin.h +153 -0
  282. data/ext/clipsruby/rulebld.c +1328 -0
  283. data/ext/clipsruby/rulebld.h +62 -0
  284. data/ext/clipsruby/rulebsc.c +517 -0
  285. data/ext/clipsruby/rulebsc.h +91 -0
  286. data/ext/clipsruby/rulecmp.c +733 -0
  287. data/ext/clipsruby/rulecmp.h +63 -0
  288. data/ext/clipsruby/rulecom.c +1583 -0
  289. data/ext/clipsruby/rulecom.h +116 -0
  290. data/ext/clipsruby/rulecstr.c +892 -0
  291. data/ext/clipsruby/rulecstr.h +53 -0
  292. data/ext/clipsruby/ruledef.c +559 -0
  293. data/ext/clipsruby/ruledef.h +179 -0
  294. data/ext/clipsruby/ruledlt.c +599 -0
  295. data/ext/clipsruby/ruledlt.h +58 -0
  296. data/ext/clipsruby/rulelhs.c +1216 -0
  297. data/ext/clipsruby/rulelhs.h +52 -0
  298. data/ext/clipsruby/rulepsr.c +1073 -0
  299. data/ext/clipsruby/rulepsr.h +61 -0
  300. data/ext/clipsruby/scanner.c +856 -0
  301. data/ext/clipsruby/scanner.h +112 -0
  302. data/ext/clipsruby/setup.h +488 -0
  303. data/ext/clipsruby/sortfun.c +433 -0
  304. data/ext/clipsruby/sortfun.h +55 -0
  305. data/ext/clipsruby/strngfun.c +1173 -0
  306. data/ext/clipsruby/strngfun.h +96 -0
  307. data/ext/clipsruby/strngrtr.c +523 -0
  308. data/ext/clipsruby/strngrtr.h +97 -0
  309. data/ext/clipsruby/symblbin.c +648 -0
  310. data/ext/clipsruby/symblbin.h +64 -0
  311. data/ext/clipsruby/symblcmp.c +893 -0
  312. data/ext/clipsruby/symblcmp.h +61 -0
  313. data/ext/clipsruby/symbol.c +1961 -0
  314. data/ext/clipsruby/symbol.h +243 -0
  315. data/ext/clipsruby/sysdep.c +894 -0
  316. data/ext/clipsruby/sysdep.h +164 -0
  317. data/ext/clipsruby/textpro.c +1388 -0
  318. data/ext/clipsruby/textpro.h +77 -0
  319. data/ext/clipsruby/tmpltbin.c +609 -0
  320. data/ext/clipsruby/tmpltbin.h +108 -0
  321. data/ext/clipsruby/tmpltbsc.c +327 -0
  322. data/ext/clipsruby/tmpltbsc.h +87 -0
  323. data/ext/clipsruby/tmpltcmp.c +450 -0
  324. data/ext/clipsruby/tmpltcmp.h +57 -0
  325. data/ext/clipsruby/tmpltdef.c +584 -0
  326. data/ext/clipsruby/tmpltdef.h +155 -0
  327. data/ext/clipsruby/tmpltfun.c +2477 -0
  328. data/ext/clipsruby/tmpltfun.h +122 -0
  329. data/ext/clipsruby/tmpltlhs.c +379 -0
  330. data/ext/clipsruby/tmpltlhs.h +50 -0
  331. data/ext/clipsruby/tmpltpsr.c +819 -0
  332. data/ext/clipsruby/tmpltpsr.h +59 -0
  333. data/ext/clipsruby/tmpltrhs.c +595 -0
  334. data/ext/clipsruby/tmpltrhs.h +55 -0
  335. data/ext/clipsruby/tmpltutl.c +637 -0
  336. data/ext/clipsruby/tmpltutl.h +82 -0
  337. data/ext/clipsruby/userdata.c +156 -0
  338. data/ext/clipsruby/userdata.h +72 -0
  339. data/ext/clipsruby/userfunctions.c +70 -0
  340. data/ext/clipsruby/usrsetup.h +7 -0
  341. data/ext/clipsruby/utility.c +1594 -0
  342. data/ext/clipsruby/utility.h +250 -0
  343. data/ext/clipsruby/watch.c +865 -0
  344. data/ext/clipsruby/watch.h +124 -0
  345. data/lib/clipsruby.rb +1 -0
  346. metadata +388 -0
@@ -0,0 +1,433 @@
1
+ /*******************************************************/
2
+ /* "C" Language Integrated Production System */
3
+ /* */
4
+ /* CLIPS Version 6.40 08/25/16 */
5
+ /* */
6
+ /* SORT FUNCTIONS MODULE */
7
+ /*******************************************************/
8
+
9
+ /*************************************************************/
10
+ /* Purpose: Contains the code for sorting functions. */
11
+ /* */
12
+ /* Principal Programmer(s): */
13
+ /* Gary D. Riley */
14
+ /* */
15
+ /* Contributing Programmer(s): */
16
+ /* */
17
+ /* Revision History: */
18
+ /* */
19
+ /* 6.23: Correction for FalseSymbol/TrueSymbol. DR0859 */
20
+ /* */
21
+ /* 6.24: The sort function leaks memory when called */
22
+ /* with a multifield value of length zero. */
23
+ /* DR0864 */
24
+ /* */
25
+ /* 6.30: Added environment cleanup call function */
26
+ /* DeallocateSortFunctionData. */
27
+ /* */
28
+ /* 6.40: Pragma once and other inclusion changes. */
29
+ /* */
30
+ /* Added support for booleans with <stdbool.h>. */
31
+ /* */
32
+ /* Removed use of void pointers for specific */
33
+ /* data structures. */
34
+ /* */
35
+ /* UDF redesign. */
36
+ /* */
37
+ /*************************************************************/
38
+
39
+ #include "setup.h"
40
+
41
+ #include "argacces.h"
42
+ #include "dffnxfun.h"
43
+ #include "envrnmnt.h"
44
+ #include "evaluatn.h"
45
+ #include "extnfunc.h"
46
+ #include "memalloc.h"
47
+ #include "multifld.h"
48
+ #include "sysdep.h"
49
+
50
+ #include "sortfun.h"
51
+
52
+ #define SORTFUN_DATA 7
53
+
54
+ struct sortFunctionData
55
+ {
56
+ struct expr *SortComparisonFunction;
57
+ };
58
+
59
+ #define SortFunctionData(theEnv) ((struct sortFunctionData *) GetEnvironmentData(theEnv,SORTFUN_DATA))
60
+
61
+ /***************************************/
62
+ /* LOCAL INTERNAL FUNCTION DEFINITIONS */
63
+ /***************************************/
64
+
65
+ static void DoMergeSort(Environment *,UDFValue *,UDFValue *,size_t,
66
+ size_t,size_t,size_t,
67
+ bool (*)(Environment *,UDFValue *,UDFValue *));
68
+ static bool DefaultCompareSwapFunction(Environment *,UDFValue *,UDFValue *);
69
+ static void DeallocateSortFunctionData(Environment *);
70
+
71
+ /****************************************/
72
+ /* SortFunctionDefinitions: Initializes */
73
+ /* the sorting functions. */
74
+ /****************************************/
75
+ void SortFunctionDefinitions(
76
+ Environment *theEnv)
77
+ {
78
+ AllocateEnvironmentData(theEnv,SORTFUN_DATA,sizeof(struct sortFunctionData),DeallocateSortFunctionData);
79
+ #if ! RUN_TIME
80
+ AddUDF(theEnv,"sort","bm",1,UNBOUNDED,"*;y",SortFunction,"SortFunction",NULL);
81
+ #endif
82
+ }
83
+
84
+ /*******************************************************/
85
+ /* DeallocateSortFunctionData: Deallocates environment */
86
+ /* data for the sort function. */
87
+ /*******************************************************/
88
+ static void DeallocateSortFunctionData(
89
+ Environment *theEnv)
90
+ {
91
+ ReturnExpression(theEnv,SortFunctionData(theEnv)->SortComparisonFunction);
92
+ }
93
+
94
+ /********************************/
95
+ /* DefaultCompareSwapFunction: */
96
+ /********************************/
97
+ static bool DefaultCompareSwapFunction(
98
+ Environment *theEnv,
99
+ UDFValue *item1,
100
+ UDFValue *item2)
101
+ {
102
+ UDFValue returnValue;
103
+
104
+ SortFunctionData(theEnv)->SortComparisonFunction->argList = GenConstant(theEnv,item1->header->type,item1->value);
105
+ SortFunctionData(theEnv)->SortComparisonFunction->argList->nextArg = GenConstant(theEnv,item2->header->type,item2->value);
106
+ ExpressionInstall(theEnv,SortFunctionData(theEnv)->SortComparisonFunction);
107
+ EvaluateExpression(theEnv,SortFunctionData(theEnv)->SortComparisonFunction,&returnValue);
108
+ ExpressionDeinstall(theEnv,SortFunctionData(theEnv)->SortComparisonFunction);
109
+ ReturnExpression(theEnv,SortFunctionData(theEnv)->SortComparisonFunction->argList);
110
+ SortFunctionData(theEnv)->SortComparisonFunction->argList = NULL;
111
+
112
+ if (returnValue.value == FalseSymbol(theEnv))
113
+ { return false; }
114
+
115
+ return true;
116
+ }
117
+
118
+ /************************************/
119
+ /* SortFunction: H/L access routine */
120
+ /* for the rest$ function. */
121
+ /************************************/
122
+ void SortFunction(
123
+ Environment *theEnv,
124
+ UDFContext *context,
125
+ UDFValue *returnValue)
126
+ {
127
+ unsigned argumentCount, i;
128
+ unsigned long k = 0;
129
+ size_t j;
130
+ UDFValue *theArguments, *theArguments2;
131
+ UDFValue theArg;
132
+ Multifield *theMultifield, *tempMultifield;
133
+ const char *functionName;
134
+ struct expr *functionReference;
135
+ size_t argumentSize = 0;
136
+ struct functionDefinition *fptr;
137
+ #if DEFFUNCTION_CONSTRUCT
138
+ Deffunction *dptr;
139
+ #endif
140
+
141
+ /*==================================*/
142
+ /* Set up the default return value. */
143
+ /*==================================*/
144
+
145
+ returnValue->lexemeValue = FalseSymbol(theEnv);
146
+
147
+ /*=============================================*/
148
+ /* Verify that the comparison function exists. */
149
+ /*=============================================*/
150
+
151
+ if (! UDFNthArgument(context,1,SYMBOL_BIT,&theArg))
152
+ { return; }
153
+
154
+ functionName = theArg.lexemeValue->contents;
155
+ functionReference = FunctionReferenceExpression(theEnv,functionName);
156
+ if (functionReference == NULL)
157
+ {
158
+ ExpectedTypeError1(theEnv,"sort",1,"function name, deffunction name, or defgeneric name");
159
+ return;
160
+ }
161
+
162
+ /*======================================*/
163
+ /* For an external function, verify the */
164
+ /* correct number of arguments. */
165
+ /*======================================*/
166
+
167
+ if (functionReference->type == FCALL)
168
+ {
169
+ fptr = functionReference->functionValue;
170
+ if ((GetMinimumArgs(fptr) > 2) ||
171
+ (GetMaximumArgs(fptr) == 0) ||
172
+ (GetMaximumArgs(fptr) == 1))
173
+ {
174
+ ExpectedTypeError1(theEnv,"sort",1,"function name expecting two arguments");
175
+ ReturnExpression(theEnv,functionReference);
176
+ return;
177
+ }
178
+ }
179
+
180
+ /*=======================================*/
181
+ /* For a deffunction, verify the correct */
182
+ /* number of arguments. */
183
+ /*=======================================*/
184
+
185
+ #if DEFFUNCTION_CONSTRUCT
186
+ if (functionReference->type == PCALL)
187
+ {
188
+ dptr = (Deffunction *) functionReference->value;
189
+ if ((dptr->minNumberOfParameters > 2) ||
190
+ (dptr->maxNumberOfParameters == 0) ||
191
+ (dptr->maxNumberOfParameters == 1))
192
+ {
193
+ ExpectedTypeError1(theEnv,"sort",1,"deffunction name expecting two arguments");
194
+ ReturnExpression(theEnv,functionReference);
195
+ return;
196
+ }
197
+ }
198
+ #endif
199
+
200
+ /*=====================================*/
201
+ /* If there are no items to be sorted, */
202
+ /* then return an empty multifield. */
203
+ /*=====================================*/
204
+
205
+ argumentCount = UDFArgumentCount(context);
206
+
207
+ if (argumentCount == 1)
208
+ {
209
+ SetMultifieldErrorValue(theEnv,returnValue);
210
+ ReturnExpression(theEnv,functionReference);
211
+ return;
212
+ }
213
+
214
+ /*=====================================*/
215
+ /* Retrieve the arguments to be sorted */
216
+ /* and determine how many there are. */
217
+ /*=====================================*/
218
+
219
+ theArguments = (UDFValue *) genalloc(theEnv,(argumentCount - 1) * sizeof(UDFValue));
220
+
221
+ for (i = 2; i <= argumentCount; i++)
222
+ {
223
+ UDFNthArgument(context,i,ANY_TYPE_BITS,&theArguments[i-2]);
224
+
225
+ if (theArguments[i-2].header->type == MULTIFIELD_TYPE)
226
+ { argumentSize += theArguments[i-2].range; }
227
+ else
228
+ { argumentSize++; }
229
+ }
230
+
231
+ if (argumentSize == 0)
232
+ {
233
+ genfree(theEnv,theArguments,(argumentCount - 1) * sizeof(UDFValue)); /* Bug Fix */
234
+ SetMultifieldErrorValue(theEnv,returnValue);
235
+ ReturnExpression(theEnv,functionReference);
236
+ return;
237
+ }
238
+
239
+ /*====================================*/
240
+ /* Pack all of the items to be sorted */
241
+ /* into a data object array. */
242
+ /*====================================*/
243
+
244
+ theArguments2 = (UDFValue *) genalloc(theEnv,argumentSize * sizeof(UDFValue));
245
+
246
+ for (i = 2; i <= argumentCount; i++)
247
+ {
248
+ if (theArguments[i-2].header->type == MULTIFIELD_TYPE)
249
+ {
250
+ tempMultifield = theArguments[i-2].multifieldValue;
251
+ for (j = theArguments[i-2].begin; j < (theArguments[i-2].begin + theArguments[i-2].range); j++, k++)
252
+ {
253
+ theArguments2[k].value = tempMultifield->contents[j].value;
254
+ }
255
+ }
256
+ else
257
+ {
258
+ theArguments2[k].value = theArguments[i-2].value;
259
+ k++;
260
+ }
261
+ }
262
+
263
+ genfree(theEnv,theArguments,(argumentCount - 1) * sizeof(UDFValue));
264
+
265
+ functionReference->nextArg = SortFunctionData(theEnv)->SortComparisonFunction;
266
+ SortFunctionData(theEnv)->SortComparisonFunction = functionReference;
267
+
268
+ for (i = 0; i < argumentSize; i++)
269
+ { RetainUDFV(theEnv,&theArguments2[i]); }
270
+
271
+ MergeSort(theEnv,argumentSize,theArguments2,DefaultCompareSwapFunction);
272
+
273
+ for (i = 0; i < argumentSize; i++)
274
+ { ReleaseUDFV(theEnv,&theArguments2[i]); }
275
+
276
+ SortFunctionData(theEnv)->SortComparisonFunction = SortFunctionData(theEnv)->SortComparisonFunction->nextArg;
277
+ functionReference->nextArg = NULL;
278
+ ReturnExpression(theEnv,functionReference);
279
+
280
+ theMultifield = CreateMultifield(theEnv,argumentSize);
281
+
282
+ for (i = 0; i < argumentSize; i++)
283
+ {
284
+ theMultifield->contents[i].value = theArguments2[i].value;
285
+ }
286
+
287
+ genfree(theEnv,theArguments2,argumentSize * sizeof(UDFValue));
288
+
289
+ returnValue->begin = 0;
290
+ returnValue->range = argumentSize;
291
+ returnValue->value = theMultifield;
292
+ }
293
+
294
+ /*******************************************/
295
+ /* MergeSort: Sorts a list of fields */
296
+ /* according to user specified criteria. */
297
+ /*******************************************/
298
+ void MergeSort(
299
+ Environment *theEnv,
300
+ size_t listSize,
301
+ UDFValue *theList,
302
+ bool (*swapFunction)(Environment *,UDFValue *,UDFValue *))
303
+ {
304
+ UDFValue *tempList;
305
+ size_t middle;
306
+
307
+ if (listSize <= 1) return;
308
+
309
+ /*==============================*/
310
+ /* Create the temporary storage */
311
+ /* needed for the merge sort. */
312
+ /*==============================*/
313
+
314
+ tempList = (UDFValue *) genalloc(theEnv,listSize * sizeof(UDFValue));
315
+
316
+ /*=====================================*/
317
+ /* Call the merge sort driver routine. */
318
+ /*=====================================*/
319
+
320
+ middle = (listSize + 1) / 2;
321
+ DoMergeSort(theEnv,theList,tempList,0,middle-1,middle,listSize - 1,swapFunction);
322
+
323
+ /*==================================*/
324
+ /* Deallocate the temporary storage */
325
+ /* needed by the merge sort. */
326
+ /*==================================*/
327
+
328
+ genfree(theEnv,tempList,listSize * sizeof(UDFValue));
329
+ }
330
+
331
+
332
+ /******************************************************/
333
+ /* DoMergeSort: Driver routine for performing a merge */
334
+ /* sort on an array of UDFValue structures. */
335
+ /******************************************************/
336
+ static void DoMergeSort(
337
+ Environment *theEnv,
338
+ UDFValue *theList,
339
+ UDFValue *tempList,
340
+ size_t s1,
341
+ size_t e1,
342
+ size_t s2,
343
+ size_t e2,
344
+ bool (*swapFunction)(Environment *,UDFValue *,UDFValue *))
345
+ {
346
+ UDFValue temp;
347
+ size_t middle, size;
348
+ size_t c1, c2, mergePoint;
349
+
350
+ /* Sort the two subareas before merging them. */
351
+
352
+ if (s1 == e1)
353
+ { /* List doesn't need to be merged. */ }
354
+ else if ((s1 + 1) == e1)
355
+ {
356
+ if ((*swapFunction)(theEnv,&theList[s1],&theList[e1]))
357
+ {
358
+ TransferDataObjectValues(&temp,&theList[s1]);
359
+ TransferDataObjectValues(&theList[s1],&theList[e1]);
360
+ TransferDataObjectValues(&theList[e1],&temp);
361
+ }
362
+ }
363
+ else
364
+ {
365
+ size = ((e1 - s1) + 1);
366
+ middle = s1 + ((size + 1) / 2);
367
+ DoMergeSort(theEnv,theList,tempList,s1,middle-1,middle,e1,swapFunction);
368
+ }
369
+
370
+ if (s2 == e2)
371
+ { /* List doesn't need to be merged. */ }
372
+ else if ((s2 + 1) == e2)
373
+ {
374
+ if ((*swapFunction)(theEnv,&theList[s2],&theList[e2]))
375
+ {
376
+ TransferDataObjectValues(&temp,&theList[s2]);
377
+ TransferDataObjectValues(&theList[s2],&theList[e2]);
378
+ TransferDataObjectValues(&theList[e2],&temp);
379
+ }
380
+ }
381
+ else
382
+ {
383
+ size = ((e2 - s2) + 1);
384
+ middle = s2 + ((size + 1) / 2);
385
+ DoMergeSort(theEnv,theList,tempList,s2,middle-1,middle,e2,swapFunction);
386
+ }
387
+
388
+ /*======================*/
389
+ /* Merge the two areas. */
390
+ /*======================*/
391
+
392
+ mergePoint = s1;
393
+ c1 = s1;
394
+ c2 = s2;
395
+
396
+ while (mergePoint <= e2)
397
+ {
398
+ if (c1 > e1)
399
+ {
400
+ TransferDataObjectValues(&tempList[mergePoint],&theList[c2]);
401
+ c2++;
402
+ mergePoint++;
403
+ }
404
+ else if (c2 > e2)
405
+ {
406
+ TransferDataObjectValues(&tempList[mergePoint],&theList[c1]);
407
+ c1++;
408
+ mergePoint++;
409
+ }
410
+ else if ((*swapFunction)(theEnv,&theList[c1],&theList[c2]))
411
+ {
412
+ TransferDataObjectValues(&tempList[mergePoint],&theList[c2]);
413
+ c2++;
414
+ mergePoint++;
415
+ }
416
+ else
417
+ {
418
+ TransferDataObjectValues(&tempList[mergePoint],&theList[c1]);
419
+ c1++;
420
+ mergePoint++;
421
+ }
422
+ }
423
+
424
+ /*=======================================*/
425
+ /* Copy them back to the original array. */
426
+ /*=======================================*/
427
+
428
+ for (c1 = s1; c1 <= e2; c1++)
429
+ { TransferDataObjectValues(&theList[c1],&tempList[c1]); }
430
+ }
431
+
432
+
433
+
@@ -0,0 +1,55 @@
1
+ /*******************************************************/
2
+ /* "C" Language Integrated Production System */
3
+ /* */
4
+ /* CLIPS Version 6.40 08/25/16 */
5
+ /* */
6
+ /* SORT FUNCTIONS HEADER MODULE */
7
+ /*******************************************************/
8
+
9
+ /*************************************************************/
10
+ /* Purpose: Contains the code for sorting functions. */
11
+ /* */
12
+ /* Principal Programmer(s): */
13
+ /* Gary D. Riley */
14
+ /* */
15
+ /* Contributing Programmer(s): */
16
+ /* */
17
+ /* Revision History: */
18
+ /* */
19
+ /* 6.23: Correction for FalseSymbol/TrueSymbol. DR0859 */
20
+ /* */
21
+ /* 6.24: The sort function leaks memory when called */
22
+ /* with a multifield value of length zero. */
23
+ /* DR0864 */
24
+ /* */
25
+ /* 6.30: Added environment cleanup call function */
26
+ /* DeallocateSortFunctionData. */
27
+ /* */
28
+ /* 6.40: Removed LOCALE definition. */
29
+ /* */
30
+ /* Pragma once and other inclusion changes. */
31
+ /* */
32
+ /* Added support for booleans with <stdbool.h>. */
33
+ /* */
34
+ /* Removed use of void pointers for specific */
35
+ /* data structures. */
36
+ /* */
37
+ /* UDF redesign. */
38
+ /* */
39
+ /*************************************************************/
40
+
41
+ #ifndef _H_sortfun
42
+
43
+ #pragma once
44
+
45
+ #define _H_sortfun
46
+
47
+ void SortFunctionDefinitions(Environment *);
48
+ void MergeSort(Environment *,size_t,UDFValue *,
49
+ bool (*)(Environment *,UDFValue *,UDFValue *));
50
+ void SortFunction(Environment *,UDFContext *,UDFValue *);
51
+
52
+ #endif /* _H_sortfun */
53
+
54
+
55
+