Advertisement
A-G-D

BooleanExpression

Feb 9th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.47 KB | None | 0 0
  1. library BooleanExpression /*
  2.  
  3.  
  4. */requires /*
  5.  
  6. */Table /*
  7. */Alloc /*
  8. */LinkedList /*
  9. */Initializer /*
  10.  
  11.  
  12. *///! novjass
  13.  
  14. |=====|
  15. | API |
  16. |=====|
  17.  
  18. struct BoolExpr/*
  19.  
  20. */readonly boolexpr boolexpr/* The boolexpr of a BoolExpr
  21.  
  22. */static method operator [] takes code c returns thistype/*
  23. - Returns a corresponding BoolExpr for the code argument
  24. - Creates a new BoolExpr is there is no BoolExpr created for the
  25. code yet
  26.  
  27. */static method create takes code c returns BoolExpr/*
  28. - Creates a new BoolExpr using code
  29.  
  30. */static method createEx takes boolexpr expr returns thistype/*
  31. - Creates a new BoolExpr using a boolexpr
  32.  
  33. */method destroy takes nothing returns nothing/*
  34. - Destroys a BoolExpr and destroys its boolexpr if the BoolExpr
  35. is created using boolexprOr()/boolexprAnd()/boolexprNot()
  36.  
  37. */method boolexprOr takes BoolExpr expr returns BoolExpr/*
  38. - Creates a new BoolExpr with a boolexpr created using Or()
  39.  
  40. */method boolexprAnd takes BoolExpr expr returns BoolExpr/*
  41. - Creates a new BoolExpr with a boolexpr created using And()
  42.  
  43. */method boolexprNot takes BoolExpr expr returns BoolExpr/*
  44. - Creates a new BoolExpr with a boolexpr created using Not()
  45.  
  46.  
  47. */struct BooleanExpression/*
  48.  
  49. */readonly BoolExpr expression/* The BoolExpr of the BooleanExpression
  50.  
  51. */static method create takes nothing returns BooleanExpression/*
  52. - Creates a new BooleanExpression
  53.  
  54. */method destroy takes nothing returns nothing/*
  55. - Destroys the BooleanExpression
  56.  
  57. */method clear takes nothing returns BooleanExpression/*
  58. - Clears all BoolExprs registered to the BooleanExpression
  59.  
  60. */method register takes BoolExpr expr returns BooleanExpression/*
  61. - Registers a BoolExpr to the BoolExpr of the BooleanExpression by
  62. joining them with boolexprOr()
  63.  
  64. */method unregister takes BoolExpr expr returns BooleanExpression/*
  65. - Unregisters a BoolExpr from the BoolExpression
  66.  
  67. */method duplicate takes nothing returns BooleanExpression/*
  68. - Creates a duplicate of the BooleanExpression containing the same
  69. BoolExpr nodes
  70.  
  71. *///! endnovjass
  72.  
  73.  
  74. struct BoolExpr extends array
  75.  
  76. private static Table expr
  77. private static Table canDestroy
  78.  
  79. implement AllocT
  80.  
  81. static method createEx takes boolexpr filter returns thistype
  82. local thistype this = allocate()
  83. local integer id = GetHandleId(filter)
  84. set expr.boolexpr[this] = filter
  85. if not expr.has(id) then
  86. set expr[id] = this
  87. endif
  88. return this
  89. endmethod
  90.  
  91. static method create takes code c returns thistype
  92. return createEx(Filter(c))
  93. endmethod
  94.  
  95. static method operator [] takes code c returns thistype
  96. local integer id = GetHandleId(Filter(c))
  97. if expr.has(id) then
  98. return expr[id]
  99. endif
  100. return create(c)
  101. endmethod
  102.  
  103. method destroy takes nothing returns nothing
  104. call .deallocate()
  105. if canDestroy.boolean[this] then
  106. call DestroyBoolExpr(expr.boolexpr[this])
  107. endif
  108. call expr.boolexpr.remove(this)
  109. call canDestroy.boolean.remove(this)
  110. endmethod
  111.  
  112. method boolexprOr takes thistype other returns thistype
  113. local thistype new = allocate()
  114. set canDestroy.boolean[new] = true
  115. set expr.boolexpr[new] = Or(expr.boolexpr[this], expr.boolexpr[other])
  116. return new
  117. endmethod
  118.  
  119. method boolexprAnd takes thistype other returns thistype
  120. local thistype new = allocate()
  121. set canDestroy.boolean[new] = true
  122. set expr.boolexpr[new] = And(expr.boolexpr[this], expr.boolexpr[other])
  123. return new
  124. endmethod
  125.  
  126. method boolexprNot takes nothing returns thistype
  127. local thistype new = allocate()
  128. set canDestroy.boolean[new] = true
  129. set expr.boolexpr[new] = Not(expr.boolexpr[this])
  130. return new
  131. endmethod
  132.  
  133. method operator boolexpr takes nothing returns boolexpr
  134. return expr.boolexpr[this]
  135. endmethod
  136.  
  137. private static method init takes nothing returns nothing
  138. set expr = Table.create()
  139. set canDestroy = Table.create()
  140. endmethod
  141.  
  142. implement Initializer
  143.  
  144. endstruct
  145.  
  146. struct BooleanExpression extends array
  147.  
  148. private static Table listHead
  149. private static Table parentTree
  150. private static Table childTree
  151. private static Table childNode
  152. private static HashTable instance
  153.  
  154. implement AllocT
  155.  
  156. static method create takes nothing returns thistype
  157. local thistype this = allocate()
  158. set listHead[this] = MasterList.create()
  159. return this
  160. endmethod
  161.  
  162. method register takes BoolExpr expr returns thistype
  163. local BoolExpr new
  164. local MasterList newNode
  165. debug if instance[this].has(expr) then
  166. debug call debug("|CFFFF0000ERROR: Attempt to double-register BoolExpr[" + I2S(expr) + "] to BooleanExpression[" + I2S(this) + "]|R")
  167. debug return 0
  168. debug endif
  169. set newNode = MasterList.allocate()
  170. if not parentTree.has(this) then
  171. set new = BoolExpr.createEx(expr.boolexpr)
  172. else
  173. set new = BoolExpr(parentTree[this]).boolexprOr(expr)
  174. endif
  175. call MasterList(listHead[this]).insertNode(newNode)
  176. set instance[this][expr] = newNode
  177. set childNode[newNode] = expr
  178. set childTree[newNode] = new
  179. set parentTree[this] = new
  180. return this
  181. endmethod
  182.  
  183. method unregister takes BoolExpr expr returns thistype
  184. local MasterList children
  185. local MasterList node
  186. local BoolExpr new
  187. local MasterList head = listHead[this]
  188. local Table t = instance[this]
  189. debug if not t.has(expr) then
  190. debug call debug("|CFFFF0000ERROR: Attempt to unregister an unregistered BoolExpr[" + I2S(expr) + "] from BooleanExpression[" + I2S(this) + "]|R")
  191. debug return 0
  192. debug endif
  193. set node = head.next
  194. loop
  195. exitwhen node == 0
  196. call BoolExpr(childTree[node]).destroy()
  197. set node = node.next
  198. endloop
  199. set node = t[expr]
  200. call node.removeNode()
  201. call node.deallocate()
  202. call t.remove(expr)
  203. set node = head.last
  204. call parentTree.remove(this)
  205. if node != 0 then
  206. loop
  207. exitwhen node == head
  208. if not parentTree.has(this) then
  209. set new = BoolExpr.createEx(BoolExpr(childNode[node]).boolexpr)
  210. else
  211. set new = BoolExpr(parentTree[this]).boolexprOr(childNode[node])
  212. endif
  213. set childTree[node] = new
  214. set parentTree[this] = new
  215. set node = node.prev
  216. endloop
  217. endif
  218. return this
  219. endmethod
  220.  
  221. method duplicate takes nothing returns thistype
  222. local MasterList head = listHead[this]
  223. local MasterList node = head.last
  224. set this = create()
  225. if node != 0 then
  226. loop
  227. exitwhen node == head
  228. call .register(childNode[node])
  229. set node = node.prev
  230. endloop
  231. endif
  232. return this
  233. endmethod
  234.  
  235. method operator expression takes nothing returns BoolExpr
  236. return parentTree[this]
  237. endmethod
  238.  
  239. method clear takes nothing returns thistype
  240. local Table t = instance[this]
  241. local MasterList node = MasterList(listHead[this]).next
  242. loop
  243. exitwhen node == 0
  244. call BoolExpr(childTree[node]).destroy()
  245. call t.remove(childNode[node])
  246. call node.removeNode()
  247. call node.deallocate()
  248. set node = node.next
  249. endloop
  250. call instance.remove(this)
  251. call parentTree.remove(this)
  252. return this
  253. endmethod
  254.  
  255. method destroy takes nothing returns nothing
  256. call .deallocate()
  257. call .clear()
  258. call MasterList(listHead[this]).deallocate()
  259. call listHead.remove(this)
  260. endmethod
  261.  
  262. private static method init takes nothing returns nothing
  263. local TableArray table = TableArray[5]
  264. set listHead = table[0]
  265. set parentTree = table[1]
  266. set childTree = table[2]
  267. set childNode = table[3]
  268. set instance = table[4]
  269. endmethod
  270.  
  271. implement Initializer
  272.  
  273. endstruct
  274.  
  275.  
  276. endlibrary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement