r10724: change from ftp to http access for boa
[kmrcl.git] / equal.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          equal.lisp
6 ;;;; Purpose:       Generalized equal function for KMRCL package
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; KMRCL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19
20 (in-package #:kmrcl)
21
22   
23 (defun generalized-equal (obj1 obj2)
24   (if (not (equal (type-of obj1) (type-of obj2)))
25       (progn
26         (terpri)
27         (describe obj1)
28         (describe obj2)
29         nil)
30     (typecase obj1
31       (double-float
32        (let ((diff (abs (/ (- obj1 obj2) obj1))))
33          (if (> diff (* 10 double-float-epsilon))
34              nil
35            t)))
36       (complex
37        (and (generalized-equal (realpart obj1) (realpart obj2))
38             (generalized-equal (imagpart obj1) (imagpart obj2))))
39       (structure-object
40        (generalized-equal-fielded-object obj1 obj2))
41       (standard-object
42        (generalized-equal-fielded-object obj1 obj2))
43       (hash-table
44        (generalized-equal-hash-table obj1 obj2)
45        )
46       (function
47        (generalized-equal-function obj1 obj2))
48       (string
49        (string= obj1 obj2))
50       (array
51        (generalized-equal-array obj1 obj2))
52       (t
53        (equal obj1 obj2)))))
54
55
56 (defun generalized-equal-function (obj1 obj2)
57   (string= (function-to-string obj1) (function-to-string obj2)))
58
59 (defun generalized-equal-array (obj1 obj2)
60   (block test
61     (when (not (= (array-total-size obj1) (array-total-size obj2)))
62       (return-from test nil))
63     (dotimes (i (array-total-size obj1))
64       (unless (generalized-equal (aref obj1 i) (aref obj2 i))
65         (return-from test nil)))
66     (return-from test t)))
67
68 (defun generalized-equal-hash-table (obj1 obj2)
69   (block test
70     (when (not (= (hash-table-count obj1) (hash-table-count obj2)))
71       (return-from test nil))
72     (maphash
73      #'(lambda (k v)
74          (multiple-value-bind (value found) (gethash k obj2)
75            (unless (and found (generalized-equal v value))
76              (return-from test nil))))
77      obj1)
78     (return-from test t)))
79
80 (defun generalized-equal-fielded-object (obj1 obj2)
81   (block test
82     (when (not (equal (class-of obj1) (class-of obj2)))
83       (return-from test nil))
84     (dolist (field (class-slot-names (class-name (class-of obj1))))
85       (unless (generalized-equal (slot-value obj1 field) (slot-value obj2 field))
86         (return-from test nil)))
87     (return-from test t)))
88
89 (defun class-slot-names (c-name)
90   "Given a CLASS-NAME, returns a list of the slots in the class."
91   #+(or allegro cmu lispworks sbcl scl)
92   (mapcar #'kmr-mop:slot-definition-name
93           (kmr-mop:class-slots (kmr-mop:find-class c-name)))
94   #+(and mcl (not openmcl))
95   (let* ((class (find-class c-name nil)))
96     (when (typep class 'standard-class)
97       (nconc (mapcar #'car (ccl:class-instance-slots class))
98              (mapcar #'car (ccl:class-class-slots class)))))
99   #-(or allegro lispworks cmu mcl sbcl scl openmcl)
100   (error "class-slot-names is not defined on this platform")
101   )
102
103 (defun structure-slot-names (s-name)
104   "Given a STRUCTURE-NAME, returns a list of the slots in the structure."
105   #+allegro (class-slot-names s-name)
106   #+lispworks (structure:structure-class-slot-names
107                (find-class s-name))
108   #+(or sbcl cmu) (mapcar #'kmr-mop:slot-definition-name
109                           (kmr-mop:class-slots (kmr-mop:find-class s-name)))
110   #+scl (mapcar #'kernel:dsd-name
111                 (kernel:dd-slots
112                  (kernel:layout-info
113                   (kernel:class-layout (find-class s-name)))))
114   #+(and mcl (not openmcl))
115   (let* ((sd (gethash s-name ccl::%defstructs%))
116                (slots (if sd (ccl::sd-slots sd))))
117           (mapcar #'car (if (symbolp (caar slots)) slots (cdr slots))))
118   #-(or allegro lispworks cmu sbcl scl mcl)
119   (error "structure-slot-names is not defined on this platform")
120   )
121
122 (defun function-to-string (obj)
123   "Returns the lambda code for a function. Relies on
124 Allegro implementation-dependent features."
125   (multiple-value-bind (lambda closurep name) (function-lambda-expression obj)
126     (declare (ignore closurep))
127     (if lambda
128           (format nil "#'~s" lambda)
129       (if name
130           (format nil "#'~s" name)
131         (progn
132           (print obj)
133           (break))))))
134