<commit_before>package peergos.shared.messaging;

import peergos.shared.cbor.*;

import java.util.*;
import java.util.stream.*;

/** A generalization of a vector clock that allows changing group membership
 *
 */
public class TreeClock implements Cborable {

    public final SortedMap<Id, Long> time;

    public TreeClock(SortedMap<Id, Long> time) {
        this.time = time;
    }

    public TreeClock merge(TreeClock other) {
        TreeMap<Id, Long> res = new TreeMap<>(time);
        for (Map.Entry<Id, Long> e : other.time.entrySet()) {
            if (! res.containsKey(e.getKey()) || res.get(e.getKey()) < e.getValue())
                res.put(e.getKey(), e.getValue());
        }
        return new TreeClock(res);
    }

    public boolean isBeforeOrEqual(TreeClock b) {
        for (Map.Entry<Id, Long> e : time.entrySet()) {
            if (!b.hasId(e.getKey()) || e.getValue() > b.getEventCounter(e.getKey()))
                return false;
        }
        return true;
    }

    public Set<Id> newMembersFrom(TreeClock other) {
        HashSet<Id> ids = new HashSet<>(other.time.keySet());
        ids.removeAll(time.keySet());
        return ids;
    }

    public TreeClock withMember(Id member) {
        TreeMap<Id, Long> res = new TreeMap<>(time);
        res.put(member, 0L);
        return new TreeClock(res);
    }

    public boolean hasId(Id member) {
        return time.containsKey(member);
    }

    public long getEventCounter(Id member) {
        Long res = time.get(member);
        if (res == null)
            throw new IllegalStateException("Id not present in clock!");
        return res;
    }
<commit_msg>Support removing a member from a TreeClock<commit_after>package peergos.shared.messaging;

import peergos.shared.cbor.*;

import java.util.*;
import java.util.stream.*;

/** A generalization of a vector clock that allows changing group membership
 *
 */
public class TreeClock implements Cborable {

    public final SortedMap<Id, Long> time;

    public TreeClock(SortedMap<Id, Long> time) {
        this.time = time;
    }

    public TreeClock merge(TreeClock other) {
        TreeMap<Id, Long> res = new TreeMap<>(time);
        for (Map.Entry<Id, Long> e : other.time.entrySet()) {
            if (! res.containsKey(e.getKey()) || res.get(e.getKey()) < e.getValue())
                res.put(e.getKey(), e.getValue());
        }
        return new TreeClock(res);
    }

    public boolean isBeforeOrEqual(TreeClock b) {
        for (Map.Entry<Id, Long> e : time.entrySet()) {
            if (!b.hasId(e.getKey()) || e.getValue() > b.getEventCounter(e.getKey()))
                return false;
        }
        return true;
    }

    public TreeClock removeMember(Id remover, Id toRemove) {
        TreeMap<Id, Long> res = new TreeMap<>(time);
        res.remove(toRemove);
        res.put(remover, res.get(remover) + 1);
        return new TreeClock(res);
    }

    public Set<Id> newMembersFrom(TreeClock other) {
        HashSet<Id> ids = new HashSet<>(other.time.keySet());
        ids.removeAll(time.keySet());
        return ids;
    }

    public TreeClock withMember(Id member) {
        TreeMap<Id, Long> res = new TreeMap<>(time);
        res.put(member, 0L);
        return new TreeClock(res);
    }

    public boolean hasId(Id member) {
        return time.containsKey(member);
    }

    public long getEventCounter(Id member) {
        Long res = time.get(member);
        if (res == null)
            throw new IllegalStateException("Id not present in clock!");
        return res;
    }

{'fraction_non_alphanumeric': 0.1298847343266798, 'fraction_numerical': 0.0008434073657576609, 'mean_word_length': 2.424446583253128, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '3620771', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1072, 'n_words': 274}