Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c“从”转换;字节";键入“到”;“通用”&引用;K";类型_C#_Algorithm_Generics_Data Structures - Fatal编程技术网

C# c“从”转换;字节";键入“到”;“通用”&引用;K";类型

C# c“从”转换;字节";键入“到”;“通用”&引用;K";类型,c#,algorithm,generics,data-structures,C#,Algorithm,Generics,Data Structures,我在c#中创建了哈夫曼树,通过使用ReadByte()读取二进制文件作为唯一参数。在我通过二进制文件中每个符号的重复计数来计算每个符号的频率之后。我将读取的字节存储在processingValue中,该字节的初始类型为“int”(在我尝试将其设置为“K”类型后,我的意思是泛型,之后会产生问题)。但一切都很顺利。 请参阅下面的代码: public class Node { public Node next, left, right;

我在c#中创建了哈夫曼树,通过使用ReadByte()读取二进制文件作为唯一参数。在我通过二进制文件中每个符号的重复计数来计算每个
符号的频率之后。我将读取的字节存储在
processingValue
中,该字节的初始类型为“
int
”(在我尝试将其设置为
“K”
类型后,我的意思是
泛型
,之后会产生问题)。但一切都很顺利。 请参阅下面的代码:

  public class Node
        {
            public Node next, left, right;
            public int symbol; //After i will make it generic by putting "K" at the place of "int"
            public int freq;
            public int is_processed;
        }
        public Node front, rear;
这个哈夫曼是构造函数,这两个函数定义(上面和下面)都在哈夫曼类中

                while (stream.BaseStream.Position < stream.BaseStream.Length) 
                {
                    int processingValue = stream.ReadByte(); //The problem is here when i tried to do Func<byte,K>
                    {
                        Node pt, temp;
                        bool is_there = false;
                        pt = front;
                        while (pt != null) 
                        {
                            if (pt.symbol == processingValue) //The problem is here it can's compare "byte" to "K" type.
                            {
                                pt.freq++;
                                is_there = true;

                                break;
                            }
                            temp = pt;
                            pt = pt.next;
                        }
                        if (is_there == false) 
                        {
                            temp = new Node();
                            temp.symbol = processingValue;
                            temp.freq = 1;
                            temp.left = null;
                            temp.right = null;
                            temp.next = null;
                            temp.is_processed = 0;
                            if (front == null) 
                            {
                                front = temp;
                            } 
                            else 
                            {
                                temp.next = front;
                                front = temp;
                            }
                        }
                    }
                }
                stream.Close();
                //////////////////////////////
            }
        }
现在产生的问题是,当我使用
ReadByte()
读取字节时,我将其保存在代码中名为“
processingValue
”的类型变量“
int
”中。现在,我将“
int
”替换为“K”以使其通用。但是当我这样做的时候,我有一个错误,说从字节到“K”没有可能的对话。 有没有人能给我一个等价的代码,让“
int
”type
processingValue
generic
type
“K”
,因为
“symbol”
也必须为“short”/“ulong”工作etc在32/64位体系结构上读取二进制文件,通过计算二进制文件中存在的符号的重复来创建频率。 我还尝试了
Func
Func processingValue=stream.ReadByte();
),但出现了错误:

error CS0029: Cannot implicitly convert type `byte' to `System.Func<byte,K>'
错误CS0029:无法将类型“byte”隐式转换为“System.Func”
编辑:我已将代码更改为for Func,但编译时没有错误,但 在执行“mono filename.exe binaryFile.bin”//I时,我正在计算此二进制文件中符号的频率。(时间符号重复的次数是它的频率),我有未处理的异常:请查看下面的错误:(它只是在错误下面给出的代码的构造函数中打印“check1”,而不是“check2”)

hp@ubuntu:~/Desktop/interporation\u Xav/templatescplus$gmcs测试。cshp@ubuntu:~/Desktop/interporation\u Xav/templatescplus$mono test.exe toto.bin check1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
未处理的异常:System.ArgumentException:目标数组不够长,无法复制集合中的所有项。检查数组索引和长度。
在System.BitConverter.PutBytes处(System.Byte*dst,System.Byte[]src,Int32开始索引,Int32计数)[0x00000]in:0
在System.BitConverter.ToInt64(System.Byte[]值,Int32 startIndex)[0x00000]中:0
在shekhar_最终版本_Csharp.Huffman`1[System.Int64]…ctor(System.String[]args,System.Func`3转换器)[0x00000]中:0
在shekhar_最终版本_Csharp.MyClass.Main(System.String[]args)[0x00000]中:0
[错误]致命的未处理异常:System.ArgumentException:目标数组不够长,无法复制集合中的所有项。检查数组索引和长度。
在System.BitConverter.PutBytes处(System.Byte*dst,System.Byte[]src,Int32开始索引,Int32计数)[0x00000]in:0
在System.BitConverter.ToInt64(System.Byte[]值,Int32 startIndex)[0x00000]中:0
在shekhar_最终版本_Csharp.Huffman`1[System.Int64]…ctor(System.String[]args,System.Func`3转换器)[0x00000]中:0
在shekhar_最终版本_Csharp.MyClass.Main(System.String[]args)[0x00000]中:0
hp@ubuntu:~/Desktop/interporation\u Xav/templatescplus$

好吧,错误正好说明了问题所在-编译器不知道如何将
字节
转换为实现
i可比较
的随机
K
。你根本无法做到这一点,因为它可以是或多或少的任何东西

如果需要由符号类型参数化的通用哈夫曼树,则需要

  • 让用户向您提供已读入并转换为适当类型的符号
  • 让用户提供一个函数,将
    byte
    转换为他们选择的
    K
    (即
    Func
    ),然后您的阅读代码可以使用该函数

  • 一般来说,我会推荐前者,因为它鼓励分离关注点。一般来说,将I/O构建到数据结构类中是一个坏主意,因为这样一来,您将无法从其他源读取数据。选项2将允许您尽可能接近现有代码。

    好吧,错误正好说明了问题所在-编译器不知道如何将
    字节
    转换为实现
    i可比较
    的随机
    K
    。你根本无法做到这一点,因为它可以是或多或少的任何东西error CS0029: Cannot implicitly convert type `byte' to `System.Func<byte,K>'
    hp@ubuntu:~/Desktop/Internship_Xav/templatescplus$ gmcs test.cshp@ubuntu:~/Desktop/Internship_Xav/templatescplus$ mono test.exe toto.bin check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    check1
    
    Unhandled Exception: System.ArgumentException: Destination array is not long enough to copy all the items in the collection. Check array index and length.
      at System.BitConverter.PutBytes (System.Byte* dst, System.Byte[] src, Int32 start_index, Int32 count) [0x00000] in <filename unknown>:0 
      at System.BitConverter.ToInt64 (System.Byte[] value, Int32 startIndex) [0x00000] in <filename unknown>:0 
      at shekhar_final_version_Csharp.Huffman`1[System.Int64]..ctor (System.String[] args, System.Func`3 converter) [0x00000] in <filename unknown>:0 
      at shekhar_final_version_Csharp.MyClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
    [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: Destination array is not long enough to copy all the items in the collection. Check array index and length.
      at System.BitConverter.PutBytes (System.Byte* dst, System.Byte[] src, Int32 start_index, Int32 count) [0x00000] in <filename unknown>:0 
      at System.BitConverter.ToInt64 (System.Byte[] value, Int32 startIndex) [0x00000] in <filename unknown>:0 
      at shekhar_final_version_Csharp.Huffman`1[System.Int64]..ctor (System.String[] args, System.Func`3 converter) [0x00000] in <filename unknown>:0 
      at shekhar_final_version_Csharp.MyClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
    hp@ubuntu:~/Desktop/Internship_Xav/templatescplus$
    
    public interface ICanLoadFromStream: IComparable{
       void LoadFromStream(Stream stream);
    }
    
    public class Huffman<K> where K :  ICanLoadFromStream{
    ...
    }
    
    Func<byte,K> converter = b=>new K(b);
    
    byte byteValue = stream.ReadByte();
    K kValue = converter(byteValue);
    
    public Huffman(Func<byte,K> converter)
    {
      m_Converter = converter;
    }
    
    int size = Marshal.SizeOf(typeof (T));    
    long length = stream.BaseStream.Length;
    for (long position = 0; position + size < length; position += size)
    {
      //other stuffs   
    }