search method Null safety

List<List<SearchResult>> search(
  1. List<Template> queries_templates,
  2. TemplateIndex templates_index,
  3. int k,
  4. {SearchAccelerationType acceleration = SearchAccelerationType.SEARCH_ACCELERATION_1}
)

Search for the k nearest Templates in the TemplatesIndex.
The search time depends on k and the size of the TemplatesIndex.
queries_templates - The Template query.
templates_index - TemplatesIndex for search.
k - Count of the nearest templates for search.
acceleration - Acceleration type.
return - Vector with min(k, templates_index.size()) elements in ascending order of distance to the query_template.

Implementation

List<List<SearchResult>> search(List<Template> queries_templates, TemplateIndex templates_index, int k,
    {SearchAccelerationType acceleration = SearchAccelerationType.SEARCH_ACCELERATION_1}) {
  final _search = _dll_handle.lookupFunction<_Recognizer_search_v2_c, _Recognizer_search_v2_dart>(_c_namespace + 'Recognizer_search_v2');
  final exception = _getException();

  final queries_count = queries_templates.length;

  Pointer<Pointer<Void>> queriesTemplatesImpl = malloc.allocate(sizeOf<Pointer<Pointer<Void>>>() * queries_templates.length);
  for (var i = 0; i < queries_templates.length; i++) {
    queriesTemplatesImpl[i] = queries_templates[i]._impl;
  }

  Pointer<Int64> result_i = malloc.allocate(k * queries_count * sizeOf<Int64>());
  Pointer<Float> result_distance = malloc.allocate(k * queries_count * sizeOf<Float>());
  Pointer<Float> result_far = malloc.allocate(k * queries_count * sizeOf<Float>());
  Pointer<Float> result_frr = malloc.allocate(k * queries_count * sizeOf<Float>());
  Pointer<Float> result_score = malloc.allocate(k * queries_count * sizeOf<Float>());

  _search(_impl, acceleration.index, queries_count, queriesTemplatesImpl, templates_index._impl, k, result_i, result_distance, result_far, result_frr,
      result_score, exception);
  checkException(exception, _dll_handle);

  List<List<SearchResult>> result = [];

  for (var i = 0; i < queries_count; i++) {
    result.add([]);
    for (var j = 0; j < k; j++) {
      var h = i * k + j;
      result[i].add(SearchResult(result_i[h], MatchResult(result_distance[h], result_far[h], result_frr[h], result_score[h])));
    }
  }

  malloc.free(result_distance);
  malloc.free(result_far);
  malloc.free(result_frr);
  malloc.free(result_score);
  malloc.free(queriesTemplatesImpl);

  return result;
}