<commit_before>package com.tinkerpop.pipes.transform;

import com.tinkerpop.pipes.AbstractPipe;
import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.util.EmptyIterator;
import com.tinkerpop.pipes.util.MetaPipe;
import com.tinkerpop.pipes.util.PipeHelper;
import com.tinkerpop.pipes.util.SingleIterator;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author <NAME> (http://markorodriguez.com)
 */
public class MemoizePipe<S, E> extends AbstractPipe<S, E> implements MetaPipe {

    protected Pipe<S, E> pipe;
    protected Map<S, List<E>> map;
    protected Iterator<E> currentIterator = new EmptyIterator<E>();

    public MemoizePipe(final Pipe<S, E> pipe) {
        this(pipe, new HashMap<S, List<E>>());
    }

    public MemoizePipe(final Pipe<S, E> pipe, final Map<S, List<E>> map) {
        this.pipe = pipe;
        this.map = map;
    }

    public E processNextStart() {
        while (true) {
            if (this.currentIterator.hasNext())
                return this.currentIterator.next();
            else {
                this.getOrCreate(this.starts.next());
            }
        }
    }

    private void getOrCreate(final S s) {
        if (this.map.containsKey(s)) {
            this.currentIterator = this.map.get(s).iterator();
        } else {
            this.pipe.setStarts(new SingleIterator<S>(s));
            final List<E> results = new LinkedList<E>();
            PipeHelper.fillCollection(this.pipe, results);
            this.map.put(s, results);
            this.currentIterator = results.iterator();
        }
    }

    public List<Pipe> getPipes() {
        return (List) Arrays.asList(pipe);
    }

    public Map<S, List<E>> getMemoization() {
        return this.map;
    }

    public void reset() {
        this.currentIterator = new EmptyIterator<E>();
        try {
            this.map = this.map.getClass().getConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        super.reset();
    }
}<commit_msg>added MemoizePipe to save the mapping of a pipe segment.<commit_after>package com.tinkerpop.pipes.transform;

import com.tinkerpop.pipes.AbstractPipe;
import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.util.EmptyIterator;
import com.tinkerpop.pipes.util.MetaPipe;
import com.tinkerpop.pipes.util.PipeHelper;
import com.tinkerpop.pipes.util.SingleIterator;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author <NAME> (http://markorodriguez.com)
 */
public class MemoizePipe<S, E> extends AbstractPipe<S, E> implements MetaPipe {

    protected Pipe<S, E> pipe;
    protected Map<S, List<E>> map;
    protected Iterator<E> currentIterator = new EmptyIterator<E>();

    public MemoizePipe(final Pipe<S, E> pipe) {
        this(pipe, new HashMap<S, List<E>>());
    }

    public MemoizePipe(final Pipe<S, E> pipe, final Map<S, List<E>> map) {
        this.pipe = pipe;
        this.map = map;
    }

    public E processNextStart() {
        while (true) {
            if (this.currentIterator.hasNext())
                return this.currentIterator.next();
            else {
                this.getOrCreate(this.starts.next());
            }
        }
    }

    private void getOrCreate(final S s) {
        if (this.map.containsKey(s)) {
            this.currentIterator = this.map.get(s).iterator();
        } else {
            this.pipe.setStarts(new SingleIterator<S>(s));
            final List<E> results = new LinkedList<E>();
            PipeHelper.fillCollection(this.pipe, results);
            this.map.put(s, results);
            this.currentIterator = results.iterator();
        }
    }

    public List<Pipe> getPipes() {
        return (List) Arrays.asList(pipe);
    }

    public Map<S, List<E>> getMemoization() {
        return this.map;
    }

    public void reset() {
        this.currentIterator = new EmptyIterator<E>();
        try {
            this.map = this.map.getClass().getConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        super.reset();
    }

    public String toString() {
        return PipeHelper.makePipeString(this, this.pipe);
    }
}
{'fraction_non_alphanumeric': 0.129896443043674, 'fraction_numerical': 0.0, 'mean_word_length': 2.509478672985782, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, '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': '4042399', 'n_tokens_mistral': 1431, 'n_tokens_neox': 1397, 'n_words': 287}