uint16.go raw

   1  package types
   2  
   3  import (
   4  	"encoding/binary"
   5  	"io"
   6  )
   7  
   8  // Uint16 is a codec for encoding and decoding 16-bit unsigned integers.
   9  type Uint16 struct {
  10  	value uint16
  11  }
  12  
  13  // Set sets the value as a uint16.
  14  func (c *Uint16) Set(value uint16) {
  15  	c.value = value
  16  }
  17  
  18  // Get gets the value as a uint16.
  19  func (c *Uint16) Get() uint16 {
  20  	return c.value
  21  }
  22  
  23  // SetInt sets the value as an int, converting it to uint16. Truncates values outside uint16 range (0-65535).
  24  func (c *Uint16) SetInt(value int) {
  25  	c.value = uint16(value)
  26  }
  27  
  28  // GetInt gets the value as an int, converted from uint16.
  29  func (c *Uint16) GetInt() int {
  30  	return int(c.value)
  31  }
  32  
  33  // MarshalWrite writes the uint16 value to the provided writer in BigEndian order.
  34  func (c *Uint16) MarshalWrite(w io.Writer) error {
  35  	return binary.Write(w, binary.BigEndian, c.value)
  36  }
  37  
  38  // UnmarshalRead reads a uint16 value from the provided reader in BigEndian order.
  39  func (c *Uint16) UnmarshalRead(r io.Reader) error {
  40  	return binary.Read(r, binary.BigEndian, &c.value)
  41  }
  42  
  43  type Uint16s []*Uint16
  44  
  45  // Union computes the union of the current Uint16s slice with another Uint16s slice. The result
  46  // contains all unique elements from both slices.
  47  func (s Uint16s) Union(other Uint16s) Uint16s {
  48  	valueMap := make(map[uint16]bool)
  49  	var result Uint16s
  50  
  51  	// Add elements from the current Uint16s slice to the result
  52  	for _, item := range s {
  53  		val := item.Get()
  54  		if !valueMap[val] {
  55  			valueMap[val] = true
  56  			result = append(result, item)
  57  		}
  58  	}
  59  
  60  	// Add elements from the other Uint16s slice to the result
  61  	for _, item := range other {
  62  		val := item.Get()
  63  		if !valueMap[val] {
  64  			valueMap[val] = true
  65  			result = append(result, item)
  66  		}
  67  	}
  68  
  69  	return result
  70  }
  71  
  72  // Intersection computes the intersection of the current Uint16s slice with another Uint16s
  73  // slice. The result contains only the elements that exist in both slices.
  74  func (s Uint16s) Intersection(other Uint16s) Uint16s {
  75  	valueMap := make(map[uint16]bool)
  76  	var result Uint16s
  77  
  78  	// Add all elements from the other Uint16s slice to the map
  79  	for _, item := range other {
  80  		valueMap[item.Get()] = true
  81  	}
  82  
  83  	// Check for common elements in the current Uint16s slice
  84  	for _, item := range s {
  85  		val := item.Get()
  86  		if valueMap[val] {
  87  			result = append(result, item)
  88  		}
  89  	}
  90  
  91  	return result
  92  }
  93  
  94  // Difference computes the difference of the current Uint16s slice with another Uint16s slice.
  95  // The result contains only the elements that are in the current slice but not in the other
  96  // slice.
  97  func (s Uint16s) Difference(other Uint16s) Uint16s {
  98  	valueMap := make(map[uint16]bool)
  99  	var result Uint16s
 100  
 101  	// Mark all elements in the other Uint16s slice
 102  	for _, item := range other {
 103  		valueMap[item.Get()] = true
 104  	}
 105  
 106  	// Add elements from the current Uint16s slice that are not in the other Uint16s slice
 107  	for _, item := range s {
 108  		val := item.Get()
 109  		if !valueMap[val] {
 110  			result = append(result, item)
 111  		}
 112  	}
 113  
 114  	return result
 115  }
 116