示例#1
0
        public unsafe Tree([NotNull] Allocator alloc, [NotNull] IMemoryAccess mem)
        {
            _alloc = alloc;
            _mem   = mem;

            NodeSize = NODE_HEAD_SIZE + sizeof(TElement);

            // Make the root node
            var res = _alloc.Alloc(NodeSize);

            if (!res.Success)
            {
                Valid = false;
                return;
            }

            // Set initial values
            Root = res.Value;
            var rootHead = new TreeNodeHead
            {
                FirstChildPtr  = -1,
                NextSiblingPtr = -1,
                ParentPtr      = -1
            };

            _mem.WriteC <TreeNodeHead, TElement>(Root, rootHead, default);

            Valid = true;
        }
示例#2
0
        /// <summary>
        /// Make a new node from an element. Sets parent node inside head, returns result of pointer to new node
        /// </summary>
        private Result <long> AllocateAndWriteNode(long parent, TElement element)
        {
            // Allocate new node and header
            var res = _alloc.Alloc(NodeSize);

            if (!res.Success)
            {
                return(Result.Fail <long>());
            }

            // Write a node head and data into memory
            var newChildHead = new TreeNodeHead
            {
                FirstChildPtr  = -1,
                NextSiblingPtr = -1,
                ParentPtr      = parent
            };

            _mem.WriteC(res.Value, newChildHead, element);
            return(res);
        }