From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Circular lists/shared structures in org-element parse-tree
Date: Fri, 28 Jun 2013 23:26:03 +0200 [thread overview]
Message-ID: <87y59u3p1w.fsf@informatimago.com> (raw)
In-Reply-To: mailman.2733.1372451360.22516.help-gnu-emacs@gnu.org
Thorsten Jolitz <tjolitz@gmail.com> 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.
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
You know you've been lisping too long when you see a recent picture of George
Lucas and think "Wait, I thought John McCarthy was dead!" -- Dalek_Baldwin
next parent reply other threads:[~2013-06-28 21:26 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <mailman.2733.1372451360.22516.help-gnu-emacs@gnu.org>
2013-06-28 21:26 ` Pascal J. Bourguignon [this message]
2013-06-28 22:06 ` Circular lists/shared structures in org-element parse-tree Thorsten Jolitz
2013-06-29 2:28 ` Stefan Monnier
[not found] ` <mailman.2745.1372457176.22516.help-gnu-emacs@gnu.org>
2013-06-28 23:20 ` Pascal J. Bourguignon
2013-06-28 20:28 Thorsten Jolitz
-- strict thread matches above, loose matches on Subject: below --
2013-06-28 19:50 Thorsten Jolitz
2013-06-28 20:43 ` Daimrod
2013-06-28 21:59 ` Thorsten Jolitz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87y59u3p1w.fsf@informatimago.com \
--to=pjb@informatimago.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.