package main

type PredicateDict struct {
	idByIRI map[string]uint32
	iriByID []string
}

func NewPredicateDict(predicates []string) *PredicateDict {
	idByIRI := make(map[string]uint32, len(predicates))
	iriByID := make([]string, 0, len(predicates))
	for _, iri := range predicates {
		if iri == "" {
			continue
		}
		if _, ok := idByIRI[iri]; ok {
			continue
		}
		id := uint32(len(iriByID))
		idByIRI[iri] = id
		iriByID = append(iriByID, iri)
	}
	return &PredicateDict{idByIRI: idByIRI, iriByID: iriByID}
}

func (d *PredicateDict) GetOrAdd(iri string) (uint32, bool) {
	if iri == "" {
		return 0, false
	}
	if id, ok := d.idByIRI[iri]; ok {
		return id, true
	}
	id := uint32(len(d.iriByID))
	d.idByIRI[iri] = id
	d.iriByID = append(d.iriByID, iri)
	return id, true
}

func (d *PredicateDict) IRIs() []string {
	return d.iriByID
}
