Page 3 of 3 FirstFirst 123
Results 21 to 27 of 27
  1. #21
    Join Date
    Jan 2007
    Posts
    640

    Well What I did...

    One of my drivers was to seperate out the actual processing code from the UI code. This is mainly because I ahve a tendency to jam all the code into the Main (or whatever) .pas file.

    I didn't do a thread approach, as I have never used threats before and haven't quite got my head around them. So how I handled the UI updates/responses was to do a couple of thing, first I declared a custom event handler as:

    Code:
    type
      TCountChangeEvent = procedure (Sender: TObject) of object;
    I then declared a property of my processing class as an instance of that. So:

    Code:
    property CountChange: TCountChangeEvent read FCountChange write SetCountChange;
    This way when my processing class made a change to the word count I'd fire an event. I could have used a TNotifyEvent but I wanted to try creating a custom event.

    The next thing I did, which isn't strickly a good thing, was the declare my Constructor as:

    Code:
    Constructor Create(AFileName: String; AOwner: TApplication);
    I did this so I could issue ProcessMessages commands, where if I had used a thread I wouldn't have to do such a thing.

    I came across an issue I don't like, though it might be corrected in D2007. I used the
    Code:
    if x in y then DoSomething;
    Where Y is a set.

    The probelm is you can't do something like
    Code:
    if x not in y then DoSomething;
    What I also considered was how I was going to store the word count, I decided on a class (definitely overkill), however because I decided on a class I could do the following:
    Code:
    var
      idx: Integer;
    begin
      idx := FWords.IndexOf(S1);
      if idx = -1 then
      begin
        FItemCount := TWordCountItem.Create;
        FItemCount.IncCount;
        FWords.AddObject(S1, FItemCount);
      end
      else
      begin
        FItemCount := TWordCountItem(FWords.Objects[idx]);
        FItemCount.IncCount;
      end;
    end;
    Of course as Liz pointed out without proper cleanup at the end, it leaks. I think I got lazy because previously I had used TObjectList wich does the cleanup automatically for you.

    As for the actual UI itself, I used a TPageControl, some panels and two TListviews. I really like TListView controls. Of course Ihad one do my top twenty list by sorting and knocking everything past the 20th item off the list. Visually it looked good, speed wise... it was slow depending on the number of words to be gone through.

    besides the bits from what Liz mentioned, I'd probably redesign the processing class. I'm not sure if I'd use a thread process but I found that when I wanted to tweak soemthing, I had a hard time locating it.

    That's about it, you can find the full code and such
    Here

    Be warned it ain't pretty.

    VK

  2. #22
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    Quote Originally Posted by VelariaKnight View Post
    The probelm is you can't do something like
    Code:
    if x not in y then DoSomething;
    The actual code would be

    Code:
    if not (x in y) then do something
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  3. #23
    Join Date
    Sep 2006
    Posts
    18
    I've looked through posted sources and found that nobody used a tree structure (correct me if I'm wrong).

    Here's what I did:

    I remembered from my "Data structures" class, that for this purpose it is best (or so I've been told) to use a tree, because it will speed up the process of counting words (actually finding a word in a list). I'll try to explain how it works: In my app a tree is an array (or could be made with pointers) of nodes. The node of the tree is a record with 4 fields: left subtree, right subtree, word and count. Left subtree - is an index in the same array of the root of the left subtree, same for right subtree. The right subtree of each node contains elements, that are greater (alphabetically) than the node, and the opposite goes for the left subtree. So if you have a tree with some number of elements in it and you need to find some element, you'll compare this element with each node and if your element is greater - you'll continue search in the right subtree, otherwise - in the left subtree. This way a lot of elements are skipped, therefore the search takes less time. But, there's a case when the tree will look the same as an array - it's when all words in the text are sorted alphabetically. I maybe explaining it not very clearly cause of my English, but If you want I can show you an example.

    So, when I have a full tree which is actually an array, I can sort it descending by field count and print first 20 elements.

    Here's the code: http://www.mediafire.com/?bdth2hy0byj
    Giggity

  4. #24
    Join Date
    Jan 2007
    Posts
    640
    I dunno... it sorta sounds like the way JVCL does it's spell-checking dictyionary. or the way XML based documents work (e.g. elements ithin elements).

    Visually sorting isn't a problem really, you ca make the VCL work for you when you do that. Sorting within a data-structure... the Tree might lend itself to some sorts.

    BTW I rewrote part of my parsing function to use a not in set test, massive time increase, I jumped from 26s for Windows.pas to 179 seconds.

  5. #25
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    oops :P hold that thought though..... you may want it...
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  6. #26
    Join Date
    Jan 2007
    Posts
    640
    I spoke too soon. not (x in y). It was the fct that I was kinda duplicating the actions of dupIgnore, I say "kinda" because I was taking more action then just ignoring the string. Down to 19 secnds. ^_^

  7. #27
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    See the new weeks challenge :P
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •