<commit_before>#include <stdio.h>
#include <stdlib.h>
#include "longnum_engine.h"
#define alloc(type) (type*)malloc(sizeof(type))

#ifdef __cplusplus
#include <queue>
//using namespace std;
#endif // __cplusplus


longnum longnum_duplicate(longnum num) {
    longnum_digit * num_curr=NULL, *new_curr=NULL, *new_tail=NULL;
    longnum new_head=NULL;

    if (num == NULL) return NULL;
    else {
        num_curr = num;
        while (num_curr->right != NULL && num_curr->digit == 0) {
            num_curr = num_curr->right;
        }
        while (num_curr != NULL) {
            new_curr = alloc(longnum_digit);
            new_curr->digit = num_curr->digit;
            new_curr->right = NULL;
            if (new_head == NULL) {
                new_curr->left = NULL;
                new_head = new_curr;
            }
            else {
                new_curr->left = new_tail;
                new_tail->right = new_curr;
            }
            new_tail = new_curr;
            num_curr = num_curr->right;
        }
    }

    return new_head;
}

longnum longnum_freezero(longnum num_head) {
    if (num_head == NULL) return NULL;

    while (num_head->right != NULL && num_head->digit == 0) {
        num_head = num_head->right;
        free(num_head->left);
    }

    num_head->left = NULL;
    return num_head;
}

longnum longnum_sum (longnum num1_head, longnum num2_head) {
        char leftover=0;
        longnum_digit *num1_curr=NULL;
        longnum_digit *num2_curr=NULL;
        longnum_digit *sum_head=NULL,  *sum_curr=NULL;

        if (num1_head == NULL && num2_head == NULL)
           return NULL;
        else if (num1_head == NULL)
            return longnum_duplicate(num2_head);
        else if (num2_head == NULL)
            return longnum_duplicate(num1_head);
        else {
             num1_curr = num1_head;
             while (num1_curr->right != NULL)
                   num1_curr = num1_curr->right;
             num2_curr = num2_head;
             while (num2_curr->right != NULL)
                   num2_curr = num2_curr->right;
        }

        while ((num1_curr != NULL) || (num2_curr != NULL)) {
          sum_curr = alloc(longnum_digit);
          if (num1_curr == NULL) {
               sum_curr->digit = num2_curr->digit + leftover;
               num2_curr = num2_curr->left;
          }
          else if (num2_curr == NULL) {
               sum_curr->digit = num1_curr->digit + leftover;
               num1_curr = num1_curr->left;
          }
          else {
               sum_curr->digit = num1_curr->digit + num2_curr->digit + leftover;
               num1_curr = num1_curr->left;
               num2_curr = num2_curr->left;
          }
          leftover = sum_curr->digit / 10;
          sum_curr->digit %= 10;
          sum_curr->left=NULL;
          if (sum_head == NULL) {
             sum_curr->right=NULL;
          }
          else {
             sum_curr->right=sum_head;
             sum_head->left=sum_curr;
          }
          sum_head = sum_curr;
    }

    while (leftover != 0) {
          sum_curr = alloc(longnum_digit);
          sum_curr->digit = leftover;
          leftover = sum_curr->digit / 10;
          sum_curr->digit %= 10;
          sum_curr->left=NULL;
          if (sum_head == NULL) {
             sum_curr->right=NULL;
          }
          else {
             sum_curr->right=sum_head;
             sum_head->left=sum_curr;
          }
          sum_head = sum_curr;
    }

    return longnum_freezero(sum_head);
}

int longnum_cmp (longnum num1_head, longnum num2_head) {
    //sign of num1-num2
    //num1 > num2? return 1
    //num1 < num2? return -1
    //return 0 otherwise
    longnum_digit *num1_curr=NULL;
    longnum_digit *num2_curr=NULL;

    if (num1_head == NULL && num2_head == NULL)
       return 0;
    else if (num1_head == NULL)
         return -1;
    else if (num2_head == NULL)
         return 1;
    else {
        num1_curr = num1_head;
        num2_curr = num2_head;

        while (num1_curr->right != NULL && num1_curr->digit == 0) {
            num1_curr = num1_curr->right;
        }
        while (num2_curr->right != NULL && num2_curr->digit == 0) {
            num2_curr = num2_curr->right;
        }

        while ((num1_curr != NULL) || (num2_curr != NULL)) {
            if (num1_curr == NULL && num2_curr != NULL) return -1;
            else if (num2_curr == NULL && num1_curr != NULL) return 1;
            else {
                num1_curr = num1_curr->right;
                num2_curr = num2_curr->right;
            }
        }
    }

    num1_curr = num1_head;
    num2_curr = num2_head;

    while (num1_curr->right != NULL && num1_curr->digit == 0) {
        num1_curr = num1_curr->right;
    }
    while (num2_curr->right != NULL && num2_curr->digit == 0) {
        num2_curr = num2_curr->right;
    }

    while ((num1_curr != NULL) || (num2_curr != NULL)) {
        if (num1_curr->digit > num2_curr->digit) return 1;
        else if (num2_curr->digit > num1_curr->digit) return -1;
        else {
            num1_curr = num1_curr->right;
            num2_curr = num2_curr->right;
        }
    }
    return 0;
}

void longnum_free (longnum num) {
     longnum tmp;
     while (num != NULL) {
           tmp = num;
           num = num->right;
           free(tmp);
     }
}
#ifdef __cplusplus
longnum longnum_multiply (longnum num1_head, longnum num2_head) {
        char leftover=0;
        longnum_digit *num1_curr=NULL;
        longnum_digit *num2_curr=NULL;
        longnum_digit *sum_head=NULL;
        longnum_digit *tmp_head=NULL, *tmp_curr=NULL, *tmp_tail=NULL;
        unsigned long long int num2_count=0, i;
        std::queue <longnum >sum_queue;

         if (num1_head == NULL && num2_head == NULL)
           return NULL;

        if (longnum_cmp (num1_head, num2_head) == -1) {
           num1_curr = num1_head;
           num1_head = num2_head;
           num2_head = num1_curr;
        }

        num1_curr = num1_head;
        while (num1_curr->right != NULL)
              num1_curr = num1_curr->right;
        num2_curr = num2_head;
        while (num2_curr->right != NULL)
              num2_curr = num2_curr->right;

        while (num2_curr != NULL) {
              if (num2_curr->digit == 0) {
                num2_curr = num2_curr->left;
                num2_count++;
                continue;
              }
              while (num1_curr != NULL) {
                    tmp_curr = alloc(longnum_digit);
                    tmp_curr->digit = (num1_curr->digit * num2_curr->digit) + leftover;
                    leftover = tmp_curr->digit / 10;
                    tmp_curr->digit %= 10;
                    tmp_curr->left=NULL;
                    if (tmp_head == NULL) {
                       tmp_tail=tmp_curr;
                       tmp_curr->right=NULL;
                    }
                    else {
                         tmp_curr->right=tmp_head;
                         tmp_head->left=tmp_curr;
                    }
                    tmp_head = tmp_curr;
                    num1_curr = num1_curr->left;
              }

              while (leftover != 0) {
                    tmp_curr = alloc(longnum_digit);
                    tmp_curr->digit = leftover % 10;
                    leftover /= 10;
                    tmp_curr->left=NULL;
                    if (tmp_head == NULL) {
                       tmp_tail=tmp_curr;
                       tmp_curr->right=NULL;
                    }
                    else {
                         tmp_curr->right=tmp_head;
                         tmp_head->left=tmp_curr;
                    }
                    tmp_head = tmp_curr;
              }

              for (i=0; i<num2_count; i++) {
                  tmp_curr = alloc(longnum_digit);
                  tmp_curr->digit = 0;
                  tmp_curr->right = NULL;
                  tmp_curr->left = tmp_tail;
                  tmp_tail->right = tmp_curr;
                  tmp_tail = tmp_curr;
              }

              /* sum_head = longnum_sum(tmp_head, last_head);
              longnum_free(last_head);
              last_head = sum_head;
              sum_head = NULL;
              longnum_free(tmp_head);
              tmp_head = NULL;
              tmp_curr = NULL;
              tmp_tail = NULL; */

              sum_queue.push(tmp_head);
              tmp_head = NULL;
              tmp_curr = NULL;
              tmp_tail = NULL;

              num1_curr = num1_head;
              while (num1_curr->right != NULL)
                num1_curr = num1_curr->right;
            num2_curr = num2_curr->left;
            num2_count++;
        }

        sum_head = longnum_multinum_sum(sum_queue);

        while (!sum_queue.empty()) {
            longnum_free(sum_queue.front());
            sum_queue.pop();
        }

        return sum_head;
}
#endif // __cplusplus
void longnum_print(longnum num) {
    if (num == NULL) return;
    longnum_digit * num_curr = num;
    char started=0;
    while (num_curr != NULL) {
          if (num_curr->digit != 0 || started) {
             printf("%hhd", num_curr->digit);
             started = 1;
          }
          num_curr = num_curr->right;
    }
    if (!started) printf("0");
}

longnum int_to_longnum(unsigned int integer) {
    longnum_digit * num_head=NULL, * num_curr=NULL;

    do {
        num_curr = alloc(longnum_digit);
        num_curr->digit = integer % 10;
        integer /= 10;
        num_curr->left = NULL;
        if (num_head == NULL) {
            num_curr->right = NULL;
        }
        else {
            num_curr->right = num_head;
            num_head->left = num_curr;
        }
        num_head = num_curr;
    } while (integer != 0);
    return num_head;
}
#ifdef __cplusplus
longnum longnum_multinum_sum(std::queue <longnum> queue) {
        char leftover=0;
        std::queue <longnum> sum_queue;
        //std::queue <longnum> tmp_queue;
        longnum num_curr=NULL;
        longnum result_head=NULL, result_curr=NULL;
        longnum new_digit=NULL;

        if (queue.empty()) return int_to_longnum(0);

        while(!queue.empty()) {
            num_curr = queue.front();
            queue.pop();
            if (num_curr == NULL) continue;
            while (num_curr->right != NULL)
                  num_curr = num_curr->right;
            sum_queue.push(num_curr);
        }

        new_digit = sum_queue.back();
        num_curr = NULL;
        while (!sum_queue.empty()) {
              result_curr = alloc(longnum_digit);
              result_curr->digit = leftover;
              while (num_curr != new_digit) {
                    num_curr = sum_queue.front();
                    sum_queue.pop();
                    result_curr->digit += num_curr->digit;
                    if (num_curr->left != NULL)
                       sum_queue.push(num_curr->left);
              }
              if (!sum_queue.empty()) new_digit = sum_queue.back();
              leftover = result_curr->digit / 10;
              result_curr->digit %= 10;
              result_curr->left=NULL;
              if (result_head == NULL) {
                 result_head = result_curr;
                 result_curr->right = NULL;
              }
              else {
                   result_curr->right = result_head;
                   result_head->left = result_curr;
              }
              result_head = result_curr;

              /*while (!tmp_queue.empty()) {
                    sum_queue.push(tmp_queue.front());
                    tmp_queue.pop();
              }*/
        }

        while (leftover != 0) {
              result_curr = alloc(longnum_digit);
              result_curr->digit = leftover % 10;
              leftover /= 10;<commit_msg>Use sum_tmp to keep in-digit sum result for "longnum_multinum_sum".<commit_after>#include <stdio.h>
#include <stdlib.h>
#include "longnum_engine.h"
#define alloc(type) (type*)malloc(sizeof(type))

#ifdef __cplusplus
#include <queue>
//using namespace std;
#endif // __cplusplus


longnum longnum_duplicate(longnum num) {
    longnum_digit * num_curr=NULL, *new_curr=NULL, *new_tail=NULL;
    longnum new_head=NULL;

    if (num == NULL) return NULL;
    else {
        num_curr = num;
        while (num_curr->right != NULL && num_curr->digit == 0) {
            num_curr = num_curr->right;
        }
        while (num_curr != NULL) {
            new_curr = alloc(longnum_digit);
            new_curr->digit = num_curr->digit;
            new_curr->right = NULL;
            if (new_head == NULL) {
                new_curr->left = NULL;
                new_head = new_curr;
            }
            else {
                new_curr->left = new_tail;
                new_tail->right = new_curr;
            }
            new_tail = new_curr;
            num_curr = num_curr->right;
        }
    }

    return new_head;
}

longnum longnum_freezero(longnum num_head) {
    if (num_head == NULL) return NULL;

    while (num_head->right != NULL && num_head->digit == 0) {
        num_head = num_head->right;
        free(num_head->left);
    }

    num_head->left = NULL;
    return num_head;
}

longnum longnum_sum (longnum num1_head, longnum num2_head) {
        char leftover=0;
        longnum_digit *num1_curr=NULL;
        longnum_digit *num2_curr=NULL;
        longnum_digit *sum_head=NULL,  *sum_curr=NULL;

        if (num1_head == NULL && num2_head == NULL)
           return NULL;
        else if (num1_head == NULL)
            return longnum_duplicate(num2_head);
        else if (num2_head == NULL)
            return longnum_duplicate(num1_head);
        else {
             num1_curr = num1_head;
             while (num1_curr->right != NULL)
                   num1_curr = num1_curr->right;
             num2_curr = num2_head;
             while (num2_curr->right != NULL)
                   num2_curr = num2_curr->right;
        }

        while ((num1_curr != NULL) || (num2_curr != NULL)) {
          sum_curr = alloc(longnum_digit);
          if (num1_curr == NULL) {
               sum_curr->digit = num2_curr->digit + leftover;
               num2_curr = num2_curr->left;
          }
          else if (num2_curr == NULL) {
               sum_curr->digit = num1_curr->digit + leftover;
               num1_curr = num1_curr->left;
          }
          else {
               sum_curr->digit = num1_curr->digit + num2_curr->digit + leftover;
               num1_curr = num1_curr->left;
               num2_curr = num2_curr->left;
          }
          leftover = sum_curr->digit / 10;
          sum_curr->digit %= 10;
          sum_curr->left=NULL;
          if (sum_head == NULL) {
             sum_curr->right=NULL;
          }
          else {
             sum_curr->right=sum_head;
             sum_head->left=sum_curr;
          }
          sum_head = sum_curr;
    }

    while (leftover != 0) {
          sum_curr = alloc(longnum_digit);
          sum_curr->digit = leftover;
          leftover = sum_curr->digit / 10;
          sum_curr->digit %= 10;
          sum_curr->left=NULL;
          if (sum_head == NULL) {
             sum_curr->right=NULL;
          }
          else {
             sum_curr->right=sum_head;
             sum_head->left=sum_curr;
          }
          sum_head = sum_curr;
    }

    return longnum_freezero(sum_head);
}

int longnum_cmp (longnum num1_head, longnum num2_head) {
    //sign of num1-num2
    //num1 > num2? return 1
    //num1 < num2? return -1
    //return 0 otherwise
    longnum_digit *num1_curr=NULL;
    longnum_digit *num2_curr=NULL;

    if (num1_head == NULL && num2_head == NULL)
       return 0;
    else if (num1_head == NULL)
         return -1;
    else if (num2_head == NULL)
         return 1;
    else {
        num1_curr = num1_head;
        num2_curr = num2_head;

        while (num1_curr->right != NULL && num1_curr->digit == 0) {
            num1_curr = num1_curr->right;
        }
        while (num2_curr->right != NULL && num2_curr->digit == 0) {
            num2_curr = num2_curr->right;
        }

        while ((num1_curr != NULL) || (num2_curr != NULL)) {
            if (num1_curr == NULL && num2_curr != NULL) return -1;
            else if (num2_curr == NULL && num1_curr != NULL) return 1;
            else {
                num1_curr = num1_curr->right;
                num2_curr = num2_curr->right;
            }
        }
    }

    num1_curr = num1_head;
    num2_curr = num2_head;

    while (num1_curr->right != NULL && num1_curr->digit == 0) {
        num1_curr = num1_curr->right;
    }
    while (num2_curr->right != NULL && num2_curr->digit == 0) {
        num2_curr = num2_curr->right;
    }

    while ((num1_curr != NULL) || (num2_curr != NULL)) {
        if (num1_curr->digit > num2_curr->digit) return 1;
        else if (num2_curr->digit > num1_curr->digit) return -1;
        else {
            num1_curr = num1_curr->right;
            num2_curr = num2_curr->right;
        }
    }
    return 0;
}

void longnum_free (longnum num) {
     longnum tmp;
     while (num != NULL) {
           tmp = num;
           num = num->right;
           free(tmp);
     }
}
#ifdef __cplusplus
longnum longnum_multiply (longnum num1_head, longnum num2_head) {
        char leftover=0;
        longnum_digit *num1_curr=NULL;
        longnum_digit *num2_curr=NULL;
        longnum_digit *sum_head=NULL;
        longnum_digit *tmp_head=NULL, *tmp_curr=NULL, *tmp_tail=NULL;
        unsigned long long int num2_count=0, i;
        std::queue <longnum >sum_queue;

         if (num1_head == NULL && num2_head == NULL)
           return NULL;

        if (longnum_cmp (num1_head, num2_head) == -1) {
           num1_curr = num1_head;
           num1_head = num2_head;
           num2_head = num1_curr;
        }

        num1_curr = num1_head;
        while (num1_curr->right != NULL)
              num1_curr = num1_curr->right;
        num2_curr = num2_head;
        while (num2_curr->right != NULL)
              num2_curr = num2_curr->right;

        while (num2_curr != NULL) {
              if (num2_curr->digit == 0) {
                num2_curr = num2_curr->left;
                num2_count++;
                continue;
              }
              while (num1_curr != NULL) {
                    tmp_curr = alloc(longnum_digit);
                    tmp_curr->digit = (num1_curr->digit * num2_curr->digit) + leftover;
                    leftover = tmp_curr->digit / 10;
                    tmp_curr->digit %= 10;
                    tmp_curr->left=NULL;
                    if (tmp_head == NULL) {
                       tmp_tail=tmp_curr;
                       tmp_curr->right=NULL;
                    }
                    else {
                         tmp_curr->right=tmp_head;
                         tmp_head->left=tmp_curr;
                    }
                    tmp_head = tmp_curr;
                    num1_curr = num1_curr->left;
              }

              while (leftover != 0) {
                    tmp_curr = alloc(longnum_digit);
                    tmp_curr->digit = leftover % 10;
                    leftover /= 10;
                    tmp_curr->left=NULL;
                    if (tmp_head == NULL) {
                       tmp_tail=tmp_curr;
                       tmp_curr->right=NULL;
                    }
                    else {
                         tmp_curr->right=tmp_head;
                         tmp_head->left=tmp_curr;
                    }
                    tmp_head = tmp_curr;
              }

              for (i=0; i<num2_count; i++) {
                  tmp_curr = alloc(longnum_digit);
                  tmp_curr->digit = 0;
                  tmp_curr->right = NULL;
                  tmp_curr->left = tmp_tail;
                  tmp_tail->right = tmp_curr;
                  tmp_tail = tmp_curr;
              }

              /* sum_head = longnum_sum(tmp_head, last_head);
              longnum_free(last_head);
              last_head = sum_head;
              sum_head = NULL;
              longnum_free(tmp_head);
              tmp_head = NULL;
              tmp_curr = NULL;
              tmp_tail = NULL; */

              sum_queue.push(tmp_head);
              tmp_head = NULL;
              tmp_curr = NULL;
              tmp_tail = NULL;

              num1_curr = num1_head;
              while (num1_curr->right != NULL)
                num1_curr = num1_curr->right;
            num2_curr = num2_curr->left;
            num2_count++;
        }

        sum_head = longnum_multinum_sum(sum_queue);

        while (!sum_queue.empty()) {
            longnum_free(sum_queue.front());
            sum_queue.pop();
        }

        return sum_head;
}
#endif // __cplusplus
void longnum_print(longnum num) {
    if (num == NULL) return;
    longnum_digit * num_curr = num;
    char started=0;
    while (num_curr != NULL) {
          if (num_curr->digit != 0 || started) {
             printf("%hhd", num_curr->digit);
             started = 1;
          }
          num_curr = num_curr->right;
    }
    if (!started) printf("0");
}

longnum int_to_longnum(unsigned int integer) {
    longnum_digit * num_head=NULL, * num_curr=NULL;

    do {
        num_curr = alloc(longnum_digit);
        num_curr->digit = integer % 10;
        integer /= 10;
        num_curr->left = NULL;
        if (num_head == NULL) {
            num_curr->right = NULL;
        }
        else {
            num_curr->right = num_head;
            num_head->left = num_curr;
        }
        num_head = num_curr;
    } while (integer != 0);
    return num_head;
}
#ifdef __cplusplus
longnum longnum_multinum_sum(std::queue <longnum> queue) {
        unsigned int leftover=0;
        std::queue <longnum> sum_queue;
        //std::queue <longnum> tmp_queue;
        longnum num_curr=NULL;
        longnum result_head=NULL, result_curr=NULL;
        longnum new_digit=NULL;
        unsigned int sum_tmp;

        if (queue.empty()) return int_to_longnum(0);

        while(!queue.empty()) {
            num_curr = queue.front();
            queue.pop();
            if (num_curr == NULL) continue;
            while (num_curr->right != NULL)
                  num_curr = num_curr->right;
            sum_queue.push(num_curr);
        }

        new_digit = sum_queue.back();
        num_curr = NULL;
        while (!sum_queue.empty()) {
              result_curr = alloc(longnum_digit);
              sum_tmp = leftover;
              while (num_curr != new_digit) {
                    num_curr = sum_queue.front();
                    sum_queue.pop();
                    sum_tmp += num_curr->digit;
                    if (num_curr->left != NULL)
                       sum_queue.push(num_curr->left);
              }
              if (!sum_queue.empty()) new_digit = sum_queue.back();
              leftover = sum_tmp / 10;
              result_curr->digit = sum_tmp % 10;
              result_curr->left=NULL;
              if (result_head == NULL) {
                 result_head = result_curr;
                 result_curr->right = NULL;
              }
              else {
                   result_curr->right = result_head;
                   result_head->left = result_curr;
              }
              result_head = result_curr;

              /*while (!tmp_queue.empty()) {
                    sum_queue.push(tmp_queue.front());
                    tmp_queue.pop();
              }*/
        }

        while (leftover != 0) {
              result_curr = alloc(longnum_digit);
              result_curr->digit = leftover % 10;
              leftover /= 10;
{'fraction_non_alphanumeric': 0.1085702163767501, 'fraction_numerical': 0.01697072549851506, 'mean_word_length': 1.4609521820839424, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 248, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7027824', 'n_tokens_mistral': 7666, 'n_tokens_neox': 7172, 'n_words': 1392}