<commit_before>package com.ictools.algorithms.graph.structures;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Sets;
import com.google.common.collect.Table;
import javafx.util.Pair;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class TableGraph implements WeightedGraph {

    private final Table<Long, Long, Long> graph;

    public TableGraph(Table<Long, Long, Long> graph) {
        this.graph = graph;
    }

    @Override
    public Optional<Long> getWeight(long a, long b) {
        Long result = graph.get(a, b);
        return result == null ? Optional.empty() : Optional.of(result);
    }

    @Override
    public boolean isAdjacent(long a, long b) {
        return graph.contains(a, b);
    }

    @Override
    public Set<Long> getNodes() {
        // TODO Implement caching so the expensive set union gets amortised
        Set<Long> outgoingNodes = Sets.newHashSet(graph.columnKeySet());
        outgoingNodes.addAll(graph.rowKeySet());
        return outgoingNodes;
    }

    @Override
    public Set<Pair<Long, Long>> getEdges() {
        return graph.cellSet().stream().map(x -> new Pair<>(x.getRowKey(), x.getColumnKey())).collect(Collectors.toSet());
    }

    @Override<commit_msg>Added caching of node-set to cut complexity of that operation<commit_after>package com.ictools.algorithms.graph.structures;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Sets;
import com.google.common.collect.Table;
import javafx.util.Pair;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class TableGraph implements WeightedGraph {

    private final Table<Long, Long, Long> graph;
    private final Set<Long> nodeCache;

    public TableGraph(Table<Long, Long, Long> graph) {
        this.graph = HashBasedTable.create(graph);
        this.nodeCache = Sets.newHashSet(graph.rowKeySet());
        nodeCache.addAll(graph.columnKeySet());
    }

    @Override
    public Optional<Long> getWeight(long a, long b) {
        Long result = graph.get(a, b);
        return result == null ? Optional.empty() : Optional.of(result);
    }

    @Override
    public boolean isAdjacent(long a, long b) {
        return graph.contains(a, b);
    }

    @Override
    public Set<Long> getNodes() {
        return nodeCache;
    }

    @Override
    public Set<Pair<Long, Long>> getEdges() {
        return graph.cellSet().stream().map(x -> new Pair<>(x.getRowKey(), x.getColumnKey())).collect(Collectors.toSet());
    }

    @Override
{'fraction_non_alphanumeric': 0.121960632960247, 'fraction_numerical': 0.0, 'mean_word_length': 3.341708542713568, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '3143890', 'n_tokens_mistral': 791, 'n_tokens_neox': 771, 'n_words': 183}