github.com/philpearl/symboltab@v1.1.4/naive.go (about)

     1  package symboltab
     2  
     3  // Naive implementation of the same function. Really just intended to compare against
     4  type Naive struct {
     5  	m map[string]int32
     6  	i []string
     7  }
     8  
     9  // NewNaive creates a new, basic implementation of the symboltable function
    10  func NewNaive(cap int) *Naive {
    11  	return &Naive{
    12  		m: make(map[string]int32, cap),
    13  		i: make([]string, 0, cap),
    14  	}
    15  }
    16  
    17  // StringToSequence converts a string to a sequence number
    18  func (n *Naive) StringToSequence(val string, addNew bool) (seq int32, found bool) {
    19  	seq, ok := n.m[val]
    20  	if ok {
    21  		return seq, true
    22  	}
    23  	if addNew {
    24  		seq := int32(len(n.m)) + 1
    25  		n.i = append(n.i, val)
    26  		n.m[val] = seq
    27  		return seq, false
    28  	}
    29  	return 0, false
    30  }
    31  
    32  // SequenceToString retrieves the string for a sequence number
    33  func (n *Naive) SequenceToString(seq int32) string {
    34  	return n.i[seq-1]
    35  }