本文整理汇总了C++中Hashtable::Clear方法的典型用法代码示例。如果您正苦于以下问题:C++ Hashtable::Clear方法的具体用法?C++ Hashtable::Clear怎么用?C++ Hashtable::Clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Hashtable
的用法示例。
在下文中一共展示了Hashtable::Clear方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
//.........这里部分代码省略.........
LogTime(MUSCLE_LOG_INFO, "Testing delete-as-you-go traveral\n");
for (HashtableIterator<String, String> st(table); st.HasData(); st++)
{
LogTime(MUSCLE_LOG_INFO, "t3 = %s -> %s (tableSize=" UINT32_FORMAT_SPEC")\n", st.GetKey()(), st.GetValue()(), table.GetNumItems());
if (table.Remove(st.GetKey()) != B_NO_ERROR) bomb("Could not remove string!\n");
#if 0
for (HashtableIterator<String,String> st2(table); st2.HasData(); st2++) printf(" tx = %s -> %s\n", nextKeyString(), nextValueString());
#endif
}
Hashtable<uint32, const char *> sillyTable;
sillyTable.Put(15, "Fifteen");
sillyTable.Put(100, "One Hundred");
sillyTable.Put(150, "One Hundred and Fifty");
sillyTable.Put(200, "Two Hundred");
sillyTable.Put((uint32)-1, "2^32 - 1!");
if (sillyTable.ContainsKey((uint32)-1) == false) bomb("large value failed!");
const char * temp = NULL;
sillyTable.Get(100, temp);
sillyTable.Get(101, temp); // will fail
printf("100 -> %s\n",temp);
printf("Entries in sillyTable:\n");
for (HashtableIterator<uint32, const char *> it(sillyTable); it.HasData(); it++)
{
const char * nextValue = NULL;
status_t ret = sillyTable.Get(it.GetKey(), nextValue);
printf("%i %s: " UINT32_FORMAT_SPEC" -> %s\n", it.HasData(), (ret == B_NO_ERROR) ? "OK" : "ERROR", it.GetKey(), nextValue);
}
}
table.Clear();
{
const uint32 NUM_ITEMS = 1000000;
const uint32 NUM_RUNS = 3;
Hashtable<int, int> testCopy;
Hashtable<String, double> tallies;
for (uint32 t=0; t<NUM_RUNS; t++)
{
Hashtable<int, int> table; (void) table.EnsureSize(NUM_ITEMS);
printf("SORT SPEED TEST ROUND " UINT32_FORMAT_SPEC"/" UINT32_FORMAT_SPEC":\n", t+1, NUM_RUNS);
uint64 startTime = GetRunTime64();
srand(0); for (uint32 i=0; i<NUM_ITEMS; i++) table.Put(rand(), rand()); // we want this to be repeatable, hence srand(0)
AddTally(tallies, "place", startTime, NUM_ITEMS);
startTime = GetRunTime64();
table.SortByValue();
AddTally(tallies, "sort", startTime, NUM_ITEMS);
startTime = GetRunTime64();
testCopy = table; // just to make sure copying a table works
AddTally(tallies, "copy", startTime, NUM_ITEMS);
startTime = GetRunTime64();
if (testCopy != table) bomb("Copy was not the same!");
AddTally(tallies, "compare", startTime, NUM_ITEMS);
startTime = GetRunTime64();
if (testCopy.IsEqualTo(table, true) == false) bomb("Copy was not the same, considering ordering!");
AddTally(tallies, "o-compare", startTime, NUM_ITEMS);
startTime = GetRunTime64();
示例2: StdinThreadEntryFunc
static unsigned __stdcall StdinThreadEntryFunc(void *)
{
if (_stdinHandle != INVALID_HANDLE_VALUE)
{
DWORD oldConsoleMode = 0;
GetConsoleMode(_stdinHandle, &oldConsoleMode);
SetConsoleMode(_stdinHandle, oldConsoleMode | ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
// The only time this thread is allowed to exit is if stdin is closed. Otherwise it runs until the
// process terminates, because Windows has no way to unblock the call to ReadFile()!
Hashtable<uint32, ConstSocketRef> temp; // declared out here only to avoid reallocations on every loop iteration
char buf[4096];
DWORD numBytesRead;
while(ReadFile(_stdinHandle, buf, sizeof(buf), &numBytesRead, NULL))
{
// Grab a temporary copy of the listeners-set. That we we don't risk blocking in SendData() while holding the mutex.
if (_slaveSocketsMutex.Lock() == B_NO_ERROR)
{
temp = _slaveSockets;
_slaveSocketsMutex.Unlock();
}
// Now send the data we read from stdin to all the registered sockets
bool trim = false;
for (HashtableIterator<uint32, ConstSocketRef> iter(temp); iter.HasData(); iter++) if (SendData(iter.GetValue(), buf, numBytesRead, true) != numBytesRead) {trim = true; iter.GetValue().Reset();}
// Lastly, remove from the registered-sockets-set any sockets that SendData() errored out on.
// This will cause the socket connection to be closed and the master thread(s) to be notified.
if ((trim)&&(_slaveSocketsMutex.Lock() == B_NO_ERROR))
{
for (HashtableIterator<uint32, ConstSocketRef> iter(_slaveSockets); iter.HasData(); iter++)
{
const ConstSocketRef * v = temp.Get(iter.GetKey());
if ((v)&&(v->IsValid() == false)) (void) _slaveSockets.Remove(iter.GetKey());
}
_slaveSocketsMutex.Unlock();
}
temp.Clear(); // it's important not to have extra Refs hanging around in case the process exits!
}
// Restore the old console mode before we leave
SetConsoleMode(_stdinHandle, oldConsoleMode);
}
// Oops, stdin failed... clear the slave sockets table so that the client objects will know to close up shop
if (_slaveSocketsMutex.Lock() == B_NO_ERROR)
{
_stdinThreadStatus = STDIN_THREAD_STATUS_EXITED;
_slaveSockets.Clear();
// We'll close our own handle, thankyouverymuch
if (_slaveThread != INVALID_HANDLE_VALUE)
{
CloseHandle(_slaveThread);
_slaveThread = INVALID_HANDLE_VALUE;
}
if (_stdinHandle != INVALID_HANDLE_VALUE)
{
CloseHandle(_stdinHandle);
_stdinHandle = INVALID_HANDLE_VALUE;
}
_slaveSocketsMutex.Unlock();
}
return 0;
}