MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / LevenshteinDistancesUTF8_call

Function LevenshteinDistancesUTF8_call

python/stringzillas.c:710–851  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

708}
709
710static PyObject *LevenshteinDistancesUTF8_call(LevenshteinDistancesUTF8 *self, PyObject *args, PyObject *kwargs) {
711 PyObject *a_obj = NULL, *b_obj = NULL, *device_obj = NULL, *out_obj = NULL;
712
713 static char *kwlist[] = {"a", "b", "device", "out", NULL};
714 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|OO", kwlist, &a_obj, &b_obj, &device_obj, &out_obj)) return NULL;
715
716 DeviceScope *device_scope = NULL;
717 if (device_obj != NULL && device_obj != Py_None) {
718 if (!PyObject_TypeCheck(device_obj, &DeviceScopeType)) {
719 PyErr_SetString(PyExc_TypeError, "device must be a DeviceScope instance");
720 return NULL;
721 }
722 device_scope = (DeviceScope *)device_obj;
723 }
724
725 szs_device_scope_t device_handle = device_scope ? device_scope->handle : default_device_scope;
726 sz_size_t kernel_input_size = 0;
727 void *kernel_a_texts_punned = NULL;
728 void *kernel_b_texts_punned = NULL;
729 sz_size_t *kernel_results = NULL;
730 sz_size_t kernel_results_stride = sizeof(sz_size_t);
731 sz_status_t (*kernel_punned)(szs_levenshtein_distances_t, szs_device_scope_t, void *, void *, sz_size_t *,
732 sz_size_t, char const **) = NULL;
733
734 // Swap allocators when engine supports CUDA
735 if (requires_unified_memory(self->capabilities))
736 if (!try_swap_to_unified_allocator(a_obj) || !try_swap_to_unified_allocator(b_obj)) return NULL;
737
738 // Handle 32-bit tape inputs
739 sz_sequence_u32tape_t a_u32tape, b_u32tape;
740 sz_bool_t a_is_u32tape = sz_py_export_strings_as_u32tape( //
741 a_obj, &a_u32tape.data, &a_u32tape.offsets, &a_u32tape.count);
742 sz_bool_t b_is_u32tape = sz_py_export_strings_as_u32tape( //
743 b_obj, &b_u32tape.data, &b_u32tape.offsets, &b_u32tape.count);
744 if (a_is_u32tape && b_is_u32tape) {
745 if (a_u32tape.count != b_u32tape.count) {
746 PyErr_SetString(PyExc_ValueError, "Input sequences must have the same length");
747 return NULL;
748 }
749
750 kernel_input_size = a_u32tape.count;
751 kernel_punned = szs_levenshtein_distances_utf8_u32tape;
752 kernel_a_texts_punned = &a_u32tape;
753 kernel_b_texts_punned = &b_u32tape;
754 }
755
756 // Handle 64-bit tape inputs
757 sz_sequence_u64tape_t a_u64tape, b_u64tape;
758 sz_bool_t a_is_u64tape = !a_is_u32tape && sz_py_export_strings_as_u64tape( //
759 a_obj, &a_u64tape.data, &a_u64tape.offsets, &a_u64tape.count);
760 sz_bool_t b_is_u64tape = !b_is_u32tape && sz_py_export_strings_as_u64tape( //
761 b_obj, &b_u64tape.data, &b_u64tape.offsets, &b_u64tape.count);
762 if (a_is_u64tape && b_is_u64tape) {
763 if (a_u64tape.count != b_u64tape.count) {
764 PyErr_SetString(PyExc_ValueError, "Input sequences must have the same length");
765 return NULL;
766 }
767 kernel_input_size = a_u64tape.count;

Callers

nothing calls this directly

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…