Created
August 29, 2015 06:52
-
-
Save krysseltillada/9e7ea45b0be95a427fec to your computer and use it in GitHub Desktop.
Custom String class container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <map> | |
#include "String.h" | |
int main () | |
{ | |
std::map <Custom::String, Custom::String> dictionary; | |
Custom::String word, meaning; | |
while (std::cin >> word) { | |
std::cin >> meaning; | |
dictionary[word] = meaning; | |
} | |
for (auto p : dictionary) { | |
std::cout << "word " << p.first << std::endl | |
<< "meaning " << p.second << std::endl; | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "String.h" | |
/// implementation section | |
namespace Custom { | |
std::allocator <char> String::a_mem; | |
String::String (const char *c_char) | |
{ | |
size_t sz = 0; | |
const char *beg = c_char, *temp = c_char; | |
for (;*beg != '\0'; ++beg) | |
++sz; | |
size_t capacity = (sz > 1) ? sz * 2 : 1; | |
char *allocated_mem = a_mem.allocate (capacity); | |
past_the_last_element = allocated_mem; | |
for (;*temp != '\0'; ++temp) | |
a_mem.construct(past_the_last_element++, *temp); | |
element = allocated_mem; | |
unconstructed_mem = capacity + element; | |
} | |
String::String (const String &cop_obj) | |
{ | |
std::pair <char *, char *> new_p = copy_n_alloc(cop_obj.Begin(), cop_obj.End()); | |
element = new_p.first; | |
past_the_last_element = unconstructed_mem = new_p.second; | |
} | |
String &String::operator = (const String &rho) | |
{ | |
std::pair <char *, char *> new_p = copy_n_alloc(rho.Begin(), rho.End()); | |
free_mem (); | |
element = new_p.first; | |
past_the_last_element = unconstructed_mem = new_p.second; | |
return *this; | |
} | |
void String::operator = (char *lit_c) | |
{ | |
size_t sz_end = 0; | |
for (char *b = lit_c ; *b != '\0'; ++b) | |
++sz_end; | |
char *beg = lit_c, *end = sz_end + lit_c; | |
std::pair <char *, char *> ass_p = copy_n_alloc(beg, end); | |
element = ass_p.first; | |
past_the_last_element = unconstructed_mem = ass_p.second; | |
} | |
void String::operator = (std::string &rho) | |
{ | |
std::pair <char *, char *> new_data = copy_n_alloc(rho.begin(), rho.end()); | |
element = new_data.first; | |
past_the_last_element = unconstructed_mem = new_data.second; | |
} | |
const char &String::operator [] (const unsigned &ind) const | |
{ | |
return *(element + ind); | |
} | |
char &String::operator [] (const unsigned &ind) | |
{ | |
return *(element + ind); | |
} | |
const bool String::operator < (const String &rho) const | |
{ | |
unsigned c = 0; | |
while (true) { | |
if ((*this)[c] < rho[c]) | |
return true; | |
else if ((*this)[c] == rho[c]) { | |
++c; | |
} else | |
return false; | |
} | |
} | |
std::ostream &operator << (std::ostream &os, const String &rho) | |
{ | |
for (char *it = rho.Begin(); it != rho.End(); ++it) | |
os << *it; | |
return os; | |
} | |
std::istream &operator >> (std::istream &is, String &rho) | |
{ | |
std::string buffer; | |
is >> buffer; | |
rho = buffer; | |
return is; | |
} | |
String::~String () | |
{ | |
free_mem(); | |
} | |
char *String::Begin () const { | |
return element; | |
} | |
char *String::End () const { | |
return past_the_last_element; | |
} | |
size_t String::Capacity () const{ | |
return unconstructed_mem - element; | |
} | |
size_t String::Size () const{ | |
return past_the_last_element - element; | |
} | |
void String::Push_Back (const char &get_c) | |
{ | |
check_n_alloc(); | |
a_mem.construct (past_the_last_element++, get_c); | |
} | |
void String::check_n_alloc () | |
{ | |
if (Size() == Capacity()) | |
reallocate_mem (); | |
} | |
void String::free_mem () | |
{ | |
if (element) { | |
for (;past_the_last_element != element;) | |
a_mem.destroy (--past_the_last_element); | |
a_mem.deallocate (element ,Capacity()); | |
} | |
} | |
void String::reallocate_mem () | |
{ | |
size_t new_capacity = (Size()) ? Size() * 2 : 1; | |
char *new_allocated_mem = a_mem.allocate (new_capacity); | |
char *Forward = new_allocated_mem; | |
char *new_element = element; | |
for (size_t Count = 0; Count != Size (); ++Count) | |
a_mem.construct (Forward++, *new_element++); | |
free_mem (); | |
element = new_allocated_mem; | |
past_the_last_element = Forward; | |
unconstructed_mem = element + new_capacity; | |
} | |
std::pair <char *, char *> String::copy_n_alloc (char *b, char *e) | |
{ | |
char *new_data = a_mem.allocate (e - b); | |
return std::make_pair (new_data, std::uninitialized_copy(b, e, new_data)); | |
} | |
std::pair <char *, char *> String::copy_n_alloc (std::string::iterator b, std::string::iterator e) | |
{ | |
char *new_data = a_mem.allocate (e - b); | |
return std::make_pair (new_data, std::uninitialized_copy(b, e, new_data)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef STRING_CONTAINER_H | |
#define STRING_CONTAINER_H | |
/// interface section | |
#include <memory> | |
#include <iterator> | |
#include <iostream> | |
#include <utility> | |
#include <iterator> | |
namespace Custom { | |
class String { | |
friend std::ostream &operator << (std::ostream &, const String &); | |
friend std::istream &operator >> (std::istream &, String &); | |
public: | |
String () : | |
element (nullptr), past_the_last_element (nullptr), unconstructed_mem (nullptr) { } | |
String (const char *); | |
String (const String &); | |
String &operator = (const String &); | |
void operator = ( char *); | |
void operator = (std::string &); | |
const char &operator [] (const unsigned &) const; | |
char &operator [] (const unsigned &); | |
const bool operator < (const String &) const; | |
~String (); | |
char *Begin () const; | |
char *End () const; | |
size_t Capacity () const; | |
size_t Size () const; | |
void Push_Back (const char &); | |
private: | |
static std::allocator <char> a_mem; | |
char *element; | |
char *past_the_last_element; | |
char *unconstructed_mem; | |
void check_n_alloc (); | |
void free_mem (); | |
void reallocate_mem (); | |
std::pair <char *, char *> copy_n_alloc (char *, char *); | |
std::pair <char *, char *> copy_n_alloc (std::string::iterator, std::string::iterator); | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment