// single-threaded.c #include #include #include #include #include #include #include #include "string-view.h" #include "scanner.h" #include "utils.h" #include "card.h" #include "user.h" #include "transaction.h" #include "transaction-heap.h" #include "stb_ds.h" unsigned int hash (register const char *str, register size_t len); extern const char *wordlist[]; void read_filtered_cards_and_users (const char *filename, struct card **cards, struct user **users) { MEASURE_SCOPE (); ASSERT (filename); ASSERT (cards); ASSERT (users); int fd = open (filename, O_RDONLY); ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno)); struct string_view sv = mmap_whole (fd); struct scanner sc; scanner__init (&sc, sv); // skip header: id, client_id, card_brand, card_type, card_number, // expires, cvv, has_chip, num_cards_issued, credit_limit, // acct_open_date, year_pin_last_changed, card_on_dark_web scanner__skip_over (&sc, '\n'); while (!scanner__reached_end (&sc)) { struct card card; struct user user; card.id = scanner__get_uint64 (&sc); scanner__skip_n (&sc, 1); user.id = scanner__get_uint64 (&sc); scanner__skip_over (&sc, '$'); uint64_t credit_limit = scanner__get_uint64 (&sc); scanner__skip_over (&sc, '\n'); if (credit_limit < 12000) continue; hmputs (*cards, card); if (hmgetp_null (*users, user.key) == NULL) hmputs (*users, user); } munmap ((char *) sv.begin, sv.end - sv.begin); close (fd); } void read_user_per_capita_incomes (const char *filename, struct user *users) { MEASURE_SCOPE (); ASSERT (filename); ASSERT (users); int fd = open (filename, O_RDONLY); ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno)); struct string_view sv = mmap_whole (fd); struct scanner sc; scanner__init (&sc, sv); // skip header: id, current_age, retirement_age, birth_year, // birth_month, gender, address, latitude, longitude, // per_capita_income, yearly_income, total_debt, credit_score, // num_credit_cards scanner__skip_over (&sc, '\n'); while (!scanner__reached_end (&sc)) { client_id_t id = scanner__get_uint64 (&sc); struct user *user = hmgetp_null (users, id); if (user == NULL) { scanner__skip_over (&sc, '\n'); continue; } scanner__skip_over (&sc, '$'); user->normalizer = 0.12/scanner__get_uint64 (&sc); scanner__skip_over (&sc, '\n'); } munmap ((char *) sv.begin, sv.end - sv.begin); close (fd); } void read_filtered_and_reduced_transactions (const char *filename, struct transaction **transactions, struct card *cards) { MEASURE_SCOPE (); ASSERT (filename); ASSERT (transactions); ASSERT (cards); int fd = open (filename, O_RDONLY); ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno)); struct string_view sv = mmap_whole (fd); struct scanner sc; scanner__init (&sc, sv); // skip header: id, date, client_id, card_id, amount, use_chip, // merchant_id, merchant_city, merchant_state, zip, mcc, errors scanner__skip_over (&sc, '\n'); while (!scanner__reached_end (&sc)) { union transaction_key transaction_key; scanner__skip_over (&sc, ','); transaction_key.year_month = scanner__get_uint64 (&sc) << 4; scanner__skip_n (&sc, 1); transaction_key.year_month |= scanner__get_uint64 (&sc); scanner__skip_over (&sc, ','); transaction_key.client_id = scanner__get_uint64 (&sc); scanner__skip_n (&sc, 1); card_id_t card_id = scanner__get_uint64 (&sc); if (hmgetp_null (cards, card_id) == NULL) { scanner__skip_over (&sc, '\n'); continue; } scanner__skip_n (&sc, 2); int64_t amount = 100*scanner__get_int64 (&sc); scanner__skip_n (&sc, 1); int sign = amount >= 0 ? 1 : -1; amount += sign*scanner__get_uint64 (&sc); scanner__skip_n (&sc, 1); scanner__skip_over (&sc, ','); scanner__skip_over (&sc, ','); struct string_view sv = scanner__get_until (&sc, ','); transaction_key.merchant_city_id = hash (sv.begin, STRING_VIEW__LENGTH (sv)); scanner__skip_over (&sc, '\n'); struct transaction *transaction = hmgetp_null (*transactions, transaction_key); if (transaction == NULL) hmput (*transactions, transaction_key, amount); else transaction->sum += amount; } munmap ((char *) sv.begin, sv.end - sv.begin); close (fd); } void normalize_transaction_sums (struct transaction *transactions, struct user *users, size_t count) { MEASURE_SCOPE ("%lu transactions", count); ASSERT (transactions); ASSERT (users); for (size_t i = 0; i < count; ++i) { struct transaction *transaction = &transactions[i]; float normalizer = hmgets (users, transaction->key.client_id).normalizer; transaction->normalized = transaction->sum*normalizer; } } int compare_transactions (const void *lhs, const void *rhs) { const struct transaction *lhs_transaction = lhs; const struct transaction *rhs_transaction = rhs; float lhs_normalized = lhs_transaction->normalized; float rhs_normalized = rhs_transaction->normalized; return (lhs_normalized < rhs_normalized) - (lhs_normalized > rhs_normalized); } void extract_top_normalized_transaction_sums (struct transaction *transactions, size_t count, struct transaction_heap *heap) { MEASURE_SCOPE ("(~10x faster then full sort)"); ASSERT (transactions); ASSERT (heap); for (size_t i = 0; i < count; ++i) transaction_heap__push (heap, transactions[i]); qsort (heap->array, heap->count, sizeof (*heap->array), compare_transactions); } void print_top_transactions (struct transaction_heap *heap) { MEASURE_SCOPE (); printf (" i client_id normalized_spending year_month city\n"); for (int i = 0; i < heap->count; ++i) { printf ("%2d %4hu %6.2f %hu-%02hu %s\n", i, heap->array[i].key.client_id, heap->array[i].normalized, heap->array[i].key.year_month >> 4, heap->array[i].key.year_month & ((1 << 4) - 1), wordlist[heap->array[i].key.merchant_city_id]); } } int main (void) { MEASURE_SCOPE (); struct card *cards = NULL; struct user *users = NULL; struct transaction *transactions = NULL; struct transaction_heap top_transactions = {0}; read_filtered_cards_and_users ("res/cards_data.csv", &cards, &users); read_filtered_and_reduced_transactions ("res/transactions_data.csv", &transactions, cards); size_t transaction_count = hmlenu (transactions); read_user_per_capita_incomes ("res/users_data.csv", users); normalize_transaction_sums (transactions, users, transaction_count); extract_top_normalized_transaction_sums (transactions, transaction_count, &top_transactions); printf ("Top %d out of %lu:\n", top_transactions.count, transaction_count); print_top_transactions (&top_transactions); return EXIT_SUCCESS; } // single-threaded.c ends here