github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/examples/sites/latency/gen_latency_immutableGen.go (about)

     1  // Code generated by immutableGen. DO NOT EDIT.
     2  
     3  package main
     4  
     5  //immutableVet:skipFile
     6  
     7  import (
     8  	"myitcv.io/immutable"
     9  )
    10  
    11  //
    12  // latencies is an immutable type and has the following template:
    13  //
    14  // 	map[location]latency
    15  //
    16  type latencies struct {
    17  	theMap  map[location]latency
    18  	mutable bool
    19  	__tmpl  _Imm_latencies
    20  }
    21  
    22  var _ immutable.Immutable = new(latencies)
    23  var _ = new(latencies).__tmpl
    24  
    25  func newLatencies(inits ...func(m *latencies)) *latencies {
    26  	res := newLatenciesCap(0)
    27  	if len(inits) == 0 {
    28  		return res
    29  	}
    30  
    31  	return res.WithMutable(func(m *latencies) {
    32  		for _, i := range inits {
    33  			i(m)
    34  		}
    35  	})
    36  }
    37  
    38  func newLatenciesCap(l int) *latencies {
    39  	return &latencies{
    40  		theMap: make(map[location]latency, l),
    41  	}
    42  }
    43  
    44  func (m *latencies) Mutable() bool {
    45  	return m.mutable
    46  }
    47  
    48  func (m *latencies) Len() int {
    49  	if m == nil {
    50  		return 0
    51  	}
    52  
    53  	return len(m.theMap)
    54  }
    55  
    56  func (m *latencies) Get(k location) (latency, bool) {
    57  	v, ok := m.theMap[k]
    58  	return v, ok
    59  }
    60  
    61  func (m *latencies) AsMutable() *latencies {
    62  	if m == nil {
    63  		return nil
    64  	}
    65  
    66  	if m.Mutable() {
    67  		return m
    68  	}
    69  
    70  	res := m.dup()
    71  	res.mutable = true
    72  
    73  	return res
    74  }
    75  
    76  func (m *latencies) dup() *latencies {
    77  	resMap := make(map[location]latency, len(m.theMap))
    78  
    79  	for k := range m.theMap {
    80  		resMap[k] = m.theMap[k]
    81  	}
    82  
    83  	res := &latencies{
    84  		theMap: resMap,
    85  	}
    86  
    87  	return res
    88  }
    89  
    90  func (m *latencies) AsImmutable(v *latencies) *latencies {
    91  	if m == nil {
    92  		return nil
    93  	}
    94  
    95  	if v == m {
    96  		return m
    97  	}
    98  
    99  	m.mutable = false
   100  	return m
   101  }
   102  
   103  func (m *latencies) Range() map[location]latency {
   104  	if m == nil {
   105  		return nil
   106  	}
   107  
   108  	return m.theMap
   109  }
   110  
   111  func (mr *latencies) WithMutable(f func(l *latencies)) *latencies {
   112  	res := mr.AsMutable()
   113  	f(res)
   114  	res = res.AsImmutable(mr)
   115  
   116  	return res
   117  }
   118  
   119  func (mr *latencies) WithImmutable(f func(l *latencies)) *latencies {
   120  	prev := mr.mutable
   121  	mr.mutable = false
   122  	f(mr)
   123  	mr.mutable = prev
   124  
   125  	return mr
   126  }
   127  
   128  func (m *latencies) Set(k location, v latency) *latencies {
   129  	if m.mutable {
   130  		m.theMap[k] = v
   131  		return m
   132  	}
   133  
   134  	res := m.dup()
   135  	res.theMap[k] = v
   136  
   137  	return res
   138  }
   139  
   140  func (m *latencies) Del(k location) *latencies {
   141  	if _, ok := m.theMap[k]; !ok {
   142  		return m
   143  	}
   144  
   145  	if m.mutable {
   146  		delete(m.theMap, k)
   147  		return m
   148  	}
   149  
   150  	res := m.dup()
   151  	delete(res.theMap, k)
   152  
   153  	return res
   154  }
   155  func (s *latencies) IsDeeplyNonMutable(seen map[interface{}]bool) bool {
   156  	if s == nil {
   157  		return true
   158  	}
   159  
   160  	if s.Mutable() {
   161  		return false
   162  	}
   163  	return true
   164  }