0

ネストされたint[]座標に座標 x、y が含まれている場合、SequenceEqual を使用してそれらを比較するにはどうすればよいですか?

リストは座標のグループです。他のすべてのリストをチェックして、同じ数の座標と同じ座標値があるかどうかを確認したいと思います。両方が一致する場合は、冗長なものを削除したいと思います。それ以外の場合は、そのままにしてください。

  private List<List<int[]>> combineList(List<List<int[]>> matches){
        Debug.Log (matches.Count());

        foreach(List<int[]> tileGroup in matches){
            foreach(List<int[]> other in matches){
                if(other == tileGroup) continue;

                if(sequenceEqual(tileGroup, other)){
                    matches.Remove(other);
                }   


            }
        }

        Debug.Log (matches.Count());

        return matches;
    }

    private bool sequenceEqual(List<int[]> groupA, List<int[]> groupB){
        if(groupA.Count() == groupB.Count()){
            int i = 0, j = 0;
            Dictionary<int, int[]>  dictA = new Dictionary<int, int[]>(), 
                                    dictB = new Dictionary<int, int[]>();   

            foreach(int[] coordinate in groupA){
                dictA.Add (i, coordinate);
                i++;
            }

            foreach(int[] coordinate in groupB){
                dictB.Add (j, coordinate);  
                j++;
            }

            return dictA.Values.SequenceEqual(dictB.Values);
        } 

        return false;
    }
4

1 に答える 1

1

おそらく最も簡単な方法は、次を実装することIEqualityComparer<int[]>です。

class IntArrayEqualityComparer : IEqualityComparer<int[]>
{
    public bool Equals(int[] x, int[] y)
    {
        if (ReferenceEquals(x, y)) return true;
        if (ReferenceEquals(null, x)) return false;
        if (ReferenceEquals(null, y)) return false;
        if (x.Length != y.Length) return false;
        for (var i = 0; i < x.Length; i++)
        {
            if (x[i] != y[i]) return false;
        }
        return true;
    }

    public int GetHashCode(int[] x)
    {
        if (x == null) return 0;
        var hashCode = 0;
        for (var i = 0; i < x.Length; i++)
        {
            hashCode = (32 * hashCode) + x[i];
        }
        return hashCode;
    }
}

そして、オーバーロードされたバージョンのIEnumerable<TSource>.SequenceEqual:

private bool sequenceEqual(List<int[]> groupA, List<int[]> groupB)
{
    if (ReferenceEquals(groupA, groupB)) return true;
    if (ReferenceEquals(null, groupA)) return false;
    if (ReferenceEquals(null, groupB)) return false;
    return groupA.SequenceEqual(groupB, new IntArrayEqualityComparer());
}

長期的にはCoordinates、単純に を実装する型を作成することが有益な場合があります。その場合、単純に 2 つのオブジェクトIEquatable<Coordinates>を比較することになります。List<Coordinates>

于 2013-05-21T01:52:51.500 に答える