<commit_before>package com.mrkevinthomas.kcards.card_swipe;

import android.speech.tts.TextToSpeech;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.mrkevinthomas.kcards.R;
import com.mrkevinthomas.kcards.models.Card;
import com.mrkevinthomas.kcards.models.Deck;
import com.mrkevinthomas.kcards.views.CardItem;

import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;
import java.util.Random;
import java.util.TreeMap;

public class CardSwipeAdapter extends BaseAdapter implements CardItem.Delegate {

    private CardSwipeActivity cardSwipeActivity;
    private Deck deck;
    private boolean isSwapped;
    private boolean isHidden;

    private List<Card> cards = new ArrayList<>();

    public CardSwipeAdapter(@NonNull CardSwipeActivity cardSwipeActivity, @NonNull Deck deck, boolean isSwapped, boolean isHidden) {
        this.cardSwipeActivity = cardSwipeActivity;
        this.deck = deck;
        this.isSwapped = isSwapped;
        this.isHidden = isHidden;
        chooseCardsToShow();
    }

    public void setSwapped(boolean swapped) {
        isSwapped = swapped;
        notifyDataSetChanged();
    }

    public void setHidden(boolean hidden) {
        isHidden = hidden;
        notifyDataSetChanged();
    }

    public void removeFirst() {
        cards.remove(0);
        notifyDataSetChanged();
    }

    public void chooseCardsToShow() {
        final int CARD_LIST_SIZE = Math.min(3, deck.size());
        final double DEFAULT_WEIGHT = 10;
        final Random RANDOM = new Random();

        // first created weighted map of all the cards in the deck
        final NavigableMap<Double, Card> map = new TreeMap<>();
        double total = 0;
        for (Card card : deck.getCards()) {
            int count = card.getCorrectCount() + card.getIncorrectCount();
            double ratio = count == 0 ? 0 : card.getCorrectCount() / count;
            double weight = ratio == 0 ? DEFAULT_WEIGHT : 1 / ratio;
            total += weight;
            map.put(total, card);
        }

        // then choose a given number of cards using random weighted selection
        while (cards.size() < CARD_LIST_SIZE) {
            double value = RANDOM.nextDouble() * total;
            Card chosen = map.ceilingEntry(value).getValue();
            if (!cards.contains(chosen)) {
                cards.add(chosen);
            }
        }

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return cards.size();
    }

    @Override
    public Object getItem(int position) {
        return cards.get(position);
    }

    @Override
    public long getItemId(int position) {
        return cards.get(position).getId();
    }<commit_msg>updated logic for how to choose cards to serve for swiping this fixed a bug with single card decks<commit_after>package com.mrkevinthomas.kcards.card_swipe;

import android.speech.tts.TextToSpeech;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.mrkevinthomas.kcards.R;
import com.mrkevinthomas.kcards.models.Card;
import com.mrkevinthomas.kcards.models.Deck;
import com.mrkevinthomas.kcards.views.CardItem;

import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;
import java.util.Random;
import java.util.TreeMap;

public class CardSwipeAdapter extends BaseAdapter implements CardItem.Delegate {

    private CardSwipeActivity cardSwipeActivity;
    private Deck deck;
    private boolean isSwapped;
    private boolean isHidden;

    private List<Card> cards = new ArrayList<>();

    public CardSwipeAdapter(@NonNull CardSwipeActivity cardSwipeActivity, @NonNull Deck deck, boolean isSwapped, boolean isHidden) {
        this.cardSwipeActivity = cardSwipeActivity;
        this.deck = deck;
        this.isSwapped = isSwapped;
        this.isHidden = isHidden;
        chooseCardsToShow();
    }

    public void setSwapped(boolean swapped) {
        isSwapped = swapped;
        notifyDataSetChanged();
    }

    public void setHidden(boolean hidden) {
        isHidden = hidden;
        notifyDataSetChanged();
    }

    public void removeFirst() {
        cards.remove(0);
        notifyDataSetChanged();
    }

    public void chooseCardsToShow() {
        final int CARD_LIST_SIZE = 3;
        final double DEFAULT_WEIGHT = 10;
        final Random RANDOM = new Random();

        // first created weighted map of all the cards in the deck
        final NavigableMap<Double, Card> map = new TreeMap<>();
        double total = 0;
        for (Card card : deck.getCards()) {
            int count = card.getCorrectCount() + card.getIncorrectCount();
            double ratio = count == 0 ? 0 : card.getCorrectCount() / count;
            double weight = ratio == 0 ? DEFAULT_WEIGHT : 1 / ratio;
            total += weight;
            map.put(total, card);
        }

        // then choose a given number of cards using random weighted selection
        while (cards.size() < CARD_LIST_SIZE) {
            double value = RANDOM.nextDouble() * total;
            Card chosen = map.ceilingEntry(value).getValue();
            if (!cards.contains(chosen) || cards.containsAll(deck.getCards())) {
                cards.add(chosen);
            }
        }

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return cards.size();
    }

    @Override
    public Object getItem(int position) {
        return cards.get(position);
    }

    @Override
    public long getItemId(int position) {
        return cards.get(position).getId();
    }
{'fraction_non_alphanumeric': 0.0937176389368312, 'fraction_numerical': 0.0031066620642043494, 'mean_word_length': 2.7005108556832695, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '1892923', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1625, 'n_words': 424}