<commit_before>package com.jpetrak.miscfastcompact.graph;

import com.jpetrak.miscfastcompact.store.StoreOfInts;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.objects.Object2IntAVLTreeMap;
import java.util.ArrayList;
import java.util.List;

/**
 * Simple Graph store. Just a first attempt to create a graph store, WIP...
 * @author <NAME>
 */
public class GraphStore {
  // The store consists of the following parts:
  // = a map from URI-String to Uri-id (an int, sequential 0...(n-1)
  // = an array that maps URI id to the out edge chunk index or -1 if no edge exists (yet)
  // = an array that maps URI id to the in edge chunk index or -1 if no edge exists (yet)
  // = out-edge store and in-edge store: two separate stores where we store,
  //   at position chunk-index, a variable block of edge-data. Edge data
  //   is a table with two integers per row: first the id or count of the edg
  //   second the id of the to/from node
  
  private StoreOfInts outEdges;
  private StoreOfInts inEdges;
  private Object2IntAVLTreeMap<String> uri2idMap;
  private IntArrayList id2OutEdgeChunk;
  private IntArrayList id2InEdgeChunk;
  private int nextId = 0;
  
  public GraphStore() {
    uri2idMap = new Object2IntAVLTreeMap<String>();
    outEdges = new StoreOfInts();
    inEdges = new StoreOfInts();
    id2OutEdgeChunk = new IntArrayList();
    id2InEdgeChunk = new IntArrayList();
  }

  /**
   * Add a node to the node list and return the id (unless it already exists,
   * then just return the id). 
   * @param uri
   * @return 
   */
  public int addNode(String uri) {
    if(uri2idMap.containsKey(uri)) {
      return uri2idMap.getInt(uri);
    } else {
      int usedId = nextId;
      uri2idMap.put(uri, nextId);
      id2InEdgeChunk.add(-1);
      id2OutEdgeChunk.add(-1);
      nextId++;
      return usedId;
    }
  }
  
  // return the id or -1 if not found
  public int getNodeId(String uri) {
    if(uri2idMap.containsKey(uri)) {
      return uri2idMap.getInt(uri);
    } else {
      return -1;
    }
  }

  // TO LOAD A GRAPH:
  // First, add all the known nodes: addNode(uri) for all different uris<commit_msg>go back to the fastutil hash map.<commit_after>package com.jpetrak.miscfastcompact.graph;

import com.jpetrak.miscfastcompact.store.StoreOfInts;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.objects.Object2IntAVLTreeMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Simple Graph store. Just a first attempt to create a graph store, WIP...
 * @author <NAME>
 */
public class GraphStore {
  // The store consists of the following parts:
  // = a map from URI-String to Uri-id (an int, sequential 0...(n-1)
  // = an array that maps URI id to the out edge chunk index or -1 if no edge exists (yet)
  // = an array that maps URI id to the in edge chunk index or -1 if no edge exists (yet)
  // = out-edge store and in-edge store: two separate stores where we store,
  //   at position chunk-index, a variable block of edge-data. Edge data
  //   is a table with two integers per row: first the id or count of the edg
  //   second the id of the to/from node
  
  private StoreOfInts outEdges;
  private StoreOfInts inEdges;
  private Object2IntAVLTreeMap<String> uri2idMap;
  //private HashMap<String,Integer> uri2idMap;
  private IntArrayList id2OutEdgeChunk;
  private IntArrayList id2InEdgeChunk;
  private int nextId = 0;
  
  public GraphStore() {
    uri2idMap = new Object2IntAVLTreeMap<String>();
    //uri2idMap = new HashMap<String,Integer>();
    outEdges = new StoreOfInts();
    inEdges = new StoreOfInts();
    id2OutEdgeChunk = new IntArrayList();
    id2InEdgeChunk = new IntArrayList();
  }

  /**
   * Add a node to the node list and return the id (unless it already exists,
   * then just return the id). 
   * @param uri
   * @return 
   */
  public int addNode(String uri) {
    if(uri2idMap.containsKey(uri)) {
      return uri2idMap.get(uri);
    } else {
      int usedId = nextId;
      uri2idMap.put(uri, nextId);
      id2InEdgeChunk.add(-1);
      id2OutEdgeChunk.add(-1);
      nextId++;
      return usedId;
    }
  }
  
  // return the id or -1 if not found
  public int getNodeId(String uri) {
    if(uri2idMap.containsKey(uri)) {
      return uri2idMap.get(uri);
    } else {
      return -1;
    }
  }

  // TO LOAD A GRAPH:
  // First, add all the known nodes: addNode(uri) for all different uris
{'fraction_non_alphanumeric': 0.09932735426008969, 'fraction_numerical': 0.011659192825112108, 'mean_word_length': 2.9723953695458594, 'pattern_counts': {'":': 0, '<': 11, '<?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': '3017073', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1458, 'n_words': 491}