Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include "data.h"
- struct keyvalue {
- char key;
- uint64_t value = 0;
- };
- const int listSize = sizeof rules / sizeof rules[0];
- char ruleList[listSize][3] = {"\0"};
- char charList[listSize][3] = {"\0"};;
- uint64_t countList[listSize] = {0};
- uint64_t bufferList[listSize] = {0};
- keyvalue countMap[listSize];
- void pairInsertion(){
- for (int i = 0; i < listSize; i++) {
- if(countList[i]== 0)continue;
- char buf1[3]= {'\0'};
- buf1[0] = ruleList[i][0];
- buf1[1] = charList[i][0];
- char buf2[3]= {'\0'};
- buf2[0] = charList[i][0];
- buf2[1] = ruleList[i][1];
- for (int j = 0; j < listSize; j++) {
- if(strcmp(buf1, ruleList[j]) == 0)bufferList[j]+=countList[i];
- if(strcmp(buf2, ruleList[j]) == 0)bufferList[j]+=countList[i];
- }
- }
- for (int i = 0; i < listSize; i++) {
- countList[i] = bufferList[i];
- bufferList[i] = 0;
- }
- }
- void initData(){
- int index = 0;
- for(auto r : rules){
- strcpy(ruleList[index],r[0]);
- strcpy(charList[index],r[1]);
- index++;
- };
- for (size_t i = 0; i < sizeof str / sizeof str[0] -1; i++)
- {
- char buf[3]= {'\0'};
- buf[0] = str[i];
- buf[1] = str[i + 1];
- for (int j = 0; j < listSize; j++)
- {
- if(strcmp(ruleList[j], buf) == 0)countList[j]++;
- }
- }
- }
- int strIndex = 0;
- void addKeyValue(char key, uint64_t value){
- bool inList = false;
- for(auto &kv : countMap){
- if(kv.key == key){
- kv.value+=value;
- inList = true;
- break;
- }
- }
- if(!inList){
- countMap[strIndex].key = key;
- countMap[strIndex++].value = value;
- }
- }
- char *uint64_to_string(uint64_t input)
- {
- static char result[21] = "";
- // Clear result from any leftover digits from previous function call.
- memset(&result[0], 0, sizeof(result));
- // temp is used as a temporary result storage to prevent sprintf bugs.
- char temp[21] = "";
- char c;
- uint8_t base = 10;
- while (input)
- {
- int num = input % base;
- input /= base;
- c = '0' + num;
- sprintf(temp, "%c%s", c, result);
- strcpy(result, temp);
- }
- return result;
- }
- void setup() {
- Serial.begin(9600);
- while(!Serial);
- initData();
- for (int x = 0; x < 40 ; x++) {
- pairInsertion();
- }
- for (int i = 0; i < listSize; i++) {
- addKeyValue(ruleList[i][1],countList[i]);
- };
- addKeyValue(str[0],1);
- uint64_t minCount=INFINITY;
- uint64_t maxCount=0;
- for(auto &kv : countMap){
- if(kv.key != NULL){
- maxCount = max(kv.value, maxCount);
- minCount = min(kv.value, minCount);
- }
- }
- Serial.println( "------" );
- Serial.println( uint64_to_string ( maxCount - minCount ));
- }
- void loop() {
- // put your main code here, to run repeatedly:
- }
Advertisement
Add Comment
Please, Sign In to add comment