uint32.go raw

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