#include <iostream>
#include <fstream>
#include <curses.h>
#include <algorithm>
using namespace std;
int search(int); // FUNCTION (CONSEC_NUM) ---------> INDEX
long sum(int[], int[], int);
void display(); // DISPLAY STUFF AS PROGRAM RUNS
void reorder();
long bits = 0; // KEEP TRACK OF TOTAL BITS READ
bool match;
int consec = 0, alt = 0;
char bit, bit_prev;
int r[2][200], n[2][200], MAX[2];
int main()
{
ifstream in("/home/cups/randombin.txt");
bit_prev = in.get(); // GRAB INITIAL BIT
consec = 1; // SET CONSEC NUMBER (for bit type of initial bit)
while (in.good()){
bit = in.get(); // GRAB NEXT BIT
match = (bit == bit_prev); // CURRENT BIT == PREVIOUS BIT ?
if (match){ // IF: YES
consec++; //-----> INCREMENT CONSEC NUMBER
}
else{ // IF: NO
int bit_type = bit_prev - '0'; //
int index = search(bit_type); //-----> SEE IF THERE ALREADY IS A RECORDED INSTANCE OF CONSEC NUMBER OF BIT_TYPE
n[bit_type][index]++; //-----> INCREMENT THE NUMBER OF INSTANCES WE ENCOUNTER CONSEC NUMBER (identified by index for BIT_TYPE)
alt++; //-----> INCREMENT NUMBER OF BIT FLIP ALTERNATIONS (program only enters else block when there's a bit flip)
consec = 1; //-----> ADJUST CONSEC NUMBER FOR THE FIRST FLIPPED BIT
}
bit_prev = bit;
bits++; // KEEP TRACK OF TOTAL NUMBER OF BITS
}
in.close(); // FINISHED READING FILE
long tot_0 = sum(r[0], n[0], MAX[0]); // GET SUM OF 0_bits
long tot_1 = sum(r[1], n[1], MAX[1]); // GET SUM OF 1_bits
system("clear");
int *tmp = new int[MAX[0]];
for (int y = 0; y <= MAX[0]; y++){
tmp[y] = 1;
}
int spots_0 = sum(tmp, n[0], MAX[0]);
cout << spots_0 << endl;
delete[] tmp;
tmp = new int[MAX[1]];
for (int y = 0; y <= MAX[1]; y++){
tmp[y] = 1;
}
int spots_1 = sum(tmp, n[1], MAX[1]);
cout << tot_0 << endl << tot_1 << endl << spots_1;
cin.get();
return 0;
}
int search(int bit_type){ // SEARCH FOR A CONSEC NUMBER (of type BIT_TYPE) TO SEE IF ALREADY ENCOUNTERED
for (int i = 1; i <= MAX[bit_type]; i++){ //GO THRU ALL ENCOUNTERED CONSEC NUMBERS SO FAR (for type BIT_TYPE)
if (consec == r[bit_type][i]) // IF: FOUND
return i; // -----> RETURN INDEX OF RECORDED CONSEC_NUM
}
// IF: NOT FOUND
r[bit_type][++MAX[bit_type]] = consec; // -----> INCREMENT MAX[bit_type] & RECORD NEW CONSEC_NUM -------> ARRAY[MAX]
n[bit_type][MAX[bit_type]] = 1;
return(MAX[bit_prev]); // -----> RETURN THE NEWLY FILLED INDEX
}
long sum(int arr_r[], int arr_n[], int limit){
long tot = 0;
for (int i = 1; i <= limit; i++){
tot += (arr_r[i])*(arr_n[i]);
}
return tot;
}
void display(){
system("cls");
cout << "bit: " << bit << "\tbool: " << match << "\tconsec: " << consec;
cout << "\n\nMAX[0]: " << MAX[0] << "\t MAX[1]: " << MAX[1];
cout << "\n\n 0\t\t 1\t\t\t\t\t\t\tbits: " << bits << "\n";
int j = 1;
int bigger;
if (MAX[0] < MAX[1])
bigger = MAX[1];
else
bigger = MAX[0];
//reorder();
do{
if (j <= MAX[0])
cout << "\nr[" << j << "]: " << r[0][j] << " (" << n[0][j] << ")";
if (j <= MAX[1])
cout << "\tr[" << j << "]: " << r[1][j] << " (" << n[1][j] << ")";
cout << endl;
j++;
} while (j <= bigger);
}