-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from ZSShen/trie
Refine the Trie API.
- Loading branch information
Showing
4 changed files
with
1,179 additions
and
1,058 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,70 @@ | ||
#include "cds.h" | ||
|
||
#define SIZE_BLAH_BUF (128) | ||
|
||
static const int BUF_SIZE = 32; | ||
|
||
|
||
int main() | ||
{ | ||
Trie *pTrie; | ||
|
||
/* You should initialize the DS before any operations. */ | ||
int32_t rc = TrieInit(&pTrie); | ||
if (rc != SUCC) | ||
return rc; | ||
/* We should initialize the container before any operations. */ | ||
Trie* trie = TrieInit(); | ||
|
||
char *szAlphaCap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\0"; | ||
char *szAlpha = "abcdefghijklmnopqrstuvwxyz\0"; | ||
const char* alpha_cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\0"; | ||
const char* alpha = "abcdefghijklmnopqrstuvwxyz\0"; | ||
|
||
/* Insert a single string into the trie. */ | ||
char szBuf[SIZE_BLAH_BUF]; | ||
int iIdx; | ||
for (iIdx = 0 ; iIdx < 26 ; ++iIdx) { | ||
szBuf[iIdx] = szAlphaCap[iIdx]; | ||
szBuf[iIdx + 1] = 0; | ||
pTrie->insert(pTrie, szBuf); | ||
/* Insert strings into the trie. */ | ||
char buf[BUF_SIZE]; | ||
int i; | ||
for (i = 0 ; i < 26 ; ++i) { | ||
buf[i] = alpha_cap[i]; | ||
buf[i + 1] = 0; | ||
trie->insert(trie, buf); | ||
} | ||
for (iIdx = 0 ; iIdx < 26 ; ++iIdx) { | ||
szBuf[iIdx] = szAlpha[iIdx]; | ||
szBuf[iIdx + 1] = 0; | ||
pTrie->insert(pTrie, szBuf); | ||
for (i = 0 ; i < 26 ; ++i) { | ||
buf[i] = alpha[i]; | ||
buf[i + 1] = 0; | ||
trie->insert(trie, buf); | ||
} | ||
|
||
/* Insert an array of strings into the trie. */ | ||
char *aStrNum[6]; | ||
aStrNum[0] = "202-555-0104\0"; | ||
aStrNum[1] = "202-555-0175\0"; | ||
aStrNum[2] = "202-556-0171\0"; | ||
aStrNum[3] = "202-555-9857\0"; | ||
aStrNum[4] = "202-552-0180\0"; | ||
aStrNum[5] = "202-455-7104\0"; | ||
pTrie->bulk_insert(pTrie, aStrNum, 6); | ||
/* Apply bulk insertion to put an array of strings into the trie. */ | ||
const char* nums[6]; | ||
nums[0] = "202-555-0104\0"; | ||
nums[1] = "202-555-0175\0"; | ||
nums[2] = "202-556-0171\0"; | ||
nums[3] = "202-555-9857\0"; | ||
nums[4] = "202-552-0180\0"; | ||
nums[5] = "202-455-7104\0"; | ||
trie->bulk_insert(trie, nums, 6); | ||
|
||
/* Check for exact string. */ | ||
assert(pTrie->has_exact(pTrie, "abcdefghijklmn") == SUCC); | ||
assert(pTrie->has_exact(pTrie, "bcdefghijklmn") == NOKEY); | ||
/* Search for the existing strings. */ | ||
assert(trie->has_exact(trie, "abcdefghijklmn") == true); | ||
assert(trie->has_exact(trie, "bcdefghijklmn") == false); | ||
|
||
/* Check for the strings matching the designated prefix. */ | ||
assert(pTrie->has_prefix_as(pTrie, "A") == SUCC); | ||
assert(pTrie->has_prefix_as(pTrie, "BCD") == NOKEY); | ||
/* Search for the strings matching the specified prefixes. */ | ||
assert(trie->has_prefix_as(trie, "A") == true); | ||
assert(trie->has_prefix_as(trie, "BCD") == false); | ||
|
||
/* Delete the string in the trie. */ | ||
pTrie->remove(pTrie, aStrNum[3]); | ||
/* Remove the string in the trie. */ | ||
trie->remove(trie, nums[3]); | ||
|
||
/* Get the array of strings matching the designated prefix. */ | ||
char **aStr; | ||
int32_t iSizeArr; | ||
pTrie->get_prefix_as(pTrie, "202", &aStr, &iSizeArr); | ||
assert(iSizeArr == 5); | ||
/* Get the array of strings matching the specified prefix. */ | ||
const char** strs; | ||
unsigned size; | ||
trie->get_prefix_as(trie, "202", &strs, &size); | ||
assert(size == 5); | ||
|
||
/* The returned strings are sorted by lexical order. */ | ||
assert(strcmp(aStr[0], aStrNum[5]) == 0); /* 202-455-7104 */ | ||
assert(strcmp(aStr[1], aStrNum[4]) == 0); /* 202-552-0180 */ | ||
assert(strcmp(aStr[2], aStrNum[0]) == 0); /* 202-555-0104 */ | ||
assert(strcmp(aStr[3], aStrNum[1]) == 0); /* 202-555-0175 */ | ||
assert(strcmp(aStr[4], aStrNum[2]) == 0); /* 202-556-0171 */ | ||
assert(strcmp(strs[0], nums[5]) == 0); /* 202-455-7104 */ | ||
assert(strcmp(strs[1], nums[4]) == 0); /* 202-552-0180 */ | ||
assert(strcmp(strs[2], nums[0]) == 0); /* 202-555-0104 */ | ||
assert(strcmp(strs[3], nums[1]) == 0); /* 202-555-0175 */ | ||
assert(strcmp(strs[4], nums[2]) == 0); /* 202-556-0171 */ | ||
|
||
/* Remember to free the returned array of strings. */ | ||
for (iIdx = 0 ; iIdx < iSizeArr ; ++iIdx) | ||
free(aStr[iIdx]); | ||
free(aStr); | ||
for (i = 0 ; i < size ; ++i) | ||
free((char*)strs[i]); | ||
free(strs); | ||
|
||
/* You should deinitialize the DS after all the relevant tasks. */ | ||
TrieDeinit(&pTrie); | ||
|
||
return SUCC; | ||
TrieDeinit(trie); | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.