examples.lisp 6.51 KB
Newer Older
kilian.sprotte's avatar
kilian.sprotte committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
;;; -*- Mode: LISP; Syntax: COMMON-LISP; Base: 10 -*-
;;; arch-tag: 7debdb50-4feb-4e57-b6fd-a1d1eaf6ceab

;;; Copyright (c) 2006, Kilian Sprotte. All rights reserved.

;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:

;;;   * Redistributions of source code must retain the above copyright
;;;     notice, this list of conditions and the following disclaimer.

;;;   * Redistributions in binary form must reproduce the above
;;;     copyright notice, this list of conditions and the following
;;;     disclaimer in the documentation and/or other materials
;;;     provided with the distribution.

;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

(in-package :cl-user)

kilian.sprotte's avatar
kilian.sprotte committed
32
;; TODO maybe use gecol:intvar-assigned
kilian.sprotte's avatar
kilian.sprotte committed
33
(defun cartesian-product ()
kilian.sprotte's avatar
kilian.sprotte committed
34
  (let ((s (gecol:make-gecolspace :intnum 3 :intmin 1 :intmax 3)))
kilian.sprotte's avatar
kilian.sprotte committed
35
    (gecol:with-var-arg-array ((loop for i below 3 collect (gecol:gecolspace-getint-int s i))
kilian.sprotte's avatar
kilian.sprotte committed
36
                               varargs)
kilian.sprotte's avatar
kilian.sprotte committed
37 38
      (gecol:branch-intvarargs-bvarsel-bvalsel s varargs :bvar-none :bval-min))
    (let ((e (gecol:make-dfs-space-int-int-stop s)))
kilian.sprotte's avatar
kilian.sprotte committed
39
      (loop
kilian.sprotte's avatar
kilian.sprotte committed
40 41 42 43 44 45 46
         for sol = (gecol:dfs-next e)
         until (cffi:null-pointer-p sol)
         do (format t "~a, ~a, ~a~%"
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 0))
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 1))
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 2)))
         do (gecol:delete-gecolspace sol))
kilian.sprotte's avatar
kilian.sprotte committed
47 48
      (gecol:delete-dfs e)
      (gecol:delete-gecolspace s))))
kilian.sprotte's avatar
kilian.sprotte committed
49 50

(defun distinct ()
kilian.sprotte's avatar
kilian.sprotte committed
51
  (let ((s (gecol:make-gecolspace :intnum 3 :intmin 1 :intmax 3)))
kilian.sprotte's avatar
kilian.sprotte committed
52
    (gecol:with-var-arg-array ((loop for i below 3 collect (gecol:gecolspace-getint-int s i))
kilian.sprotte's avatar
kilian.sprotte committed
53
                               varargs)
kilian.sprotte's avatar
kilian.sprotte committed
54 55 56
      (gecol:distinct-intvarargs-intconlevel s varargs :icl-def)
      (gecol:branch-intvarargs-bvarsel-bvalsel s varargs :bvar-none :bval-min))
    (let ((e (gecol:make-dfs-space-int-int-stop s)))
kilian.sprotte's avatar
kilian.sprotte committed
57
      (loop
kilian.sprotte's avatar
kilian.sprotte committed
58 59 60 61 62 63 64
         for sol = (gecol:dfs-next e)
         until (cffi:null-pointer-p sol)
         do (format t "~a, ~a, ~a~%"
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 0))
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 1))
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 2)))
         do (gecol:delete-gecolspace sol))
kilian.sprotte's avatar
kilian.sprotte committed
65 66
      (gecol:delete-dfs e)
      (gecol:delete-gecolspace s))))
kilian.sprotte's avatar
kilian.sprotte committed
67 68

(defun sorted ()
kilian.sprotte's avatar
kilian.sprotte committed
69 70
  (let ((s (gecol:make-gecolspace :intnum 3 :intmin 1 :intmax 3)))
    (gecol:rel-intvar-intreltype-intvar-intconlevel s
kilian.sprotte's avatar
kilian.sprotte committed
71 72 73 74
                                                    (gecol:gecolspace-getint-int s 0)
                                                    :irt-<
                                                    (gecol:gecolspace-getint-int s 1)
                                                    :icl-def)
kilian.sprotte's avatar
kilian.sprotte committed
75
    (gecol:rel-intvar-intreltype-intvar-intconlevel s
kilian.sprotte's avatar
kilian.sprotte committed
76 77 78 79 80
                                                    (gecol:gecolspace-getint-int s 1)
                                                    :irt-<
                                                    (gecol:gecolspace-getint-int s 2)
                                                    :icl-def)

kilian.sprotte's avatar
kilian.sprotte committed
81
    (gecol:with-var-arg-array ((loop for i below 3 collect (gecol:gecolspace-getint-int s i))
kilian.sprotte's avatar
kilian.sprotte committed
82
                               varargs)
kilian.sprotte's avatar
kilian.sprotte committed
83 84
      (gecol:branch-intvarargs-bvarsel-bvalsel s varargs :bvar-none :bval-min))
    (let ((e (gecol:make-dfs-space-int-int-stop s)))
kilian.sprotte's avatar
kilian.sprotte committed
85
      (loop
kilian.sprotte's avatar
kilian.sprotte committed
86 87 88 89 90 91 92
         for sol = (gecol:dfs-next e)
         until (cffi:null-pointer-p sol)
         do (format t "~a, ~a, ~a~%"
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 0))
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 1))
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 2)))
         do (gecol:delete-gecolspace sol))
kilian.sprotte's avatar
kilian.sprotte committed
93 94
      (gecol:delete-dfs e)
      (gecol:delete-gecolspace s))))
kilian.sprotte's avatar
kilian.sprotte committed
95 96

(defun cartesian-product-distr-max ()
kilian.sprotte's avatar
kilian.sprotte committed
97
  "Like cartesian-product, but using a different branching."
kilian.sprotte's avatar
kilian.sprotte committed
98
  (let ((s (gecol:make-gecolspace :intnum 3 :intmin 1 :intmax 3)))
kilian.sprotte's avatar
kilian.sprotte committed
99
    (gecol:with-var-arg-array ((loop for i below 3 collect (gecol:gecolspace-getint-int s i))
kilian.sprotte's avatar
kilian.sprotte committed
100
                               varargs)
kilian.sprotte's avatar
kilian.sprotte committed
101 102 103
      ;; branching
      (gecol:branch-intvarargs-bvarsel-bvalsel s varargs :bvar-none :bval-max))
    (let ((e (gecol:make-dfs-space-int-int-stop s)))
kilian.sprotte's avatar
kilian.sprotte committed
104
      (loop
kilian.sprotte's avatar
kilian.sprotte committed
105 106 107 108 109 110 111
         for sol = (gecol:dfs-next e)
         until (cffi:null-pointer-p sol)
         do (format t "~a, ~a, ~a~%"
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 0))
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 1))
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 2)))
         do (gecol:delete-gecolspace sol))
kilian.sprotte's avatar
kilian.sprotte committed
112 113
      (gecol:delete-dfs e)
      (gecol:delete-gecolspace s))))
kilian.sprotte's avatar
kilian.sprotte committed
114

kilian.sprotte's avatar
kilian.sprotte committed
115 116 117
(defun distinct-minimal-third ()
  "Like distinct, but minimizing the third variable."
  (let ((s (gecol:make-gecolspace :intnum 3 :intmin 1 :intmax 3
kilian.sprotte's avatar
kilian.sprotte committed
118
                                  :bab-intvar-ind 2 :bab-intreltype :irt-<)))
kilian.sprotte's avatar
kilian.sprotte committed
119
    (gecol:with-var-arg-array ((loop for i below 3 collect (gecol:gecolspace-getint-int s i))
kilian.sprotte's avatar
kilian.sprotte committed
120
                               varargs)
kilian.sprotte's avatar
kilian.sprotte committed
121 122 123 124
      (gecol:distinct-intvarargs-intconlevel s varargs :icl-def)
      (gecol:branch-intvarargs-bvarsel-bvalsel s varargs :bvar-none :bval-min))
    (let ((e (gecol:make-bab-space-int-int-stop s)))
      (loop
kilian.sprotte's avatar
kilian.sprotte committed
125 126 127 128 129 130 131
         for sol = (gecol:bab-next e)
         until (cffi:null-pointer-p sol)
         do (format t "~a, ~a, ~a~%"
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 0))
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 1))
                    (gecol:intvar-val (gecol:gecolspace-getint-int sol 2)))
         do (gecol:delete-gecolspace sol))
kilian.sprotte's avatar
kilian.sprotte committed
132 133
      (gecol:delete-bab e)
      (gecol:delete-gecolspace s))))