⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collection.zc

📁 实现树形结构
💻 ZC
字号:
//[of]:description
//[c]Linked list of elements
//[c]
//[c]Full sub-classing is performed by implementing the following equates:
//[c]	- equ first (c)
//[c]	- equ last (c)
//[c]	- equ each (c)
//[c]	Note: an equ does not generate code
//[cf]
//[of]:structures
//[c]
//[c]Element of a collection
//[c]
public struct element
	next sibling: element
end
//[c]
//[c]Collection
//[c]
public struct collection
	first: element
	last: element
end
//[cf]
//[c]
//[of]:collection
//[of]:initialize
//[of]:collection
//[c]
public equ collection (return c: collection) = initialize (c)
//[cf]
//[of]:initialize (c)
//[c]
public func initialize (c: collection)
	first (c) = nil
	last (c) = nil
end
//[cf]
//[cf]
//[of]:enumerating
//[of]:each (c)
//[c]
public equ each (c: collection)

	def e = first (c)
	while not nil (e)
		yield (e)
		e = next sibling (e)
	end

end
//[cf]
//[cf]
//[of]:adding - removing
//[of]:append (c, element)
//[c]Appends an element
//[c]
public func append (c: collection, e: element)

	if not nil (last(c))
		next sibling (last (c)) = e
	end
	
	last (c) = e
	
	if is nil (first (c)) 
		first (c) = e
	end
	
	// the next is set when inserting in collection
	next sibling (e) = nil

end
//[cf]
//[of]:add (c, element)
//[c]
public equ add (c: collection, e: element) = append (c, e)
//[cf]
//[of]:add first (c, element)
//[c]Inserts an element at the beginning
//[c]
public func add first (c: collection, e: element)

	next sibling (e) = first (c)
	first (c) = e
		
	if is nil (last(c))
		last (c) = e
	end
	
end
//[cf]
//[c]
//[of]:remove all (m)
//[c]Removes all elements from the collection
//[c]
public equ remove all (c: collection) = 
	initialize (c)
//[cf]
//[cf]
//[of]:accessing
//[of]:size (c)
//[c]Computes size
//[c]
public func size(c: collection)

	def size = 0
	def e = first (c)
	while not nil (e)
		e = next sibling (e)
		++ size
	end
	return size

end
//[cf]
//[cf]
//[of]:testing
//[of]:is empty (c)
//[c]
public equ is empty (c: collection) = is nil (first (c))
//[cf]
//[of]:not empty (c)
//[c]
public equ not empty (c: collection) = not nil (first (c))
//[cf]
//[cf]
//[cf]
//[of]:element
//[of]:adding
//[of]:append (e1, e2)
//[c]
public func append (e1: element, e2: element)
	next sibling (e1) = e2
end
//[cf]
//[of]:add (e1, e2)
//[c]
public equ add (e1: element, e2: element) = 
	append (e1, e2)
//[cf]
//[cf]
//[of]:accessing
//[of]:number of elements (e)
//[c]Counts the numbers of elements starting from this one
//[c]
public func number of elements (e: element)

	def size = 0
	while not nil (e)
		e = next sibling (e)
		++size
	end
	return size

end
//[cf]
//[cf]
//[of]:enumerating
//[of]:each (e)
//[c]Enumerates all elements, starting from this one
//[c]
public equ each (item: element)

	def e = item
	while not nil (e)
		yield (e)
		e = next sibling (e)
	end

end
//[cf]
//[cf]
//[of]:testing
//[of]:is last (e)
//[c]Returns true if the element is the last element of the collection
//[c]
public equ is last (e: element) = is nil (next sibling (e))
//[cf]
//[cf]
//[c]
//[cf]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -