Fast two dimensional array in prolog
What is the most efficient way to represent two dimensional arrays in prolog? I thought of one long list or list of lists, but they have linear access time which seems to be too slow for my problem. I'm not necessarily looking for a ready solution, but rather a concept how 开发者_运维问答it should be implemented.
You can get logarithmic time access with AVL trees or Red-Black trees, see library(assoc) and library(rbtrees) in SWI and YAP. For constant time access, make a term with N arguments, and use arg/3 for efficient access. Each of these arguments can again be a term with arity N, so you have an array with efficient read-access. Using setarg/3, you can even destructively modify elements, at the cost of losing nice logical properties and much more painful debugging and testing. In many cases, you can reformulate your algorithms to not require random-access, and work with lists of lists. If this is not possible, AVL or other balanced trees are often a very good option.
精彩评论