2 Replies - 327 Views - Last Post: 04 February 2013 - 04:07 PM Rate Topic: -----

#1 DamianMRLY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-December 12

Sort on frequency/bytes from .txt

Posted 04 February 2013 - 02:23 PM

Hi,

I want to read an file like (readthisfile.txt) read all the bytes like T=116 and the frequency(occurence of each byte) example: "test"

http://i50.tinypic.com/1pgn76.png

         private void btn_Click(object sender, EventArgs e)
        {
            
            long[] far = new long[256];
            if(ofd.ShowDialog()==DialogResult.OK)
            {
                //read file and put in bar(bytearray)
                FileStream s = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(s);               
                byte[] bar = br.ReadBytes((int)s.Length);
                foreach (var b in bar)
                {
                    //determine freqeuncy and put in far     
                    far[b]++;
                }
            }
            //put nodes in dll-list
            int index=0;
            for (byte i = 0; i < far.Length; i++)
			{
                //make node if frequency of byte <> 0
			    if(far[i]!=0)
                {
                    //create node and change head
                    cN.nar[index]=new cN(null,far[i],i,null);
                    cN.mAddNode2List(cN.nar[index]);
                }
			}
            cN.nar[512] = cN.H;
           

        }
    }


        class cN
    {
        public static cN[] nar=new cN[512];     //node array

        public static cN H=null;    //head
        private static cN W = null; //walker
        private static cN T=null;    //Tail

        private cN P;           //Previous  dll
        private long F;         //Frequency
        private byte B;         //Bytevalue
        private cN N;           //Next dll

        public cN(cN p,long f, byte b, cN n)
        {
            P=p;
            F=f;
            B=b;
            N=n;
        }

        public static void mAddNode2List(cN nX)
        {
            if (H == null)
            {
                //set first node as head
                H = nX;
            }
            else 
            {
                
                    
            }
        }
    }



With the help of an doubly linked list i want to add it in an list

But at this point i am a bit stuck, can someone give me some directions. I am not asking for the full written out code, just a little push:)

Is This A Good Question/Topic? 0
  • +

Replies To: Sort on frequency/bytes from .txt

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,463
  • Joined: 05-May 12

Re: Sort on frequency/bytes from .txt

Posted 04 February 2013 - 03:55 PM

Silly question time: Why would you want to put your frequency table into a linked list?

After reading your data and getting the frequency, you are using only 256 * 8 bytes. And that memory is one nice contiguous chunk of memory which is good for cache hits. By building a doubly linked list, you are going to be allocating 256 * (8 + 1 + 8 + 8) bytes (assuming a 64-bit processor), with no guarantees that memory will be contiguous.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: Sort on frequency/bytes from .txt

Posted 04 February 2013 - 04:07 PM

Personally, I'm lazy.
I'd just use a Dictionary(char,int) to represent this:
Posted Image

Each time you get a value such as 115, you add 1 to the value of that key.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1