排列数据结构

发布于 2024-12-11 17:10:15 字数 889 浏览 0 评论 0原文

我需要一些有关排列的帮助。

我的问题是这样的:在我们的系统中,我们有各种设备运行各种软件组件。我有兴趣找到所述组件版本的所有排列(唯一组合),并最终得到元组或结构的列表,

struct Permutation
{
    IComparable Software1{ get; set; }
    IComparable Software2{ get; set; }
    IComparable Software3{ get; set; } 
}

然后得到这样的列表:

Software1: v1
Software2: v1
Software3: v1

Software1: v1
Software2: v2
Software3: v1

Software1: v2
Software2: v1
Software3: v1

该软件存在于以树结构组织的各种组件上(节点->项目)。子节点的类型告诉我要查找哪种软件

 Node->Root (L0)
    Node->Parent (L1) 
       Node->ChildType1 (L2): has property Software1, Software2
       Node->ChildType2 (L2): has property Software3

我可以使用 node.Children (IList) 和 node 轻松导航树.Parent节点)。

我想按顺序迭代树并构建所有排列的列表。 .net 框架中是否有一个好的现有数据结构可供我使用,或者有人对如何解决它有任何建议吗?

I'm in need of some assistance regarding permutations.

My problem is this: In our system we have various devices running various software components. Im interested in finding all the permutations (unique combinations) of the versions of said components, and end up with a list of tuples or structs ala this

struct Permutation
{
    IComparable Software1{ get; set; }
    IComparable Software2{ get; set; }
    IComparable Software3{ get; set; } 
}

Then end up with a list like this:

Software1: v1
Software2: v1
Software3: v1

Software1: v1
Software2: v2
Software3: v1

Software1: v2
Software2: v1
Software3: v1

The software exists on various components organized in a tree structure (Node->Item). The type of the child node tells me which kind of software to look up

 Node->Root (L0)
    Node->Parent (L1) 
       Node->ChildType1 (L2): has property Software1, Software2
       Node->ChildType2 (L2): has property Software3

I can easily navigate the tree with node.Children (IList<Node>) and node.Parent (Node).

I want to inorder-iterate the tree and build a list of all permutations. Is there a good existing data structure in the .net framework which I can use for this, or does anyone have any suggestions on how to solve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒江雪… 2024-12-18 17:10:15

我的想法是这样的:

    var list = from parent in root.Parents()
               select new Permutation
                  {
                      Software1 = parent.ChildA.Software1,
                      Software2 = parent.ChildA.Software2,
                      Software3 = parent.ChildB.Software3,
                  };

    foreach (var perm in list.Distinct())
    {
       // do something 
    }

您需要确保排列具有可比性和等同性:

struct Permutation : IEquatable<Permutation>, IComparable<Permutation>
{
    public IComparable Software1 { get; set; }
    public IComparable Software2 { get; set; }
    public IComparable Software3 { get; set; }

    public bool Equals(Permutation other)
    {
        return Equals(other.Software1, Software1) && Equals(other.Software2, Software2) && Equals(other.Software3, Software3);
    }

    public int CompareTo(Permutation other)
    {
        int cmp = 0;
        if (0 == cmp) cmp = other.Software1.CompareTo(Software1);
        if (0 == cmp) cmp = other.Software2.CompareTo(Software2);
        if (0 == cmp) cmp = other.Software3.CompareTo(Software3);
        return cmp;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (obj.GetType() != typeof(Permutation)) return false;
        return Equals((Permutation)obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int result = (Software1 != null ? Software1.GetHashCode() : 0);
            result = (result * 397) ^ (Software2 != null ? Software2.GetHashCode() : 0);
            result = (result * 397) ^ (Software3 != null ? Software3.GetHashCode() : 0);
            return result;
        }
    }

    public static bool operator ==(Permutation left, Permutation right)
    {
        return left.Equals(right);
    }

    public static bool operator !=(Permutation left, Permutation right)
    {
        return !left.Equals(right);
    }
}

我使用 resharper 来进行跑腿工作:)

My thoughts would be along these lines:

    var list = from parent in root.Parents()
               select new Permutation
                  {
                      Software1 = parent.ChildA.Software1,
                      Software2 = parent.ChildA.Software2,
                      Software3 = parent.ChildB.Software3,
                  };

    foreach (var perm in list.Distinct())
    {
       // do something 
    }

You'll want to make sure that Permutation is comparable and equatable just fine:

struct Permutation : IEquatable<Permutation>, IComparable<Permutation>
{
    public IComparable Software1 { get; set; }
    public IComparable Software2 { get; set; }
    public IComparable Software3 { get; set; }

    public bool Equals(Permutation other)
    {
        return Equals(other.Software1, Software1) && Equals(other.Software2, Software2) && Equals(other.Software3, Software3);
    }

    public int CompareTo(Permutation other)
    {
        int cmp = 0;
        if (0 == cmp) cmp = other.Software1.CompareTo(Software1);
        if (0 == cmp) cmp = other.Software2.CompareTo(Software2);
        if (0 == cmp) cmp = other.Software3.CompareTo(Software3);
        return cmp;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (obj.GetType() != typeof(Permutation)) return false;
        return Equals((Permutation)obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int result = (Software1 != null ? Software1.GetHashCode() : 0);
            result = (result * 397) ^ (Software2 != null ? Software2.GetHashCode() : 0);
            result = (result * 397) ^ (Software3 != null ? Software3.GetHashCode() : 0);
            return result;
        }
    }

    public static bool operator ==(Permutation left, Permutation right)
    {
        return left.Equals(right);
    }

    public static bool operator !=(Permutation left, Permutation right)
    {
        return !left.Equals(right);
    }
}

I used resharper to do the legwork there :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文