/
inc_search.cpp
141 lines (119 loc) · 3.55 KB
/
inc_search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <curses.h>
#include <iostream>
#include <vector>
#include <stack>
#include <string>
#include <algorithm>
#define ESC 27
#define BCKSPC 127
using namespace std;
vector<string > in_array;
vector<int > look_for(const string search_term, int pos, const vector<string > &in_array, vector<int > &indexes)
{
vector<int > new_indexes;
char compare_ch, ch;
if (search_term.empty())
return new_indexes;
if (pos < 0)
pos = 0;
for (vector<int >::iterator index = indexes.begin(); index != indexes.end(); ++index)
{
compare_ch = (in_array[*index])[pos];
ch = search_term[pos];
if (compare_ch == ch)
new_indexes.push_back(*index);
}
return new_indexes;
}
int main()
{
// Initialize the input array of strings.
vector<string > in_array;
in_array.push_back("abde");
in_array.push_back("abce");
in_array.push_back("bda");
in_array.push_back("bdecf");
in_array.push_back("bfeda");
in_array.push_back("cade");
in_array.push_back("cabe");
in_array.push_back("abcd");
in_array.push_back("abc");
in_array.push_back("bdac");
// Stores the search_term
string search_term;
// Key pressed by the user
char ch;
// Stack of vectors. Each vector stores a list of indexes that match the current search_term.
// Top of the stack has the vector with latest matching entries.
// Bottom of the stack has the vector with all the indexes in in_array.
// Each entry is the stack will be a subset of the previous entry.
stack<vector <int > > possibles;
// Current position of the cursor where the next character will be entered.
int cursor_pos = 0;
// Current position of the character to look for in the in_array's strings.
// Initialize to -1, since no key is pressed yet.
int pos = -1;
vector<int > indices; // Vector to store the indexes of in_array that match the current search_term
int index = 0;
// Initialize the indices with all the entries in in_array.
for (int i = 0 ; i < in_array.size(); i++)
{
indices.push_back(i);
}
// Add 'indices' to the stack. This will always be the bottom of the stack.
possibles.push(indices);
// ncurses initialization
initscr();
cbreak();
noecho();
for (vector<int >::iterator it = indices.begin(); it != indices.end(); ++it )
{
printw("%s\n",in_array[*it].c_str());
//printf("%s\n",in_array[*it].c_str());
}
// Keep getting characters until esc key is pressed.
while (1)
{
// Get one character
ch = getch();
if (ESC == ch) // If current char is ESC, break out of loop
break;
// If backspace is pressed then pop the last char from the search_term
else if (BCKSPC == ch)
{
if ( !search_term.empty() ) // Do not try to erase an empty string. cursor_pos can never be less than 0
{
search_term.erase(cursor_pos - 1); // Remove the char from the string
if (cursor_pos > 0)
cursor_pos--;
}
while (possibles.size() > cursor_pos+1)
possibles.pop();
}
else if ( (ch >= 'a' && ch <= 'z') || (ch >='A' && ch <= 'Z') || (ch >='0' && ch <='9') )
{
search_term.push_back(ch); // store the current key to the search string
cursor_pos++;
}
pos = cursor_pos - 1;
while(pos < search_term.size())
{
indices = look_for(search_term, pos, in_array, possibles.top());
pos++;
possibles.push(indices);
}
vector<int > cur_results;
cur_results = possibles.top();
//Clear the screen before printing the results
clear();
// print results
for (vector<int >::iterator it = cur_results.begin(); it != cur_results.end(); ++it )
{
printw("%s\n",in_array[*it].c_str());
//printf("%s\n",in_array[*it].c_str());
}
}
endwin();
return 0;
}