First commit

This commit is contained in:
Lynix
2012-05-01 16:43:48 +02:00
commit 71b4262c51
208 changed files with 46084 additions and 0 deletions

133
Algorithms/1.FindLast.cpp Normal file
View File

@@ -0,0 +1,133 @@
#include <cstring>
#include <string>
int test1(const std::string& str, const char* pattern)
{
const char* ch = &str[0];
unsigned int lastResult = -1;
while (true)
{
char* r = strstr(ch, pattern);
if (!r)
return lastResult;
lastResult = r - &str[0];
ch = r+1;
}
}
int test2(const std::string& str, const char* pattern)
{
const char* s = str.c_str();
unsigned int size = std::strlen(pattern);
const char* ptr = &s[str.size()-1];
const char* limit = &s[size-1];
while (true)
{
if (*ptr == pattern[size-1])
{
const char* p = &pattern[size-1];
for (; p >= &pattern[0]; --p, --ptr)
{
if (*ptr != *p)
break;
if (p == &pattern[0])
return ptr-s;
if (ptr == s)
return -1;
}
}
else if (ptr-- <= limit)
break;
}
return -1;
}
int test3(const std::string& str, const char* pattern)
{
const char* strPtr = str.c_str();
const char* ptr = &strPtr[str.size()-1];
do
{
if (*ptr == pattern[0])
{
const char* p = &pattern[1];
const char* tPtr = ptr+1;
while (true)
{
if (*p == '\0')
return ptr-strPtr;
if (*tPtr != *p)
break;
p++;
tPtr++;
}
}
}
while (ptr-- != strPtr);
return -1;
}
int test4(const std::string& str, const std::string& pattern)
{
const char* s = str.c_str();
unsigned int size = pattern.size();
const char* ptr = &s[str.size()-1];
const char* limit = &s[size-1];
while (true)
{
if (*ptr == pattern[size-1])
{
const char* p = &pattern[size-1];
for (; p >= &pattern[0]; --p, --ptr)
{
if (*ptr != *p)
break;
if (p == &pattern[0])
return ptr-s;
if (ptr == s)
return -1;
}
}
else if (ptr-- <= limit)
break;
}
return -1;
}
int test5(const std::string& str, const std::string& pattern)
{
unsigned int size = pattern.size();
const char* ptr = &str[str.size()];
const char* limit = &str[size-1];
const char* patternStr = pattern.c_str();
while (ptr-- > limit)
{
if (*ptr == pattern[size-1])
{
if (static_cast<unsigned int>(&patternStr[str.size()-1]-ptr) < size)
continue;
if (std::memcmp(ptr-size+1, patternStr, size*sizeof(char)) == 0)
return (ptr-size+1) - &str[0];
}
}
return -1;
}

View File

@@ -0,0 +1,80 @@
#include <cstring>
#include <string>
int test1(const std::string& str, const char* pattern)
{
const char* strPtr = str.c_str();
const char* ptr = &strPtr[str.size()-1];
do
{
if (*ptr == pattern[0])
{
if (ptr != strPtr && !std::isspace(*(ptr-1)))
continue;
const char* p = &pattern[1];
const char* tPtr = ptr+1;
while (true)
{
if (*p == '\0')
{
if (*tPtr == '\0' || std::isspace(*tPtr))
return ptr-strPtr;
else
break;
}
if (*tPtr != *p)
break;
p++;
tPtr++;
}
}
}
while (ptr-- != strPtr);
return -1;
}
int test2(const std::string& str, const std::string& pattern)
{
const char* s = str.c_str();
unsigned int size = pattern.size();
const char* ptr = &s[str.size()-1];
const char* limit = &s[size-1];
do
{
if (*ptr == pattern[size-1])
{
if (*(ptr+1) != '\0' && !std::isspace(*(ptr+1)))
continue;
const char* p = &pattern[size-1];
for (; p >= &pattern[0]; --p, --ptr)
{
if (*ptr != *p)
break;
if (p == &pattern[0])
{
if (ptr == s || std::isspace(*(ptr-1)))
return ptr-s;
else
break;
}
if (ptr == s)
return -1;
}
}
}
while (ptr-- > limit);
return -1;
}

79
Algorithms/3.FindWord.cpp Normal file
View File

@@ -0,0 +1,79 @@
#include <cstring>
#include <string>
int test1(const std::string& str, const char* pattern)
{
const char* s = str.c_str();
const char* ch = s;
unsigned int size = std::strlen(pattern);
while (true)
{
char* r = strstr(ch, pattern);
if (!r)
return -1;
// Si le pattern est bien isolé
if ((r == s || std::isspace(*(r-1))) && (*(r+size) == '\0' || std::isspace(*(r+size))))
return r-s;
ch = r+1;
}
}
int test2(const std::string& str, const std::string& pattern)
{
const char* s = str.c_str();
const char* ch = s;
unsigned int size = pattern.size();
while (true)
{
char* r = strstr(ch, pattern.c_str());
if (!r)
return -1;
// Si le pattern est bien isolé
if ((r == s || std::isspace(*(r-1))) && (*(r+size) == '\0' || std::isspace(*(r+size))))
return r-s;
ch = r+1;
}
}
int test3(const std::string& str, const char* pattern)
{
const char* s = str.c_str();
const char* ptr = s;
do
{
if (*ptr == pattern[0])
{
if (ptr != s && !std::isspace(*(ptr-1)))
continue;
const char* p = &pattern[1];
const char* tPtr = ptr+1;
while (true)
{
if (*p == '\0')
{
if (*tPtr == '\0' || std::isspace(*tPtr))
return ptr-s;
else
break;
}
if (*tPtr != *p)
break;
p++;
tPtr++;
}
}
}
while (*ptr++ != '\0');
return -1;
}

60
Algorithms/4.Replace.cpp Normal file
View File

@@ -0,0 +1,60 @@
#include <cstring>
#include <string>
int test1(const std::string& str, const std::string& oldString, const std::string& replaceString)
{
unsigned int count = 0;
unsigned int newSize = str.size()+15; // Pour éviter le Count, on considère que la chaine finale aura quinze caractères de plus
char* newString = new char[newSize+1];
char* ptr = &newString[0];
for (unsigned int i = 0; i < str.size(); ++i)
{
if (str.c_str()[i] == oldString[0] && std::memcmp(&str.c_str()[i], oldString.c_str(), oldString.size()*sizeof(char)) == 0)
{
std::memcpy(ptr, replaceString.c_str(), replaceString.size()*sizeof(char));
ptr += replaceString.size();
i += oldString.size()-1;
++count;
}
else
*ptr++ = str.c_str()[i];
}
newString[newSize] = '\0';
delete[] newString;
return count;
}
int test2(const std::string& str, const std::string& oldString, const std::string& replaceString)
{
unsigned int count = 0;
unsigned int newSize = str.size()+15; // Pour éviter le Count, on considère que la chaine finale aura quinze caractères de plus
char* newString = new char[newSize+1];
char* ptr = newString;
const char* p = str.c_str();
char* r = strstr(p, oldString.c_str());
while (true)
{
std::memcpy(ptr, p, (r-p)*sizeof(char));
ptr += r-p;
std::memcpy(ptr, replaceString.c_str(), (replaceString.size())*sizeof(char));
ptr += replaceString.size();
p = r+oldString.size();
count++;
r = strstr(p+1, oldString.c_str());
if (!r)
break;
}
std::strcpy(ptr, p);
delete[] newString;
return count;
}

View File

@@ -0,0 +1,67 @@
#include <cstring>
#include <string>
inline char lower(char c)
{
return ((c >= 65 && c <= 91) ? c+32 : c);
}
inline char upper(char c)
{
return ((c >= 97 && c <= 123) ? c-32 : c);
}
int test1(const std::string& str, char character)
{
char character_lower = lower(character);
char character_upper = upper(character);
unsigned int count = 0;
const char* ptr = str.c_str();
do
{
if (*ptr == character_lower || *ptr == character_upper)
count++;
}
while (*ptr++);
return count;
}
int test2(const std::string& str, char character)
{
static char tosearch[3];
char character_lower = lower(character);
char character_upper = upper(character);
tosearch[0] = character_lower;
tosearch[1] = character_upper;
tosearch[2] = '\0';
const char* ptr = str.c_str();
unsigned int count = 0;
while (true)
{
ptr = std::strpbrk(ptr, tosearch);
if (!ptr)
break;
count++;
ptr++;
}
return count;
}
int test3(const std::string& str, char character)
{
char character_lower = lower(character);
unsigned int count = 0;
const char* ptr = str.c_str();
do
{
if (lower(*ptr) == character_lower)
count++;
}
while (*ptr++);
return count;
}