Users >> Pisto-UniCodesSourceCode
Pisto-Uni Codes Source Code

The different files listed here are associated with this page.

main.cpp

/*
uCodeBase3 - Shows the potential of universal codes using 89 trits
Copyright (C) 2009  Pier-Luc St-Onge <pl_so@hotmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "UniCode3.h"

int main(void)
{
	PureInt3().exec();
	FixedInt3().exec();
	PureFibo3().exec();
	FiboElias3().exec();
	PureLeven3().exec();

	char pause;
	scanf_s("%c", &pause);

	return 0;
}

UniCode3.h

/*
uCodeBase3 - Shows the potential of universal codes using 89 trits
Copyright (C) 2009  Pier-Luc St-Onge <pl_so@hotmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef UNICODE3_H
#define UNICODE3_H

#include <math.h>
#include <stdio.h>
#include <string>

using namespace std;

class UniCode3
{
public:
	void exec() const;

	static double decodeInt3(const string &code);

	static string encodeInt3(double val, size_t nb = 0);
	static string encodeInt3(long long val, size_t nb = 0);

	static size_t nbTrits(const string &code);

protected:
	virtual string decode(const string &code) const = 0;
	virtual string encode(const string &int3) const = 0;

	virtual string f(size_t nb, bool plusOne = false) const;
	virtual size_t maxNbIter() const = 0;
	virtual string title() const = 0;

};

class PureInt3: public UniCode3
{
protected:
	string decode(const string &code) const;
	string encode(const string &int3) const;

	size_t maxNbIter() const { return 89; }
	string title() const { return "PureInt3"; }

};

class FixedInt3: public UniCode3
{
protected:
	string decode(const string &code) const;
	string encode(const string &int3) const;

	size_t maxNbIter() const { return 84; }
	string title() const { return "FixedInt3Length"; }

};

class PureFibo3: public UniCode3
{
public:
	static double decodeFibo3(const string &fibo3);
	static string encodeFibo3(long long val);

protected:
	string decode(const string &code) const;
	string encode(const string &int3) const;

	string f(size_t nb, bool plusOne = false) const;
	size_t maxNbIter() const { return 87; }
	string title() const { return "SignedFibo3"; }

};

class FiboElias3: public UniCode3
{
protected:
	string decode(const string &code) const;
	string encode(const string &int3) const;

	size_t maxNbIter() const { return 82; }
	string title() const { return "FiboElias3"; }

};

class PureLeven3: public UniCode3
{
protected:
	string decode(const string &code) const;
	string encode(const string &int3) const;

	size_t maxNbIter() const { return 80; }
	string title() const { return "SignedLeven3"; }

};

#endif	// UNICODE3_H

UniCode3.cpp

/*
uCodeBase3 - Shows the potential of universal codes using 89 trits
Copyright (C) 2009  Pier-Luc St-Onge <pl_so@hotmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "UniCode3.h"

#define SHOWCODE code.c_str(), decodeInt3(decode(code)), nbTrits(code)

void UniCode3::exec() const
{
	printf("%s\nFunction\tCode\tValue\tNb trits\n", title().c_str());

	size_t nbMax = maxNbIter();
	string code = encode(f(0));
	printf("f(%d)\t%s\t%g\t%d\n", 0, SHOWCODE);

	for (size_t nb = 1; nb <= nbMax; nb++) {
		code = encode(f(nb));
		printf("f(%d)\t%s\t%g\t%d\n", nb, SHOWCODE);

		code = encode(f(nb, true));
		printf("f(%d)+1\t%s\t%g\t%d\n", nb, SHOWCODE);
	}
}

double UniCode3::decodeInt3(const string &code)
{
	double tritVal = 1, val = 0;

	for (size_t trit = 0; trit < code.size(); trit++) {
		if (code[trit] == 'N') val -= tritVal;
		if (code[trit] == 'P') val += tritVal;

		tritVal *= 3;
	}

	return val;
}

string UniCode3::encodeInt3(double val, size_t nb)
{
	const string n = (val > 0 ? "N" : "P"), p = (val > 0 ? "P" : "N"), z = "O";
	double abso = 2 * (val < 0 ? -val : val), tritVal = 1;
	string code;

	while (3 * tritVal < abso) tritVal *= 3;

	while (tritVal > 0.5) {
		if (abso > tritVal) {
			code.insert(0, p);
			abso -= 2 * tritVal;
		} else if (abso < -tritVal) {
			code.insert(0, n);
			abso += 2 * tritVal;
		} else {
			code.insert(0, z);
		}

		tritVal /= 3;
	}

	if (nb > code.size()) code.append(string(nb - code.size(), 'O'));

	return code;
}

string UniCode3::encodeInt3(long long val, size_t nb)
{
	const string n = (val > 0 ? "N" : "P"), p = (val > 0 ? "P" : "N"), z = "O";
	long long abso = 2 * (val < 0 ? -val : val), tritVal = 1;
	string code;

	while (3 * tritVal < abso) tritVal *= 3;

	while (tritVal > 0) {
		if (abso > tritVal) {
			code.insert(0, p);
			abso -= 2 * tritVal;
		} else if (abso < -tritVal) {
			code.insert(0, n);
			abso += 2 * tritVal;
		} else {
			code.insert(0, z);
		}

		tritVal /= 3;
	}

	if (nb > code.size()) code.append(string(nb - code.size(), 'O'));

	return code;
}

string UniCode3::f(size_t nb, bool plusOne) const
{
	if (nb == 0) return "O";
	return string(nb, plusOne ? 'N' : 'P') + (plusOne ? "P" : "");
}

size_t UniCode3::nbTrits(const string &code)
{
	size_t nb = 0;

	for (size_t ind = 0; ind < code.size(); ind++) {
		char trit = code[ind];

		if (trit == 'N' || trit == 'O' || trit == 'P') nb++;
	}

	return nb;
}

PureInt3.cpp - PureInt3

/*
uCodeBase3 - Shows the potential of universal codes using 89 trits
Copyright (C) 2009  Pier-Luc St-Onge <pl_so@hotmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "UniCode3.h"

string PureInt3::decode(const string &code) const
{
	return code;
}

string PureInt3::encode(const string &int3) const
{
	return int3;
}

FixedInt3.cpp - FixedInt3Length

/*
uCodeBase3 - Shows the potential of universal codes using 89 trits
Copyright (C) 2009  Pier-Luc St-Onge <pl_so@hotmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "UniCode3.h"

string FixedInt3::decode(const string &code) const
{
	if (code.size() < 6) { return "NaN"; }

	size_t length = (size_t)decodeInt3(code.substr(0, 5));

	if (code.size() != 6 + length) { return "NaN"; }
	if (length == 0) { return "O"; }

	return code.substr(6);
}

string FixedInt3::encode(const string &int3) const
{
	return encodeInt3((long long)int3.size(), 5) + " " + int3;
}

PureFibo3.cpp - SignedFibo3

/*
uCodeBase3 - Shows the potential of universal codes using 89 trits
Copyright (C) 2009  Pier-Luc St-Onge <pl_so@hotmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "UniCode3.h"

string PureFibo3::decode(const string &code) const
{
	if (code == "O") return code;
	return encodeInt3(decodeFibo3(code.substr(2)));
}

string PureFibo3::encode(const string &int3) const
{
	return int3;
}

string PureFibo3::f(size_t nb, bool plusOne) const
{
	if (nb == 0) return "O";

	string code("P ");

	if (plusOne) {
		for (size_t trit = 0; trit < nb; trit++) {
			code.append(1, 'N');
		}
	} else {
		for (size_t trit = 0; trit < nb; trit++) {
			code.append(1, (nb - trit) & 1 ? 'P' : 'O');
		}
	}

	code.append(plusOne ? 2 : 1, 'P');
	return code;
}

double PureFibo3::decodeFibo3(const string &fibo3)
{
	if (fibo3.size() < 2) return -1;

	bool prevIsPlus = false, valid = false;
	double prev = 0.5, cur = 1, eval = 0;

	for (int ind = 0; ind < (int)fibo3.size(); ind++) {
		if (fibo3[ind] == 'P') {
			if (prevIsPlus) {
				valid = true;
				break;
			}

			prevIsPlus = true;
			eval += cur;
		} else {
			prevIsPlus = false;
			if (fibo3[ind] == 'N') eval -= cur;
		}

		double next = 2 * (prev + cur);
		prev = cur;
		cur = next;
	}

	if (!valid) eval = -1;

	return eval;
}

string PureFibo3::encodeFibo3(long long val)
{
	if (val <= 0) return "NaFibo3";
	if (val == 1) return "PP";

	long long sumPrev = 1, prev = 1, cur = 3;

	while (val >= cur + 2 * prev - sumPrev) {
		long long next = 2 * (prev + cur);
		prev = cur;
		cur = next;
		sumPrev += prev;
	}

	string y = "P";

	while (cur > 0) {
		if (-val > sumPrev) {
			y = "N" + y;
			val += cur;
		} else if (val < cur - sumPrev) {
			y = "O" + y;
		} else {
			y = "P" + y;
			val -= cur;
		}

		long long pre_prev = (cur - 2 * prev) / 2;
		sumPrev -= prev;
		cur = prev;
		prev = pre_prev;
	}

	return y;
}

FiboElias3.cpp - FiboElias3

/*
uCodeBase3 - Shows the potential of universal codes using 89 trits
Copyright (C) 2009  Pier-Luc St-Onge <pl_so@hotmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "UniCode3.h"

string FiboElias3::decode(const string &code) const
{
	size_t length = (size_t)PureFibo3::decodeFibo3(code);

	return code.substr(code.size() - length);
}

string FiboElias3::encode(const string &int3) const
{
	return PureFibo3::encodeFibo3(int3.size()) + " " + int3;
}

PureLeven3.cpp - SignedLeven3

/*
uCodeBase3 - Shows the potential of universal codes using 89 trits
Copyright (C) 2009  Pier-Luc St-Onge <pl_so@hotmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "UniCode3.h"

string PureLeven3::decode(const string &code) const
{
	size_t h = 0;

	while (h < code.size() && code[h] != 'O') h++;
	if (h == code.size()) return "NaN";

	char mst = 'O';
	string int3 = string(1, mst);
	size_t nextT = h + 1;

	while (h > 0) {
		if (mst == 'N') int3.append("P");

		double toReadF = decodeInt3(int3);
		size_t toRead = (size_t)(toReadF <= 90 ? toReadF : 90);
		if (nextT + toRead > code.size()) return "NaN";

		mst = code[--h];
		int3 = code.substr(nextT, toRead) + string(1, mst);
		nextT += toRead + 1;
	}

	return int3;
}

string PureLeven3::encode(const string &int3) const
{
	if (int3.empty()) { return "NaN"; }

	string header, tmp3(int3), body;
	size_t last = tmp3.size() - 1;

	if (last > 0 && tmp3[last] == 'O') { return "NaN"; }

	while (tmp3[last] != 'O') {
		header.append(1, tmp3[last]);
		body = (last > 0 ? " " : "") + tmp3.substr(0, last) + body;

		tmp3 = encodeInt3((long long)last);
		last = tmp3.size() - 1;

		if (last > 0 && tmp3[last] == 'P' && tmp3[last - 1] == 'N') {
			tmp3.erase(last--);
		}
	}

	return header + tmp3 + body;
}
Page last modified on November 14, 2009, at 04:59 AM

  © 2023 TERNARY RESEARCH CORPORATION All rights reserved. Users' works are copyrighted by their respective authors.