Ejemplo n.º 1
0
        public void LinearAddressTest()
        {
            var par1 = new Parameter("par1", 0, 4, 1);
            var par2 = new Parameter("par2", 0, 4, 1);
            var pars = new RefList <Parameter>();

            pars.Add(par1);
            pars.Add(par2);
            //var pars = new Parameters();

            pars.Ref(0).CurrentPosition = 1;
            pars.Ref(1).CurrentPosition = 0;

            Assert.AreEqual(5, pars.LinearAddress());

            pars.Ref(1).CurrentPosition = 1;

            Assert.AreEqual(6, pars.LinearAddress());

            pars.Ref(0).CurrentPosition = 4;
            pars.Ref(1).CurrentPosition = 3;
            Assert.AreEqual(23, pars.LinearAddress());

            var pars2 = pars.SetPositionsFromLinearAddress(23);

            Assert.AreEqual(4, pars2[0].CurrentPosition);
            Assert.AreEqual(3, pars2[1].CurrentPosition);
        }
        /// <summary>
        /// Moves source elements into the given list.
        /// This list is expanded as necessary to fit all source items.
        /// Returns the number of items in the source.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int ToRefList <T>(this IEnumerable <T> source, RefList <T> list)
        {
            var itr = source.GetEnumerator();
            int n   = 0;

            // insert while theres space in the list
            while (itr.MoveNext())
            {
                if (list.Count == n)
                {
                    break;
                }

                list[n++] = itr.Current;
            }

            // add any remaining elements
            while (itr.MoveNext())
            {
                list.Add(itr.Current);
                n++;
            }

            return(n);
        }
Ejemplo n.º 3
0
 public void Add(Parameter parameter)
 {
     if (_parameters.Any(p => p.Code == parameter.Code))
     {
         throw new ArgumentException($"Parameter with the code {parameter.Code} is alreadyin the collection.");
     }
     _parameters.Add(parameter);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Traversal of <see cref="DecisionTree"/> is done by recusrively calling the <see cref="ADecisionNode.TreeTraversal(ADecisionNode)"/>.
        /// </summary>
        public static RefList <AIDecisionTreeChoice> traverseDecisionTree(DecisionTree p_decisionTree)
        {
            RefList <AIDecisionTreeChoice> l_choices = new RefList <AIDecisionTreeChoice>();

            RefList <TraversalStack> l_traversalStacks = new RefList <TraversalStack>();

            l_traversalStacks.Add(TraversalStack.build(p_decisionTree.RootNode));

            while (l_traversalStacks.Count > 0)
            {
                ref TraversalStack l_currentTraversalStack = ref l_traversalStacks.ValueRef(l_traversalStacks.Count - 1);
                //If there if the current node has links
                if (l_currentTraversalStack.DecisionNode.LinkedNodes != null)
                {
                    if (l_currentTraversalStack.LinkIterationCounter < l_currentTraversalStack.DecisionNode.LinkedNodes.Count)
                    {
                        //We traverse the link and go one level deeper
                        ADecisionNode l_nextNode = l_currentTraversalStack.DecisionNode.LinkedNodes[l_currentTraversalStack.LinkIterationCounter];

                        l_currentTraversalStack.LinkIterationCounter += 1;
                        TraversalStack l_oneLevelDepperStack = TraversalStack.build(l_nextNode);
                        l_traversalStacks.AddRef(ref l_oneLevelDepperStack);

                        l_nextNode.TreeTraversal(l_currentTraversalStack.DecisionNode);
                    }
                    else
                    {
                        #region Updating stack

                        l_traversalStacks.RemoveAt(l_traversalStacks.Count - 1);

                        #endregion
                    }
                }
                else // No links have been found, this means that the current node is a leaf node.
                {
                    #region Creating the choice as the current node is a leaf

                    ADecisionNode[] l_choiceNodes = new ADecisionNode[l_traversalStacks.Count];
                    for (int i = 0; i < l_traversalStacks.Count; i++)
                    {
                        l_choiceNodes[i] = l_traversalStacks.ValueRef(i).DecisionNode;
                    }

                    AIDecisionTreeChoice l_choice = AIDecisionTreeChoice.build(l_choiceNodes);
                    l_choices.AddRef(ref l_choice);

                    #endregion

                    l_traversalStacks.RemoveAt(l_traversalStacks.Count - 1);
                }
            }
        /// <exception cref="System.IO.IOException"></exception>
        private Ref ReadRef(string name, RefList <Ref> packed)
        {
            RefList <RefDirectory.LooseRef> curList = looseRefs.Get();
            int idx = curList.Find(name);

            if (0 <= idx)
            {
                RefDirectory.LooseRef o = curList.Get(idx);
                RefDirectory.LooseRef n = ScanRef(o, name);
                if (n == null)
                {
                    if (looseRefs.CompareAndSet(curList, curList.Remove(idx)))
                    {
                        modCnt.IncrementAndGet();
                    }
                    return(packed.Get(name));
                }
                if (o == n)
                {
                    return(n);
                }
                if (looseRefs.CompareAndSet(curList, curList.Set(idx, n)))
                {
                    modCnt.IncrementAndGet();
                }
                return(n);
            }
            RefDirectory.LooseRef n_1 = ScanRef(null, name);
            if (n_1 == null)
            {
                return(packed.Get(name));
            }
            // check whether the found new ref is the an additional ref. These refs
            // should not go into looseRefs
            for (int i = 0; i < additionalRefsNames.Length; i++)
            {
                if (name.Equals(additionalRefsNames[i]))
                {
                    return(n_1);
                }
            }
            if (looseRefs.CompareAndSet(curList, curList.Add(idx, n_1)))
            {
                modCnt.IncrementAndGet();
            }
            return(n_1);
        }