Skip to content

Commit

Permalink
feat: basic implementation of iString and comparative testing code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lovelylavender4 committed Jan 29, 2024
1 parent ac7b7c3 commit 938d7d8
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 8 deletions.
66 changes: 62 additions & 4 deletions iVerse/include/string/iString.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,74 @@
****/
#pragma once

#include <aliases.h>
#include <codecvt>
#include <functional>
#include <iFamily.h>
#include <string>

namespace MiracleForest::inline i
namespace MiracleForest::inline i::inline iVerse
{
namespace string
namespace String
{
class iString
{
private:
Ptr<u8char> _data;
size_t _length;
size_t _begin;
size_t _capacity;
std::allocator<u8char> _alloc;

public:
iString(CPtr<char> dat) { _create((CPtr<u8char>)dat); }
iString(CRef<std::string> dat) { _create((CPtr<u8char>)dat.c_str(), dat.length()); }
iString(CPtr<u8char> dat) { _create(dat); }
iString(CPtr<wchar> dat)
{
auto tempStr = std::wstring_convert<std::codecvt_utf16<wchar>>().to_bytes(dat);
_create((CPtr<u8char>)tempStr.c_str(), tempStr.length());
}
~iString() { _alloc.deallocate(_data, _capacity); }
operator std::string() const { return operator CPtr<char>(); }
operator CPtr<char>() const { return (CPtr<char>)(_data + _begin); }

public:
size_t length() const { return _length; }

private:
void _create(CPtr<u8char> dat)
{
for (_length = 0; *(dat + _length); _length++) {}
_capacity = 2 * _length;
_data = _alloc.allocate(_capacity);
_begin = _length / 2;
memcpy(_data + _begin, dat, _length + 1);
}

void _create(CPtr<u8char> dat, size_t length)
{
_capacity = 2 * length;
_data = _alloc.allocate(_capacity);
_begin = length / 2;
_length = length;
memcpy(_data + _begin, dat, length + 1);
}
};
} // namespace string
} // namespace MiracleForest::inline i
} // namespace String
} // namespace MiracleForest::inline i::inline iVerse

namespace std
{
template<>
struct hash<MiracleForest::String::iString>
{
size_t operator()(MiracleForest::CRef<MiracleForest::String::iString> key)
{
MiracleForest::uint hash = 0;
MiracleForest::CPtr<char> str = key;
while (*str) { hash = hash * 131 + (*str++); }
return (hash & 0x7FFFFFFF);
}
};
} // namespace std
66 changes: 66 additions & 0 deletions iVerse/tests/iVerseTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "string/iString.h"
#include <chrono>
#include <iostream>
using istring = MiracleForest::String::iString;

#define TESTSTR \
"Hello world!你好世界!Привет, мир!Witaj świecie!안녕하세요!こんにちは!Hej världen!¡Hola, mundo!Γεια " \
"σας " \
"κόσμε!Chào thế giới!สวัสดีชาวโลก!හෙලෝ " \
"වර්ල්ඩ්!ສະ​ບາຍ​ດີ​ຊາວ​ໂລກ!नमस्कार विश्व !مرحبا " \
"بالعالم!ހެލޯ " \
"ވޯލްޑް!"
#define TESTSTRU8 \
u8"Hello world!你好世界!Привет, мир!Witaj świecie!안녕하세요!こんにちは!Hej världen!¡Hola, " \
u8"mundo!Γεια σας " \
u8"κόσμε!Chào thế giới!สวัสดีชาวโลก!හෙලෝ " \
u8"වර්ල්ඩ්!ສະ​ບາຍ​ດີ​ຊາວ​ໂລກ!नमस्कार विश्व !مرحبا " \
u8"بالعالم!ހެލޯ " \
u8"ވޯލްޑް!"


void compare()
{
char c[] = TESTSTR;
char* d = c + 5;
{
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < 1000'0000; i++) { std::string str(d); }
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);

std::cout << "\033[38;2;160;255;112m"
"std::string took "
<< duration.count() << " ms to execute." << std::endl;
}
{
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < 1000'0000; i++) { istring str(d); }
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);

std::cout << "\033[38;2;140;230;255m"
"istring took "
<< duration.count() << " ms to execute." << std::endl;
}
}

int main()
{
system("chcp 65001");
char8_t c[] = TESTSTRU8;
istring str = TESTSTR;
// istring str1(c);
std::cout << (const char*)c << std::endl;
std::cout << str << std::endl;
std::cout << "\033[38;2;240;255;100m"
"hash: "
<< std::hash<istring>()(str) << std::endl;
std::cout << "\033[38;2;255;112;245m"
"Press any key to start the test."
<< std::endl;
std::cin.get();
std::cin.get();
for (size_t i = 0; i < 10; i++) { compare(); }
return 0;
}
4 changes: 0 additions & 4 deletions iVerse/tests/iVerseTest.cpp.cpp

This file was deleted.

0 comments on commit 938d7d8

Please sign in to comment.