From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Thorsten Jolitz Newsgroups: gmane.emacs.help Subject: Re: Circular lists/shared structures in org-element parse-tree Date: Sat, 29 Jun 2013 00:06:00 +0200 Message-ID: <87wqpd99h3.fsf@gmail.com> References: <87y59u3p1w.fsf@informatimago.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1372457185 23693 80.91.229.3 (28 Jun 2013 22:06:25 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 28 Jun 2013 22:06:25 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Jun 29 00:06:26 2013 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1UsgoD-0005BS-BT for geh-help-gnu-emacs@m.gmane.org; Sat, 29 Jun 2013 00:06:25 +0200 Original-Received: from localhost ([::1]:37828 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UsgoC-0005sP-R3 for geh-help-gnu-emacs@m.gmane.org; Fri, 28 Jun 2013 18:06:24 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37439) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Usgo2-0005sG-Ga for help-gnu-emacs@gnu.org; Fri, 28 Jun 2013 18:06:15 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Usgo1-0001nh-EN for help-gnu-emacs@gnu.org; Fri, 28 Jun 2013 18:06:14 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:41563) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Usgo1-0001li-7W for help-gnu-emacs@gnu.org; Fri, 28 Jun 2013 18:06:13 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1Usgny-0004wl-On for help-gnu-emacs@gnu.org; Sat, 29 Jun 2013 00:06:10 +0200 Original-Received: from e178117198.adsl.alicedsl.de ([85.178.117.198]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 29 Jun 2013 00:06:10 +0200 Original-Received: from tjolitz by e178117198.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 29 Jun 2013 00:06:10 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 83 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: e178117198.adsl.alicedsl.de User-Agent: Gnus/5.130002 (Ma Gnus v0.2) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:6bWH9Xx/j8C47+QsHnT1DCY7xjA= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:91833 Archived-At: "Pascal J. Bourguignon" writes: > Thorsten Jolitz writes: > >> [Originally posted on the Org-mode mailing list, but I post it here too >> since it is more an elisp oriented mailing list] >> >> Hi List, >> >> I wonder how I can find out in a (elisp) program the points in the parse >> tree (returned by org-element-parse-buffer) where shared structures are >> used. >> >> In the read-syntax, its easy to see (especially with `print-circle' set >> to non-nil): >> >> #+begin_src emacs-lisp >> #2=(org-data nil #1=(headline (:raw-value "header 1" >> [...] :parent #2#) [...] >> #+end_src >> >> but when processing the parse tree as a list in elisp, how can I detect the >> fact that >> >> ,------------ >> | :parent #2# >> `------------ >> >> refers to >> >> ,----------------- >> | #2=(org-data nil >> `----------------- >> >> i.e. points back to an already existing structure? > > > 1- there is not a unique solution in general: it depends on the order in > which you choose to walk the branches. > > 2- you just walk the cons tree, checking if you've not already visited > them. > > > Something like this: > > (let* ((counter 0) > (objects (make-hash-table))) > (labels ((walk (object) > (let ((reference (gethash object objects)))) > (if reference > (already-seen object reference)) > (progn > (new-reference object > (setf (gethash object objects) (incf counter))) > (cond > ((vector object) > (dotimes (i (length vector)) > (walk (aref vector i)))) > ((cons object) > (walk (car object)) > (walk (cdr object))) > ( ; you may also want to walk structures, > ; hashtable, etc > ))))) > (walk root))) > > If you don't want to generate references for objects present only once, > then you can transform this in a two-pass algorithm where you first fill > the hash-table with a counter of occurence: > > (incf (gethash object objects 0)) > > and then only keep and renumber the entries that have a value greater > than 1. Thank you for the detailled instructions, very helpful indeed! I thought Emacs might have some inbuilt functionality to deal with circular list, but it seems to require some individual effort. -- cheers, Thorsten