我要评分
获取效率
正确性
完整性
易理解

Find

Scenario Description

Parameter structures store key-value pairs that are stored in the hash table. Based on a given key keys whose value is to be searched for, the value defaultValue inserted when the search fails, and the length value, if the key's value is found, it is stored in values. For example:

int64_t keyBucket[] = {104, 101, 201, -1};    // Key values stored in the hash table.
float valueBucket[] = {4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7,
                           1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7,
                           2, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7,
                           0, 0, 0, 0, 0, 0, 0, 0};
keys: [101, 110, 201, 104]
defaultValue: [1, 1, 1, 1, 1, 1, 1, 1]
values: [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7,
         1,  1,   1,   1,   1,   1,   1,   1,
         2, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7,
         4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7]
length: 4

Code Example

#include <cmath>
#include <random>
#include <cstdint>
#include <iostream>

#include "ktfop.h"
void KtfopLog(int level, const char *message)
{
  std::cout << level << ' ' << message << std::endl;
}
int main()
{
    using namespace ktfop;
    
    SetExternalLogFunc(KtfopLog);
    int64_t numBuckets = 4;          // Configure the number of hash buckets.
    int64_t valueSize = 8;           // Configure the length of the embedding layer.
    int64_t emptyKey = -1;           // Value of the empty key.
    int64_t deletedKey = -2;         // Value of the deleted key.
    int64_t keyBucket[] = {104, 101, 201, -1};
    float valueBucket[] = {4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7,
                           1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7,
                           2, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7,
                           0, 0, 0, 0, 0, 0, 0, 0};
    TableInfo info {numBuckets, valueSize, emptyKey, deletedKey, keyBucket, valueBucket};
    int64_t length = 4;
    int64_t keys[] = {101, 110, 201, 104};                     
    auto *values = new float[info.valueSize * length];
    float defaultValue[] = {1, 1, 1, 1, 1, 1, 1, 1};       
    int ret = Find(info, keys, values, defaultValue, length); 
    std::cout << "values: [";
    for (int i = 0; i < info.valueSize * length; ++i) {
        std::cout << values[i];
        if (i < info.valueSize * length - 1) {
            std::cout << ", ";
        }
    }
    std::cout << "]" << std::endl;
    // If the key list contains empty keys, print the log "ERROR Using the emptyKey as a table key is not allowed."
    keys[0] = info.emptyKey;
    ret = Find(info, keys, values, defaultValue, length);                     
    
    // If the key list contains deleted keys, print the log "ERROR Using the deletedKey as a table key is not allowed."
    keys[0] = info.deletedKey;
    ret = Find(info, keys, values, defaultValue, length);    
    // Enter a null pointer and print the log "ERROR Parameter verification failed for the Find Op."             
    keys[0] = 101;
    ret = Find(info, nullptr, values, defaultValue, length);                 
    
    // Enter a null pointer and print the log "ERROR Parameter verification failed for the Find Op."
    info.valueBucket = nullptr;
    ret = Find(info, keys, values, defaultValue, length);    
         
    // If emptyKey is equal to deletedKey, print the log"ERROR Empty and deleted key cannot be equal."       
    info.valueBucket = valueBucket;
    info.emptyKey = info.deletedKey;
    ret = Find(info, keys, values, defaultValue, length);       
         
    // If numBuckets is not a power of 2, print the log "ERROR numBuckets must be power of 2."
    info.emptyKey = -1;
    info.numBuckets = 5;
    ret = Find(info, keys, values, defaultValue, length);      
                 
    // If numBuckets is smaller than 0, print the log "ERROR numBuckets is invalid."
    info.numBuckets = 4;
    info.numBuckets = -1;
    ret = Find(info, keys, values, defaultValue, length);   
               
    // If the length value is smaller than 0, print the log "ERROR Length value is invalid."
    info.numBuckets = 4;
    ret = Find(info, keys, values, defaultValue, static_cast<size_t>(-1)); 
    // If valueSize is smaller than 0, print the log "ERROR valueSize is invalid."
    info.valueSize = -1;
    ret = Find(info, keys, values, defaultValue, length);       
          
    // If keyBucket layout rule is incorrect, print the log "ERROR Internal error in MutableDenseHashTable lookup."
    info.valueSize = valueSize;
    info.keyBucket[3] = 202;
    ret = Find(info, keys, values, defaultValue, length);                 
    delete[] values;
 }