uint64.go raw

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