prePEND一个C#数组 [英] Prepend to a C# Array

查看:147
本文介绍了prePEND一个C#数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于人口稠密字节[]值在C#中,我要prePEND值(字节)为0x00 到阵列。我认为这将需要制作一个新的数组,并添加旧数组的内容。速度是我的应用程序的一个重要方面。什么是做到这一点的最好方法是什么?

Given a populated byte[] values in C#, I want to prepend the value (byte)0x00 to the array. I assume this will require making a new array and adding the contents of the old array. Speed is an important aspect of my application. What is the best way to do this?

- 编辑 -

字节[] 用于存储DSA(数字签名算法)参数。该操作将只需要每个阵列执行一次,而且速度是很重要的,因为我有可能在执行此操作很多不同的字节[] 秒。

The byte[] is used to store DSA (Digital Signature Algorithm) parameters. The operation will only need to be performed once per array, but speed is important because I am potentially performing this operation on many different byte[]s.

推荐答案

如果你只打算执行这个操作一次,然后没有一大堆的选择。由梦露的回答提供的code 应该做的就好了。

If you are only going to perform this operation once then there isn't a whole lot of choices. The code provided by Monroe's answer should do just fine.

byte[] newValues = new byte[values.Length + 1];
newValues[0] = 0x00;                                // set the prepended value
Array.Copy(values, 0, newValues, 1, values.Length); // copy the old values

不过,如果你要你有一些更多的选择多次被执行此操作。有是p $ ppending数据$到一个数组的一个根本问题是不是一个有效的操作,所以你可以选择使用替代数据结构。

If, however, you're going to be performing this operation multiple times you have some more choices. There is a fundamental problem that prepending data to an array isn't an efficient operation, so you could choose to use an alternate data structure.

A 的LinkedList 可以有效prePEND的数据,但它在一般对大多数任务的效率较低,因为它涉及到了很多更多的内存分配/释放,也失去记忆locallity ,所以它可能不是一个净赢。

A LinkedList can efficiently prepend data, but it's less efficient in general for most tasks as it involves a lot more memory allocation/deallocation and also looses memory locallity, so it may not be a net win.

一个双端队列(称为双端)将是你一个梦幻般的数据结构。可以有效地添加到开始或结束,高效地访问结构中的任何位置的数据(但不能有效地将除开始或结束的地方除外)。这里的主要问题是,.NET不提供双端队列的实现。你需要找到一个实现第三方库。

A double ended queue (known as a deque) would be a fantastic data structure for you. You can efficiently add to the start or the end, and efficiently access data anywhere in the structure (but you can't efficiently insert somewhere other than the start or end). The major problem here is that .NET doesn't provide an implementation of a deque. You'd need to find a 3rd party library with an implementation.

您还可以通过跟踪的数据,我需要prePEND(使用列表/队列/等),然后等待复制时节省大量实际prePEND数据只要尽可能,这样就尽可能地,以及限制现有元素的副本数量。最小化的新阵列的创建

You can also save yourself a lot when copying by keeping track of "data that I need to prepend" (using a List/Queue/etc.) and then waiting to actually prepend the data as long as possible, so that you minimize the creation of new arrays as much as possible, as well as limiting the number of copies of existing elements.

您也可以考虑让你加入到了最后,而不是一开始(即使你知道你会需要稍后扭转它),你是否可以调整结构。如果在很短的时间空间,追加了很多它可能是值得存储在数据中的列表(可有效地添加到的结束的)和添加到最后。根据您的需求,它甚至可能是值得的一类,它是一个List的包装和隐藏它扭转了事实。你可以做一个映射索引 I COUNT-I 等,使其显示,从外面看,就好像你的数据通常存储,即使内部列表实际持有数据倒退。

You could also consider whether you could adjust the structure so that you're adding to the end, rather than the start (even if you know that you'll need to reverse it later). If you are appending a lot in a short space of time it may be worth storing the data in a List (which can efficiently add to the end) and adding to the end. Depending on your needs, it may even be worth making a class that is a wrapper for a List and that hides the fact that it is reversed. You could make an indexer that maps i to Count-i, etc. so that it appears, from the outside, as though your data is stored normally, even though the internal List actually holds the data backwards.

这篇关于prePEND一个C#数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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