CharArray ========= .. cpp:class:: numpy::CharArray numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use CharArray CharArray obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``CharArray(const std::vector&shape)`` - NP_CHARARRAY.H:54 - * - ``CharArray(const std::vector&shape, const std::string &fill_value)`` - NP_CHARARRAY.H:57 - * - ``CharArray(const NDArray>&array)`` - NP_CHARARRAY.H:60 - * - ``CharArray(const CharArray &other)`` - NP_CHARARRAY.H:64 - Operators --------- .. list-table:: :widths: 40 25 15 20 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``CharArray & operator=(const CharArray &other)`` - CharArray & - NP_CHARARRAY.H:67 - * - ``std::string operator[](const std::vector&indices)`` - std::string - NP_CHARARRAY.H:84 - * - ``std::string operator[](size_tindex)`` - std::string - NP_CHARARRAY.H:93 - * - ``NDArray operator==(const CharArray &other)`` - NDArray - NP_CHARARRAY.H:391 - * - ``NDArray operator!=(const CharArray &other)`` - NDArray - NP_CHARARRAY.H:402 - * - ``CharArray operator+(const CharArray &other)`` - CharArray - NP_CHARARRAY.H:414 - * - ``CharArray operator+(const std::string &scalar)`` - CharArray - NP_CHARARRAY.H:418 - * - ``CharArray operator\*(size_trepeats)`` - CharArray - NP_CHARARRAY.H:422 - Array Creation -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string trimmed(const str\_&s)`` - std::string - NP_CHARARRAY.H:40 - Math Operations --------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``CharArray add(const CharArray &other)`` - CharArray - NP_CHARARRAY.H:104 - :ref:`View ` * - ``CharArray add(const std::string &scalar)`` - CharArray - NP_CHARARRAY.H:108 - :ref:`View ` * - ``CharArray multiply(size_trepeats)`` - CharArray - NP_CHARARRAY.H:118 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``const NDArray>& array()`` - const NDArray>& - NP_CHARARRAY.H:75 - :ref:`View ` * - ``NDArray>& array()`` - NDArray>& - NP_CHARARRAY.H:76 - :ref:`View ` * - ``CharArray capitalize()`` - CharArray - NP_CHARARRAY.H:123 - :ref:`View ` * - ``CharArray lower()`` - CharArray - NP_CHARARRAY.H:150 - :ref:`View ` * - ``CharArray lstrip(const std::string &chars = " \\n\\r\\f\\v")`` - CharArray - NP_CHARARRAY.H:219 - :ref:`View ` * - ``size_t ndim()`` - size_t - NP_CHARARRAY.H:81 - :ref:`View ` * - ``str\_ padded(const std::string &s)`` - str\_ - NP_CHARARRAY.H:48 - * - ``void print()`` - void - NP_CHARARRAY.H:432 - :ref:`View ` * - ``CharArray rstrip(const std::string &chars = " \\n\\r\\f\\v")`` - CharArray - NP_CHARARRAY.H:235 - :ref:`View ` * - ``void set(const std::vector&indices, const std::string &value)`` - void - NP_CHARARRAY.H:88 - :ref:`View ` * - ``void set(size_tindex, const std::string &value)`` - void - NP_CHARARRAY.H:97 - :ref:`View ` * - ``const std::vector& shape()`` - const std::vector& - NP_CHARARRAY.H:79 - :ref:`View ` * - ``size_t size()`` - size_t - NP_CHARARRAY.H:80 - :ref:`View ` * - ``CharArray strip(const std::string &chars = " \\n\\r\\f\\v")`` - CharArray - NP_CHARARRAY.H:198 - :ref:`View ` * - ``CharArray swapcase()`` - CharArray - NP_CHARARRAY.H:180 - :ref:`View ` * - ``CharArray title()`` - CharArray - NP_CHARARRAY.H:161 - :ref:`View ` * - ``std::string toString()`` - std::string - NP_CHARARRAY.H:427 - :ref:`View ` * - ``CharArray upper()`` - CharArray - NP_CHARARRAY.H:139 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-chararray-add-0: .. dropdown:: add (np_test_1_all.cpp:6410) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6400 :emphasize-lines: 11 void testCharArrayStringOperationsCharArray() { std::cout << "========= testStringOperations ======================="; // Test string concatenation std::vector words1 = {"Hello", "Good", "Nice"}; std::vector words2 = {" World", " Day", " Work"}; auto arr1 = array<32>(words1); auto arr2 = array<32>(words2); auto result = add(arr1, arr2); // std::cout << "String concatenation (add):" << std::endl; for (size_t i = 0; i < result.size(); ++i) { // std::cout << "'" << arr1[i] << "' + '" << arr2[i] << "' = '" << result[i] << "'"; } // Test scalar addition auto scalar_result = add(arr1, "!"); // std::cout << "Scalar concatenation:" << std::endl; for (size_t i = 0; i < scalar_result.size(); ++i) { // std::cout << "'" << arr1[i] << "' + '!' = '" << scalar_result[i] << "'"; .. _example-chararray-add-1: .. dropdown:: add (np_test_1_all.cpp:6410) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6400 :emphasize-lines: 11 void testCharArrayStringOperationsCharArray() { std::cout << "========= testStringOperations ======================="; // Test string concatenation std::vector words1 = {"Hello", "Good", "Nice"}; std::vector words2 = {" World", " Day", " Work"}; auto arr1 = array<32>(words1); auto arr2 = array<32>(words2); auto result = add(arr1, arr2); // std::cout << "String concatenation (add):" << std::endl; for (size_t i = 0; i < result.size(); ++i) { // std::cout << "'" << arr1[i] << "' + '" << arr2[i] << "' = '" << result[i] << "'"; } // Test scalar addition auto scalar_result = add(arr1, "!"); // std::cout << "Scalar concatenation:" << std::endl; for (size_t i = 0; i < scalar_result.size(); ++i) { // std::cout << "'" << arr1[i] << "' + '!' = '" << scalar_result[i] << "'"; .. _example-chararray-multiply-2: .. dropdown:: multiply (np_test_1_all.cpp:6426) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6416 :emphasize-lines: 11 // Test scalar addition auto scalar_result = add(arr1, "!"); // std::cout << "Scalar concatenation:" << std::endl; for (size_t i = 0; i < scalar_result.size(); ++i) { // std::cout << "'" << arr1[i] << "' + '!' = '" << scalar_result[i] << "'"; } // Test string multiplication std::vector patterns = {"Ha", "Ho", "Hi"}; auto pattern_arr = array<32>(patterns); auto repeated = multiply(pattern_arr, 3); // std::cout << "String multiplication:" << std::endl; for (size_t i = 0; i < repeated.size(); ++i) { // std::cout << "'" << pattern_arr[i] << "' * 3 = '" << repeated[i] << "'"; } std::cout << " -> tests passed" << std::endl; } void testCaseConversionsCharArray() { std::cout << "========= testCaseConversions ======================="; .. _example-chararray-array-3: .. dropdown:: array (np_test_1_all.cpp:243) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 233 :emphasize-lines: 11 // std::cout << "Min: " << array.minArray() << std::endl; // std::cout << "Max: " << array.maxArray() << std::endl; std::cout << " -> tests passed" << std::endl; } void testReshapeAndFlatten() { std::cout << "========= testReshapeAndFlatten ======================="; auto range_array = NDArray::createRange(0, 12, 1); // std::cout << "Range array (0-11):" << std::endl; //range_array.printArray(); auto reshaped = range_array.reshapeArray({3, 4}); // std::cout << "Reshaped to 3x4:"; //reshaped.printArray(); auto flattened = reshaped.flattenArray(); // std::cout << "Flattened:" << std::endl; //flattened.printArray(); .. _example-chararray-array-4: .. dropdown:: array (np_test_1_all.cpp:243) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 233 :emphasize-lines: 11 // std::cout << "Min: " << array.minArray() << std::endl; // std::cout << "Max: " << array.maxArray() << std::endl; std::cout << " -> tests passed" << std::endl; } void testReshapeAndFlatten() { std::cout << "========= testReshapeAndFlatten ======================="; auto range_array = NDArray::createRange(0, 12, 1); // std::cout << "Range array (0-11):" << std::endl; //range_array.printArray(); auto reshaped = range_array.reshapeArray({3, 4}); // std::cout << "Reshaped to 3x4:"; //reshaped.printArray(); auto flattened = reshaped.flattenArray(); // std::cout << "Flattened:" << std::endl; //flattened.printArray(); .. _example-chararray-capitalize-5: .. dropdown:: capitalize (np_test_1_all.cpp:6461) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6451 :emphasize-lines: 11 } // Test lower case auto lower_result = lower(arr); // std::cout << "Lower case:" << std::endl; for (size_t i = 0; i < lower_result.size(); ++i) { // std::cout << " '" << lower_result[i] << "'"; } // Test capitalize auto cap_result = capitalize(arr); // std::cout << "Capitalize:" << std::endl; for (size_t i = 0; i < cap_result.size(); ++i) { // std::cout << " '" << cap_result[i] << "'"; } // Test title auto title_result = title(arr); // std::cout << "Title case:" << std::endl; for (size_t i = 0; i < title_result.size(); ++i) { // std::cout << " '" << title_result[i] << "'"; .. _example-chararray-lower-6: .. dropdown:: lower (np_test_1_all.cpp:6454) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6444 :emphasize-lines: 11 } // Test upper case auto upper_result = upper(arr); // std::cout << "Upper case:" << std::endl; for (size_t i = 0; i < upper_result.size(); ++i) { // std::cout << " '" << upper_result[i] << "'"; } // Test lower case auto lower_result = lower(arr); // std::cout << "Lower case:" << std::endl; for (size_t i = 0; i < lower_result.size(); ++i) { // std::cout << " '" << lower_result[i] << "'"; } // Test capitalize auto cap_result = capitalize(arr); // std::cout << "Capitalize:" << std::endl; for (size_t i = 0; i < cap_result.size(); ++i) { // std::cout << " '" << cap_result[i] << "'"; .. _example-chararray-lstrip-7: .. dropdown:: lstrip (np_test_1_all.cpp:6641) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6631 :emphasize-lines: 11 } // Test strip auto stripped = strip(arr); // std::cout << "After strip():" << std::endl; for (size_t i = 0; i < stripped.size(); ++i) { // std::cout << "'" << stripped[i] << "'"; } // Test lstrip auto left_stripped = lstrip(arr); // std::cout << "After lstrip():" << std::endl; for (size_t i = 0; i < left_stripped.size(); ++i) { // std::cout << "'" << left_stripped[i] << "'"; } // Test rstrip auto right_stripped = rstrip(arr); // std::cout << "After rstrip():" << std::endl; for (size_t i = 0; i < right_stripped.size(); ++i) { // std::cout << "'" << right_stripped[i] << "'"; .. _example-chararray-ndim-8: .. dropdown:: ndim (np_test_2_all.cpp:3526) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3516 :emphasize-lines: 11 void testArrayInfo() { std::cout << "Testing array information functions...\n"; // Test basic info functions auto arr = createFloat64Array({ 2, 3, 4 }); for (size_t i = 0; i < arr.getSize(); ++i) { arr.setElementAt({ i / 12, (i / 4) % 3, i % 4 }, static_cast(i)); } // Test ndim if (!(ndim(arr) == 3)) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ndim(arr) == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test size if (!(size(arr) == 24)) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(size(arr) == 24)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-chararray-print-9: .. dropdown:: print (np_test_1_all.cpp:6395) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6385 :emphasize-lines: 11 std::vector strings = {"Alice", "Bob", "Charlie"}; auto arr2 = array<32>(strings); // std::cout << "Created from strings vector:" << std::endl; for (size_t i = 0; i < arr2.size(); ++i) { // std::cout << "arr2[" << i << "] = '" << arr2[i] << "'"; } // Test 2D array creation chararray16 arr_2d({2, 3}, "test"); // std::cout << "Created 2D chararray16 with shape {2, 3}:"; // arr_2d.print(); std::cout << " -> tests passed" << std::endl; } void testCharArrayStringOperationsCharArray() { std::cout << "========= testStringOperations ======================="; // Test string concatenation std::vector words1 = {"Hello", "Good", "Nice"}; std::vector words2 = {" World", " Day", " Work"}; .. _example-chararray-rstrip-10: .. dropdown:: rstrip (np_test_1_all.cpp:6648) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6638 :emphasize-lines: 11 } // Test lstrip auto left_stripped = lstrip(arr); // std::cout << "After lstrip():" << std::endl; for (size_t i = 0; i < left_stripped.size(); ++i) { // std::cout << "'" << left_stripped[i] << "'"; } // Test rstrip auto right_stripped = rstrip(arr); // std::cout << "After rstrip():" << std::endl; for (size_t i = 0; i < right_stripped.size(); ++i) { // std::cout << "'" << right_stripped[i] << "'"; } std::cout << " -> tests passed" << std::endl; } void testStringComparisonCharArray() { std::cout << "========= testStringComparison ======================="; .. _example-chararray-set-11: .. dropdown:: set (np_test_1_all.cpp:6747) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6737 :emphasize-lines: 11 std::cout << "========= testArrayPrinting ======================="; // Test 1D array printing std::vector words = {"Hello", "World", "Test"}; auto arr_1d = array<16>(words); // std::cout << "1D CharArray:" << std::endl; // arr_1d.print(); // Test 2D array printing chararray16 arr_2d({2, 2}, "test"); arr_2d.set({0, 0}, "A"); arr_2d.set({0, 1}, "B"); arr_2d.set({1, 0}, "C"); arr_2d.set({1, 1}, "D"); // std::cout << "2D CharArray:" << std::endl; // arr_2d.print(); std::cout << " -> tests passed" << std::endl; } .. _example-chararray-set-12: .. dropdown:: set (np_test_1_all.cpp:6747) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6737 :emphasize-lines: 11 std::cout << "========= testArrayPrinting ======================="; // Test 1D array printing std::vector words = {"Hello", "World", "Test"}; auto arr_1d = array<16>(words); // std::cout << "1D CharArray:" << std::endl; // arr_1d.print(); // Test 2D array printing chararray16 arr_2d({2, 2}, "test"); arr_2d.set({0, 0}, "A"); arr_2d.set({0, 1}, "B"); arr_2d.set({1, 0}, "C"); arr_2d.set({1, 1}, "D"); // std::cout << "2D CharArray:" << std::endl; // arr_2d.print(); std::cout << " -> tests passed" << std::endl; } .. _example-chararray-shape-13: .. dropdown:: shape (np_test_1_all.cpp:3751) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3741 :emphasize-lines: 11 } for (size_t j = 0; j < 3; ++j) { arr3.setElementAt({0, j}, static_cast((j + 1) * 100)); // [100, 200, 300] } // Broadcast all arrays together std::vector> arrays = {arr1, arr2, arr3}; auto broadcasted = broadcast_arrays(arrays); // All should have shape (2, 3) for (const auto& arr : broadcasted) { if (!((arr.getShape() == std::vector{2, 3}))) { std::string description = std::string("test_broadcast_arrays():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((arr.getShape() == std::vector{2, 3}))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // Check values if (!(broadcasted[0].getElementAt({0, 1}) == 2.0)) { .. _example-chararray-size-14: .. dropdown:: size (np_test_1_all.cpp:47) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 37 :emphasize-lines: 11 using namespace numpy; using namespace numpy::benchmark; using namespace numpy::char_; // Helper functions for array comparison tests namespace { const double TOLERANCE = 1e-10; bool allTrue(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0); do { if (!arr.getElementAt(indices)) { return false; } } while (incrementIndices(indices, arr.getShape())); return true; } bool allFalse(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0); .. _example-chararray-strip-15: .. dropdown:: strip (np_test_1_all.cpp:6634) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6624 :emphasize-lines: 11 std::vector padded = {" hello ", "\t\tworld\t\t", " spaces "}; auto arr = array<32>(padded); // std::cout << "Original padded strings:" << std::endl; for (size_t i = 0; i < arr.size(); ++i) { // std::cout << "'" << arr[i] << "'"; } // Test strip auto stripped = strip(arr); // std::cout << "After strip():" << std::endl; for (size_t i = 0; i < stripped.size(); ++i) { // std::cout << "'" << stripped[i] << "'"; } // Test lstrip auto left_stripped = lstrip(arr); // std::cout << "After lstrip():" << std::endl; for (size_t i = 0; i < left_stripped.size(); ++i) { // std::cout << "'" << left_stripped[i] << "'"; .. _example-chararray-swapcase-16: .. dropdown:: swapcase (np_test_5_all.cpp:1434) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1424 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } void np_test_string_swapcase() { std::cout << "========= swapcase formatting ======================="; std::vector strings = { "Hello World", "TEST", "test" }; auto arr = numpy::char_::array<32>(strings); auto result = numpy::char_::swapcase(arr); bool passed = (result.size() == arr.size()); if (!passed) { std::cout << " [FAIL] : in np_test_string_swapcase() : size mismatch"; throw std::runtime_error("np_test_string_swapcase failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-chararray-title-17: .. dropdown:: title (np_test_1_all.cpp:6468) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6458 :emphasize-lines: 11 } // Test capitalize auto cap_result = capitalize(arr); // std::cout << "Capitalize:" << std::endl; for (size_t i = 0; i < cap_result.size(); ++i) { // std::cout << " '" << cap_result[i] << "'"; } // Test title auto title_result = title(arr); // std::cout << "Title case:" << std::endl; for (size_t i = 0; i < title_result.size(); ++i) { // std::cout << " '" << title_result[i] << "'"; } std::cout << " -> tests passed" << std::endl; } void testStringTestingCharArray() { std::cout << "========= testStringTesting ======================="; .. _example-chararray-tostring-18: .. dropdown:: toString (np_test_1_all.cpp:6826) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6816 :emphasize-lines: 11 void testDatetime64CreationDateTime() { std::cout << "========= testDatetime64CreationDateTime ======================="; // Test default constructor datetime64 default_dt; if (!(default_dt.isNaT())) { std::string description = std::string("testDatetime64CreationDateTime():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(default_dt.isNaT())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Default datetime64 is NaT: " << default_dt.toString() << std::endl; // Test value constructor datetime64 epoch_day(0, DateTimeUnit::Day); // std::cout << "Epoch day: " << epoch_day.toString() << std::endl; // Test string constructor datetime64 date_from_string("2024-01-15"); // std::cout << "Date from string '2024-01-15': " << date_from_string.toString() << std::endl; datetime64 datetime_from_string("2024-01-15T10:30:45"); .. _example-chararray-upper-19: .. dropdown:: upper (np_test_1_all.cpp:6447) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6437 :emphasize-lines: 11 std::vector mixed_case = {"hello world", "GOOD DAY", "Mixed Case"}; auto arr = array<32>(mixed_case); // std::cout << "Original strings:" << std::endl; for (size_t i = 0; i < arr.size(); ++i) { // std::cout << " '" << arr[i] << "'"; } // Test upper case auto upper_result = upper(arr); // std::cout << "Upper case:" << std::endl; for (size_t i = 0; i < upper_result.size(); ++i) { // std::cout << " '" << upper_result[i] << "'"; } // Test lower case auto lower_result = lower(arr); // std::cout << "Lower case:" << std::endl; for (size_t i = 0; i < lower_result.size(); ++i) { // std::cout << " '" << lower_result[i] << "'";