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