This repository was archived by the owner on Apr 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
157 lines (142 loc) · 5.02 KB
/
test.cpp
File metadata and controls
157 lines (142 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <cstdint>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <vector>
// Forward declare OneIndexedArray template
template <typename T>
class OneIndexedArray;
#define OneIndexedArray_DEFINED
// Helper function to set the internal array's size as a string
template <typename T>
void setInternalArraySize(T& element, size_t size) {
element = static_cast<T>(size);
}
// Specialization for std::string
template <>
void setInternalArraySize<std::string>(std::string& element, size_t size) {
element = std::to_string(size);
}
// One-indexed dynamic array class
template <typename T>
class OneIndexedArray {
private:
std::vector<T> internalArray;
public:
OneIndexedArray() {
internalArray.push_back(T{}); // Placeholder for element count
}
void add(const T& newElement) {
internalArray.push_back(newElement);
setInternalArraySize(internalArray[0], internalArray.size() - 1);
}
void setArray(const std::vector<T>& newArray) {
internalArray.resize(newArray.size() + 1);
std::copy(newArray.begin(), newArray.end(), internalArray.begin() + 1);
setInternalArraySize(internalArray[0], newArray.size());
}
T& operator[](size_t index) {
if (index >= internalArray.size()) {
internalArray.resize(index + 1);
setInternalArraySize(internalArray[0], internalArray.size() - 1);
}
return internalArray[index];
}
const T& operator[](size_t index) const {
if (index >= internalArray.size()) {
throw std::out_of_range("Index out of range");
}
return internalArray[index];
}
size_t size() const {
return static_cast<size_t>(internalArray.size() - 1);
}
};
// Function to split text into words based on a delimiter
std::vector<std::string> split(const std::string& text, const std::string& delimiter) {
std::vector<std::string> words;
std::istringstream stream(text);
std::string word;
while (std::getline(stream, word, delimiter[0])) { // assuming single character delimiter
words.push_back(word);
}
return words;
}
// Function to split text into a OneIndexedArray
OneIndexedArray<std::string> arrSplit(const std::string& text, const std::string& delimiter) {
OneIndexedArray<std::string> array;
std::vector<std::string> words = split(text, delimiter);
array.setArray(words);
return array;
}
// Print function that converts all types to string if needed
template <typename T>
void print(const T& value) {
if constexpr (std::is_same_v<T, std::string>) {
std::cout << value << std::endl;
} else if constexpr (std::is_same_v<T, int>) {
std::cout << std::to_string(value) << std::endl;
} else if constexpr (std::is_same_v<T, float>) {
std::cout << std::to_string(value) << std::endl;
} else if constexpr (std::is_same_v<T, double>) {
std::cout << std::to_string(value) << std::endl;
} else if constexpr (std::is_same_v<T, size_t>) {
std::cout << std::to_string(value) << std::endl;
} else if constexpr (std::is_same_v<T, bool>) {
std::cout << (value ? "1" : "0") << std::endl;
}
#ifdef OneIndexedArray_DEFINED
else if constexpr (std::is_base_of_v<OneIndexedArray<std::string>, T>) {
for (size_t i = 1; i <= value.size(); ++i) {
std::cout << value[i] << std::endl;
}
} else if constexpr (std::is_base_of_v<OneIndexedArray<int>, T>) {
for (size_t i = 1; i <= value.size(); ++i) {
std::cout << std::to_string(value[i]) << std::endl;
}
} else if constexpr (std::is_base_of_v<OneIndexedArray<float>, T>) {
for (size_t i = 1; i <= value.size(); ++i) {
std::cout << std::to_string(value[i]) << std::endl;
}
} else if constexpr (std::is_base_of_v<OneIndexedArray<double>, T>) {
for (size_t i = 1; i <= value.size(); ++i) {
std::cout << std::to_string(value[i]) << std::endl;
}
}
#endif
else {
std::cout << "Unsupported type" << std::endl;
}
}
void testIntArray(OneIndexedArray<int> intArray, std::string secondParam)
{
print(intArray);
print(std::string("the secondParam is: ") + secondParam);
}
void testStrArray(OneIndexedArray<std::string> strArray, std::string secondParam)
{
print(strArray);
print(std::string("the secondParam is: ") + secondParam);
}
void testFloatArray(OneIndexedArray<float> floatArray, std::string secondParam)
{
print(floatArray);
print(std::string("the secondParam is: ") + secondParam);
}
// Simply label the main function since before we used other functions
int main(int argc, char* argv[])
{
OneIndexedArray<int> intArray;
OneIndexedArray<std::string> strArray;
OneIndexedArray<float> floatArray;
intArray.add(5);
strArray.add(std::string("hello"));
floatArray.add(3.14);
std::string secondParam = std::string("this is the secondParam");
testIntArray ( intArray , secondParam ) ;
testStrArray ( strArray , secondParam ) ;
testFloatArray ( floatArray , secondParam ) ;
return 0;
}