From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Drew Adams" Newsgroups: gmane.emacs.devel Subject: ring.el patch: new functions member, next, previous, convert-sequence etc. Date: Mon, 8 Oct 2007 15:24:30 -0700 Message-ID: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0071_01C809BF.524C1320" X-Trace: sea.gmane.org 1191882351 17837 80.91.229.12 (8 Oct 2007 22:25:51 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 8 Oct 2007 22:25:51 +0000 (UTC) To: "Emacs-Devel" Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Oct 09 00:25:49 2007 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1If12q-0001fe-4y for ged-emacs-devel@m.gmane.org; Tue, 09 Oct 2007 00:25:48 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1If12k-0004vY-Ge for ged-emacs-devel@m.gmane.org; Mon, 08 Oct 2007 18:25:42 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1If12g-0004vT-AS for emacs-devel@gnu.org; Mon, 08 Oct 2007 18:25:38 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1If12d-0004v5-Rp for emacs-devel@gnu.org; Mon, 08 Oct 2007 18:25:36 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1If12d-0004v2-L1 for emacs-devel@gnu.org; Mon, 08 Oct 2007 18:25:35 -0400 Original-Received: from agminet01.oracle.com ([141.146.126.228]) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1If12d-00036X-S1 for emacs-devel@gnu.org; Mon, 08 Oct 2007 18:25:36 -0400 Original-Received: from agmgw1.us.oracle.com (agmgw1.us.oracle.com [152.68.180.212]) by agminet01.oracle.com (Switch-3.2.4/Switch-3.1.7) with ESMTP id l98MPW5j013106 for ; Mon, 8 Oct 2007 17:25:32 -0500 Original-Received: from acsmt350.oracle.com (acsmt350.oracle.com [141.146.40.150]) by agmgw1.us.oracle.com (Switch-3.2.0/Switch-3.2.0) with ESMTP id l98Kjx8g015794 for ; Mon, 8 Oct 2007 16:25:31 -0600 Original-Received: from dhcp-4op11-4op12-west-130-35-178-158.us.oracle.com by acsmt350.oracle.com with ESMTP id 3276367501191882266; Mon, 08 Oct 2007 15:24:26 -0700 X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0) Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3138 X-Brightmail-Tracker: AAAAAQAAAAI= X-Brightmail-Tracker: AAAAAQAAAAI= X-Whitelist: TRUE X-Whitelist: TRUE X-Detected-Kernel: Linux 2.4-2.6 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:80433 Archived-At: This is a multi-part message in MIME format. ------=_NextPart_000_0071_01C809BF.524C1320 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit The attached patch to ring.el provides these new functions: `ring-convert-sequence-to-ring', `ring-insert+extend', `ring-remove+insert+extend', `ring-member', `ring-next', `ring-previous' Change log: 2007-10-08 Drew Adams * ring.el: Added functions ring-convert-sequence-to-ring, ring-insert+extend, ring-remove+insert+extend, ring-member, ring-next, ring-previous ------=_NextPart_000_0071_01C809BF.524C1320 Content-Type: application/octet-stream; name="ring-2007-10-08a.el" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="ring-2007-10-08a.el" ;;; ring.el --- handle rings of items=0A= =0A= ;; Copyright (C) 1992, 2001, 2002, 2003, 2004, 2005,=0A= ;; 2006, 2007 Free Software Foundation, Inc.=0A= =0A= ;; Maintainer: FSF=0A= ;; Keywords: extensions=0A= =0A= ;; This file is part of GNU Emacs.=0A= =0A= ;; GNU Emacs is free software; you can redistribute it and/or modify=0A= ;; it under the terms of the GNU General Public License as published by=0A= ;; the Free Software Foundation; either version 3, or (at your option)=0A= ;; any later version.=0A= =0A= ;; GNU Emacs is distributed in the hope that it will be useful,=0A= ;; but WITHOUT ANY WARRANTY; without even the implied warranty of=0A= ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the=0A= ;; GNU General Public License for more details.=0A= =0A= ;; You should have received a copy of the GNU General Public License=0A= ;; along with GNU Emacs; see the file COPYING. If not, write to the=0A= ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,=0A= ;; Boston, MA 02110-1301, USA.=0A= =0A= ;;; Commentary:=0A= =0A= ;; This code defines a ring data structure. A ring is a=0A= ;; (hd-index length . vector)=0A= ;; list. You can insert to, remove from, and rotate a ring. When the = ring=0A= ;; fills up, insertions cause the oldest elts to be quietly dropped.=0A= ;;=0A= ;; In ring-ref, 0 is the index of the newest element. Higher indexes=0A= ;; correspond to older elements; when the index equals the ring length,=0A= ;; it wraps to the newest element again.=0A= ;;=0A= ;; hd-index =3D vector index of the oldest ring item.=0A= ;; Newer items follow this item; at the end of the vector,=0A= ;; they wrap around to the start of the vector.=0A= ;; length =3D number of items currently in the ring.=0A= ;; This never exceeds the length of the vector itself.=0A= ;;=0A= ;; These functions are used by the input history mechanism, but they can=0A= ;; be used for other purposes as well.=0A= =0A= ;;; Code:=0A= =0A= ;;; User Functions:=0A= =0A= ;;;###autoload=0A= (defun ring-p (x)=0A= "Return t if X is a ring; nil otherwise."=0A= (and (consp x) (integerp (car x))=0A= (consp (cdr x)) (integerp (car (cdr x)))=0A= (vectorp (cdr (cdr x)))))=0A= =0A= ;;;###autoload=0A= (defun make-ring (size)=0A= "Make a ring that can contain SIZE elements."=0A= (cons 0 (cons 0 (make-vector size nil))))=0A= =0A= (defun ring-insert-at-beginning (ring item)=0A= "Add to RING the item ITEM. Add it at the front, as the oldest item."=0A= (let* ((vec (cdr (cdr ring)))=0A= (veclen (length vec))=0A= (hd (car ring))=0A= (ln (car (cdr ring))))=0A= (setq ln (min veclen (1+ ln))=0A= hd (ring-minus1 hd veclen))=0A= (aset vec hd item)=0A= (setcar ring hd)=0A= (setcar (cdr ring) ln)))=0A= =0A= (defun ring-plus1 (index veclen)=0A= "Return INDEX+1, with wraparound."=0A= (let ((new-index (+ index 1)))=0A= (if (=3D new-index veclen) 0 new-index)))=0A= =0A= (defun ring-minus1 (index veclen)=0A= "Return INDEX-1, with wraparound."=0A= (- (if (=3D 0 index) veclen index) 1))=0A= =0A= (defun ring-length (ring)=0A= "Return the number of elements in the RING."=0A= (car (cdr ring)))=0A= =0A= (defun ring-index (index head ringlen veclen)=0A= "Convert nominal ring index INDEX to an internal index.=0A= The internal index refers to the items ordered from newest to oldest.=0A= HEAD is the index of the oldest element in the ring.=0A= RINGLEN is the number of elements currently in the ring.=0A= VECLEN is the size of the vector in the ring."=0A= (setq index (mod index ringlen))=0A= (mod (1- (+ head (- ringlen index))) veclen))=0A= =0A= (defun ring-empty-p (ring)=0A= "Return t if RING is empty; nil otherwise."=0A= (zerop (car (cdr ring))))=0A= =0A= (defun ring-size (ring)=0A= "Return the size of RING, the maximum number of elements it can = contain."=0A= (length (cdr (cdr ring))))=0A= =0A= (defun ring-copy (ring)=0A= "Return a copy of RING."=0A= (let* ((vec (cdr (cdr ring)))=0A= (hd (car ring))=0A= (ln (car (cdr ring))))=0A= (cons hd (cons ln (copy-sequence vec)))))=0A= =0A= (defun ring-insert (ring item)=0A= "Insert onto ring RING the item ITEM, as the newest (last) item.=0A= If the ring is full, dump the oldest item to make room."=0A= (let* ((vec (cdr (cdr ring)))=0A= (veclen (length vec))=0A= (hd (car ring))=0A= (ln (car (cdr ring))))=0A= (prog1=0A= (aset vec (mod (+ hd ln) veclen) item)=0A= (if (=3D ln veclen)=0A= (setcar ring (ring-plus1 hd veclen))=0A= (setcar (cdr ring) (1+ ln))))))=0A= =0A= (defun ring-remove (ring &optional index)=0A= "Remove an item from the RING. Return the removed item.=0A= If optional INDEX is nil, remove the oldest item. If it's=0A= numeric, remove the element indexed."=0A= (if (ring-empty-p ring)=0A= (error "Ring empty")=0A= (let* ((hd (car ring))=0A= (ln (car (cdr ring)))=0A= (vec (cdr (cdr ring)))=0A= (veclen (length vec))=0A= (tl (mod (1- (+ hd ln)) veclen))=0A= oldelt)=0A= (if (null index)=0A= (setq index (1- ln)))=0A= (setq index (ring-index index hd ln veclen))=0A= (setq oldelt (aref vec index))=0A= (while (/=3D index tl)=0A= (aset vec index (aref vec (ring-plus1 index veclen)))=0A= (setq index (ring-plus1 index veclen)))=0A= (aset vec tl nil)=0A= (setcar (cdr ring) (1- ln))=0A= oldelt)))=0A= =0A= (defun ring-ref (ring index)=0A= "Return RING's INDEX element.=0A= INDEX =3D 0 is the most recently inserted; higher indices=0A= correspond to older elements.=0A= INDEX need not be <=3D the ring length; the appropriate modulo operation=0A= will be performed."=0A= (if (ring-empty-p ring)=0A= (error "Accessing an empty ring")=0A= (let* ((hd (car ring)) (ln (car (cdr ring))) (vec (cdr (cdr = ring))))=0A= (aref vec (ring-index index hd ln (length vec))))))=0A= =0A= (defun ring-elements (ring)=0A= "Return a list of the elements of RING, in order, newest first."=0A= (let ((start (car ring))=0A= (size (ring-size ring))=0A= (vect (cddr ring))=0A= lst)=0A= (dotimes (var (cadr ring) lst)=0A= (push (aref vect (mod (+ start var) size)) lst))))=0A= =0A= ;;; provide ourself:=0A= =0A= (provide 'ring)=0A= =0A= ;;; arch-tag: e707682b-ed69-47c9-b20f-cf2c68cc92d2=0A= ;;; ring.el ends here=0A= ------=_NextPart_000_0071_01C809BF.524C1320 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Emacs-devel mailing list Emacs-devel@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-devel ------=_NextPart_000_0071_01C809BF.524C1320--