  This file is one of components of CL - YACLYAML system , licenced under GPL , see COPYING for details

(in-package #:cl-yaclyaml)

;;; Converting representation graph into representation tree.

(defparameter node-aliases (make-hash-table :test #'eq))
(defparameter alias-count 0)
(defparameter encountered-nodes (make-hash-table :test #'eq))

(defun %nserialize (node)
  ( format t " encountered nodes : ~a~% " ( hash->assoc encountered - nodes ) )
  ( format t " node : ~a~% " node )
  ( format t " ~a~% " ( multiple - value - list ( gethash node encountered - nodes ) ) )
  (let ((it (gethash node node-aliases)))
    (if it
	(values it t)
	(if (gethash node encountered-nodes)
	    ;; need to create an alias node
  ( format t " Encountered node for at least second time.~% " )
	      (let ((alias-name (format nil "a~a" (incf alias-count))))
   : here we assume that : PROPERTIES is the first assoc - element
  and content is the second
		(setf (car node) `(:properties (:anchor . ,alias-name) ,.(cdar node)))
		(values (setf (gethash node node-aliases) `(:alias . ,alias-name))
			t)))
  ( format t " Encountered node for the first time . " )
	      (setf (gethash node encountered-nodes) t)
	      (cond ((scalar-p node)
  ( format t " scalar~% " )
		     nil)
		    ((mapping-p node)
		     ;; (format t "mapping~%")
		     (iter (for map-entry in (cddr (assoc :content node)))
			   (multiple-value-bind (it got) (%nserialize (car map-entry))
			     (if got
				 (setf (car map-entry) it)))
			   (multiple-value-bind (it got) (%nserialize (cdr map-entry))
			     (if got
				 (setf (cdr map-entry) it)))))
		    ;; sequence
		    (t
		     ;; (format t "sequence~%")
		     (iter (for subnode on (cdr (assoc :content node)))
			   (multiple-value-bind (it got) (%nserialize (car subnode))
			     (if got
				 (setf (car subnode) it))))))
	      (values node nil))))))

(defun nserialize (representation-graph)
  "Destructively convert representation graph (which may contain loops) to representation tree,
where all shared-ness is represented via alias-nodes."
  (let ((node-aliases (make-hash-table :test #'eq))
	(encountered-nodes (make-hash-table :test #'eq))
	(alias-count 0))
    (%nserialize representation-graph)))

  

 Converting representation graph into representation tree.
 need to create an alias node
 (format t "mapping~%")
 sequence
 (format t "sequence~%")
  This file is one of components of CL - YACLYAML system , licenced under GPL , see COPYING for details

(in-package #:cl-yaclyaml)


(defparameter node-aliases (make-hash-table :test #'eq))
(defparameter alias-count 0)
(defparameter encountered-nodes (make-hash-table :test #'eq))

(defun %nserialize (node)
  ( format t " encountered nodes : ~a~% " ( hash->assoc encountered - nodes ) )
  ( format t " node : ~a~% " node )
  ( format t " ~a~% " ( multiple - value - list ( gethash node encountered - nodes ) ) )
  (let ((it (gethash node node-aliases)))
    (if it
	(values it t)
	(if (gethash node encountered-nodes)
  ( format t " Encountered node for at least second time.~% " )
	      (let ((alias-name (format nil "a~a" (incf alias-count))))
   : here we assume that : PROPERTIES is the first assoc - element
  and content is the second
		(setf (car node) `(:properties (:anchor . ,alias-name) ,.(cdar node)))
		(values (setf (gethash node node-aliases) `(:alias . ,alias-name))
			t)))
  ( format t " Encountered node for the first time . " )
	      (setf (gethash node encountered-nodes) t)
	      (cond ((scalar-p node)
  ( format t " scalar~% " )
		     nil)
		    ((mapping-p node)
		     (iter (for map-entry in (cddr (assoc :content node)))
			   (multiple-value-bind (it got) (%nserialize (car map-entry))
			     (if got
				 (setf (car map-entry) it)))
			   (multiple-value-bind (it got) (%nserialize (cdr map-entry))
			     (if got
				 (setf (cdr map-entry) it)))))
		    (t
		     (iter (for subnode on (cdr (assoc :content node)))
			   (multiple-value-bind (it got) (%nserialize (car subnode))
			     (if got
				 (setf (car subnode) it))))))
	      (values node nil))))))

(defun nserialize (representation-graph)
  "Destructively convert representation graph (which may contain loops) to representation tree,
where all shared-ness is represented via alias-nodes."
  (let ((node-aliases (make-hash-table :test #'eq))
	(encountered-nodes (make-hash-table :test #'eq))
	(alias-count 0))
    (%nserialize representation-graph)))

  
