Exemplo n.º 1
0
        internal CandidateSet(Candidate[] candidates)
        {
            Count = candidates.Length;

            switch (candidates.Length)
            {
            case 0:
                return;

            case 1:
                _state0 = new CandidateState(candidates[0].Endpoint, candidates[0].Score);
                break;

            case 2:
                _state0 = new CandidateState(candidates[0].Endpoint, candidates[0].Score);
                _state1 = new CandidateState(candidates[1].Endpoint, candidates[1].Score);
                break;

            case 3:
                _state0 = new CandidateState(candidates[0].Endpoint, candidates[0].Score);
                _state1 = new CandidateState(candidates[1].Endpoint, candidates[1].Score);
                _state2 = new CandidateState(candidates[2].Endpoint, candidates[2].Score);
                break;

            case 4:
                _state0 = new CandidateState(candidates[0].Endpoint, candidates[0].Score);
                _state1 = new CandidateState(candidates[1].Endpoint, candidates[1].Score);
                _state2 = new CandidateState(candidates[2].Endpoint, candidates[2].Score);
                _state3 = new CandidateState(candidates[3].Endpoint, candidates[3].Score);
                break;

            default:
                _state0 = new CandidateState(candidates[0].Endpoint, candidates[0].Score);
                _state1 = new CandidateState(candidates[1].Endpoint, candidates[1].Score);
                _state2 = new CandidateState(candidates[2].Endpoint, candidates[2].Score);
                _state3 = new CandidateState(candidates[3].Endpoint, candidates[3].Score);

                _additionalCandidates = new CandidateState[candidates.Length - 4];
                for (var i = 4; i < candidates.Length; i++)
                {
                    _additionalCandidates[i - 4] = new CandidateState(candidates[i].Endpoint, candidates[i].Score);
                }
                break;
            }

            // Initialize validity to valid by default.
            if (Count < BitVectorSize)
            {
                // Sets the bit for each candidate that exists (bits > Count will be 0).
                _validity = new BitVector32(unchecked ((int)~(0xFFFFFFFFu << Count)));
            }
            else
            {
                _largeCapactityValidity = new BitArray(Count, defaultValue: true);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Replaces the <see cref="Endpoint"/> at the provided <paramref name="index"/> with the
        /// provided <paramref name="endpoint"/>.
        /// </summary>
        /// <param name="index">The candidate index.</param>
        /// <param name="endpoint">
        /// The <see cref="Endpoint"/> to replace the original <see cref="Endpoint"/> at
        /// the <paramref name="index"/>. If <paramref name="endpoint"/> the candidate will be marked
        /// as invalid.
        /// </param>
        /// <param name="values">
        /// The <see cref="RouteValueDictionary"/> to replace the original <see cref="RouteValueDictionary"/> at
        /// the <paramref name="index"/>.
        /// </param>
        public void ReplaceEndpoint(int index, Endpoint endpoint, RouteValueDictionary values)
        {
            // Friendliness for inlining
            if ((uint)index >= Count)
            {
                ThrowIndexArgumentOutOfRangeException();
            }

            switch (index)
            {
            case 0:
                _state0 = new CandidateState(endpoint, values, _state0.Score);
                break;

            case 1:
                _state1 = new CandidateState(endpoint, values, _state1.Score);
                break;

            case 2:
                _state2 = new CandidateState(endpoint, values, _state2.Score);
                break;

            case 3:
                _state3 = new CandidateState(endpoint, values, _state3.Score);
                break;

            default:
                _additionalCandidates[index - 4] = new CandidateState(endpoint, values, _additionalCandidates[index - 4].Score);
                break;
            }

            if (endpoint == null)
            {
                SetValidity(index, false);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// <para>
        /// Initializes a new instances of the <see cref="CandidateSet"/> class with the provided <paramref name="endpoints"/>,
        /// <paramref name="values"/>, and <paramref name="scores"/>.
        /// </para>
        /// <para>
        /// The constructor is provided to enable unit tests of implementations of <see cref="EndpointSelector"/>
        /// and <see cref="IEndpointSelectorPolicy"/>.
        /// </para>
        /// </summary>
        /// <param name="endpoints">The list of endpoints, sorted in descending priority order.</param>
        /// <param name="values">The list of <see cref="RouteValueDictionary"/> instances.</param>
        /// <param name="scores">The list of endpoint scores. <see cref="CandidateState.Score"/>.</param>
        public CandidateSet(Endpoint[] endpoints, RouteValueDictionary[] values, int[] scores)
        {
            if (endpoints == null)
            {
                throw new ArgumentNullException(nameof(endpoints));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (scores == null)
            {
                throw new ArgumentNullException(nameof(scores));
            }

            if (endpoints.Length != values.Length || endpoints.Length != scores.Length)
            {
                throw new ArgumentException($"The provided {nameof(endpoints)}, {nameof(values)}, and {nameof(scores)} must have the same length.");
            }

            Count = endpoints.Length;

            switch (endpoints.Length)
            {
            case 0:
                return;

            case 1:
                _state0 = new CandidateState(endpoints[0], values[0], scores[0]);
                break;

            case 2:
                _state0 = new CandidateState(endpoints[0], values[0], scores[0]);
                _state1 = new CandidateState(endpoints[1], values[1], scores[1]);
                break;

            case 3:
                _state0 = new CandidateState(endpoints[0], values[0], scores[0]);
                _state1 = new CandidateState(endpoints[1], values[1], scores[1]);
                _state2 = new CandidateState(endpoints[2], values[2], scores[2]);
                break;

            case 4:
                _state0 = new CandidateState(endpoints[0], values[0], scores[0]);
                _state1 = new CandidateState(endpoints[1], values[1], scores[1]);
                _state2 = new CandidateState(endpoints[2], values[2], scores[2]);
                _state3 = new CandidateState(endpoints[3], values[3], scores[3]);
                break;

            default:
                _state0 = new CandidateState(endpoints[0], values[0], scores[0]);
                _state1 = new CandidateState(endpoints[1], values[1], scores[1]);
                _state2 = new CandidateState(endpoints[2], values[2], scores[2]);
                _state3 = new CandidateState(endpoints[3], values[3], scores[3]);

                _additionalCandidates = new CandidateState[endpoints.Length - 4];
                for (var i = 4; i < endpoints.Length; i++)
                {
                    _additionalCandidates[i - 4] = new CandidateState(endpoints[i], values[i], scores[i]);
                }
                break;
            }

            // Initialize validity to valid by default.
            if (Count < BitVectorSize)
            {
                // Sets the bit for each candidate that exists (bits > Count will be 0).
                _validity = new BitVector32(unchecked ((int)~(0xFFFFFFFFu << Count)));
            }
            else
            {
                _largeCapactityValidity = new BitArray(Count, defaultValue: true);
            }
        }