diff --git a/README.md b/README.md
index e018148..3040962 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,8 @@
-Рабочий проект по изучению и анализу разнообразных алгоритмов.
+Весь этот проект - это сборка, растянутая во времени и пространстве, реализаций и модернизаций разнообразных алгоритмов.
+Папка tmp - разный мусор/заметки/фигзнает, который когда-то будет разобран.
+Особником стоит папка tasks. Это сборище задачь, которые мне показались интересными или просто что-то что может показать применение рассмотренных алгоритмов.
+
+Для запуска всего того что хочется, нужно просто воспользоваться bin/main.cpp
diff --git a/bin/main.cpp b/bin/main.cpp
index 76ce514..c5abc24 100644
--- a/bin/main.cpp
+++ b/bin/main.cpp
@@ -1,18 +1,8 @@
#include
#include "tasks/001.hpp"
-// #include "sort/insertion.hpp"
-
auto main() -> int
{
- // {
- // std::vector v { 5, 4, 1, 3, 6, 9, 7, 2, 8, 0, 10 };
- // // alg::sort::insertion(v, 0, v.size() - 1);
- // alg::sort::insertion(v);
- // hack::log()(v);
- // return 0;
- // }
-
- alg::tasks::run();
+ // alg::tasks::run_001();
}
diff --git a/src/search/grphs/dept.hpp b/src/search/grphs/dept.hpp
deleted file mode 100644
index 816a3ec..0000000
--- a/src/search/grphs/dept.hpp
+++ /dev/null
@@ -1,7 +0,0 @@
-#pragma once
-
-
-namespace alg::search
-{
-
-}
diff --git a/src/tasks/001.hpp b/src/tasks/001.hpp
index 385191f..0504906 100644
--- a/src/tasks/001.hpp
+++ b/src/tasks/001.hpp
@@ -7,9 +7,9 @@
namespace alg::tasks
{
// Есть набор чисел, необходимо из них создать такую перестановку
- // при котором разности их величин будутминимально отличаться.
+ // при котором разности их величин будут минимально отличаться.
// Т.е. как бы максимально их уравнять
- inline void run()
+ inline void run_001()
{
// { x, y }
// x - число
diff --git a/src/tmp/ImplementingUsefulAlgorithms/AutoRegressionTest/test.cpp b/src/tmp/ImplementingUsefulAlgorithms/AutoRegressionTest/test.cpp
new file mode 100644
index 0000000..4e0e1df
--- /dev/null
+++ b/src/tmp/ImplementingUsefulAlgorithms/AutoRegressionTest/test.cpp
@@ -0,0 +1,43 @@
+#include "../Utils/UtilsTestAuto.h"
+#include "../Sorting/SortTestAuto.h"
+#include "../RandomTreap/DynamicSortedSequenceTestAuto.h"
+#include "../HashTable/HashTableTestAuto.h"
+#include "../Heaps/HeapTestAuto.h"
+#include "../Graphs/GraphsTestAuto.h"
+#include "../ExternalMemoryAlgorithms/ExternalMemoryAlgorithmsTestAuto.h"
+#include "../StringAlgorithms/StringAlgorithmsTestAuto.h"
+#include "../Compression/CompressionTestAuto.h"
+#include "../MiscAlgs/MiscAlgsTestAuto.h"
+#include "../Optimization/OptTestAuto.h"
+#include "../LargeNumbers/LargeNumberTestAuto.h"
+#include "../ComputationalGeometry/ComputationalGeometryTestAuto.h"
+#include "../ErrorCorrectingCodes/ErrorCorrectingCodesTestAuto.h"
+#include "../Cryptography/CryptographyTestAuto.h"
+#include "../NumericalMethods/NumericalMethodsTestAuto.h"
+#include "../FinancialCalculations/FinancialCalculationsTestAuto.h"
+
+using namespace igmdk;
+
+int main()
+{
+ DEBUG("All Tests Auto");
+ testAllAutoUtils();
+ testAllAutoSort();
+ testAllAutoDynamicSortedSequence();
+ testAllAutoHashTable();
+ testAllAutoHeaps();
+ testAllAutoGraphs();
+ testAllAutoExternalMemoryAlgorithms();
+ testAllAutoStringAlgorithms();
+ testAllAutoCompression();
+ testAllAutoMiscAlgorithms();
+ testAllAutoOpt();
+ testAllAutoComputationalGeometry();
+ testAllAutoErrorCorrectingCodes();
+ testAllAutoCryptography();
+ testAllAutoNumericalMethods();
+ testAllAutoFinancialCalculations();
+ DEBUG("All Tests Auto passed");
+
+ return 0;
+}
diff --git a/src/tmp/ImplementingUsefulAlgorithms/Compression/Compression.h b/src/tmp/ImplementingUsefulAlgorithms/Compression/Compression.h
new file mode 100644
index 0000000..eebe749
--- /dev/null
+++ b/src/tmp/ImplementingUsefulAlgorithms/Compression/Compression.h
@@ -0,0 +1,130 @@
+#ifndef IGMDK_COMPRESSION_H
+#define IGMDK_COMPRESSION_H
+#include "../StringAlgorithms/SuffixArray.h"
+#include "Stream.h"
+#include "StaticCodes.h"
+#include "HuffmanTree.h"
+#include "LZW.h"
+#include
+namespace igmdk{
+
+enum {RLE_E1 = (1 << numeric_limits::digits) - 1,
+ RLE_E2 = RLE_E1 - 1};
+Vector RLECompress(Vector const& byteArray)
+{
+ Vector result;
+ for(int i = 0; i < byteArray.getSize();)
+ {
+ unsigned char byte = byteArray[i++];
+ result.append(byte);
+ int count = 0;
+ while(count < RLE_E2 - 1 && i + count < byteArray.getSize() &&
+ byteArray[i + count] == byte) ++count;
+ if(count > 1 || (byte == RLE_E1 && count == 1))
+ {
+ result.append(RLE_E1);
+ result.append(count);
+ i += count;
+ }
+ else if(byte == RLE_E1) result.append(RLE_E2);
+ }
+ return result;
+}
+Vector RLEUncompress(Vector const& byteArray)
+{
+ Vector result;
+ for(int i = 0; i < byteArray.getSize();)
+ {
+ unsigned char byte = byteArray[i++];
+ if(byte == RLE_E1 && byteArray[i] != RLE_E1)
+ {
+ unsigned char count = byteArray[i++];
+ if(count == RLE_E2) count = 1;
+ else byte = result.lastItem();//need temp if vector reallocates
+ while(count--) result.append(byte);
+ }
+ else result.append(byte);
+ }
+ return result;
+}
+
+Vector MoveToFrontTransform(bool compress,
+ Vector const& byteArray)
+{
+ unsigned char list[1 << numeric_limits::digits], j, letter;
+ for(int i = 0; i < sizeof(list); ++i) list[i] = i;
+ Vector resultArray;
+ for(int i = 0; i < byteArray.getSize(); ++i)
+ {
+ if(compress)
+ {//find and output rank
+ j = 0;
+ letter = byteArray[i];
+ while(list[j] != letter) ++j;
+ resultArray.append(j);
+ }
+ else
+ {//rank to byte
+ j = byteArray[i];
+ letter = list[j];
+ resultArray.append(letter);
+ }//move list back to make space for front item
+ for(; j > 0; --j) list[j] = list[j - 1];
+ list[0] = letter;
+ }
+ return resultArray;
+}
+
+Vector BurrowsWheelerTransform(
+ Vector const& byteArray)
+{
+ int original = 0, size = byteArray.getSize();
+ Vector BTWArray = suffixArray(byteArray.getArray(), size);
+ Vector result;
+ for(int i = 0; i < size; ++i)
+ {
+ int suffixIndex = BTWArray[i];
+ if(suffixIndex == 0)
+ {//found the original string
+ original = i;
+ suffixIndex = size;//avoid the % size in next step
+ }
+ result.append(byteArray[suffixIndex - 1]);
+ }//assume that 4 bytes is enough
+ Vector code = ReinterpretEncode(original, 4);
+ for(int i = 0; i < code.getSize(); ++i) result.append(code[i]);
+ return result;
+}
+
+Vector BurrowsWheelerReverseTransform(
+ Vector const& byteArray)
+{
+ enum{M = 1 << numeric_limits::digits};
+ int counts[M], firstPositions[M], textSize = byteArray.getSize() - 4;
+ for(int i = 0; i < M; ++i) counts[i] = 0;
+ Vector ranks(textSize);//compute ranks
+ for(int i = 0; i < textSize; ++i) ranks[i] = counts[byteArray[i]]++;
+ firstPositions[0] = 0;//compute first positions
+ for(int i = 0; i < M - 1; ++i)
+ firstPositions[i + 1] = firstPositions[i] + counts[i];
+ Vector index, result(textSize);//extract original rotation
+ for(int i = 0; i < 4; ++i) index.append(byteArray[i + textSize]);
+ //construct in reverse order
+ for(int i = textSize - 1, ix = ReinterpretDecode(index); i >= 0; --i)
+ ix = ranks[ix] + firstPositions[result[i] = byteArray[ix]];
+ return result;
+}
+
+Vector BWTCompress(Vector const& byteArray)
+{
+ return HuffmanCompress(RLECompress(MoveToFrontTransform(true,
+ BurrowsWheelerTransform(byteArray))));
+}
+Vector BWTUncompress(Vector const& byteArray)
+{
+ return BurrowsWheelerReverseTransform(MoveToFrontTransform(false,
+ RLEUncompress(HuffmanUncompress(byteArray))));
+}
+
+}//end namespace
+#endif
diff --git a/src/tmp/ImplementingUsefulAlgorithms/Compression/CompressionTestAuto.h b/src/tmp/ImplementingUsefulAlgorithms/Compression/CompressionTestAuto.h
new file mode 100644
index 0000000..d3da7d8
--- /dev/null
+++ b/src/tmp/ImplementingUsefulAlgorithms/Compression/CompressionTestAuto.h
@@ -0,0 +1,80 @@
+#ifndef IGMDK_COMPRESSION_TEST_AUTO_H
+#define IGMDK_COMPRESSION_TEST_AUTO_H
+#include
+using namespace std;
+#include "Compression.h"
+
+namespace igmdk{
+
+void testGammaCodeAuto()
+{
+ DEBUG("testGammaCodeAuto");
+ BitStream result;
+ for(int i = 1; i < 1000; ++i) GammaEncode(i, result);
+ for(int i = 1; i < 1000; ++i) assert(GammaDecode(result) == i);
+ DEBUG("testGammaCodeAuto passed");
+}
+
+void testFibonacciCodeAuto()
+{
+ DEBUG("testFibonacciCodeAuto");
+ BitStream result;
+ for(int i = 1; i < 1000; ++i) FibonacciEncode(i, result);
+ for(int i = 1; i < 1000; ++i) assert(FibonacciDecode(result) == i);
+ DEBUG("testFibonacciCodeAuto passed");
+}
+
+void testByteCodeAuto()
+{
+ DEBUG("testGammaCodeAuto");
+ BitStream result;
+ for(int i = 0; i < 1000; ++i) byteEncode(i, result);
+ for(int i = 0; i < 1000; ++i) assert(byteDecode(result) == i);
+ DEBUG("testGammaCodeAuto passed");
+}
+
+Vector getRandomBytes(int n = 10000)
+{
+ Vector w(n, 0);
+ for(int i = 0; i < n; ++i) w[i] = GlobalRNG().next();
+ return w;
+}
+void testBWTCompressAuto()
+{
+ DEBUG("testBWTCompressAuto");
+ Vector byteArray = getRandomBytes();
+ assert(byteArray == BWTUncompress(BWTCompress(byteArray)));
+ DEBUG("testBWTCompressAuto passed");
+}
+
+void testLZWAuto()
+{
+ DEBUG("testLZWAuto");
+ Vector byteArray = getRandomBytes(), code;
+ {
+ BitStream in(byteArray);
+ BitStream out;
+ LZWCompress(in, out);
+ code = ExtraBitsCompress(out.bitset);
+ }
+ {
+ BitStream in(ExtraBitsUncompress(code));
+ BitStream out;
+ LZWUncompress(in, out);
+ assert(byteArray == out.bitset.getStorage());
+ }
+ DEBUG("testLZWAuto passed");
+}
+
+void testAllAutoCompression()
+{
+ DEBUG("testAllAutoCompression");
+ testGammaCodeAuto();
+ testFibonacciCodeAuto();
+ testByteCodeAuto();
+ testBWTCompressAuto();
+ testLZWAuto();
+}
+
+}//end namespace
+#endif
diff --git a/src/tmp/ImplementingUsefulAlgorithms/Compression/Compressor.cpp b/src/tmp/ImplementingUsefulAlgorithms/Compression/Compressor.cpp
new file mode 100644
index 0000000..e10b21c
--- /dev/null
+++ b/src/tmp/ImplementingUsefulAlgorithms/Compression/Compressor.cpp
@@ -0,0 +1,126 @@
+#include "../ExternalMemoryAlgorithms/File.h"
+#include "../ExternalMemoryAlgorithms/CSV.h"
+#include "../Utils/Debug.h"
+#include
+#include "Compression.h"
+using namespace std;
+
+using namespace igmdk;
+
+int compressor(File& in, File&out, bool compress, string const& smethod)
+{
+ char method;
+ enum{HUF, BWT, LZW};
+ if(smethod == "Huffman") method = HUF;
+ else if(smethod == "BWT") method = BWT;
+ else if(smethod == "LZW") method = LZW;
+ else{DEBUG("Method Unknown"); return 0;}
+
+ enum{N = 8096};
+ unsigned char buffer[N];
+ Vector original, v;
+ for(;;)
+ {
+ int size = min(N, in.bytesToEnd());
+ in.read(buffer, size);
+ for(int i = 0; i < size; ++i)
+ {
+ original.append(buffer[i]);
+ }
+ if(size < N) break;
+ }
+ if(compress)
+ {
+ if(method == LZW)
+ {
+ BitStream result;
+ BitStream in(original);
+ LZWCompress(in, result);
+ v = ExtraBitsCompress(result.bitset);
+ }
+ else if(method == BWT)
+ {
+ v = BWTCompress(original);
+ }
+ else if(method == HUF)
+ {
+ v = HuffmanCompress(original);
+ }
+ }
+ else
+ {
+ if(method == LZW)
+ {
+ BitStream in(ExtraBitsUncompress(original));
+ BitStream result;
+ LZWUncompress(in, result);
+ v = result.bitset.getStorage();
+ }
+ else if(method == BWT)
+ {
+ v = BWTUncompress(original);
+ }
+ else if(method == HUF)
+ {
+ v = HuffmanUncompress(original);
+ }
+ }
+ out.append(v.getArray(), v.getSize());
+ return out.getSize();
+}
+
+void testAllMethods()
+{
+ //AAR decomp has bug for all
+ string methods[] = {"Huf", "BWT", "LZW"}, files[] = {"a.txt", "bible.txt",
+ "dickens.txt", "ecoli.txt", "mobydick.txt", "pi10mm.txt",//
+ "world192.txt"};
+ Vector > matrix;
+ Vector titles;
+ titles.append("File");
+ titles.append("Size");
+ for(int j = 0; j < sizeof(methods)/sizeof(methods[0]); ++j)
+ titles.append(methods[j]);
+ matrix.append(titles);
+ for(int i = 0; i < sizeof(files)/sizeof(files[0]); ++i)
+ {
+ File in(files[i].c_str(), false);
+ Vector row;
+ DEBUG(files[i]);
+ row.append(files[i]);
+ int oriSize = in.getSize();
+ row.append(to_string(oriSize));
+ for(int j = 0; j < sizeof(methods)/sizeof(methods[0]); ++j)
+ {
+ in.setPosition(0);
+ DEBUG(methods[j]);
+ int size;
+ string outName = files[i] + "." + methods[j],
+ backName = outName + ".ori";
+ {
+ File out(outName.c_str(), true);
+ int start = clock();
+ size = compressor(in, out, true, methods[j]);
+ row.append(to_string(size));
+ int elapsed = clock()-start;
+ }
+ {
+ File out(outName.c_str(), false), back(backName.c_str(), true);
+ int start = clock();
+ int size2 = compressor(out, back, false, methods[j]);
+ assert(oriSize == size2);
+ int elapsed = clock()-start;
+ }
+ File::remove(outName.c_str());
+ File::remove(backName.c_str());
+ }
+ matrix.append(row);
+ }
+ createCSV(matrix, "CompressionResult.csv");
+}
+
+int main(int argc, char *argv[])
+{
+ testAllMethods();
+ return 0;
+}
diff --git a/src/tmp/ImplementingUsefulAlgorithms/Compression/HuffmanTree.h b/src/tmp/ImplementingUsefulAlgorithms/Compression/HuffmanTree.h
new file mode 100644
index 0000000..daa6c44
--- /dev/null
+++ b/src/tmp/ImplementingUsefulAlgorithms/Compression/HuffmanTree.h
@@ -0,0 +1,125 @@
+#ifndef IGMDK_HUFFMAN_TREE_H
+#define IGMDK_HUFFMAN_TREE_H
+#include "../Heaps/Heap.h"
+#include "Stream.h"
+#include "StaticCodes.h"
+#include "../Utils/GCFreeList.h"
+#include
+namespace igmdk{
+
+struct HuffmanTree
+{
+ enum{W = numeric_limits::digits, N = 1 << W};
+ struct Node
+ {
+ unsigned char letter;
+ int count;
+ Node *left, *right;
+ Node(int theCount, Node* theLeft, Node* theRight,
+ unsigned char theLetter): left(theLeft), right(theRight),
+ count(theCount), letter(theLetter) {}
+ bool operator<(Node const& rhs)const{return count < rhs.count;}
+ void traverse(Bitset* codebook,
+ Bitset& currentCode)
+ {
+ if(left)//internal node
+ {
+ currentCode.append(false);//went left
+ left->traverse(codebook, currentCode);
+ currentCode.removeLast();
+ currentCode.append(true);//went right
+ right->traverse(codebook, currentCode);
+ currentCode.removeLast();
+ }
+ else codebook[letter] = currentCode;//leaf
+ }
+ void append(Bitset& result)
+ {
+ result.append(!left);//0 for nonleaf, 1 for leaf
+ if(left)
+ {
+ left->append(result);
+ right->append(result);
+ }
+ else result.appendValue(letter, W);
+ }
+ }* root;
+ Freelist f;
+
+ HuffmanTree(Vector const& byteArray)
+ {//calculate frequencies
+ int counts[N];
+ for(int i = 0; i < N; ++i) counts[i] = 0;
+ for(int i = 0; i < byteArray.getSize(); ++i) ++counts[byteArray[i]];
+ //create leaf nodes
+ Heap > queue;
+ for(int i = 0; i < N; ++i) if(counts[i] > 0) queue.insert(
+ new(f.allocate())Node(counts[i], 0, 0, i));
+ //merge leaf nodes to create the tree
+ while(queue.getSize() > 1)//until forest merged
+ {
+ Node *first = queue.deleteMin(), *second = queue.deleteMin();
+ queue.insert(new(f.allocate())
+ Node(first->count + second->count, first, second, 0));
+ }
+ root = queue.getMin();
+ }
+
+ Node* readHuffmanTree(BitStream& text)
+ {
+ Node *left = 0, *right = 0;
+ unsigned char letter;
+ if(text.readBit()) letter = text.readValue(W);//got to a leaf
+ else
+ {//process internal nodes recursively
+ left = readHuffmanTree(text);
+ right = readHuffmanTree(text);
+ }
+ return new(f.allocate())Node(0, left, right, letter);
+ }
+ HuffmanTree(BitStream& text){root = readHuffmanTree(text);}
+
+ void writeTree(Bitset& result){root->append(result);}
+ void populateCodebook(Bitset* codebook)
+ {
+ Bitset temp;
+ root->traverse(codebook, temp);
+ }
+
+ Vector decode(BitStream& text)
+ {//wrong bits will give wrong result, but not a crash
+ Vector result;
+ for(Node* current = root;;
+ current = text.readBit() ? current->right : current->left)
+ {
+ if(!current->left)
+ {
+ result.append(current->letter);
+ current = root;
+ }
+ if(!text.bitsLeft()) break;
+ }
+ return result;
+ }
+};
+
+Vector HuffmanCompress(Vector const& byteArray)
+{
+ HuffmanTree tree(byteArray);
+ Bitset codebook[HuffmanTree::N], result;
+ tree.populateCodebook(codebook);
+ tree.writeTree(result);
+ for(int i = 0; i < byteArray.getSize(); ++i)
+ result.appendBitset(codebook[byteArray[i]]);
+ return ExtraBitsCompress(result);
+}
+
+Vector HuffmanUncompress(Vector const& byteArray)
+{
+ BitStream text(ExtraBitsUncompress(byteArray));
+ HuffmanTree tree(text);
+ return tree.decode(text);
+}
+
+}//end namespace
+#endif
diff --git a/src/tmp/ImplementingUsefulAlgorithms/Compression/LZW.h b/src/tmp/ImplementingUsefulAlgorithms/Compression/LZW.h
new file mode 100644
index 0000000..3426855
--- /dev/null
+++ b/src/tmp/ImplementingUsefulAlgorithms/Compression/LZW.h
@@ -0,0 +1,64 @@
+#ifndef IGMDK_LZW_H
+#define IGMDK_LZW_H
+#include "../RandomTreap/Trie.h"
+#include "Stream.h"
+#include
+namespace igmdk{
+
+void LZWCompress(BitStream& in, BitStream& out, int maxBits = 16)
+{
+ assert(in.bytesLeft());
+ byteEncode(maxBits, out);//store as config
+ TernaryTreapTrie dictionary;
+ TernaryTreapTrie::Handle h;
+ int n = 0;
+ while(n < (1 << numeric_limits::digits))
+ {//initialize with all bytes
+ unsigned char letter = n;
+ dictionary.insert(&letter, 1, n++);
+ }
+ Vector word;
+ while(in.bytesLeft())
+ {
+ unsigned char c = in.readByte();
+ word.append(c);
+ //if found keep appending
+ if(!dictionary.findIncremental(word.getArray(), word.getSize(), h))
+ {//word without the last byte guaranteed to be in the dictionary
+ out.writeValue(*dictionary.find(word.getArray(),
+ word.getSize() - 1), lgCeiling(n));
+ if(n < twoPower(maxBits))//add new word if have space
+ dictionary.insert(word.getArray(), word.getSize(), n++);
+ word = Vector(1, c);//set to read byte
+ }
+ }
+ out.writeValue(*dictionary.find(word.getArray(), word.getSize()),
+ lgCeiling(n));
+}
+
+void LZWUncompress(BitStream& in, BitStream& out)
+{
+ int maxBits = byteDecode(in), size = twoPower(maxBits), n = 0,
+ lastIndex = -1;
+ assert(maxBits >= numeric_limits::digits);
+ Vector > dictionary(size);
+ for(; n < (1 << numeric_limits::digits); ++n)
+ dictionary[n].append(n);
+ while(in.bitsLeft())
+ {
+ int index = in.readValue(lastIndex == -1 ? 8 :
+ min(maxBits, lgCeiling(n + 1)));
+ if(lastIndex != -1 && n < size)
+ {
+ Vector word = dictionary[lastIndex];
+ word.append((index == n ? word : dictionary[index])[0]);
+ dictionary[n++] = word;
+ }
+ for(int i = 0; i < dictionary[index].getSize(); ++i)
+ out.writeByte(dictionary[index][i]);
+ lastIndex = index;
+ }
+}
+
+}//end namespace
+#endif
diff --git a/src/tmp/ImplementingUsefulAlgorithms/Compression/README.txt b/src/tmp/ImplementingUsefulAlgorithms/Compression/README.txt
new file mode 100644
index 0000000..c3d43e9
--- /dev/null
+++ b/src/tmp/ImplementingUsefulAlgorithms/Compression/README.txt
@@ -0,0 +1,10 @@
+Please download test files into this directory from http://introcs.cs.princeton.edu/java/data/
+
+bible.txt
+dickens.txt
+ecoli.txt
+mobydick.txt
+pi10mm.txt
+world192.txt
+
+These are for use with Compressor.cpp. Other files can be used as well.
\ No newline at end of file
diff --git a/src/tmp/ImplementingUsefulAlgorithms/Compression/StaticCodes.h b/src/tmp/ImplementingUsefulAlgorithms/Compression/StaticCodes.h
new file mode 100644
index 0000000..d4ec4e7
--- /dev/null
+++ b/src/tmp/ImplementingUsefulAlgorithms/Compression/StaticCodes.h
@@ -0,0 +1,116 @@
+#ifndef IGMDK_STATIC_CODES_H
+#define IGMDK_STATIC_CODES_H
+#include "Stream.h"
+#include
+namespace igmdk{
+
+Vector ExtraBitsCompress(Bitset const& bitset)
+{
+ assert(bitset.getSize() > 0);//makes no sense otherwise
+ Vector result = bitset.getStorage();
+ result.append(bitset.garbageBits());
+ return result;
+}
+Bitset ExtraBitsUncompress(Vector byteArray)
+{
+ assert(byteArray.getSize() > 1 && byteArray.lastItem() < BitStream::B);
+ int garbageBits = byteArray.lastItem();
+ byteArray.removeLast();
+ Bitset result(byteArray);
+ while(garbageBits--) result.removeLast();
+ return result;
+}
+
+void byteEncode(unsigned long long n, BitStream& result)
+{
+ enum{M05 = 1 << (numeric_limits::digits - 1)};
+ do
+ {
+ unsigned char r = n % M05;
+ n /= M05;
+ if(n) r += M05;
+ result.writeByte(r);
+ }while(n);
+}
+unsigned long long byteDecode(BitStream& stream)
+{
+ unsigned long long n = 0, base = 1;
+ enum{M05 = 1 << (numeric_limits::digits - 1)};
+ for(;; base *= M05)
+ {
+ unsigned char code = stream.readByte(), value = code % M05;
+ n += base * value;
+ if(value == code) break;
+ }
+ return n;
+}
+
+void UnaryEncode(int n, BitStream& result)
+{
+ while(n--) result.writeBit(true);
+ result.writeBit(false);
+}
+int UnaryDecode(BitStream& code)
+{
+ int n = 0;
+ while(code.readBit()) ++n;
+ return n;
+}
+
+void GammaEncode(unsigned long long n, BitStream& result)
+{
+ assert(n > 0);
+ int N = lgFloor(n);
+ UnaryEncode(N, result);
+ if(N > 0) result.writeValue(n - twoPower(N), N);
+}
+unsigned long long GammaDecode(BitStream& code)
+{
+ int N = UnaryDecode(code);
+ return twoPower(N) + (N > 0 ? code.readValue(N) : 0);
+}
+
+void advanceFib(unsigned long long& f1, unsigned long long& f2)
+{
+ unsigned long long temp = f2;
+ f2 += f1;
+ f1 = temp;
+}
+void FibonacciEncode(unsigned long long n, BitStream& result)
+{
+ assert(n > 0);
+ //find largest fib number f1 <= n
+ unsigned long long f1 = 1, f2 = 2;
+ while(f2 <= n) advanceFib(f1, f2);
+ //mark the numbers from highest to lowest
+ Bitset reverse;
+ while(f2 > 1)
+ {
+ reverse.append(n >= f1);
+ if(n >= f1) n -= f1;
+ unsigned long long temp = f1;
+ f1 = f2 - f1;
+ f2 = temp;
+ }//change order to lowest to highest and add terminator
+ reverse.reverse();
+ result.bitset.appendBitset(reverse);
+ result.writeBit(true);
+}
+unsigned long long FibonacciDecode(BitStream& code)
+{
+ unsigned long long n = 0, f1 = 1, f2 = 2;
+ for(bool prevBit = false;; advanceFib(f1, f2))
+ {//add on the next Fibonacci number until see 11
+ bool bit = code.readBit();
+ if(bit)
+ {
+ if(prevBit) break;
+ n += f1;
+ }
+ prevBit = bit;
+ }
+ return n;
+}
+
+}//end namespace
+#endif
diff --git a/src/tmp/ImplementingUsefulAlgorithms/Compression/Stream.h b/src/tmp/ImplementingUsefulAlgorithms/Compression/Stream.h
new file mode 100644
index 0000000..41c5015
--- /dev/null
+++ b/src/tmp/ImplementingUsefulAlgorithms/Compression/Stream.h
@@ -0,0 +1,75 @@
+#ifndef IGMDK_STREAM_H
+#define IGMDK_STREAM_H
+#include "../Utils/Bitset.h"
+#include "../Utils/Vector.h"
+namespace igmdk{
+
+Vector ReinterpretEncode(unsigned long long n, int size)
+{
+ assert(size > 0);
+ enum{M = 1 << numeric_limits::digits};
+ Vector result;
+ while(size-- > 0)
+ {
+ result.append(n % M);
+ n /= M;
+ }
+ return result;
+}
+unsigned long long ReinterpretDecode(Vector const& code)
+{
+ assert(code.getSize() > 0);
+ unsigned long long n = 0, base = 1;
+ enum{M = 1 << numeric_limits::digits};
+ for(int i = 0; i < code.getSize(); ++i)
+ {
+ n += base * code[i];
+ base *= M;
+ }
+ return n;
+}
+
+struct Stream
+{
+ unsigned long long position;
+ Stream(): position(0) {}
+};
+struct BitStream : public Stream
+{
+ Bitset bitset;//unsigned char for portability
+ enum{B = numeric_limits::digits};
+ BitStream() {}
+ BitStream(Bitset const& aBitset): bitset(aBitset) {}
+ BitStream(Vector const& vector): bitset(vector) {}
+ void writeBit(bool value){bitset.append(value);}
+ bool readBit()
+ {
+ assert(bitsLeft());
+ return bitset[position++];
+ }
+ void writeByte(unsigned char byte){writeValue(byte, B);}
+ void writeBytes(Vector const& bytes)
+ {for(int i = 0; i < bytes.getSize(); ++i) writeByte(bytes[i]);}
+ unsigned char readByte(){return readValue(B);}
+ Vector readBytes(int n)
+ {
+ assert(n <= bytesLeft());
+ Vector result(n);
+ for(int i = 0; i < n; ++i) result[i] = readByte();
+ return result;
+ }
+ void debug()const{bitset.debug();}
+ void writeValue(unsigned long long value, int bits)
+ {bitset.appendValue(value, bits);}
+ unsigned long long readValue(int bits)
+ {
+ assert(bits <= bitsLeft());
+ position += bits;
+ return bitset.getValue(position - bits, bits);
+ }
+ unsigned long long bitsLeft()const{return bitset.getSize() - position;}
+ unsigned long long bytesLeft()const{return bitsLeft()/B;}
+};
+
+}//end namespace
+#endif
diff --git a/src/tmp/ImplementingUsefulAlgorithms/Compression/test.cpp b/src/tmp/ImplementingUsefulAlgorithms/Compression/test.cpp
new file mode 100644
index 0000000..be21ac3
--- /dev/null
+++ b/src/tmp/ImplementingUsefulAlgorithms/Compression/test.cpp
@@ -0,0 +1,43 @@
+#include "Compression.h"
+#include "CompressionTestAuto.h"
+#include
+using namespace igmdk;
+
+void compress(Vector const& byteArray)
+{
+ BitStream result;
+ FibonacciEncode(1597, result);//first large value that loses to byte code
+ result.bitset.debug();
+ DEBUG(FibonacciDecode(result));
+
+ GammaEncode(32, result);
+ result.bitset.debug();
+ DEBUG(GammaDecode(result));
+
+ byteEncode(128 * 128, result);
+ result.bitset.debug();
+ DEBUG(byteDecode(result));
+
+ HuffmanTree HuffTree(byteArray);
+ Vector MTF_Mississippi = MoveToFrontTransform(true, byteArray);
+
+ cout << "breakpoint" << endl;//if have to recompute HUFFMAN do MISSISSIPPI not "large ascii text"
+}
+
+void timeRT()
+{
+ char text[] = //"abbabab";
+ "mississippi";
+ Vector uncompressed;
+ for(int i = 0; i < sizeof(text)-1; ++i) uncompressed.append(text[i]);
+
+ cout << "uncompressed.size" << uncompressed.getSize() << endl;
+ compress(uncompressed);
+}
+
+int main()
+{
+ timeRT();//fail Huffman has bug???
+ testAllAutoCompression();
+ return 0;
+}
diff --git a/src/tmp/ImplementingUsefulAlgorithms/ComputationalGeometry/BuildingArea.h b/src/tmp/ImplementingUsefulAlgorithms/ComputationalGeometry/BuildingArea.h
new file mode 100644
index 0000000..9f08436
--- /dev/null
+++ b/src/tmp/ImplementingUsefulAlgorithms/ComputationalGeometry/BuildingArea.h
@@ -0,0 +1,52 @@
+#ifndef IGMDK_BUILDING_AREA_H
+#define IGMDK_BUILDING_AREA_H
+
+#include