Example #1
0
        /// <summary>
        /// Creates new instance and indexes the tree (requires 2*N steps).
        /// </summary>
        /// <param name="ufTree"></param>
        private void CreateIndex(UFTree ufTree)
        {
            if (ufTree.NodesCount > (Int64)int.MaxValue - 1)
            {
                throw new ArgumentOutOfRangeException(String.Format("Flat tree size {0} is too large.", ufTree.NodesCount));
            }
            _childrenBeginIdx = new int[ufTree.NodesCount + 1];
            _childrenIdx      = new int[ufTree.NodesCount];

            WalkUFTreePP <UFTree, Context> walk = new WalkUFTreePP <UFTree, Context>();

            walk.OnNodeBegin = OnNodeBegin1;
            walk.OnNodeEnd   = OnNodeEnd1;
            walk.Walk(ufTree);

            int beginIdx = 0;

            for (int i = 0; i < _childrenBeginIdx.Length; ++i)
            {
                int tmp = _childrenBeginIdx[i];
                _childrenBeginIdx[i] = beginIdx;
                beginIdx            += tmp;
            }

            walk.OnNodeBegin = OnNodeBegin2;
            walk.OnNodeEnd   = null;
            walk.Walk(ufTree);
        }
Example #2
0
 void OnNodeEnd1(UFTree tree, Context[] stack, int depth)
 {
     if (depth > 0)
     {
         stack[depth - 1].ChildCount++;
     }
     _childrenBeginIdx[stack[depth].NodeIdx] = stack[depth].ChildCount;
 }
Example #3
0
        /// <summary>
        /// Binary deserialization from file to memory.
        /// </summary>
        public static T Read <T>(string fileName) where T : UFTree, new()
        {
            T tree;

            using (BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)))
            {
                tree = UFTree.Read <T>(br);
            }
            return(tree);
        }
Example #4
0
 void OnNodeBegin2(UFTree tree, Context[] stack, int depth)
 {
     stack[depth].ChildCount = 0;
     if (depth > 0)
     {
         int begin = _childrenBeginIdx[stack[depth - 1].NodeIdx];
         _childrenIdx[begin + stack[depth - 1].ChildCount] = (int)stack[depth].NodeIdx;
         stack[depth - 1].ChildCount++;
     }
 }
Example #5
0
        /// <summary>
        /// Syncronize trees.
        /// </summary>
        /// <param name="userData">User data to pass from/to the algo.</param>
        /// <returns></returns>
        public static bool Sync <TreeT, NodeT>(TreeT uniTree, NodeT uniRoot, UFTree ufTree, SyncDelegate <TreeT, NodeT> sync, object userData)
        {
            Data <TreeT, NodeT> data = new Data <TreeT, NodeT>
            {
                Sync        = sync,
                UniGetChild = TreeDefs <TreeT, NodeT, IteratorT> .FindTreeGetChildMethod(),
                UniTree     = uniTree,
                UfTree      = ufTree,
                UfNode      = 0,
                UserData    = userData
            };

            return(SyncInternal(data, uniRoot, 0, default(IteratorT)));
        }
Example #6
0
        ///<summary>Create instance for file acesss.</summary>
        ///<param name="forceCreation">If true - always create a new index file, otherwise create only if no file exists.</param>
        public UFTreeChildrenIndex(UFTree ufTree, string fileName, bool forceCreation)
        {
            if (forceCreation || !File.Exists(fileName))
            {
                CreateIndex(ufTree);
                using (BinaryWriter bw = new BinaryWriter(File.Open(fileName, FileMode.Create, FileAccess.Write)))
                {
                    BdsVersion v = new BdsVersion(Assembly.GetExecutingAssembly());
                    v.Description = "UFTreeChildrenIndex for : '" + ufTree.Version.Description + "'";
                    v.Write(bw);
                    bw.Write(SERIALIZATION_FORMAT_VERSION);
                    bw.Write(ufTree.NodesCount);
                    for (long i = 0; i < _childrenBeginIdx.Length; ++i)
                    {
                        bw.Write(_childrenBeginIdx[i]);
                    }
                    for (long i = 0; i < _childrenIdx.Length; ++i)
                    {
                        bw.Write(_childrenIdx[i]);
                    }
                }
            }
            _childrenBeginIdxReader =
                new BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read));
            BdsVersion v1 = new BdsVersion();

            v1.Read(_childrenBeginIdxReader);
            int serFmtVer = _childrenBeginIdxReader.ReadInt32();

            if (serFmtVer > SERIALIZATION_FORMAT_VERSION)
            {
                throw new ApplicationException(
                          string.Format("Unsupported serialization format '{0}', max supported: '{1}'", serFmtVer,
                                        SERIALIZATION_FORMAT_VERSION));
            }
            long nodesCount = _childrenBeginIdxReader.ReadInt64();

            _childrenBeginIdxFilePos = _childrenBeginIdxReader.BaseStream.Position;
            _childrenIdxFilePos      = _childrenBeginIdxFilePos + (nodesCount + 1) * 4;
            _childrenIdxReader       = new BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read));

            _childrenBeginIdx = _childrenIdx = null;
        }
Example #7
0
 /// <summary>
 /// Creates new instance for file access.
 /// </summary>
 /// <param name="ufTree">UF tree</param>
 /// <param name="forceCreation">If true - always create a new index file, otherwise create only if no file exists.</param>
 public UFToUniAdapter(UFTree ufTree, string fileName, bool forceCreation)
     : base(ufTree, fileName, forceCreation)
 {
     UfTree = ufTree;
 }
Example #8
0
 /// <summary>
 /// Creates new instance and indexes the tree (requires 2*N steps) for memory access.
 /// </summary>
 /// <param name="ufTree"></param>
 public UFToUniAdapter(UFTree ufTree) : base(ufTree)
 {
     UfTree = ufTree;
 }
Example #9
0
 /// <summary>
 /// Create instance for memory access.
 /// </summary>
 public UFTreeChildrenIndex(UFTree ufTree)
 {
     CreateIndex(ufTree);
 }
Example #10
0
 void OnNodeBegin1(UFTree tree, Context[] stack, int depth)
 {
     stack[depth].ChildCount = 0;
 }
Example #11
0
 /// <summary>
 /// Synchronize trees.
 /// </summary>
 public static bool Sync <NodeT>(NodeT uniRoot, UFTree ufTree, SyncDelegate <NodeT, NodeT> sync)
 {
     return(Sync(uniRoot, uniRoot, ufTree, sync, null));
 }
Example #12
0
 /// <summary>
 /// Synchronize trees.
 /// </summary>
 public static bool Sync <NodeT>(NodeT uniRoot, UFTree ufTree, SyncDelegate <NodeT, NodeT> sync, object userData)
 {
     return(Sync(uniRoot, uniRoot, ufTree, sync, userData));
 }