mutable.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package rx
  2. type Cell struct {
  3. value Object
  4. }
  5. func CreateCell(init_val Object) *Cell {
  6. return &Cell { init_val }
  7. }
  8. func (c *Cell) Get() Observable {
  9. return NewSync(func()(Object, bool) {
  10. return c.value, true
  11. })
  12. }
  13. func (c *Cell) Set(new_val Object) Observable {
  14. return NewSync(func()(Object, bool) {
  15. c.value = new_val
  16. return nil, true
  17. })
  18. }
  19. func (c *Cell) Swap(f func(Object)Object) Observable {
  20. return NewSync(func()(Object, bool) {
  21. c.value = f(c.value)
  22. return nil, true
  23. })
  24. }
  25. type StringHashMap map[string] Object
  26. func CreateStringHashMap(m map[string] Object) StringHashMap {
  27. return StringHashMap(m)
  28. }
  29. func (m StringHashMap) Has(key string) Observable {
  30. return NewSync(func()(Object, bool) {
  31. var _, exists = m[key]
  32. return exists, true
  33. })
  34. }
  35. func (m StringHashMap) Get(key string) Observable {
  36. return NewSync(func()(Object, bool) {
  37. var val, exists = m[key]
  38. if exists {
  39. return Optional { true, val }, true
  40. } else {
  41. return Optional {}, true
  42. }
  43. })
  44. }
  45. func (m StringHashMap) Set(key string, val Object) Observable {
  46. return NewSync(func()(Object, bool) {
  47. m[key] = val
  48. return nil, true
  49. })
  50. }
  51. func (m StringHashMap) Delete(key string) Observable {
  52. return NewSync(func()(Object, bool) {
  53. var deleted, exists = m[key]
  54. if exists {
  55. delete(m, key)
  56. return Optional { true, deleted }, true
  57. } else {
  58. return Optional {}, true
  59. }
  60. })
  61. }
  62. type NumberHashMap map[uint] Object
  63. func CreateNumberHashMap(m map[uint] Object) NumberHashMap {
  64. return NumberHashMap(m)
  65. }
  66. func (m NumberHashMap) Has(key uint) Observable {
  67. return NewSync(func()(Object, bool) {
  68. var _, exists = m[key]
  69. return exists, true
  70. })
  71. }
  72. func (m NumberHashMap) Get(key uint) Observable {
  73. return NewSync(func()(Object, bool) {
  74. var val, exists = m[key]
  75. if exists {
  76. return Optional { true, val }, true
  77. } else {
  78. return Optional {}, true
  79. }
  80. })
  81. }
  82. func (m NumberHashMap) Set(key uint, val Object) Observable {
  83. return NewSync(func()(Object, bool) {
  84. m[key] = val
  85. return nil, true
  86. })
  87. }
  88. func (m NumberHashMap) Delete(key uint) Observable {
  89. return NewSync(func()(Object, bool) {
  90. var deleted, exists = m[key]
  91. if exists {
  92. delete(m, key)
  93. return Optional { true, deleted }, true
  94. } else {
  95. return Optional {}, true
  96. }
  97. })
  98. }
  99. type Buffer struct {
  100. data *([] byte)
  101. }
  102. func CreateBuffer(capacity uint) Buffer {
  103. var data = make([] byte, 0, capacity)
  104. return Buffer { &data }
  105. }
  106. func (buf Buffer) Write(bytes ([] byte)) Observable {
  107. return NewSync(func() (Object, bool) {
  108. *buf.data = append(*buf.data, bytes...)
  109. return nil, true
  110. })
  111. }
  112. func (buf Buffer) Dump() Observable {
  113. return NewGoroutine(func(sender Sender) {
  114. var dumped = make([] byte, len(*buf.data))
  115. copy(dumped, *buf.data)
  116. sender.Next(dumped)
  117. sender.Complete()
  118. })
  119. }