C数据结构来模仿C#的名单,LT;名单< INT>&GT ;? [英] C data structure to mimic C#'s List<List<int>>?

查看:251
本文介绍了C数据结构来模仿C#的名单,LT;名单< INT>&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待重构,试图一个C#方法到c函数来获得一些速度,然后调用C DLL在C#中让我的程序使用的功能。

I am looking to refactor a c# method into a c function in an attempt to gain some speed, and then call the c dll in c# to allow my program to use the functionality.

目前的C#方法采用整数列表,并返回整数列表的列表。计算设定整数如此的3个整数的输入将产生以下输出功率的方法,(在此阶段的整数的值,因为它被用作一个内部的加权值并不重要)

Currently the c# method takes a list of integers and returns a list of lists of integers. The method calculated the power set of the integers so an input of 3 ints would produce the following output (at this stage the values of the ints is not important as it is used as an internal weighting value)

1
2
3
1,2
1,3
2,3
1,2,3

每一行再presents整数列表。输出表示第一列表的索引(与偏移1),而不是该值。所以1,2表示在索引0和1的元件是功率集合的一个元素

Where each line represents a list of integers. The output indicates the index (with an offset of 1) of the first list, not the value. So 1,2 indicates that the element at index 0 and 1 are an element of the power set.

我不熟悉C,那么,什么是数据结构我最好的选择,将允许C#来访问返回的数据?

I am unfamiliar with c, so what are my best options for data structures that will allow the c# to access the returned data?

在此先感谢

更新

感谢大家的意见至今。这是一个有点背景,问题的性质。

Thank you all for your comments so far. Here is a bit of a background to the nature of the problem.

有关计算设置一组电源的迭代方法是相当简单的。两个循环和一个点滴操纵的一切就是真的。它只是得到called..a很多(事实上数十亿倍如果集的大小足够大)。

The iterative method for calculating the power set of a set is fairly straight forward. Two loops and a bit of bit manipulation is all there is to it really. It just get called..a lot (in fact billions of times if the size of the set is big enough).

我的周围用c(C ++作为人指出)的几点思考是,它提供了性能调整更多的空间。直接端口可能不提供任何增加,但它打开了道路,更为复杂的方法来获取更多的速度出来。即使每次迭代小幅增长将等同于一个可衡量的增长。

My thoughs around using c (c++ as people have pointed out) are that it gives more scope for performance tuning. A direct port may not offer any increase, but it opens the way for more involved methods to get a bit more speed out of it. Even a small increase per iteration would equate to a measurable increase.

我的想法是端口直接的版本,然后努力增加。然后重构它随着时间的推移(与大家在这里帮助在SO)。

My idea was to port a direct version and then work to increase it. And then refactor it over time (with help from everyone here at SO).

更新2

从jalf另一公平点,我没有使用列表或equivelent。如果有更好的方法,然后我愿意接受建议。对于列表中的唯一原因是,各组的结果是不相同的尺寸。

Another fair point from jalf, I dont have to use list or equivelent. If there is a better way then I am open to suggestions. The only reason for the list was that each set of results is not the same size.

的code到目前为止...

public List<List<int>> powerset(List<int> currentGroupList)
{
    _currentGroupList = currentGroupList;
    int max;
    int count;

    //Count the objects in the group
    count = _currentGroupList.Count;
    max = (int)Math.Pow(2, count);

    //outer loop
    for (int i = 0; i < max; i++)
    {
        _currentSet = new List<int>();

        //inner loop
        for (int j = 0; j < count; j++)
        {              
            if ((i & (1 << j)) == 0)
            {
                _currentSetList.Add(_currentGroupList.ElementAt(j));                          
            }
        }
        outputList.Add(_currentSetList);
    }   
    return outputList;
}

正如你所看到的,不是很多吧。它只是打转了很多!

As you can see, not a lot to it. It just goes round and round a lot!

我接受列表的创建和建筑物可能不是最有效的方式,但我需要在一个可管理的方式提供所述结果传回的一些方法。

I accept that the creating and building of lists may not be the most efficient way, but I need some way of providing the results back in a manageable way.

更新2

感谢所有的投入和实施工作。只是为了澄清几个募穴:我不需要输出是在自然顺序,也是我不是空集返回感兴趣。

Thanks for all the input and implementation work. Just to clarify a couple of points raised: I dont need the output to be in 'natural order', and also I am not that interested in the empty set being returned.

hughdbrown的实现intesting,但我认为我会需要存储在某个点的结果(或至少其中的一个子集)。这听起来像内存limitiations将适用于很久以前运行时间成为一个真正的问题。
部分原因是因为这一点,我想我可以逃脱使用字节而不是整数,让更多的潜在的存储。

hughdbrown's implementation is intesting but i think that i will need to store the results (or at least a subset of them) at some point. It sounds like memory limitiations will apply long before running time becomes a real issue. Partly because of this, I think I can get away with using bytes instead of integers, giving more potential storage.

真正的问题则是:我们已经达到了这个calcualtion在C#中的最高速度是多少?难道非托管code的选项提供任何更多的余地。我知道,在许多方面,答案是徒劳的,因为即使我们havled运行的时间,这将只允许在原设定一个额外的值。

The question really is then: Have we reached the maximum speed for this calcualtion in C#? Does the option of unmanaged code provide any more scope. I know in many respects the answer is futile, as even if we havled the time to run, it would only allow an extra values in the original set.

推荐答案

这将返回一次一个集幂的。它是基于Python code <一个href=\"http://groups.google.com/group/comp.lang.python/browse_thread/thread/d9211cd6c65e1d3a/\">here.它适用于超过32元的powersets。如果需要少于32个,你可以改变长期为int。这是pretty快 - 比我的previous算法快,比快(我修改为使用的收益回报版)芘爸爸的code

This returns one set of a powerset at a time. It is based on python code here. It works for powersets of over 32 elements. If you need fewer than 32, you can change long to int. It is pretty fast -- faster than my previous algorithm and faster than (my modified to use yield return version of) P Daddy's code.

static class PowerSet4<T>
{
	static public IEnumerable<IList<T>> powerset(T[] currentGroupList)
	{
		int count = currentGroupList.Length;
		Dictionary<long, T> powerToIndex = new Dictionary<long, T>();
		long mask = 1L;
		for (int i = 0; i < count; i++)
		{
			powerToIndex[mask] = currentGroupList[i];
			mask <<= 1;
		}

		Dictionary<long, T> result = new Dictionary<long, T>();
		yield return result.Values.ToArray();

		long max = 1L << count;
		for (long i = 1L; i < max; i++)
		{
			long key = i & -i;
			if (result.ContainsKey(key))
				result.Remove(key);
			else
				result[key] = powerToIndex[key];
			yield return result.Values.ToArray();
		}
	}
}

您可以下载所有的最快的版本我已经测试这里

You can download all the fastest versions I have tested here.

我真的觉得用收益回报,使得计算大powersets可能的变化。分配大量内存的前期大大增加运行时间,并导致算法失败的内存不足很早。楼主应该弄清楚他做了多少套幂都需要一次。拿着所有的人是不是真的有> 24元素的选择。

I really think that using yield return is the change that makes calculating large powersets possible. Allocating large amounts of memory upfront increases runtime dramatically and causes algorithms to fail for lack of memory very early on. Original Poster should figure out how many sets of a powerset he needs at once. Holding all of them is not really an option with >24 elements.

这篇关于C数据结构来模仿C#的名单,LT;名单&LT; INT&GT;&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆