CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Join Date
    Jun 2003
    Posts
    91

    Fast keyword search through CSortedArray??? Anyone know how?

    Hi There,

    I have a virtual listview that I load up anywhere from 20,000 to 200,000 records into. The virtual listview handles this no problem at all. I am using the CSortedArray by PJ Naughter class to store a array of custom objects. I can sort the array very fast no problems.

    My issue occurs when I have to do a keyword search. I have to be able to allow the user to search as they type into a text box. So the user starts off with a FULL list of ALL the items appearing in the virtual listview, as the type keys into the textbox the virtual listview starts drilling down and only showing items that have the letters being typed in the name. so simple example:

    List
    ====
    Greg
    John
    Steve
    George

    If the user types G in the text box the list will now look like below

    List
    ====
    Greg
    George


    I have this working using a simple for loop to loop through all the items at each keystroke and remove any items from the list that do not match the keys typed. However, when my list gets to 30,000 or higher the search begins to take up WAY too much CPU. Is there some faster way I can do search like this using the CSortedArry?

    Thanks
    Greg

  2. #2
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    I think the bottle neck here would be in the listing of the items in the listbox...the search should be very very fast...
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  3. #3
    Join Date
    Jun 2003
    Posts
    91

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    Well it's pretty fast... doesn't take it long, but when it searches as you type keys it pauses in between keystrokes because it is taking so much cpu I think...

    Is this right?

  4. #4
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    might try changing the search logic where it keeps track of the last find and then when the next key is pressed it starts searching from the last find point and not from the start.
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    Certainly if your array were sorted a binary search would be a lot quicker than a sequential one.

  6. #6
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    Quote Originally Posted by GCDEF
    Certainly if your array were sorted a binary search would be a lot quicker than a sequential one.
    I was headed there next with that suggestion...great minds.....
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  7. #7
    Join Date
    Jun 2003
    Posts
    91

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    The thing is though.... That I have to check each single item no matter what anyway to see if that items name contains any of the letters typed into the text box, so would it really matter in this instance if I used a binary search or not?

    Do I not have to check each and every item anyway?

    Also I have not seen any binary search examples that can return multiple items.. usually they only search for one single item and bring back a single found or not found.. do you know of any exmaples where they do a binary search that brings back multiples?

    Thanks,
    Greg

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    Let me make sure I understand what you're asking.

    If the list contained

    Frog
    go
    golf

    and the user typed in G, you wouldn't want frog to show up would you?

    As to the binary search, it's pretty easily modified. Search until you find an item that matches your criteria, then read sequentially forward and backward from that point until your criteria is no longer met. Return everything in between.

  9. #9
    Join Date
    Jun 2003
    Posts
    91

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    Yes I would want frog to show up

  10. #10
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    Actually you would only need to start searching from your last find forward if the list is sorted, and you are searching for a word that starts with the first letter of the list and the user only adds chars to the end of the search word. If any other char is changed in the search string you would have to start from the beginning.

    If you are searching for a word within a word then you only option is to start from the beginning.
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    Wow, then you're kind of screwed. You could use some king of hash table or multimap I suppose. It would be a little faster.

  12. #12
    Join Date
    Jun 2003
    Posts
    91

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    HAHAHA ok! Maybe I have to change my seach engine then... I might have to give the option to search within a word or from the begingin and just let my users know that when they want to search within a word, they are gonig to experience speed issues..

    Incidentally, would putting the search in a seperate thread help with the search freezing up the rest of my app while typing?

    Thanks,
    Greg

  13. #13
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    Quote Originally Posted by revg75
    HAHAHA ok! Maybe I have to change my seach engine then... I might have to give the option to search within a word or from the begingin and just let my users know that when they want to search within a word, they are gonig to experience speed issues..

    Incidentally, would putting the search in a seperate thread help with the search freezing up the rest of my app while typing?

    Thanks,
    Greg
    A little but not much as the thread and your main GUI will have same priority...
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    Quote Originally Posted by revg75
    HAHAHA ok! Maybe I have to change my seach engine then... I might have to give the option to search within a word or from the begingin and just let my users know that when they want to search within a word, they are gonig to experience speed issues..

    Incidentally, would putting the search in a seperate thread help with the search freezing up the rest of my app while typing?

    Thanks,
    Greg
    Yeah, but what's the point. You can't start the second search until you have the results of the first I assume. You could wait until they've entered everything they're looking for before beginning your search too.

    Your requirements seem a little weird though. Sounds like you're saying a user could type grof and frog would be returned. Without knowing what you're up to, that seems unusual.

  15. #15
    Join Date
    Jun 2003
    Posts
    91

    Re: Fast keyword search through CSortedArray??? Anyone know how?

    Ya my search requirements are a bit weird, but not that weird.

    If an item in the list was

    iamadonkey

    The user could type 'don', or 'donk', or 'key' and it would return a match. However if the user typed 'yeknod' it would NOT return a match.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured