index: Add a print() function

This function is used in debug mode to print the current status of the
index.  I also created a print_keys() function to only list the keys.

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2013-10-06 21:58:22 -04:00 committed by Anna Schumaker
parent 79d592ed64
commit 629cbeb2e9
6 changed files with 122 additions and 18 deletions

View File

@ -403,9 +403,12 @@ Index: (lib/index.cpp)
Index::Index(filename);
void load();
void save();
void print();
void insert(key, unsigned int);
void remove(key);
void remove(key, unsigned int);
const set<string>::iterator keys_begin();
const set<string>::iterator keys_end();
const set<unsigned int> &operator[](string);
@ -416,35 +419,45 @@ Index: (lib/index.cpp)
File << map[key].size() << int_0 << int_1 << ... << int_n << endl;
- API:
Index : Index(filename);
Index :: Index(filename);
Initializes an index using ~/.ocarina{-debug}K/filename. Pass
an empty string if you do not want this index to be saved.
void Index : load();
void Index :: load();
Reads data from a file. Call after static initialization of
Ocarina to ensure idle tasks are configured
void Index : save();
void Index :: save();
Saves data to file
void Index : insert(key, unsigned int);
void Index :: print();
This function exists only if CONFIG_DEBUG is enabled.
Following a similar format for writing to disk, print the
index to the console in a human-readable format.
void Index :: print_keys();
This function exists only if CONFIG_DEBUG is enabled.
Print the database keys to the console in a human-readable
format.
void Index :: insert(key, unsigned int);
1) If key does not exist, create it.
2) Add int to the set for the given key
void Index : remove(key);
void Index :: remove(key);
Remove a key from the index;
Index : remove(key, unsigned int);
void Index :: remove(key, unsigned int);
1) Remove int from the set of values associated with key
2) If the set is empty, remove the key
const set<string>::iterator void Index : keys_begin()
const set<string>::iterator void Index :: keys_begin()
Return an iterator pointing to the beginning of the keys set.
const set<string>::iterator void Index : keys_end()
const set<string>::iterator void Index :: keys_end()
Return an iterator pointing to the end of the keys set.
Index : operator[](string key);
const set<unsigned int> &Index :: operator[](string key);
Return the set associated with key

View File

@ -22,9 +22,12 @@ Index: (lib/index.cpp)
Index::Index(filename);
void load();
void save();
void print();
void insert(key, unsigned int);
void remove(key);
void remove(key, unsigned int);
const set<string>::iterator keys_begin();
const set<string>::iterator keys_end();
const set<unsigned int> &operator[](string);
@ -35,33 +38,43 @@ Index: (lib/index.cpp)
File << map[key].size() << int_0 << int_1 << ... << int_n << endl;
- API:
Index : Index(filename);
Index :: Index(filename);
Initializes an index using ~/.ocarina{-debug}K/filename. Pass
an empty string if you do not want this index to be saved.
void Index : load();
void Index :: load();
Reads data from a file. Call after static initialization of
Ocarina to ensure idle tasks are configured
void Index : save();
void Index :: save();
Saves data to file
void Index : insert(key, unsigned int);
void Index :: print();
This function exists only if CONFIG_DEBUG is enabled.
Following a similar format for writing to disk, print the
index to the console in a human-readable format.
void Index :: print_keys();
This function exists only if CONFIG_DEBUG is enabled.
Print the database keys to the console in a human-readable
format.
void Index :: insert(key, unsigned int);
1) If key does not exist, create it.
2) Add int to the set for the given key
void Index : remove(key);
void Index :: remove(key);
Remove a key from the index;
Index : remove(key, unsigned int);
void Index :: remove(key, unsigned int);
1) Remove int from the set of values associated with key
2) If the set is empty, remove the key
const set<string>::iterator void Index : keys_begin()
const set<string>::iterator void Index :: keys_begin()
Return an iterator pointing to the beginning of the keys set.
const set<string>::iterator void Index : keys_end()
const set<string>::iterator void Index :: keys_end()
Return an iterator pointing to the end of the keys set.
Index : operator[](string key);
const set<unsigned int> &Index :: operator[](string key);
Return the set associated with key

View File

@ -5,6 +5,7 @@
#define OCARINA_INDEX_H
#include <file.h>
#include <print.h>
#include <map>
#include <set>
@ -20,6 +21,10 @@ public:
Index(std::string);
void save();
void load();
#ifdef CONFIG_DEBUG
void print();
void print_keys();
#endif /* CONFIG_DEBUG */
void insert(std::string, unsigned int);
void remove(std::string);

View File

@ -52,6 +52,36 @@ void Index :: load()
file.close();
}
void Index :: print()
{
std::set<std::string>::iterator s_it;
std::set<unsigned int>::iterator u_it;
for (s_it = keys.begin(); s_it != keys.end(); s_it++) {
std::string key = *s_it;
:: print("index[%s] = {", key.c_str());
for (u_it = index[key].begin(); u_it != index[key].end(); u_it++) {
if (u_it != index[key].begin())
:: print(" ");
:: print("%d", *u_it);
}
:: print("}\n");
}
:: print("\n");
}
void Index :: print_keys()
{
std::set<std::string>::iterator it;
:: print("Keys:");
for (it = keys.begin(); it != keys.end(); it++)
:: print(" %s", it->c_str());
:: print("\n");
}
void Index :: insert(std::string key, unsigned int value)
{
if (index.find(key) == index.end()) {

View File

@ -148,6 +148,19 @@ void test_5()
print("index[nokey].size() = %u\n", index["nokey"].size());
}
/*
* Test the print() function
*/
void test_6()
{
print("Test 6\n");
Index index("");
populate_index(index);
index.print_keys();
index.print();
}
int main(int argc, char **argv)
{
test_0();
@ -156,5 +169,6 @@ int main(int argc, char **argv)
test_3();
test_4();
test_5();
test_6();
return 0;
}

View File

@ -104,3 +104,32 @@ index[z] = {3 4 5 6 7 8 9}
Test 5
index[nokey].size() = 0
Test 6
Keys: a b c d e f g h i j k l m n o p q r s t u v w x y z
index[a] = {0 1 2 3 4 5 6 7 8 9}
index[b] = {0 1 2 3 4 5 6 7 8 9}
index[c] = {0 1 2 3 4 5 6 7 8 9}
index[d] = {0 1 2 3 4 5 6 7 8 9}
index[e] = {0 1 2 3 4 5 6 7 8 9}
index[f] = {0 1 2 3 4 5 6 7 8 9}
index[g] = {0 1 2 3 4 5 6 7 8 9}
index[h] = {0 1 2 3 4 5 6 7 8 9}
index[i] = {0 1 2 3 4 5 6 7 8 9}
index[j] = {0 1 2 3 4 5 6 7 8 9}
index[k] = {0 1 2 3 4 5 6 7 8 9}
index[l] = {0 1 2 3 4 5 6 7 8 9}
index[m] = {0 1 2 3 4 5 6 7 8 9}
index[n] = {0 1 2 3 4 5 6 7 8 9}
index[o] = {0 1 2 3 4 5 6 7 8 9}
index[p] = {0 1 2 3 4 5 6 7 8 9}
index[q] = {0 1 2 3 4 5 6 7 8 9}
index[r] = {0 1 2 3 4 5 6 7 8 9}
index[s] = {0 1 2 3 4 5 6 7 8 9}
index[t] = {0 1 2 3 4 5 6 7 8 9}
index[u] = {0 1 2 3 4 5 6 7 8 9}
index[v] = {0 1 2 3 4 5 6 7 8 9}
index[w] = {0 1 2 3 4 5 6 7 8 9}
index[x] = {0 1 2 3 4 5 6 7 8 9}
index[y] = {0 1 2 3 4 5 6 7 8 9}
index[z] = {0 1 2 3 4 5 6 7 8 9}