Skip to content
This repository has been archived by the owner on Nov 24, 2024. It is now read-only.

Commit

Permalink
created few modules
Browse files Browse the repository at this point in the history
  • Loading branch information
SaptakBhoumik committed Dec 10, 2022
1 parent 07a6a72 commit c8bc4de
Show file tree
Hide file tree
Showing 4 changed files with 364 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@
"locale": "cpp",
"queue": "cpp",
"stack": "cpp",
"*.ipp": "cpp"
"*.ipp": "cpp",
"__memory": "cpp",
"*.inc": "cpp"
},
"mesonbuild.configureOnOpen": true,
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.tabCompletion": "on",
"diffEditor.codeLens": true,
}
65 changes: 65 additions & 0 deletions lib/dictionary.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef __PEREGRINE__STR__
#define __PEREGRINE__STR__
//TODO: Use peregrine exception instead of c++ throw
#include "list.hpp"
namespace Peregrine{
template<typename T1,typename T2>
struct pair{
T1 first;
T2 second;
};
//Ordered dictionary
template<typename T1,typename T2>
class dict{
size_t m_iter_index=0;
list<T1> m_keys;
list<T2> m_values;
public:
dict(list<T1> keys,list<T2> values){
m_keys=keys;
m_values=values;
}
dict(){}
dict<T1,T2>& operator=(const dict<T1,T2>& other){
if(this!=&other){
m_keys=other.m_keys;
m_values=other.m_values
}
return *this;
}
dict<T1,T2>& operator=(dict<T1,T2>&& other){
if(this!=&other){
m_keys=other.m_keys;
m_values=other.m_values;
other.clear();
}
return *this;
}
size_t ____mem____P____P______len__()const{
return m_keys.____mem____P____P______len__();
}
size_t ____mem____P____P______iter__(){
m_iter_index=0;//reseting it to 0.Dont remove this line
return m_keys.____mem____P____P______len__();
}
pair<T1,T2> ____mem____P____P______iterate__(){
m_iter_index++;
return pair<T1,T2>{m_keys[m_iter_index-1],m_values[m_iter_index-1]};
}
dict<T1,T2>& ____mem____P____P______enter__(){
return *this;
}
void ____mem____P____P______end__(){}
list<T1>& keys(){
return m_keys;
}
list<T2>& values(){
return m_values;
}
void clear(){
m_values.clear();
m_keys.clear();
}
};
}
#endif
145 changes: 145 additions & 0 deletions lib/list.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#ifndef __PEREGRINE__LIST__
#define __PEREGRINE__LIST__
//TODO: Use peregrine exception instead of c++ throw
#include <cstddef>
#include <cstdint>
#include <iostream>
namespace Peregrine{
template<typename T>
class list{
T* m_data;
size_t m_size=0;
size_t m_capacity=0;
size_t m_iter_index=0;
public:
list(){
m_data=nullptr;
}
list(size_t size){
m_data=new T[size];
this->m_capacity=size;
}
list(const list<T>& other){
m_data=new T[other.m_size];
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
for(size_t i=0;i<m_size;i++){
m_data[i]=other.m_data[i];
}
}
list(list<T>&& other){
m_data=other.m_data;
other.m_data=nullptr;
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
}
~list(){
delete[] m_data;
}
list<T>& operator=(const list<T>& other){
if(this!=&other){
delete[] m_data;
m_data=new T[other.m_size];
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
for(size_t i=0;i<m_size;i++){
m_data[i]=other.m_data[i];
}
}
return *this;
}
list<T>& operator=(list<T>&& other){
if(this!=&other){
delete[] m_data;
m_data=new T[other.m_size];
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
for(size_t i=0;i<m_size;i++){
m_data[i]=other.m_data[i];
}
delete[] other.m_data;
}
return *this;
}
T& ____mem____P____P______getitem__(int64_t index){
if(index<0){
index+=(int64_t)m_size;
}
if(index<0||index>=m_capacity){
throw std::out_of_range("index out of range");
}
if(index>=m_size){
m_size=index+1;
}
return m_data[index];
}
const T& ____mem____P____P______getitem__(int64_t index)const{
if(index<0){
index+=(int64_t)m_size;
}
if(index<0||index>=m_capacity){
throw std::out_of_range("index out of range");
}
return m_data[index];
}
//TODO: list[1:5]
size_t ____mem____P____P______len__()const{
return m_size;
}
size_t ____mem____P____P______iter__(){
m_iter_index=0;//reseting it to 0.Dont remove this line
return m_size;
}
T ____mem____P____P______iterate__(){
m_iter_index++;
return m_data[m_iter_index-1];
}
list<T>& ____mem____P____P______enter__(){
return *this;
}
void ____mem____P____P______end__(){}
//TODO: __reverse__
void extend(const list<T>& other){
if(m_capacity<=(m_size+other.m_size)){
m_capacity+=other.m_capacity;
T* new_data=new T[m_capacity];
for(size_t i=0;i<m_size;i++){
new_data[i]=m_data[i];
}
delete[] m_data;
m_data=new_data;
}
for (size_t i = 0; i < other.m_size; i++)
{
m_size++;
m_data[m_size-1]=other.m_data[i];
}

}
void append(T value){
if(m_size==m_capacity){
if(m_capacity==0){
m_capacity=1;
}
else{
m_capacity*=2;
}
T* new_data=new T[m_capacity];
for(size_t i=0;i<m_size;i++){
new_data[i]=m_data[i];
}
delete[] m_data;
m_data=new_data;
}
m_size++;
m_data[m_size-1]=value;
}
void clear(){
m_size=0;
delete[] m_data;
m_data=nullptr;
m_capacity=0;
}
};
}
#endif
149 changes: 149 additions & 0 deletions lib/string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#ifndef __PEREGRINE__STR__
#define __PEREGRINE__STR__
//TODO: Use peregrine exception instead of c++ throw
#include <cstddef>
#include <cstdint>
#include <iostream>
namespace Peregrine{
class str{
char* m_data;
size_t m_size=0;
size_t m_capacity=0;
size_t m_iter_index=0;
public:
str(){
m_data=nullptr;
}
str(const char* string,size_t size){
m_data=new char[size];
for(size_t i=0;i<size;i++){
m_data[i]=string[i];
}
this->m_capacity=size;
}
str(const char c){
m_data=new char[1];
m_data[0]=c;
this->m_capacity=1;
}
str(const str& other){
m_data=new char[other.m_size];
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
for(size_t i=0;i<m_size;i++){
m_data[i]=other.m_data[i];
}
}
str(str&& other){
m_data=other.m_data;
other.m_data=nullptr;
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
}
~str(){
delete[] m_data;
}
str& operator=(const str& other){
if(this!=&other){
delete[] m_data;
m_data=new char[other.m_size];
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
for(size_t i=0;i<m_size;i++){
m_data[i]=other.m_data[i];
}
}
return *this;
}
str& operator=(str&& other){
if(this!=&other){
delete[] m_data;
m_data=other.m_data;
other.m_data=nullptr;
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
}
return *this;
}
char& ____mem____P____P______getitem__(int64_t index){
if(index<0){
index+=(int64_t)m_size;
}
if(index<0||index>=m_capacity){
throw std::out_of_range("index out of range");
}
if(index>=m_size){
m_size=index+1;
}
return m_data[index];
}
const char& ____mem____P____P______getitem__(int64_t index)const{
if(index<0){
index+=(int64_t)m_size;
}
if(index<0||index>=m_capacity){
throw std::out_of_range("index out of range");
}
return m_data[index];
}
//TODO: str[1:5]
size_t ____mem____P____P______len__()const{
return m_size;
}
size_t ____mem____P____P______iter__(){
m_iter_index=0;//reseting it to 0.Dont remove this line
return m_size;
}
char ____mem____P____P______iterate__(){
m_iter_index++;
return m_data[m_iter_index-1];
}
str& ____mem____P____P______enter__(){
return *this;
}
void ____mem____P____P______end__(){}
//TODO: __reverse__
void append(const str& other){
if(m_capacity<=(m_size+other.m_size)){
m_capacity+=other.m_capacity;
char* new_data=new char[m_capacity];
for(size_t i=0;i<m_size;i++){
new_data[i]=m_data[i];
}
delete[] m_data;
m_data=new_data;
}
for (size_t i = 0; i < other.m_size; i++)
{
m_size++;
m_data[m_size-1]=other.m_data[i];
}

}
void append(char value){
if(m_size==m_capacity){
if(m_capacity==0){
m_capacity=1;
}
else{
m_capacity*=2;
}
char* new_data=new char[m_capacity];
for(size_t i=0;i<m_size;i++){
new_data[i]=m_data[i];
}
delete[] m_data;
m_data=new_data;
}
m_size++;
m_data[m_size-1]=value;
}
void clear(){
m_size=0;
delete[] m_data;
m_data=nullptr;
m_capacity=0;
}
};
}
#endif

0 comments on commit c8bc4de

Please sign in to comment.