How to Implement Tab Completion - c++

I'm trying to figure out how to implement tab completion for subcommands in a C++ application. I would like it to function much like Git's tab completion. I'm trolling through Git's source, but it's not jumping out at me.
I've searched for ways to implement tab completion and haven't found a straight-forward answer, so I'm guessing it might not necessarily be a feature each individual application has to implement. Is tab completion a feature of the particular shell the application is being executed from? What are the basics I need to know about getting my application to support tab completion (particularly in C++)?

The question was answered in the comments.
Is tab completion a feature of the particular shell the application is being executed from?
yes
What are the basics I need to know about getting my application to support tab completion (particularly in C++)?
basically learn more about bash-completion

I've searched for ways to implement tab completion and haven't found a
straight-forward answer
Look at the code here. This should give you a pretty good starting point.
What are the basics I need to know about getting my application to
support tab completion
You should be familiar with Trie data structure, as this is the common data structure used to implement tab completion. There are lots of tutorials explaining it online, look it up.
Pseudo-code (given a list of strings):
For each string in the list, store its characters in Trie data structure.
when the user hit tab key:
(GeeksForGeeks)
Given a query prefix, we search for all words having this query.
Search for given query using standard Trie search algorithm.
If query prefix itself is not present, return -1 to indicate the same.
If query is present and is end of word in Trie, print query. This can quickly checked by seeing if last matching node has isEndWord flag
set. We use this flag in Trie to mark end of word nodes for purpose of
searching.
If last matching node of query has no children, return.
Else recursively print all nodes under subtree of last matching node.

Related

Solr/Lucene "kit" to test searching?

Is there a "code free" way to get SOLR/LUCENE (or something similar) pointed at a set of word docs to make them quickly searchable by a user?
I am prototyping, seeing if there is value in, a system to search through some homegrown news articles. Before I stand up code to handle search string input and document indexing, I wanted to see if it was even worth it before I starting trying to figure it all out.
Thanks,
Judd
Using the bin/post tool of Solr and the Tika handler (named the ExtractingRequestHandler), you should be able to get something up and running for prototyping rather quickly.
See the introduction of Uploading Data with Solr Cell using Apache Tika. Tika is used to process a wide range of different document types.
You can give the Solr post tool a directory or a list of files to submit to the index.
Automatically detect content types in a folder, and recursively scan it for documents for indexing into gettingstarted.
bin/post -c gettingstarted afolder/

how to realize a network-based book query system

Please notice that if i do not want to use database.
i am now learning Unix networking programming. And i have my university book library all book list in seperated txt files. for example, the 'b' begin books is stored in b.txt. all a-z book count is about 1 million record. a line for a book' name and detailed other info.
Now i want to do a program to provide the query service of book list, for example, giving a book name, it can return the detailed info of this bool is it exists.
So i need to first build a module to take the function of query.
Then write the server side to call the query module and get the result and sending the result to the client module.
My question is , if i do not using database. How to realize the query module using c/c++, just first locating the first letter, for example, H begin book name should find in H.txt or H1.txt and H2.txt, using fopen open the file, then read line by line, then compare with queried book name using strFind, strCmp similar function, if have then return the result. i just think this is a time consuming thing and is not realize for using. And if have any such query system could for reference not using database but is bearable in time?
There are several options. The cheapest option (=low development time, low maintenance, low hardware requirements), IMO, is to create a html page on a separate site that links to all the data files. Then you set up another page that uses google.com to search that site. Then you just tell the google web spider to index your site. That way you get excellent performance with minimal work. But... you don't get to program any C.
Simple solution using C:
Do as you yourself suggest. If you have lots of memory available for file caching the performance won't be so bad unless the load gets high.
There will still be some work to do with the rest of the solution since you should delegate the search to worker threads.
Intermediate solution using C:
Find a 3rd party search engine and integrate it with your network code.
Advanced solution using C:
Implement your own search engine.
The problem is WHY DON'T U WANT TO USE DATABASE ?
1. make it easier to deploy?
sqlite may a good choice .
2. trying another method ?
lucene is a good choice of information retrieval, which is written by java.
clucene is someone rewrite the lucene to c .
You may also need stemmer tool(get the root of words),ictclas(chinese words' term extract) etc .
3. would like to do anything by yourself ?
It is easy to manage text file in system , while , as for a "query system", store is not enough , the main problem is IR(information retrieval ).
You may learn something about index building, store and query the index

Loading a text file in to memory and analyze its contents

For educational purposes, I would like to build an IDE for PHP coding.
I made a form app and added OpenFileDialog ..(my c# knowledge was useful, because it was easy ... even though without intelisense!)
Loading a file and reading lines from it is basically the same in every language (even PERL).
But my goal is to write homemade intelisense. I don't need info on the richtextBox and the events it generates, endline, EOF, etc, etc.
The problem I have is, how do I handle the data? line for line?
a struct for each line of text file?
looping all the structs in a linked list? ...
while updating the richtextBox?
searching for opening and closing brackets, variables, etc, etc
I think Microsoft stores a SQL type of database in the app project folders.
But how would you keep track of the variables and simulate them in some sort of form?
I would like to know how to handle this efficiently on dynamic text.
Having never thought this through before, it sounds like an interesting challenge.
Personally, I think you'll have to implement a lexical scanner, tokenizing the entire source file into a source tree, with each token also having information about it mapping the token to a line/character inside of the source file.
From there you can see how far you want to go with it - when someone hovers over a token, it can use the context of the code around it to be more intelligent about the "intellisense" you are providing.
Hovering over something would map back to your source tree, which (as you are building it) you would load up with any information that you want to display.
Maybe it's overkill, but it sounds like a fun project.
This sounds to be related to this question:
https://softwareengineering.stackexchange.com/questions/189471/how-do-ide-s-provide-auto-completion-instant-error-checking-and-debugging
The accepted answer of that question recommends this link which I found very interesting:
http://msdn.microsoft.com/en-us/magazine/cc163781.aspx
In a nutshell, most IDEs generate the parse tree from the code and that is what they stores and manage.

Testing if a string contains one of several thousand substrings

I'm going to be running through live twitter data and attempting to pull out tweets that mention, for example, movie titles. Assuming I have a list of ~7000 hard-coded movie titles I'd like to look against, what's the best way to select the relevant tweets? This project is in it's infancy so I'm open to any looking into any solution (i.e. language agnostic.) Any help would be greatly appreciated.
Update: I'd be curious if anyone had any insight to how the Yahoo! Placemaker API, solves this problem. It can take a text string and return a geocoded JSON result of all the locations mentioned in it.
You could try Wu and Manber's A Fast Algorithm For Multi-Pattern Searching.
The multi-pattern matching problem lies at the heart of virus scanning, so you might look to scanner implementations for inspiration. ClamAV, for example, is open source and some papers have been published describing its algorithms:
Lin, Lin and Lai: A Hybrid Algorithm of Backward Hashing and Automaton Tracking for Virus Scanning (a variant of Wu-Manber; the paper is behind the IEEE paywall).
Cha, Moraru, et al: SplitScreen: Enabling Efficient, Distributed Malware Detection
If you use compiled regular expressions, it should be pretty fast. Maybe especially if you put lots of titles in one expression.
Efficiently searching for many terms in a long character sequence would require a specialized algorithm to avoid testing for every term at every position.
But since it sounds like you have short strings with a known pattern, you should be able to use something fairly simple. Store the set of titles you care about in a hash table or tree. Parse out "string1" and "string2" from each tweet using a regex, and test whether they are contained in the set.
Working off what erickson suggested, the most feasible search is for the ("is better than" in your example), then checking for one of the 7,000 terms. You could instead narrow the set by creating 7,000 searches for "[movie] is better than" and then filtering manually on the second movie, but you'll probably hit the search rate limit pretty quickly.
You could speed up the searching by using a dedicated search service like Solr instead of using text parsing. You might be able to pull out titles quickly using some natural language processing service (OpenCalais?), but that would be better suited to batch processing.
For simultaneously searching for a large number of possible targets, the Rabin-Karp algorithm can often be useful.

TreeView control and Nonrecursion

I can fill a TreeView control with particular Registry keys by enumerating registry keys recursively and put them in the TreeView control; then for performance reason, I attempt to
use a nonrecursive/iterative approach to enumerate registry keys, but how can I fill the TreeView since a "tree" is naturally recursive (at least, in my understanding)? Is recursion is the only way to achieve it? Would someone give some code snippets/examples or redirect me to webpages explain this matter?
BTW, I'm a Delphi/Free Pascal programmer, but C/C++ programming language explanation should be no problem at all. Cheers :-)
I have asked about this on the Free Pascal mailing list, too.
Thanks in advance.
First of all, worrying about the "performance" of recursion when you're reading the data from the registry and putting data into a tree control sounds a bit silly. The costs of recursion are in the nanosecond range, while the cost of reading from the registry is in the microsecond to millisecond range. The cost of inserting into the tree control will depend on things like visibility and how many items it contains, but it's typically closer to the range of the registry than of recursion. If you're going to insert a lot of items into a control, you usually want to lock the control so it's not updated during the insertion, then turn updating back on after you're done.
Second, yes, it can be done without recursion. The usual way is to have a container of some sort to hold data that will need to be processed, such as a queue or stack. When you're walking the registry tree, you retrieve data, and when/if you encounter a "subdirectory" in the tree, you push it on the stack. When you finish with the current "directory", you retrieve the next one from the stack/queue/whatever and process it the same way. When the collection is empty, you're done.
If performance is the issue, consider delayed filling of a TreeView. Start from creating top level of a tree. Fill each next level of a tree only when user expands it. This should solve a stack overflow problem as well.
Here you can find sources of the Native Registry Editor project which you could use as a sample.