package ec.gp.semantic.geometry;

import ec.gp.semantic.ISemantics;

import java.util.HashSet;

/**
 * @author Tomasz Pawlak
 */
public class L1ConvexHullDetector implements IConvexHullDetector {

    protected final HashSet<ISemantics> existingSemantics = new HashSet<ISemantics>(1024);

    protected ISemantics minSemantics = null;
    protected ISemantics maxSemantics = null;

    public boolean isEmpty() {
        return this.minSemantics == null;
    }

    public void clear() {
        this.minSemantics = null;
        this.maxSemantics = null;
        this.existingSemantics.clear();
    }

    public int size() {
        return this.existingSemantics.size();
    }

    public void addSupportPoint(ISemantics semantics) {
        assert semantics != null;

        this.existingSemantics.add(semantics);
        if (this.minSemantics == null) {
            this.minSemantics = semantics;
            this.maxSemantics = semantics;
        } else {
            this.minSemantics = this.minSemantics.min(semantics);
            this.maxSemantics = this.maxSemantics.max(semantics);
        }
    }

    public boolean isInConvexHull(ISemantics semantics) {
        assert semantics != null;

        if (this.existingSemantics.size() <= 1) {
            // there is no convex hull
            return false;
        }

        //return this.minSemantics.isAnyComponentSmallerThan(semantics) && this.maxSemantics.isAnyComponentBiggerThan(semantics);
        return !(semantics.isAnyComponentSmallerThan(this.minSemantics) || semantics.isAnyComponentBiggerThan(this.maxSemantics));
    }
}
