PageRenderTime 62ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/src/textree.sc

https://bitbucket.org/bunny351/ezd
Scala | 180 lines | 161 code | 19 blank | 0 comment | 3 complexity | 5583d2b1b0037b07004190279f24d7ed MD5 | raw file
  1. ;;; ezd - easy drawing for X11 displays.
  2. ;;;
  3. ;;; Text in text areas is stored in a balanced tree composed of strings and
  4. ;;; TEXTREE entries.
  5. ;* Copyright 1990-1993 Digital Equipment Corporation
  6. ;* All Rights Reserved
  7. ;*
  8. ;* Permission to use, copy, and modify this software and its documentation is
  9. ;* hereby granted only under the following terms and conditions. Both the
  10. ;* above copyright notice and this permission notice must appear in all copies
  11. ;* of the software, derivative works or modified versions, and any portions
  12. ;* thereof, and both notices must appear in supporting documentation.
  13. ;*
  14. ;* Users of this software agree to the terms and conditions set forth herein,
  15. ;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
  16. ;* right and license under any changes, enhancements or extensions made to the
  17. ;* core functions of the software, including but not limited to those affording
  18. ;* compatibility with other hardware or software environments, but excluding
  19. ;* applications which incorporate this software. Users further agree to use
  20. ;* their best efforts to return to Digital any such changes, enhancements or
  21. ;* extensions that they make and inform Digital of noteworthy uses of this
  22. ;* software. Correspondence should be provided to Digital at:
  23. ;*
  24. ;* Director of Licensing
  25. ;* Western Research Laboratory
  26. ;* Digital Equipment Corporation
  27. ;* 250 University Avenue
  28. ;* Palo Alto, California 94301
  29. ;*
  30. ;* This software may be distributed (but not offered for sale or transferred
  31. ;* for compensation) to third parties, provided such third parties agree to
  32. ;* abide by the terms and conditions of this notice.
  33. ;*
  34. ;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  35. ;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  36. ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  37. ;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  38. ;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  39. ;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  40. ;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  41. ;* SOFTWARE.
  42. (module textree)
  43. (include "struct.sch")
  44. ;;; A TEXTREE entry consists of a structure with the following fields.
  45. ;;;
  46. ;;; LEFT-CNT # of lines in the left subtree.
  47. ;;; HEIGHT tree height of this node.
  48. ;;; LEFT left subtree.
  49. ;;; RIGHT right subtree.
  50. (define-structure TEXTREE
  51. left-cnt
  52. height
  53. left
  54. right)
  55. (define-in-line-structure-access TEXTREE left-cnt height left right)
  56. ;;; The n'th line of text in a balanced tree composed of TEXTREE entries and
  57. ;;; strings is returned by the following procedure.
  58. (define (TEXTREE-TEXT tree n)
  59. (cond ((null? tree) "")
  60. ((string? tree) (if (eq? n 0) tree ""))
  61. (else (let ((left-cnt (textree-left-cnt tree)))
  62. (if (< n left-cnt)
  63. (textree-text (textree-left tree) n)
  64. (textree-text (textree-right tree)
  65. (- n left-cnt)))))))
  66. ;;; The n'th line of text in a balanced tree composed of TEXTREE entries and
  67. ;;; strings is replaced by the following procedure. It returns the new tree
  68. ;;; in order to cover the degenerate case of a tree consisting of a single
  69. ;;; string.
  70. (define (TEXTREE-TEXT! tree n text)
  71. (if (string? tree)
  72. text
  73. (begin (let ((left-cnt (textree-left-cnt tree)))
  74. (if (< n left-cnt)
  75. (let ((left (textree-left tree)))
  76. (if (string? left)
  77. (textree-left! tree text)
  78. (textree-text! left n text)))
  79. (let ((right (textree-right tree)))
  80. (if (string? right)
  81. (textree-right! tree text)
  82. (textree-text! right (- n left-cnt) text)))))
  83. tree)))
  84. ;;; A new line is inserted before the n'th line of text in a balanced tree
  85. ;;; composed of TEXTREE entries and strings by the following procedure.
  86. (define (INSERT-TEXTREE tree n text)
  87. (cond ((null? tree) text)
  88. ((string? tree)
  89. (if (zero? n)
  90. (make-textree 1 1 text tree)
  91. (make-textree 1 1 tree text)))
  92. ((<= n (textree-left-cnt tree))
  93. (textree-left-cnt! tree (+ (textree-left-cnt tree) 1))
  94. (textree-left! tree (insert-textree (textree-left tree) n text))
  95. (adjust-textree tree))
  96. (else
  97. (textree-right! tree (insert-textree (textree-right tree)
  98. (- n (textree-left-cnt tree)) text))
  99. (adjust-textree tree))))
  100. ;;; The n'th line of text in a balanced tree composed of TEXTREE entries and
  101. ;;; strings is deleted by the following procedure.
  102. (define (DELETE-TEXTREE tree n)
  103. (cond ((string? tree) '())
  104. ((< n (textree-left-cnt tree))
  105. (let ((left (textree-left tree)))
  106. (if (string? left)
  107. (textree-right tree)
  108. (begin (textree-left-cnt! tree
  109. (- (textree-left-cnt tree) 1))
  110. (textree-left! tree (delete-textree left n))
  111. (adjust-textree tree)))))
  112. (else
  113. (let ((right (textree-right tree)))
  114. (if (string? right)
  115. (textree-left tree)
  116. (begin (textree-right! tree
  117. (delete-textree right
  118. (- n (textree-left-cnt tree))))
  119. (adjust-textree tree)))))))
  120. ;;; Nodes in a balanced tree composed of TEXTREE entries and strings are
  121. ;;; abjusted by the following procedure. The adjusted tree is the return
  122. ;;; value.
  123. (define (ADJUST-TEXTREE tree)
  124. (define (COUNT tree)
  125. (if (string? tree)
  126. 1
  127. (+ (textree-left-cnt tree) (count (textree-right tree)))))
  128. (if (string? tree)
  129. tree
  130. (let* ((tx-height
  131. (lambda (x) (if (string? x) 0 (textree-height x))))
  132. (left (textree-left tree))
  133. (left-height (tx-height left))
  134. (right (textree-right tree))
  135. (right-height (tx-height right))
  136. (delta (- left-height right-height)))
  137. (cond ((>= delta 2)
  138. (textree-left! tree (textree-right left))
  139. (textree-left-cnt! tree (count (textree-right left)))
  140. (textree-height! tree
  141. (+ 1 (max (tx-height (textree-left tree))
  142. (tx-height (textree-right tree)))))
  143. (textree-right! left tree)
  144. (textree-height! left
  145. (+ 1 (max (tx-height (textree-left left))
  146. (tx-height tree))))
  147. left)
  148. ((<= delta -2)
  149. (textree-right! tree (textree-left right))
  150. (textree-height! tree
  151. (+ 1 (max (tx-height (textree-left tree))
  152. (tx-height (textree-right tree)))))
  153. (textree-left! right tree)
  154. (textree-left-cnt! right (count tree))
  155. (textree-height! right
  156. (+ 1 (max (tx-height (textree-right right))
  157. (tx-height tree))))
  158. right)
  159. (else (textree-height! tree
  160. (+ 1 (max left-height right-height)))
  161. tree)))))