-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d81125
commit 0728d95
Showing
2 changed files
with
93 additions
and
115 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,61 @@ | ||
#include "IntList.h" | ||
|
||
namespace iProlog | ||
{ | ||
IntList::IntList(int const head) : head(head), tail(nullptr) | ||
{ | ||
} | ||
//IntList constructur declaration | ||
IntList::IntList(int const X, IntList *const Xs) : head(X), tail(Xs) | ||
{ | ||
} | ||
|
||
bool IntList::isEmpty(IntList *const Xs) | ||
{ | ||
return nullptr == Xs; | ||
} | ||
|
||
int IntList::head(IntList *const Xs) | ||
{ | ||
return Xs->head_Renamed; | ||
} | ||
|
||
IntList *IntList::tail(IntList *const Xs) | ||
{ | ||
return Xs->tail_Renamed; | ||
} | ||
|
||
IntList *IntList::cons(int const X, IntList *const Xs) | ||
{ | ||
return new IntList(X, Xs); | ||
} | ||
|
||
IntList *IntList::app(std::vector<int> &xs, IntList *const Ys) | ||
{ | ||
IntList *Zs = Ys; | ||
|
||
int i = xs.size() - 1; | ||
while(i>=0) | ||
{ | ||
Zs = cons(xs[i], Zs); | ||
i--; | ||
} | ||
|
||
return Zs; | ||
} | ||
//To ints conversion | ||
IntStack *IntList::toInts(IntList *Xs) | ||
{ | ||
IntStack * const is = new IntStack(); | ||
while (!isEmpty(Xs)) | ||
{ | ||
is->push(head(Xs)); | ||
Xs = tail(Xs); | ||
} | ||
return is; | ||
} | ||
//Lenght of the file | ||
int IntList::len(IntList *const Xs) | ||
{ | ||
return toInts(Xs)->size(); | ||
} | ||
|
||
std::wstring IntList::toString() | ||
{ | ||
return toInts(this)->toString(); | ||
} | ||
|
||
} | ||
|
||
#include "IntList.h" | ||
|
||
IntList::IntList(int head) { | ||
this->_head = head; | ||
_tail = NULL; | ||
} | ||
|
||
IntList::IntList(int X, IntList * Xs) { | ||
_head = X; | ||
_tail = Xs; | ||
} | ||
|
||
bool IntList::isEmpty(IntList *Xs) { | ||
return NULL == Xs; | ||
} | ||
|
||
int IntList::head(IntList *Xs) { | ||
return Xs->_head; | ||
} | ||
|
||
IntList * IntList::tail(IntList * Xs) { | ||
return Xs->_tail; | ||
} | ||
|
||
IntList * IntList::cons(int X, IntList *Xs) { | ||
return new IntList(X, Xs); | ||
} | ||
|
||
IntList *IntList::app(vector<int> xs, IntList * Ys) { | ||
IntList * Zs = Ys; | ||
for (int i = xs.size() - 1; i >= 0; i--) { | ||
Zs = cons(xs[i], Zs); | ||
} | ||
return Zs; | ||
} | ||
|
||
IntStack * IntList::toInts(IntList * Xs) { | ||
IntStack *is = new IntStack(); | ||
while (!isEmpty(Xs)) { | ||
is->push(head(Xs)); | ||
Xs = tail(Xs); | ||
} | ||
return is; | ||
} | ||
|
||
int IntList::len(IntList *Xs) { | ||
IntStack * is = toInts(Xs); | ||
int r = is->size(); | ||
delete is; | ||
return r; | ||
// return toInts(Xs).size(); | ||
} | ||
|
||
string IntList::toString() { | ||
IntStack * is = toInts(this); | ||
string s = is->toString(); | ||
delete is; | ||
return s; | ||
// return toInts(this).toString(); | ||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,32 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace iProlog | ||
{ | ||
class IntList | ||
{ | ||
|
||
private: | ||
const int head_Renamed; | ||
IntList *const tail_Renamed; | ||
|
||
public: | ||
virtual ~IntList() | ||
{ | ||
delete tail; | ||
} | ||
|
||
private: | ||
IntList(int const head); | ||
|
||
IntList(int const X, IntList *const Xs); | ||
|
||
public: | ||
static bool isEmpty(IntList *const Xs); | ||
|
||
static int head(IntList *const Xs); | ||
|
||
static constexpr IntList *empty = nullptr; | ||
|
||
static IntList *tail(IntList *const Xs); | ||
|
||
static IntList *cons(int const X, IntList *const Xs); | ||
|
||
static IntList *app(std::vector<int> &xs, IntList *const Ys); | ||
|
||
static IntStack *toInts(IntList *Xs); | ||
|
||
static int len(IntList *const Xs); | ||
|
||
std::wstring toString() override; | ||
}; | ||
|
||
} | ||
#ifndef __INTLIST_H__ | ||
#define __INTLIST_H__ | ||
|
||
#include <string> | ||
#include <vector> | ||
#include "IntStack.h" | ||
|
||
using namespace std; | ||
|
||
class IntList { | ||
|
||
private: | ||
|
||
int _head; | ||
IntList * _tail; | ||
|
||
IntList(int head); | ||
IntList(int X, IntList * Xs); | ||
|
||
public: | ||
|
||
static bool isEmpty(IntList *Xs); | ||
static int head(IntList *Xs); | ||
static IntList * tail(IntList * Xs); | ||
static IntList * cons(int X, IntList *Xs); | ||
static IntList * app(vector<int> xs, IntList * Ys); | ||
static IntStack * toInts(IntList * Xs); | ||
static int len(IntList *Xs); | ||
string toString(); | ||
}; | ||
|
||
#endif |