CharArray#
-
class numpy::CharArray#
numpy C++ class.
Example#
#include <numpy/np_ndarray.h>
using namespace numpy;
// Use CharArray
CharArray obj;
// ... operations ...
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
NP_CHARARRAY.H:54 |
|
|
NP_CHARARRAY.H:57 |
|
|
NP_CHARARRAY.H:60 |
|
|
NP_CHARARRAY.H:64 |
Operators#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
CharArray & |
NP_CHARARRAY.H:67 |
|
|
std::string |
NP_CHARARRAY.H:84 |
|
|
std::string |
NP_CHARARRAY.H:93 |
|
|
NDArray<bool> |
NP_CHARARRAY.H:391 |
|
|
NDArray<bool> |
NP_CHARARRAY.H:402 |
|
|
CharArray |
NP_CHARARRAY.H:414 |
|
|
CharArray |
NP_CHARARRAY.H:418 |
|
|
CharArray |
NP_CHARARRAY.H:422 |
Array Creation#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
std::string |
NP_CHARARRAY.H:40 |
Math Operations#
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
const NDArray<str_<N>>& |
NP_CHARARRAY.H:75 |
|
|
NDArray<str_<N>>& |
NP_CHARARRAY.H:76 |
|
|
CharArray |
NP_CHARARRAY.H:123 |
|
|
CharArray |
NP_CHARARRAY.H:150 |
|
|
CharArray |
NP_CHARARRAY.H:219 |
|
|
size_t |
NP_CHARARRAY.H:81 |
|
|
str_<N> |
NP_CHARARRAY.H:48 |
|
|
void |
NP_CHARARRAY.H:432 |
|
|
CharArray |
NP_CHARARRAY.H:235 |
|
|
void |
NP_CHARARRAY.H:88 |
|
|
void |
NP_CHARARRAY.H:97 |
|
|
const std::vector<size_t>& |
NP_CHARARRAY.H:79 |
|
|
size_t |
NP_CHARARRAY.H:80 |
|
|
CharArray |
NP_CHARARRAY.H:198 |
|
|
CharArray |
NP_CHARARRAY.H:180 |
|
|
CharArray |
NP_CHARARRAY.H:161 |
|
|
std::string |
NP_CHARARRAY.H:427 |
|
|
CharArray |
NP_CHARARRAY.H:139 |
Code Examples#
The following examples are extracted from the test suite.
add (np_test_1_all.cpp:6410)
6400void testCharArrayStringOperationsCharArray() {
6401 std::cout << "========= testStringOperations =======================";
6402
6403 // Test string concatenation
6404 std::vector<std::string> words1 = {"Hello", "Good", "Nice"};
6405 std::vector<std::string> words2 = {" World", " Day", " Work"};
6406
6407 auto arr1 = array<32>(words1);
6408 auto arr2 = array<32>(words2);
6409
6410 auto result = add(arr1, arr2);
6411 // std::cout << "String concatenation (add):" << std::endl;
6412 for (size_t i = 0; i < result.size(); ++i) {
6413 // std::cout << "'" << arr1[i] << "' + '" << arr2[i] << "' = '" << result[i] << "'";
6414 }
6415
6416 // Test scalar addition
6417 auto scalar_result = add(arr1, "!");
6418 // std::cout << "Scalar concatenation:" << std::endl;
6419 for (size_t i = 0; i < scalar_result.size(); ++i) {
6420 // std::cout << "'" << arr1[i] << "' + '!' = '" << scalar_result[i] << "'";
add (np_test_1_all.cpp:6410)
6400void testCharArrayStringOperationsCharArray() {
6401 std::cout << "========= testStringOperations =======================";
6402
6403 // Test string concatenation
6404 std::vector<std::string> words1 = {"Hello", "Good", "Nice"};
6405 std::vector<std::string> words2 = {" World", " Day", " Work"};
6406
6407 auto arr1 = array<32>(words1);
6408 auto arr2 = array<32>(words2);
6409
6410 auto result = add(arr1, arr2);
6411 // std::cout << "String concatenation (add):" << std::endl;
6412 for (size_t i = 0; i < result.size(); ++i) {
6413 // std::cout << "'" << arr1[i] << "' + '" << arr2[i] << "' = '" << result[i] << "'";
6414 }
6415
6416 // Test scalar addition
6417 auto scalar_result = add(arr1, "!");
6418 // std::cout << "Scalar concatenation:" << std::endl;
6419 for (size_t i = 0; i < scalar_result.size(); ++i) {
6420 // std::cout << "'" << arr1[i] << "' + '!' = '" << scalar_result[i] << "'";
multiply (np_test_1_all.cpp:6426)
6416 // Test scalar addition
6417 auto scalar_result = add(arr1, "!");
6418 // std::cout << "Scalar concatenation:" << std::endl;
6419 for (size_t i = 0; i < scalar_result.size(); ++i) {
6420 // std::cout << "'" << arr1[i] << "' + '!' = '" << scalar_result[i] << "'";
6421 }
6422
6423 // Test string multiplication
6424 std::vector<std::string> patterns = {"Ha", "Ho", "Hi"};
6425 auto pattern_arr = array<32>(patterns);
6426 auto repeated = multiply(pattern_arr, 3);
6427 // std::cout << "String multiplication:" << std::endl;
6428 for (size_t i = 0; i < repeated.size(); ++i) {
6429 // std::cout << "'" << pattern_arr[i] << "' * 3 = '" << repeated[i] << "'";
6430 }
6431
6432 std::cout << " -> tests passed" << std::endl;
6433}
6434
6435void testCaseConversionsCharArray() {
6436 std::cout << "========= testCaseConversions =======================";
array (np_test_1_all.cpp:243)
233 // std::cout << "Min: " << array.minArray() << std::endl;
234 // std::cout << "Max: " << array.maxArray() << std::endl;
235
236 std::cout << " -> tests passed" << std::endl;
237}
238
239void testReshapeAndFlatten() {
240 std::cout << "========= testReshapeAndFlatten =======================";
241
242 auto range_array = NDArray<int32>::createRange(0, 12, 1);
243 // std::cout << "Range array (0-11):" << std::endl;
244 //range_array.printArray();
245
246 auto reshaped = range_array.reshapeArray({3, 4});
247 // std::cout << "Reshaped to 3x4:";
248 //reshaped.printArray();
249
250 auto flattened = reshaped.flattenArray();
251 // std::cout << "Flattened:" << std::endl;
252 //flattened.printArray();
array (np_test_1_all.cpp:243)
233 // std::cout << "Min: " << array.minArray() << std::endl;
234 // std::cout << "Max: " << array.maxArray() << std::endl;
235
236 std::cout << " -> tests passed" << std::endl;
237}
238
239void testReshapeAndFlatten() {
240 std::cout << "========= testReshapeAndFlatten =======================";
241
242 auto range_array = NDArray<int32>::createRange(0, 12, 1);
243 // std::cout << "Range array (0-11):" << std::endl;
244 //range_array.printArray();
245
246 auto reshaped = range_array.reshapeArray({3, 4});
247 // std::cout << "Reshaped to 3x4:";
248 //reshaped.printArray();
249
250 auto flattened = reshaped.flattenArray();
251 // std::cout << "Flattened:" << std::endl;
252 //flattened.printArray();
capitalize (np_test_1_all.cpp:6461)
6451 }
6452
6453 // Test lower case
6454 auto lower_result = lower(arr);
6455 // std::cout << "Lower case:" << std::endl;
6456 for (size_t i = 0; i < lower_result.size(); ++i) {
6457 // std::cout << " '" << lower_result[i] << "'";
6458 }
6459
6460 // Test capitalize
6461 auto cap_result = capitalize(arr);
6462 // std::cout << "Capitalize:" << std::endl;
6463 for (size_t i = 0; i < cap_result.size(); ++i) {
6464 // std::cout << " '" << cap_result[i] << "'";
6465 }
6466
6467 // Test title
6468 auto title_result = title(arr);
6469 // std::cout << "Title case:" << std::endl;
6470 for (size_t i = 0; i < title_result.size(); ++i) {
6471 // std::cout << " '" << title_result[i] << "'";
lower (np_test_1_all.cpp:6454)
6444 }
6445
6446 // Test upper case
6447 auto upper_result = upper(arr);
6448 // std::cout << "Upper case:" << std::endl;
6449 for (size_t i = 0; i < upper_result.size(); ++i) {
6450 // std::cout << " '" << upper_result[i] << "'";
6451 }
6452
6453 // Test lower case
6454 auto lower_result = lower(arr);
6455 // std::cout << "Lower case:" << std::endl;
6456 for (size_t i = 0; i < lower_result.size(); ++i) {
6457 // std::cout << " '" << lower_result[i] << "'";
6458 }
6459
6460 // Test capitalize
6461 auto cap_result = capitalize(arr);
6462 // std::cout << "Capitalize:" << std::endl;
6463 for (size_t i = 0; i < cap_result.size(); ++i) {
6464 // std::cout << " '" << cap_result[i] << "'";
lstrip (np_test_1_all.cpp:6641)
6631 }
6632
6633 // Test strip
6634 auto stripped = strip(arr);
6635 // std::cout << "After strip():" << std::endl;
6636 for (size_t i = 0; i < stripped.size(); ++i) {
6637 // std::cout << "'" << stripped[i] << "'";
6638 }
6639
6640 // Test lstrip
6641 auto left_stripped = lstrip(arr);
6642 // std::cout << "After lstrip():" << std::endl;
6643 for (size_t i = 0; i < left_stripped.size(); ++i) {
6644 // std::cout << "'" << left_stripped[i] << "'";
6645 }
6646
6647 // Test rstrip
6648 auto right_stripped = rstrip(arr);
6649 // std::cout << "After rstrip():" << std::endl;
6650 for (size_t i = 0; i < right_stripped.size(); ++i) {
6651 // std::cout << "'" << right_stripped[i] << "'";
ndim (np_test_2_all.cpp:3526)
3516 void testArrayInfo() {
3517 std::cout << "Testing array information functions...\n";
3518
3519 // Test basic info functions
3520 auto arr = createFloat64Array({ 2, 3, 4 });
3521 for (size_t i = 0; i < arr.getSize(); ++i) {
3522 arr.setElementAt({ i / 12, (i / 4) % 3, i % 4 }, static_cast<double>(i));
3523 }
3524
3525 // Test ndim
3526 if (!(ndim(arr) == 3)) {
3527 std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ndim(arr) == 3)";
3528 std::cout << std::string("[FAIL] ") + description << std::endl;
3529 throw std::runtime_error(description);
3530 }
3531
3532 // Test size
3533 if (!(size(arr) == 24)) {
3534 std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(size(arr) == 24)";
3535 std::cout << std::string("[FAIL] ") + description << std::endl;
3536 throw std::runtime_error(description);
print (np_test_1_all.cpp:6395)
6385 std::vector<std::string> strings = {"Alice", "Bob", "Charlie"};
6386 auto arr2 = array<32>(strings);
6387 // std::cout << "Created from strings vector:" << std::endl;
6388 for (size_t i = 0; i < arr2.size(); ++i) {
6389 // std::cout << "arr2[" << i << "] = '" << arr2[i] << "'";
6390 }
6391
6392 // Test 2D array creation
6393 chararray16 arr_2d({2, 3}, "test");
6394 // std::cout << "Created 2D chararray16 with shape {2, 3}:";
6395 // arr_2d.print();
6396
6397 std::cout << " -> tests passed" << std::endl;
6398}
6399
6400void testCharArrayStringOperationsCharArray() {
6401 std::cout << "========= testStringOperations =======================";
6402
6403 // Test string concatenation
6404 std::vector<std::string> words1 = {"Hello", "Good", "Nice"};
6405 std::vector<std::string> words2 = {" World", " Day", " Work"};
rstrip (np_test_1_all.cpp:6648)
6638 }
6639
6640 // Test lstrip
6641 auto left_stripped = lstrip(arr);
6642 // std::cout << "After lstrip():" << std::endl;
6643 for (size_t i = 0; i < left_stripped.size(); ++i) {
6644 // std::cout << "'" << left_stripped[i] << "'";
6645 }
6646
6647 // Test rstrip
6648 auto right_stripped = rstrip(arr);
6649 // std::cout << "After rstrip():" << std::endl;
6650 for (size_t i = 0; i < right_stripped.size(); ++i) {
6651 // std::cout << "'" << right_stripped[i] << "'";
6652 }
6653
6654 std::cout << " -> tests passed" << std::endl;
6655}
6656
6657void testStringComparisonCharArray() {
6658 std::cout << "========= testStringComparison =======================";
set (np_test_1_all.cpp:6747)
6737 std::cout << "========= testArrayPrinting =======================";
6738
6739 // Test 1D array printing
6740 std::vector<std::string> words = {"Hello", "World", "Test"};
6741 auto arr_1d = array<16>(words);
6742 // std::cout << "1D CharArray:" << std::endl;
6743 // arr_1d.print();
6744
6745 // Test 2D array printing
6746 chararray16 arr_2d({2, 2}, "test");
6747 arr_2d.set({0, 0}, "A");
6748 arr_2d.set({0, 1}, "B");
6749 arr_2d.set({1, 0}, "C");
6750 arr_2d.set({1, 1}, "D");
6751
6752 // std::cout << "2D CharArray:" << std::endl;
6753 // arr_2d.print();
6754
6755 std::cout << " -> tests passed" << std::endl;
6756}
set (np_test_1_all.cpp:6747)
6737 std::cout << "========= testArrayPrinting =======================";
6738
6739 // Test 1D array printing
6740 std::vector<std::string> words = {"Hello", "World", "Test"};
6741 auto arr_1d = array<16>(words);
6742 // std::cout << "1D CharArray:" << std::endl;
6743 // arr_1d.print();
6744
6745 // Test 2D array printing
6746 chararray16 arr_2d({2, 2}, "test");
6747 arr_2d.set({0, 0}, "A");
6748 arr_2d.set({0, 1}, "B");
6749 arr_2d.set({1, 0}, "C");
6750 arr_2d.set({1, 1}, "D");
6751
6752 // std::cout << "2D CharArray:" << std::endl;
6753 // arr_2d.print();
6754
6755 std::cout << " -> tests passed" << std::endl;
6756}
shape (np_test_1_all.cpp:3751)
3741 }
3742
3743 for (size_t j = 0; j < 3; ++j) {
3744 arr3.setElementAt({0, j}, static_cast<double>((j + 1) * 100)); // [100, 200, 300]
3745 }
3746
3747 // Broadcast all arrays together
3748 std::vector<NDArray<double>> arrays = {arr1, arr2, arr3};
3749 auto broadcasted = broadcast_arrays(arrays);
3750
3751 // All should have shape (2, 3)
3752 for (const auto& arr : broadcasted) {
3753 if (!((arr.getShape() == std::vector<size_t>{2, 3}))) {
3754 std::string description = std::string("test_broadcast_arrays():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((arr.getShape() == std::vector<size_t>{2, 3}))";
3755 std::cout << std::string("[FAIL] ") + description << std::endl;
3756 throw std::runtime_error(description);
3757 }
3758 }
3759
3760 // Check values
3761 if (!(broadcasted[0].getElementAt({0, 1}) == 2.0)) {
size (np_test_1_all.cpp:47)
37using namespace numpy;
38using namespace numpy::benchmark;
39using namespace numpy::char_;
40
41// Helper functions for array comparison tests
42namespace {
43 const double TOLERANCE = 1e-10;
44
45 bool allTrue(const NDArray<bool>& arr) {
46 std::vector<size_t> indices(arr.getShape().size(), 0);
47 do {
48 if (!arr.getElementAt(indices)) {
49 return false;
50 }
51 } while (incrementIndices(indices, arr.getShape()));
52 return true;
53 }
54
55 bool allFalse(const NDArray<bool>& arr) {
56 std::vector<size_t> indices(arr.getShape().size(), 0);
strip (np_test_1_all.cpp:6634)
6624 std::vector<std::string> padded = {" hello ", "\t\tworld\t\t", " spaces "};
6625 auto arr = array<32>(padded);
6626
6627 // std::cout << "Original padded strings:" << std::endl;
6628 for (size_t i = 0; i < arr.size(); ++i) {
6629 // std::cout << "'" << arr[i] << "'";
6630 }
6631
6632 // Test strip
6633 auto stripped = strip(arr);
6634 // std::cout << "After strip():" << std::endl;
6635 for (size_t i = 0; i < stripped.size(); ++i) {
6636 // std::cout << "'" << stripped[i] << "'";
6637 }
6638
6639 // Test lstrip
6640 auto left_stripped = lstrip(arr);
6641 // std::cout << "After lstrip():" << std::endl;
6642 for (size_t i = 0; i < left_stripped.size(); ++i) {
6643 // std::cout << "'" << left_stripped[i] << "'";
swapcase (np_test_5_all.cpp:1434)
1424 }
1425
1426 std::cout << " -> tests passed" << std::endl;
1427 }
1428
1429 void np_test_string_swapcase() {
1430 std::cout << "========= swapcase formatting =======================";
1431
1432 std::vector<std::string> strings = { "Hello World", "TEST", "test" };
1433 auto arr = numpy::char_::array<32>(strings);
1434 auto result = numpy::char_::swapcase(arr);
1435
1436 bool passed = (result.size() == arr.size());
1437
1438 if (!passed) {
1439 std::cout << " [FAIL] : in np_test_string_swapcase() : size mismatch";
1440 throw std::runtime_error("np_test_string_swapcase failed");
1441 }
1442
1443 std::cout << " -> tests passed" << std::endl;
1444 }
title (np_test_1_all.cpp:6468)
6458 }
6459
6460 // Test capitalize
6461 auto cap_result = capitalize(arr);
6462 // std::cout << "Capitalize:" << std::endl;
6463 for (size_t i = 0; i < cap_result.size(); ++i) {
6464 // std::cout << " '" << cap_result[i] << "'";
6465 }
6466
6467 // Test title
6468 auto title_result = title(arr);
6469 // std::cout << "Title case:" << std::endl;
6470 for (size_t i = 0; i < title_result.size(); ++i) {
6471 // std::cout << " '" << title_result[i] << "'";
6472 }
6473
6474 std::cout << " -> tests passed" << std::endl;
6475}
6476
6477void testStringTestingCharArray() {
6478 std::cout << "========= testStringTesting =======================";
toString (np_test_1_all.cpp:6826)
6816void testDatetime64CreationDateTime() {
6817 std::cout << "========= testDatetime64CreationDateTime =======================";
6818
6819 // Test default constructor
6820 datetime64 default_dt;
6821 if (!(default_dt.isNaT())) {
6822 std::string description = std::string("testDatetime64CreationDateTime():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(default_dt.isNaT())";
6823 std::cout << std::string("[FAIL] ") + description << std::endl;
6824 throw std::runtime_error(description);
6825 }
6826 // std::cout << "Default datetime64 is NaT: " << default_dt.toString() << std::endl;
6827
6828 // Test value constructor
6829 datetime64 epoch_day(0, DateTimeUnit::Day);
6830 // std::cout << "Epoch day: " << epoch_day.toString() << std::endl;
6831
6832 // Test string constructor
6833 datetime64 date_from_string("2024-01-15");
6834 // std::cout << "Date from string '2024-01-15': " << date_from_string.toString() << std::endl;
6835
6836 datetime64 datetime_from_string("2024-01-15T10:30:45");
upper (np_test_1_all.cpp:6447)
6437 std::vector<std::string> mixed_case = {"hello world", "GOOD DAY", "Mixed Case"};
6438 auto arr = array<32>(mixed_case);
6439
6440 // std::cout << "Original strings:" << std::endl;
6441 for (size_t i = 0; i < arr.size(); ++i) {
6442 // std::cout << " '" << arr[i] << "'";
6443 }
6444
6445 // Test upper case
6446 auto upper_result = upper(arr);
6447 // std::cout << "Upper case:" << std::endl;
6448 for (size_t i = 0; i < upper_result.size(); ++i) {
6449 // std::cout << " '" << upper_result[i] << "'";
6450 }
6451
6452 // Test lower case
6453 auto lower_result = lower(arr);
6454 // std::cout << "Lower case:" << std::endl;
6455 for (size_t i = 0; i < lower_result.size(); ++i) {
6456 // std::cout << " '" << lower_result[i] << "'";