github.com/ontio/ontology@v1.14.4/vm/neovm/types/map_value.go (about)

     1  /*
     2   * Copyright (C) 2018 The ontology Authors
     3   * This file is part of The ontology library.
     4   *
     5   * The ontology is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU Lesser General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * The ontology is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU Lesser General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU Lesser General Public License
    16   * along with The ontology.  If not, see <http://www.gnu.org/licenses/>.
    17   */
    18  
    19  package types
    20  
    21  import (
    22  	"sort"
    23  )
    24  
    25  type MapValue struct {
    26  	Data map[string][2]VmValue
    27  }
    28  
    29  func NewMapValue() *MapValue {
    30  	return &MapValue{Data: make(map[string][2]VmValue)}
    31  }
    32  func (this *MapValue) Set(key, value VmValue) error {
    33  	skey, err := key.GetMapKey()
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	this.Data[skey] = [2]VmValue{key, value}
    39  	return nil
    40  }
    41  
    42  func (this *MapValue) Reset() {
    43  	this.Data = make(map[string][2]VmValue)
    44  }
    45  
    46  func (this *MapValue) Remove(key VmValue) error {
    47  	skey, err := key.GetMapKey()
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	delete(this.Data, skey)
    53  
    54  	return nil
    55  }
    56  
    57  func (this *MapValue) Get(key VmValue) (value VmValue, ok bool, err error) {
    58  	skey, e := key.GetMapKey()
    59  	if e != nil {
    60  		err = e
    61  		return
    62  	}
    63  
    64  	val, ok := this.Data[skey]
    65  	value = val[1]
    66  	return
    67  }
    68  
    69  func (this *MapValue) GetMapSortedKey() []VmValue {
    70  	sortedKeys := this.getMapSortedKey()
    71  	sortedKey := make([]VmValue, 0, len(sortedKeys))
    72  	for _, k := range sortedKeys {
    73  		sortedKey = append(sortedKey, this.Data[k][0])
    74  	}
    75  	return sortedKey
    76  }
    77  
    78  func (this *MapValue) getMapSortedKey() []string {
    79  	var unsortKey []string
    80  	for k := range this.Data {
    81  		unsortKey = append(unsortKey, k)
    82  	}
    83  	sort.Strings(unsortKey)
    84  	return unsortKey
    85  }
    86  
    87  func (this *MapValue) GetValues() ([]VmValue, error) {
    88  	keys := this.getMapSortedKey()
    89  	values := make([]VmValue, 0, len(this.Data))
    90  	for _, v := range keys {
    91  		values = append(values, this.Data[v][1])
    92  	}
    93  	return values, nil
    94  }