safe-vector.zc

来自「实现树形结构」· ZC 代码 · 共 65 行

ZC
65
字号
//[of]:description
//[c]Safe Vector
//[c]
//[c]This vector is especially design to allows deletion of an item while 
//[c]enumerating the content.
//[c]
//[c]This vector can not include nil values.
//[cf]
//[of]:imports
//[c]
import "base/types"
import "collection/vector"
//[cf]
//[of]:structure
//[c]
public struct safe vector: local vector
	// empty
end
//[cf]
//[c]
//[of]:adding - removing
//[of]:add (m, object)
//[c]
public func add (m: safe vector,  o: object)

	def i = 0:d
	def n = size (m)
	while i < n
		if is nil (m[i])
			m[i] = o
			return
		end
		++i
	end
	
	add (super (m), o)

end
//[cf]
//[of]:remove (m, object)
//[c]
public func remove (m: safe vector, o: object)

	def i = index (m, o)
	if i <> -1
		m [i] = nil
	end

end
//[cf]
//[cf]
//[of]:enumerating
//[c]Enumerates all items
//[c]
public equ each (m: safe vector)

	each (super (m)) ? e
		if not nil (e)
			yield (e)
		end
	end

end
//[cf]

⌨️ 快捷键说明

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