Ejemplo n.º 1
0
        /// <summary>
        /// 左部分木の最大値のノードを削除しauxに格納する
        /// 戻り値は削除により修正された左部分木
        /// </summary>
        private Node DeleteMax(Node t, Trio aux)
        {
            if (t.rst == null)
            {
                //最大値
                aux.lmax = t.key;
                switch (t.color)
                {
                case RBColor.R:
                    aux.change = false;
                    break;

                case RBColor.B:
                    //パス上の黒の数が変わるので修正フラグを立てる
                    aux.change = true;
                    break;
                }
                //親子関係
                if (t.lst != null)
                {
                    t.lst.parent = t.parent;
                }
                //左部分木を昇格させる
                return(t.lst);
            }
            else
            {
                //最大値に向かって進む
                t.rst = DeleteMax(t.rst, aux);
                return(BalanceR(t, aux));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 再帰的なkeyのエントリーの削除操作
        /// </summary>
        private Node Delete(Node t, Node parent, K key, Trio aux)
        {
            if (t == null)
            {
                aux.change = false;
                return(null);
            }
            else if (key.CompareTo(t.key) < 0)
            {
                //左(小さい方)に進む
                t.lst = Delete(t.lst, t, key, aux);
                return(BalanceL(t, aux));
            }
            else if (key.CompareTo(t.key) > 0)
            {
                //右(大きい方)に進む
                t.rst = Delete(t.rst, t, key, aux);
                return(BalanceR(t, aux));
            }
            else
            {
                if (t.lst == null)
                {
                    //左端
                    switch (t.color)
                    {
                    case RBColor.R:
                        aux.change = false;
                        break;

                    case RBColor.B:
                        //パス上の黒の数が変わるので修正フラグを立てる
                        aux.change = true;
                        break;
                    }
                    //親子関係
                    if (t.rst != null)
                    {
                        t.rst.parent = t.parent;
                    }
                    //前後関係
                    SetPrevNextAtDeleted(t);
                    //右部分木を昇格する
                    return(t.rst);
                }
                else
                {
                    //前後関係の設定
                    SetPrevNextAtDeleted(t);
                    //左部分木の最大値で置き換える
                    t.lst = DeleteMax(t.lst, aux);
                    t.key = aux.lmax;
                    return(BalanceL(t, aux));
                }
            }
        }
Ejemplo n.º 3
0
 public override int GetHashCode()
 {
     unchecked
     {
         var hashCode = Overall.GetHashCode();
         hashCode = hashCode * 397 ^ (Solo != null ? Solo.GetHashCode() : 0);
         hashCode = hashCode * 397 ^ (Duo != null ? Duo.GetHashCode() : 0);
         hashCode = hashCode * 397 ^ (Trio != null ? Trio.GetHashCode() : 0);
         hashCode = hashCode * 397 ^ (Squad != null ? Squad.GetHashCode() : 0);
         hashCode = hashCode * 397 ^ (Ltm != null ? Ltm.GetHashCode() : 0);
         return(hashCode);
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// 右部分木のエントリー削除に伴う赤黒木の修正(4つのパターン
 /// 戻り値は修正された木。auzに付加情報を返す
 /// </summary>
 private Node BalanceR(Node t, Trio aux)
 {
     //BalanceLと左右対称
     if (!aux.change)
     {
         return(t);
     }
     else if (IsB(t.lst) && IsR(t.lst.rst))
     {
         RBColor rb = t.color;
         t           = RotateLR(t);
         t.color     = rb;
         t.rst.color = RBColor.B;
         aux.change  = false;
     }
     else if (IsB(t.lst) && IsR(t.lst.lst))
     {
         RBColor rb = t.color;
         t           = RotateR(t);
         t.color     = rb;
         t.lst.color = t.rst.color = RBColor.B;
         aux.change  = false;
     }
     else if (IsB(t.lst))
     {
         RBColor rb = t.color;
         t.color     = RBColor.B;
         t.lst.color = RBColor.R;
         aux.change  = rb == RBColor.B;
     }
     else if (IsR(t.lst))
     {
         t           = RotateR(t);
         t.color     = RBColor.B;
         t.rst.color = RBColor.R;
         t.rst       = BalanceR(t.rst, aux);
         aux.change  = false;
     }
     else
     {
         throw new Exception("(L) This program is buggy");
     }
     return(t);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 左部分木のエントリー削除に伴う赤黒木の修正(4つのパターン
 /// 戻り値は修正された木。auxに付加情報を返す
 /// </summary>
 private Node BalanceL(Node t, Trio aux)
 {
     if (!aux.change)
     {
         return(t);
     }
     else if (IsB(t.rst) && IsR(t.rst.lst))
     {
         RBColor rb = t.color;
         t           = RotateRL(t);
         t.color     = rb;
         t.lst.color = RBColor.B;                        //左側に黒が追加されて修正
         aux.change  = false;
     }
     else if (IsB(t.rst) && IsR(t.rst.rst))
     {
         RBColor rb = t.color;
         t           = RotateL(t);
         t.color     = rb;
         t.lst.color = t.rst.color = RBColor.B;                  //左側に黒が追加されて修正
         aux.change  = false;
     }
     else if (IsB(t.rst))
     {
         RBColor rb = t.color;
         t.color     = RBColor.B;
         t.rst.color = RBColor.R;
         aux.change  = rb == RBColor.B;                  //tが黒の場合は黒が減るので修正フラグを立て木を遡る(親で何とかする
     }
     else if (IsR(t.rst))
     {
         t           = RotateL(t);
         t.color     = RBColor.B;
         t.lst.color = RBColor.R;
         t.lst       = BalanceL(t.lst, aux);             //再帰は必ず一段で終わる
         aux.change  = false;
     }
     else
     {
         throw new Exception("(L) This program is buggy");
     }
     return(t);
 }
Ejemplo n.º 6
0
        public static Trio Of(SampleDataset dataset)
        {
            var fingerprints = dataset.Fingerprints;
            var scores       = ScoreTable.Of(dataset);
            var matching     = new List <double>();
            var nonmatching  = new List <double>();
            var selfmatching = new List <double>();

            foreach (var probe in fingerprints)
            {
                foreach (var candidate in fingerprints)
                {
                    var score = scores[probe.Id][candidate.Id];
                    if (probe.Id == candidate.Id)
                    {
                        selfmatching.Add(score);
                    }
                    else if (probe.Finger.Id == candidate.Finger.Id)
                    {
                        matching.Add(score);
                    }
                    else
                    {
                        nonmatching.Add(score);
                    }
                }
            }
            matching.Sort();
            nonmatching.Sort();
            selfmatching.Sort();
            var trio = new Trio();

            trio.Matching     = matching.ToArray();
            trio.Nonmatching  = nonmatching.ToArray();
            trio.Selfmatching = selfmatching.ToArray();
            return(trio);
        }
Ejemplo n.º 7
0
			public Trio(Trio a, int b, int c) {
				this.a = a;
				this.b = b;
				this.c = c;
			}
Ejemplo n.º 8
0
		List<int> _longestCommonSubsequence(IList a, IList b) {
			int aStart = 0;
			int aFinish = a.Count-1;
			List<int> matchVector = new List<int>();
			Hashtable bMatches;
			
			// initialize matchVector to length of a
			for (int i = 0; i < a.Count; i++)
				matchVector.Add(-1);
			
			if (!IsPrepared(out bMatches)) {
				int bStart = 0;
				int bFinish = b.Count-1;
		
				// First we prune off any common elements at the beginning
				while (aStart <= aFinish && bStart <= bFinish && compare(a[aStart], b[bStart]))
					matchVector[aStart++] = bStart++;
		
				// now the end
				while (aStart <= aFinish && bStart <= bFinish && compare(a[aFinish], b[bFinish]))
					matchVector[aFinish--] = bFinish--;
		
				// Now compute the equivalence classes of positions of elements
				bMatches =
				  _withPositionsOfInInterval(b, bStart, bFinish);
			}
			
			List<int> thresh = new List<int>();
			List<Trio> links = new List<Trio>();
		
			for (int i = aStart; i <= aFinish; i++) {
				List<int> aimatches = (List<int>)bMatches[a[i]];
				if (aimatches != null) {
					int k = 0;
					for (int ji = 0; ji < aimatches.Count; ji++) {
						int j = aimatches[ji];
						// # optimization: most of the time this will be true
						if (k>0 && (int)thresh[k] > j && (int)thresh[k-1] < j)
							thresh[k] = j;
						else
							k = _replaceNextLargerWith(thresh, j, k);
		
						// oddly, it's faster to always test this (CPU cache?).
						if (k != -1) {
							Trio t = new Trio( (Trio)( k>0 ? links[k-1] : null ), i, j );
							if (k == links.Count)
								links.Add( t );
							else
								links[k] = t;
						}
					}
				}
			}
		
			if (thresh.Count > 0) {
				for (Trio link = (Trio)links[thresh.Count-1]; link != null; link = link.a)
					matchVector[link.b] = link.c;
			}
			
			return matchVector;
		}
Ejemplo n.º 9
0
    public bool hasTerrain(int x, int y)
    {
        var trio = new Trio <int, int, Terrain>(0, 0, null);

        return(tryGetTerrain(x, y, out trio));
    }
Ejemplo n.º 10
0
 //tries to get the terrain from the given x and y coords
 public bool tryGetTerrain(int x, int y, out Trio <int, int, Terrain> t)
 {
     return(terrains.TryGetValue(stringifyCoords(x, y), out t));
 }
Ejemplo n.º 11
0
        private void SetupGame()
        {
            _cardsPlayers.Clear();
            _bidPlayerTeam = null;
            _round = null;
            _biddingPlayerIndex = 0;
            _isDouble = false;
            _players.ForEach(p => p.Score = 0);
            var playerIndex = 0;
            var cardsShuffler = new CardsShuffler();
            var deck = cardsShuffler.GetShuffledDeck();

            for (var i = 0; i < deck.Count; i++)
            {
                if (i != 0 && i % 13 == 0)
                    playerIndex++;
                _cardsPlayers.Add(deck[i], _players[playerIndex]);
            }

            _eventHandlerList[GameSetupCompletedConstant].SafelyInvoke(this,
                                                                       new GameSetupCompletedEventArgs { CardsPlayers = _cardsPlayers });
            _eventHandlerList[BiddingStartedConstant].SafelyInvoke(this,
                                                                   new BidEventArgs { NextCaller = _players[_biddingPlayerIndex] });
        }
Ejemplo n.º 12
0
 private void AssignBidByPlayer(Player player, Bid bid)
 {
     _bidPlayerTeam = new Trio<Bid, Player, Team>
                          {
                              First = bid,
                              Second = player,
                              Third = _teams.First(t => t.Players.Contains(player))
                          };
 }
Ejemplo n.º 13
0
        public static int[][] FindConvexes(Polygon polygon)
        {
            var maxCircles = 10 * polygon.Points.Length;

            var vertices = polygon.Points.Index().ToList();
            List <List <int> > convexes = new List <List <int> >();

            Trio ToTriangleTrio(Trio trio) => new Trio(vertices[trio.i], vertices[trio.j], vertices[trio.k]);

            int CorrectInd(int i) => (i + vertices.Count) % vertices.Count;
            int CorrectDelInd(int i, int delI) => i < delI ? i : CorrectInd(i - 1);
            int NextInd(int i, int count = 1) => CorrectInd(i + count);
            int PrevInd(int i, int count = 1) => CorrectInd(i - count);
            Trio NextTrio(Trio trio) => new Trio(trio.j, trio.k, NextInd(trio.k));

            TrioPairInfo ToPairInfo(Trio trio) => new TrioPairInfo
            {
                Line  = new Line2(polygon[trio.i], polygon[trio.j]),
                Point = polygon[trio.k]
            };

            Vector2 GetPoint(int i) => polygon[vertices[i]];
            TrioPairInfo GetTrioPairInfo(Trio trio) => ToPairInfo(ToTriangleTrio(trio));
            bool IsLeftTrio(Trio trio) => GetTrioPairInfo(trio).IsLeftPoint;

            var t = new Trio(0, 1, 2);
            var n = 0;

            while (vertices.Count > 2 && n++ < maxCircles)
            {
                var validateCount = 0;
                while (!IsLeftTrio(t) && validateCount++ < vertices.Count)
                {
                    t = NextTrio(t);
                }

                if (validateCount == vertices.Count)
                {
                    n = maxCircles;
                    break;
                }

                var convexStartOutInd = PrevInd(t.i);

                var convexEndInfo = GetTrioPairInfo(t);
                var convexCount   = 0;
                while (convexCount++ < vertices.Count)
                {
                    var tInfo = GetTrioPairInfo(t);
                    if (!tInfo.IsLeftPoint)
                    {
                        break;
                    }

                    if (!convexEndInfo.CheckLeftPoint(GetPoint(t.k)))
                    {
                        break;
                    }

                    t = NextTrio(t);
                }

                if (convexCount == vertices.Count)
                {
                    convexes.Add(vertices);
                    break;
                }

                var pairInfo = GetTrioPairInfo(t);
                var convex   = new List <int>();

                var l          = PrevInd(t.i);
                var pointCount = 2;
                while (l != convexStartOutInd && pairInfo.CheckLeftPoint(GetPoint(l)))
                {
                    l = PrevInd(l);
                    pointCount++;
                }

                for (var i = 1; i <= pointCount; i++)
                {
                    convex.Add(vertices[NextInd(l, i)]);
                }

                var convexLines    = convex.SelectCirclePair((j, k) => new Line2(polygon[j], polygon[k])).ToArray();
                var hasInsidePoint = vertices.Except(convex).Select(i => polygon[i]).Any(p => convexLines.All(l => l.IsLeft(p)));

                if (hasInsidePoint)
                {
                    continue;
                }

                for (var i = 2; i < pointCount; i++)
                {
                    var delI = NextInd(l, 2);
                    vertices.RemoveAt(delI);
                    l = CorrectDelInd(l, delI);
                }

                convexes.Add(convex);

                t = new Trio(PrevInd(l, 2), PrevInd(l), l);
            }

            var result = convexes.Select(list => list.ToArray()).ToArray();

            if (n == maxCircles)
            {
                throw new PolygonFillException(result);
            }

            return(result);
        }
Ejemplo n.º 14
0
        private IntList _longestCommonSubsequence(IList a, IList b)
        {
            Hashtable bMatches;
            int       num  = 0;
            int       num2 = a.Count - 1;
            IntList   list = new IntList();

            for (int i = 0; i < a.Count; i++)
            {
                list.Add(-1);
            }
            if (!this.IsPrepared(out bMatches))
            {
                int start = 0;
                int end   = b.Count - 1;
                while (((num <= num2) && (start <= end)) && this.compare(a[num], b[start]))
                {
                    list[num++] = start++;
                }
                while (((num <= num2) && (start <= end)) && this.compare(a[num2], b[end]))
                {
                    list[num2--] = end--;
                }
                bMatches = this._withPositionsOfInInterval(b, start, end);
            }
            IntList   array = new IntList();
            ArrayList list3 = new ArrayList();

            for (int j = num; j <= num2; j++)
            {
                IntList list4 = (IntList)bMatches[a[j]];
                if (list4 != null)
                {
                    int high = 0;
                    for (int k = 0; k < list4.Count; k++)
                    {
                        int num9 = list4[k];
                        if (((high > 0) && (array[high] > num9)) && (array[high - 1] < num9))
                        {
                            array[high] = num9;
                        }
                        else
                        {
                            high = this._replaceNextLargerWith(array, num9, high);
                        }
                        if (high != -1)
                        {
                            Trio trio = new Trio((high > 0) ? ((Trio)list3[high - 1]) : null, j, num9);
                            if (high == list3.Count)
                            {
                                list3.Add(trio);
                            }
                            else
                            {
                                list3[high] = trio;
                            }
                        }
                    }
                }
            }
            if (array.Count > 0)
            {
                for (Trio trio2 = (Trio)list3[array.Count - 1]; trio2 != null; trio2 = trio2.a)
                {
                    list[trio2.b] = trio2.c;
                }
            }
            return(list);
        }
Ejemplo n.º 15
0
 public Trio(Trio a, int b, int c)
 {
     this.a = a;
     this.b = b;
     this.c = c;
 }
Ejemplo n.º 16
0
        private IntList _longestCommonSubsequence(IList a, IList b)
        {
            int       aStart      = 0;
            int       aFinish     = a.Count - 1;
            IntList   matchVector = new IntList();
            Hashtable bMatches;

            // initialize matchVector to length of a
            for (int i = 0; i < a.Count; i++)
            {
                matchVector.Add(-1);
            }

            if (!IsPrepared(out bMatches))
            {
                int bStart  = 0;
                int bFinish = b.Count - 1;

                // First we prune off any common elements at the beginning
                while (aStart <= aFinish && bStart <= bFinish && compare(a[aStart], b[bStart]))
                {
                    matchVector[aStart++] = bStart++;
                }

                // now the end
                while (aStart <= aFinish && bStart <= bFinish && compare(a[aFinish], b[bFinish]))
                {
                    matchVector[aFinish--] = bFinish--;
                }

                // Now compute the equivalence classes of positions of elements
                bMatches =
                    _withPositionsOfInInterval(b, bStart, bFinish);
            }

            IntList  thresh = new IntList();
            TrioList links  = new TrioList();

            for (int i = aStart; i <= aFinish; i++)
            {
                IntList aimatches = (IntList)bMatches[a[i]];
                if (aimatches != null)
                {
                    int k = 0;
                    foreach (int j in aimatches)
                    {
                        // # optimization: most of the time this will be true
                        if (k > 0 && thresh[k] > j && thresh[k - 1] < j)
                        {
                            thresh[k] = j;
                        }
                        else
                        {
                            k = _replaceNextLargerWith(thresh, j, k);
                        }

                        // oddly, it's faster to always test this (CPU cache?).
                        if (k != -1)
                        {
                            Trio t = new Trio((Trio)(k > 0 ? links[k - 1] : null), i, j);
                            if (k == links.Count)
                            {
                                links.Add(t);
                            }
                            else
                            {
                                links[k] = t;
                            }
                        }
                    }
                }
            }

            if (thresh.Count > 0)
            {
                for (Trio link = (Trio)links[thresh.Count - 1]; link != null; link = link.a)
                {
                    matchVector[link.b] = link.c;
                }
            }

            return(matchVector);
        }
Ejemplo n.º 17
0
 public TrioEnumerator(Trio trio)
 {
     _trio = trio;
 }