Mercurial > hg > amuse
comparison base/methods.lisp @ 304:fbeaedd242c6
Add some base methods for searching and sorting events.
Ignore-this: 4906722dfd6d1cfb91df8b85591f8b46
darcs-hash:20091020091537-16a00-1efcd0569b9a242be75bf99908b9117dc5946762.gz
author | j.forth <j.forth@gold.ac.uk> |
---|---|
date | Tue, 20 Oct 2009 10:15:37 +0100 |
parents | ed3ace83c975 |
children | a85c59bcd88b |
comparison
equal
deleted
inserted
replaced
303:3c5bf3f7b7a0 | 304:fbeaedd242c6 |
---|---|
564 ;; directly. There may be sensible defaults, but it's the job | 564 ;; directly. There may be sensible defaults, but it's the job |
565 ;; of an implementation's author to solve that. | 565 ;; of an implementation's author to solve that. |
566 (error 'insufficient-information :operation 'beat-period :datatype (class-of composition))))) | 566 (error 'insufficient-information :operation 'beat-period :datatype (class-of composition))))) |
567 | 567 |
568 | 568 |
569 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 569 ;;;======================================================================== |
570 ;; Copying events in time | 570 ;;; Copying events in time |
571 ;;;======================================================================== | |
571 | 572 |
572 (defmethod move-to-first-bar ((composition composition)) | 573 (defmethod move-to-first-bar ((composition composition)) |
573 (let ((offset (floor (timepoint (elt composition 0))))) | 574 (let ((offset (floor (timepoint (elt composition 0))))) |
574 (loop | 575 (loop |
575 for event in (%list-slot-sequence-data composition) | 576 for event in (%list-slot-sequence-data composition) |
576 do (setf event (copy-event event)) | 577 do (setf event (copy-event event)) |
577 do (setf (timepoint event) | 578 do (setf (timepoint event) |
578 (- (timepoint event) offset)) | 579 (- (timepoint event) offset)) |
579 collect event into shifted-events | 580 collect event into shifted-events |
580 finally (return shifted-events)))) | 581 finally (return shifted-events)))) |
582 | |
583 | |
584 ;;;======================================================================== | |
585 ;;; Searching for events | |
586 ;;;======================================================================== | |
587 | |
588 (defmethod find-next-event ((source-event event) &key predicate test | |
589 break-test search-list) | |
590 "Ideally a sorted search list that begins with the first event after | |
591 the source-event should be provided, otherwise, the search will begin | |
592 from the beginning." | |
593 (unless search-list (setf search-list (composition source-event))) | |
594 (cond | |
595 ((and test predicate) | |
596 (error "Supplied both a test and a predicate.")) | |
597 (test | |
598 (sequence:dosequence (e search-list nil) | |
599 (when (and (time> (onset e) (onset source-event)) | |
600 (funcall test source-event e)) | |
601 (return e)) | |
602 (when break-test | |
603 (when (funcall break-test source-event e) | |
604 (return nil))))) | |
605 (predicate | |
606 (sequence:dosequence (e search-list nil) | |
607 (when (and (time> (onset e) (onset source-event)) | |
608 (funcall predicate e)) | |
609 (return e)) | |
610 (when break-test | |
611 (when (funcall break-test source-event e) | |
612 (return nil))))))) | |
613 | |
614 | |
615 ;;;======================================================================== | |
616 ;;; Sorting Compositions | |
617 ;;;======================================================================== | |
618 | |
619 (defmethod event< ((event1 event) (event2 event) attribute-list) | |
620 (dolist (attribute attribute-list nil) ;nil if equal | |
621 (if (< (funcall attribute event1) (funcall attribute event2)) | |
622 (return t) | |
623 (if (> (funcall attribute event1) (funcall attribute event2)) | |
624 (return nil))))) | |
625 | |
626 (defun make-event< (attribute-list) | |
627 (lambda (event1 event2) | |
628 (funcall #'event< event1 event2 attribute-list))) | |
629 | |
630 (defmethod sort-composition ((composition composition) dimension-spec) | |
631 (sequence:make-sequence-like composition | |
632 (length composition) | |
633 :initial-contents | |
634 (stable-sort | |
635 (copy-seq composition) | |
636 (make-event< dimension-spec)))) |