当前位置: 首页>>代码示例>>C++>>正文


C++ Hashtable::EnsureSize方法代码示例

本文整理汇总了C++中Hashtable::EnsureSize方法的典型用法代码示例。如果您正苦于以下问题:C++ Hashtable::EnsureSize方法的具体用法?C++ Hashtable::EnsureSize怎么用?C++ Hashtable::EnsureSize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Hashtable的用法示例。


在下文中一共展示了Hashtable::EnsureSize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main


//.........这里部分代码省略.........
      {  
         const Rect & key = iter.GetKey();
         printf("key=%f,%f,%f,%f val=%i\n", key.left(), key.top(), key.right(), key.bottom(), iter.GetValue());
      }
      int * ra = tupleTable.Get(a);
      int * rb = tupleTable.Get(b);
      printf("Rect: ra=[%p] rb=[%p]\n", ra, rb);
   }

   {
      LogTime(MUSCLE_LOG_INFO, "Testing Point as a Hashtable key...\n");

      // A quick test of the Tuple class as a Hashtable key
      Hashtable<Point, int> tupleTable;

      Point a(9,10);
      Point b(-11,-12);
      tupleTable.Put(a, 1);
      tupleTable.Put(b, 2);
      for (HashtableIterator<Point, int> iter(tupleTable); iter.HasData(); iter++)  
      {  
         const Point & key = iter.GetKey();
         printf("key=%f,%f val=%i\n", key.x(), key.y(), iter.GetValue());
      }
      int * ra = tupleTable.Get(a);
      int * rb = tupleTable.Get(b);
      printf("Point: ra=[%p] rb=[%p]\n", ra, rb);
   }

   {
      LogTime(MUSCLE_LOG_INFO, "Preparing large table for sort...\n");

      const uint32 numItems = 100000;
      Hashtable<int, Void> table; (void) table.EnsureSize(100000);
      for (uint32 i=0; i<numItems; i++) table.PutWithDefault((int)rand());
      uint32 actualNumItems = table.GetNumItems();  // may be smaller than numItems, due to duplicate values!
      (void) table.CountAverageLookupComparisons(true);

      LogTime(MUSCLE_LOG_INFO, "Sorting...\n");
      uint64 start = GetRunTime64();
      table.SortByKey();
      uint64 end = GetRunTime64();

      LogTime(MUSCLE_LOG_INFO, "Time to sort " UINT32_FORMAT_SPEC" items: " UINT64_FORMAT_SPEC "ms\n", numItems, (end-start)/1000);

      // Check the resulting sorted table for correctness in both directions
      CheckTable(table, actualNumItems, false);
      CheckTable(table, actualNumItems, true);
   }

   // Test the sort algorithm for efficiency and correctness
   {
      LogTime(MUSCLE_LOG_INFO, "Preparing large table for sort...\n");

      const uint32 numItems = 100000;
      Hashtable<int, Void> table;
      for (uint32 i=0; i<numItems; i++) table.PutWithDefault((int)rand());
      uint32 actualNumItems = table.GetNumItems();  // may be smaller than numItems, due to duplicate values!

      LogTime(MUSCLE_LOG_INFO, "Sorting...\n");
      uint64 start = GetRunTime64();
      table.SortByKey();
      uint64 end = GetRunTime64();

      LogTime(MUSCLE_LOG_INFO, "Time to sort " UINT32_FORMAT_SPEC" items: " UINT64_FORMAT_SPEC "ms\n", numItems, (end-start)/1000);
开发者ID:ModeenF,项目名称:muscle,代码行数:66,代码来源:testhashtable.cpp

示例2: main

int main(int argc, char ** argv)
{
   CompleteSetupSystem css;

   PrintExampleDescription();

   Hashtable<String, int> table;

   // If we know up-front a limit on the number of items we are likely to place
   // into the table, we can reserve that many slots in advance, and thereby 
   // avoid any chance of the Hashtable having to reallocate its internal array 
   // while we are adding items to it.  
   //
   // That avoids some inefficiency, and it also means we don't have
   // to worry about out-of-memory errors, or the memory-locations of key or 
   // value-items changing, while populating the table.

   if (table.EnsureSize(20) != B_NO_ERROR) WARN_OUT_OF_MEMORY;

   // Put some initial data into the table
   table.Put("One", 1);
   table.Put("Two", 2);

   // table.GetWithDefault() returns a reference to the value of the specified
   // key, or a reference to a default-constructed value otherwise.
   const int & oneRef   = table.GetWithDefault("One");
   const int & twoRef   = table.GetWithDefault("Two");
   const int & threeRef = table.GetWithDefault("Three");
   printf("A: OneRef=%i twoRef=%i threeRef=%i\n", oneRef, twoRef, threeRef);

   printf("\n");

   // The [] operator is a synonym for GetWithDefault()
   printf("B: table[\"One\"]=%i table[\"Two\"]=%i table[\"Three\"]=%i\n", table["One"], table["Two"], table["Three"]);

   printf("\n");

   // GetOrPut() returns a pointer to the value of the given key, if the key is present
   // If the key isn't present, it places a key/value pair into the Hashtable and returns
   // a pointer to the (default-constructed) placed value.  This is very useful when 
   // demand-allocating records.
   int * pEight = table.GetOrPut("Eight");
   printf("C:  table.GetOrPut(\"Eight\") returned %p\n", pEight);
   if (pEight) *pEight = 8;
          else WARN_OUT_OF_MEMORY;   // GetOrPut() returns NULL only on memory-exhaustion

   printf("\n");

   // The next time we call GetOrPut() we'll get a pointer to the existing value
   pEight = table.GetOrPut("Eight");
   printf("C:  Second call to table.GetOrPut(\"Eight\") returned %p (aka %i)\n", pEight, pEight?*pEight:666);

   printf("\n");

   // We can also call GetOrPut() with a suggested default-value which will be
   // placed into the key/value pair if the supplied key isn't already present.
   int * pNine = table.GetOrPut("Nine", 9);
   printf("C:  table.GetOrPut(\"Nine\", 9) returned %p (aka %i)\n", pNine, pNine?*pNine:666);

   printf("\n");

   // PutAndGet() is similar to GetOrPut() except it *always* places a value.
   // (if the key already existed in the table, its value will be overwritten)
   int * pTen = table.PutAndGet("Ten", 10);
   printf("D:  table.PutAndGet(\"Ten\", 10) returned %p (aka %i)\n", pTen, pTen?*pTen:666);

   // Demonstrate PutAndGet()'s overwrite of the previous value
   pTen = table.PutAndGet("Ten", 11);
   printf("E:  table.PutAndGet(\"Ten\", 11) returned %p (aka %i)\n", pTen, pTen?*pTen:666);

   // If you want a Hashtable with keys only and don't need values at all
   // (similar to e.g. a std::unordered_set<>), a good way to get that is
   // to use the Void class as your value-type.  A Void object is just a
   // placeholder that contains no data.
   Hashtable<String, Void> keysOnlyTable;
   (void) keysOnlyTable.PutWithDefault("Blue");
   (void) keysOnlyTable.PutWithDefault("Red");
   (void) keysOnlyTable.PutWithDefault("Green");

   printf("\n");

   return 0;
}
开发者ID:jfriesne,项目名称:muscle,代码行数:83,代码来源:example_4_idioms.cpp


注:本文中的Hashtable::EnsureSize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。