c# - Reading generic type using ReadByte() -


i c# beginner , trying read symbol inside binary file. read binary file @ sole argument using readbyte() in c#.by doing

using(var stream = new binaryreader(system.io.file.openread(args[0])))

and after do

 while (stream.basestream.position < stream.basestream.length)   {    int symbol = stream.readbyte();     //and stuff  }    

what until here every thing fine.

but next step make symbol of generic type "<t>". algorithm :

namespace final  {     public class <t> t: icomparable <t>      {         public class node         {             public t symbol; //this symbol has generic type because symbol may int/long/uint etc. on 32/64 bit archtecture.             public node next;             public int freq;         }         public node front;          public a(string[] args) //it's constructor         {             front = null;             using(var stream = new binaryreader(system.io.file.openread(args[0])))              {                                     while (stream.basestream.position < stream.basestream.length)                  {                       func < byte, t > converter = b = > new t(b);                     byte bytevalue = stream.readbyte();                     t processingvalue = converter(bytevalue); //here problem                     node pt, temp;                     pt = front;                     while (pt != null)                     {                         if (pt.symbol == processingvalue) //here problem                         {                             pt.freq++;                             break;                         }                         temp = pt;                         pt = pt.next;                     }                 }             }             stream.close();         }     }     public class myclass     {         public static void main(string[] args)         {             <t> objsym = new <t> (args); //object creation                 }     } } 

please pay attention there three class class a,class node (containing symbol) , class myclass (containing main() function create object) , inside class have constructor.

now trying achieve since yesterday is, trying make "symbol" generic data type, mean symbol may int/uint/long etc. on 32/64 bit architecture.also please note reading symbols binary file at sole argument , symbol in binary of form (11010101 etc.).

i appreciate if 1 please correct algorithm editing in achieving target of making symbol "generic"() type on stuck since yesterday. thank much.

edit: full code:

using system; using system.io; using system.collections.generic; using system.linq; using system.text; using system.runtime.interopservices;   namespace shekhar_final_version_csharp  {   //  public class huffman<k> k :  icomparable<k>     public class huffman < t > t: struct, icomparable < t > , iequatable < t >      {         public int data_size, length, i, is_there;          public class node         {             public node next, left, right;             public t symbol;             public int freq;             public int is_processed;         }         public node front, rear;         ///////////////////////////////////////////////       //  public huffman(string[] args)          public huffman(string[] args, func < byte[], int, t > converter)          {             front = null;             rear = null;             int size = marshal.sizeof(typeof (t));             // console.writeline("size: {0}  ", size);             using(var stream = new binaryreader(system.io.file.openread(args[0])))              {                 while (stream.basestream.position < stream.basestream.length)                  {                    byte[] bytes = stream.readbytes(size);                        t processingvalue = converter(bytes, 0);                      {                         node pt, temp;                         bool is_there = false;                         pt = front;                         while (pt != null)                          {                               if (pt.symbol.equals(processingvalue))                             {                                 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                              {                             //    console.writeline("symbol : {0}  frequency : {1}", pt.symbol, pt.freq);                                 temp.next = front;                                 front = temp;                             }                         }                     }                     console.writeline("check1");                 }                 console.writeline("check2");                                 stream.close();                  //////////////////////////////             }         }         public void print_tree( node treee)          {             node pt = treee;             while (pt != null)              {                 console.writeline("symbol : {0}  frequency : {1}", pt.symbol, pt.freq);                 pt = pt.next;             }         }          /////////////////////////////         public node find_two_smallest(ref  node pmin1, ref  node pmin2)          {             node temp = front;             node temp6 = front;             node address = null;             node min1;             min1 = new node();             min1.freq = int.maxvalue;             node min2;             min2 = new node();             min2.freq = int.maxvalue;             while (temp != null)             {                 if (temp.is_processed == 0)                  {                     if (temp.freq < min2.freq)                      {                         min1 = min2;                         min2 = temp;                     }                      else if (temp.freq < min1.freq && temp.freq != min2.freq)                      {                         min1 = temp;                     }                     temp = temp.next;                 }                  else if (temp.is_processed == 1)                  {                     temp = temp.next;                 }             }             pmin1 = min1;             pmin2 = min2;             //  below code find address of first minimum arriving on traversal of list "front" next execution.             while (temp6 != null)              {                 if (temp6.freq == min1.freq || temp6.freq == min2.freq)                  {                     address = temp6;                     break;                 }                 temp6 = temp6.next;              }             return address;         }         ///////////////////////////////////////////////////////////////////         public int count_remaining()          {             int remaining = 0;             node pt = front;             while (pt != null)              {                 if (pt.is_processed == 0)                  {                     remaining += 1;                 }                 pt = pt.next;             }             return remaining;         }         ////////////////////////////////////////////////////////////////////////////         public void huffman_node_processing()          {             node temp, temp2;             temp2 = front;             while (temp2 != null)              {                 if (temp2.next == null)                  {                     rear = temp2;                     break;                 }                 temp2 = temp2.next;             }             int remaining;             int counter = 0;             remaining = count_remaining();  // can un-comment these console.writeline(..); commented below if want see how addition of nodes taking place(which nodes added),  //i have commented inorder give direct , clear output.             while (front != rear)              {                 if (counter == 0)                  {                     temp = new node();                     console.writeline("first element1 {0} ", front.freq);                     console.writeline("second element1 {0} ", front.next.freq);                     temp.freq = front.freq + front.next.freq;                     front.is_processed = 1;                     front.next.is_processed = 1;                     temp.is_processed = 0;                     temp.left = front;                     temp.right = front.next;                     temp.next = null;                     rear.next = temp;                     front = front.next.next;                     console.writeline("tempcheck1 {0} ", temp.freq);                     rear = rear.next;                     remaining = count_remaining();                     if (remaining == 1)                      {                         break;                     }                 }                 if (rear.freq == front.freq)                  {                     //console.writeline("first element2 {0} ", front.freq);                     //console.writeline("second element2{0} ", front.next.freq);                     temp = new node();                     temp.freq = front.freq + rear.freq;                     rear.is_processed = 1;                     front.is_processed = 1;                     temp.is_processed = 0;                     temp.left = front;                     temp.right = rear;                     temp.next = null;                     rear.next = temp;                     front = front.next;                     //console.writeline("temp check2 {0} ", front.freq);                     rear = rear.next;                     remaining = count_remaining();                     if (remaining == 1)                      {                         break;                     }                 }                  if (rear.freq > front.freq)                  {                     node pmin1 = null;                     node pmin2 = null;                     node address = find_two_smallest(ref pmin1, ref pmin2);                     temp = new node();                     console.writeline("pmin1check3 {0} ", pmin1.freq);                     console.writeline("pmin2check3 {0} ", pmin2.freq);                     temp.freq = pmin1.freq + pmin2.freq;                     pmin1.is_processed = 1;                     pmin2.is_processed = 1;                     temp.is_processed = 0;                     temp.left = pmin2;                     temp.right = pmin1;                     temp.next = null;                     rear.next = temp;                     front = address;                     console.writeline("tempcheck3 {0} ", temp.freq);                     rear = rear.next;                     remaining = count_remaining();                     if (remaining == 1)                      {                         break;                     }                 }                  if (rear.freq < front.freq)                  {                     node pmin1 = null;                     node pmin2 = null;                     node address = find_two_smallest(ref pmin1, ref pmin2);                     temp = new node();                     //console.writeline("pmin1check4 {0} ", pmin1.freq);                     //console.writeline("pmin2check4 {0} ", pmin2.freq);                     temp.freq = pmin1.freq + pmin2.freq;                     pmin1.is_processed = 1;                     pmin2.is_processed = 1;                     temp.is_processed = 0;                     temp.left = pmin2;                     temp.right = pmin1;                     temp.next = null;                     rear.next = temp;                     front = address;                     //console.writeline("tempcheck4 {0} ", temp.freq);                     rear = rear.next;                     remaining = count_remaining();                     if (remaining == 1)                      {                         break;                     }                 }                 counter++;             }          }         ////////////////////////////////////////////         public void generatecode( node parentnode, string code)          {             if (parentnode != null)              {                 generatecode(parentnode.left, code + "0");                 if (parentnode.left == null && parentnode.right == null)                     console.writeline("symbol : " + parentnode.symbol + "  frequency :  " + code);                 generatecode(parentnode.right, code + "1");             }         }      }     public class myclass      {         public static void main(string[] args)          {            // huffman<k>  objsym = new huffman<k>(args); //object creation             huffman < long > objsym = new huffman < long > (args, bitconverter.toint64);             console.writeline("\nreading binary file......");             objsym.print_tree(objsym.front);             objsym.huffman_node_processing();             console.writeline("\nthe encoding of symbols :");             objsym.generatecode(objsym.rear, "");          }     } } 

and error : (for debugging tried print check1 , check2 in constructor prints "check1" not "check2").please until end of output:

hp@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 not long enough copy items in collection. check array index , length.   @ system.bitconverter.putbytes (system.byte* dst, system.byte[] src, int32 start_index, int32 count) [0x00000] in <filename unknown>:0    @ system.bitconverter.toint64 (system.byte[] value, int32 startindex) [0x00000] in <filename unknown>:0    @ shekhar_final_version_csharp.huffman`1[system.int64]..ctor (system.string[] args, system.func`3 converter) [0x00000] in <filename unknown>:0    @ shekhar_final_version_csharp.myclass.main (system.string[] args) [0x00000] in <filename unknown>:0  [error] fatal unhandled exception: system.argumentexception: destination array not long enough copy items in collection. check array index , length.   @ system.bitconverter.putbytes (system.byte* dst, system.byte[] src, int32 start_index, int32 count) [0x00000] in <filename unknown>:0    @ system.bitconverter.toint64 (system.byte[] value, int32 startindex) [0x00000] in <filename unknown>:0    @ shekhar_final_version_csharp.huffman`1[system.int64]..ctor (system.string[] args, system.func`3 converter) [0x00000] in <filename unknown>:0    @ shekhar_final_version_csharp.myclass.main (system.string[] args) [0x00000] in <filename unknown>:0  hp@ubuntu:~/desktop/internship_xav/templatescplus$ 

you have pass conversion function , size of numeric type constructor of a:

public a(string[] args, int size, func<byte[], int, t> converter) { ... }  a<long> objsym = new a<long>(args, sizeof(long), bitconverter.toint64) 

instead of passing size directly, use marshal.sizeof:

int size = marshal.sizeof(typeof(t)); 

then can read required number of bytes reader , conversion:

byte[] bytes = stream.readbytes(size); t processingvalue = converter(bytes, 0); 

you can check symbol using equals:

if (pt.symbol.equals(processingvalue)) 

you can avoid boxing during equality check if further constrain type parameter implement iequatable<t>:

public class a<t> t : struct, icomparable <t>, iequatable<t> {     public class node     {         public t symbol;         public node next;         public int freq;     }      public node front;      public a(string[] args, func<byte[], int, t> converter)     {         int size = marshal.sizeof(typeof(t));         front = null;         using(var stream = new binaryreader(system.io.file.openread(args[0])))          {                                 while (stream.basestream.position < stream.basestream.length)              {                 byte[] bytes = stream.readbytes(size);                 t processingvalue = converter(bytes, 0);                  node pt, temp;                 pt = front;                 while (pt != null)                 {                     if (pt.symbol.equals(processingvalue))                     {                         pt.freq++;                         break;                     }                     temp = pt;                     pt = pt.next;                 }             }         }     } } 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -