PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/error.go

https://github.com/feyeleanor/slices
Go | 602 lines | 542 code | 60 blank | 0 comment | 153 complexity | ec4e0015c1bd257aac984bd8524abdb3 MD5 | raw file
Possible License(s): JSON
  1. package slices
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type ESlice []error
  7. func (s ESlice) release_references(i, n int) {
  8. var zero error
  9. for ; n > 0; n-- {
  10. s[i] = zero
  11. i++
  12. }
  13. }
  14. func (s ESlice) Len() int { return len(s) }
  15. func (s ESlice) Cap() int { return cap(s) }
  16. func (s ESlice) At(i int) interface{} { return s[i] }
  17. func (s ESlice) Set(i int, v interface{}) { s[i] = v.(error) }
  18. func (s ESlice) Clear(i int) { s[i] = nil }
  19. func (s ESlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  20. func (s *ESlice) RestrictTo(i, j int) { *s = (*s)[i:j] }
  21. func (s *ESlice) Cut(i, j int) {
  22. a := *s
  23. l := len(a)
  24. if i < 0 {
  25. i = 0
  26. }
  27. if j > l {
  28. j = l
  29. }
  30. if j > i {
  31. n := j - i
  32. copy(a[i:], a[j:l])
  33. a.release_references(l - n, n)
  34. *s = a[:l - n]
  35. }
  36. }
  37. func (s *ESlice) Trim(i, j int) {
  38. a := *s
  39. l := len(a)
  40. if i < 0 {
  41. i = 0
  42. }
  43. if j > l {
  44. j = l
  45. }
  46. if j > i {
  47. copy(a, a[i:j])
  48. n := j - i
  49. a.release_references(n, l - n)
  50. *s = a[:n]
  51. }
  52. }
  53. func (s *ESlice) Delete(i int) {
  54. a := *s
  55. n := len(a)
  56. if i > -1 && i < n {
  57. end := n - 1
  58. copy(a[i:end], a[i + 1:n])
  59. a.release_references(end, 1)
  60. *s = a[:end]
  61. }
  62. }
  63. func (s *ESlice) DeleteIf(f interface{}) {
  64. a := *s
  65. p := 0
  66. switch f := f.(type) {
  67. case error: for i, v := range a {
  68. if i != p {
  69. a[p] = v
  70. }
  71. if v != f {
  72. p++
  73. }
  74. }
  75. case func(error) bool: for i, v := range a {
  76. if i != p {
  77. a[p] = v
  78. }
  79. if !f(v) {
  80. p++
  81. }
  82. }
  83. case func(interface{}) bool: for i, v := range a {
  84. if i != p {
  85. a[p] = v
  86. }
  87. if !f(v) {
  88. p++
  89. }
  90. }
  91. default: panic(f)
  92. }
  93. s.release_references(p, len(a) - p)
  94. *s = a[:p]
  95. }
  96. func (s ESlice) Each(f interface{}) {
  97. switch f := f.(type) {
  98. case func(error): for _, v := range s { f(v) }
  99. case func(int, error): for i, v := range s { f(i, v) }
  100. case func(interface{}, error): for i, v := range s { f(i, v) }
  101. case func(interface{}): for _, v := range s { f(v) }
  102. case func(int, interface{}): for i, v := range s { f(i, v) }
  103. case func(interface{}, interface{}): for i, v := range s { f(i, v) }
  104. default: panic(f)
  105. }
  106. }
  107. func (s ESlice) While(f interface{}) int {
  108. switch f := f.(type) {
  109. case func(interface{}) bool: for i, v := range s {
  110. if !f(v) {
  111. return i
  112. }
  113. }
  114. case func(error) bool: for i, v := range s {
  115. if !f(v) {
  116. return i
  117. }
  118. }
  119. case func(int, interface{}) bool: for i, v := range s {
  120. if !f(i, v) {
  121. return i
  122. }
  123. }
  124. case func(int, error) bool: for i, v := range s {
  125. if !f(i, v) {
  126. return i
  127. }
  128. }
  129. case func(interface{}, interface{}) bool: for i, v := range s {
  130. if !f(i, v) {
  131. return i
  132. }
  133. }
  134. case func(interface{}, error) bool: for i, v := range s {
  135. if !f(i, v) {
  136. return i
  137. }
  138. }
  139. default: panic(f)
  140. }
  141. return len(s)
  142. }
  143. func (s ESlice) Until(f interface{}) int {
  144. switch f := f.(type) {
  145. case func(interface{}) bool: for i, v := range s {
  146. if f(v) {
  147. return i
  148. }
  149. }
  150. case func(error) bool: for i, v := range s {
  151. if f(v) {
  152. return i
  153. }
  154. }
  155. case func(int, interface{}) bool: for i, v := range s {
  156. if f(i, v) {
  157. return i
  158. }
  159. }
  160. case func(int, error) bool: for i, v := range s {
  161. if f(i, v) {
  162. return i
  163. }
  164. }
  165. case func(interface{}, interface{}) bool: for i, v := range s {
  166. if f(i, v) {
  167. return i
  168. }
  169. }
  170. case func(interface{}, error) bool: for i, v := range s {
  171. if f(i, v) {
  172. return i
  173. }
  174. }
  175. default: panic(f)
  176. }
  177. return len(s)
  178. }
  179. func (s ESlice) String() (t string) {
  180. elements := []string{}
  181. for _, v := range s {
  182. elements = append(elements, fmt.Sprintf("%v", v))
  183. }
  184. return fmt.Sprintf("(%v)", strings.Join(elements, " "))
  185. }
  186. func (s ESlice) BlockCopy(destination, source, count int) {
  187. if destination < len(s) {
  188. if end := destination + count; end >= len(s) {
  189. copy(s[destination:], s[source:])
  190. } else {
  191. copy(s[destination : end], s[source:])
  192. }
  193. }
  194. }
  195. func (s ESlice) BlockClear(start, count int) {
  196. if start > -1 && start < len(s) {
  197. copy(s[start:], make(ESlice, count, count))
  198. }
  199. }
  200. func (s ESlice) Overwrite(offset int, container interface{}) {
  201. switch container := container.(type) {
  202. case ESlice: copy(s[offset:], container)
  203. case []error: copy(s[offset:], container)
  204. default: panic(container)
  205. }
  206. }
  207. func (s *ESlice) Reallocate(length, capacity int) {
  208. switch {
  209. case length > capacity: s.Reallocate(capacity, capacity)
  210. case capacity != cap(*s): x := make(ESlice, length, capacity)
  211. copy(x, *s)
  212. *s = x
  213. default: *s = (*s)[:length]
  214. }
  215. }
  216. func (s *ESlice) Extend(n int) {
  217. c := cap(*s)
  218. l := len(*s) + n
  219. if l > c {
  220. c = l
  221. }
  222. s.Reallocate(l, c)
  223. }
  224. func (s *ESlice) Expand(i, n int) {
  225. if i < 0 {
  226. i = 0
  227. }
  228. l := s.Len()
  229. if l < i {
  230. i = l
  231. }
  232. l += n
  233. c := s.Cap()
  234. if c < l {
  235. c = l
  236. }
  237. if c != s.Cap() {
  238. x := make(ESlice, l, c)
  239. copy(x, (*s)[:i])
  240. copy(x[i + n:], (*s)[i:])
  241. *s = x
  242. } else {
  243. a := (*s)[:l]
  244. for j := l - 1; j >= i; j-- {
  245. a[j] = a[j - n]
  246. }
  247. *s = a
  248. }
  249. }
  250. func (s ESlice) Depth() (c int) {
  251. for _, v := range s {
  252. if v, ok := v.(Nested); ok {
  253. if r := v.Depth() + 1; r > c {
  254. c = r
  255. }
  256. }
  257. }
  258. return
  259. }
  260. func (s ESlice) Reverse() {
  261. end := len(s) - 1
  262. for i := 0; i < end; i++ {
  263. s[i], s[end] = s[end], s[i]
  264. end--
  265. }
  266. }
  267. func (s *ESlice) Append(v interface{}) {
  268. switch v := v.(type) {
  269. case error: *s = append(*s, v)
  270. case ESlice: *s = append(*s, v...)
  271. case []error: *s = append(*s, v...)
  272. default: *s = append(*s, v.(error))
  273. }
  274. }
  275. func (s *ESlice) Prepend(v interface{}) {
  276. switch v := v.(type) {
  277. case error: l := s.Len() + 1
  278. n := make(ESlice, l, l)
  279. n[0] = v
  280. copy(n[1:], *s)
  281. *s = n
  282. case ESlice: l := s.Len() + len(v)
  283. n := make(ESlice, l, l)
  284. copy(n, v)
  285. copy(n[len(v):], *s)
  286. *s = n
  287. case []error: s.Prepend(ESlice(v))
  288. default: panic(v)
  289. }
  290. }
  291. func (s ESlice) Repeat(count int) ESlice {
  292. length := len(s) * count
  293. capacity := cap(s)
  294. if capacity < length {
  295. capacity = length
  296. }
  297. destination := make(ESlice, length, capacity)
  298. for start, end := 0, len(s); count > 0; count-- {
  299. copy(destination[start:end], s)
  300. start = end
  301. end += len(s)
  302. }
  303. return destination
  304. }
  305. func (s ESlice) equal(o ESlice) (r bool) {
  306. if len(s) == len(o) {
  307. r = true
  308. for i, v := range s {
  309. switch v := v.(type) {
  310. case Equatable: r = v.Equal(o[i])
  311. default: r = v == o[i]
  312. }
  313. if !r {
  314. return
  315. }
  316. }
  317. }
  318. return
  319. }
  320. func (s ESlice) Equal(o interface{}) (r bool) {
  321. switch o := o.(type) {
  322. case ESlice: r = s.equal(o)
  323. case []error: r = s.equal(o)
  324. }
  325. return
  326. }
  327. func (s ESlice) Car() (h interface{}) {
  328. if s.Len() > 0 {
  329. h = s[0]
  330. }
  331. return
  332. }
  333. func (s ESlice) Cdr() (t ESlice) {
  334. if s.Len() > 1 {
  335. t = s[1:]
  336. }
  337. return
  338. }
  339. func (s *ESlice) Rplaca(v interface{}) {
  340. switch {
  341. case s == nil: *s = ESlice{v.(error)}
  342. case s.Len() == 0: *s = append(*s, v.(error))
  343. default: (*s)[0] = v.(error)
  344. }
  345. }
  346. func (s *ESlice) Rplacd(v interface{}) {
  347. if s == nil {
  348. *s = ESlice{v.(error)}
  349. } else {
  350. ReplaceSlice := func(v ESlice) {
  351. if l := len(v); l < cap(*s) {
  352. copy((*s)[1:], v)
  353. *s = (*s)[:l + 1]
  354. } else {
  355. l++
  356. n := make(ESlice, l, l)
  357. copy(n, (*s)[:1])
  358. copy(n[1:], v)
  359. *s = n
  360. }
  361. }
  362. switch v := v.(type) {
  363. case *ESlice: ReplaceSlice(*v)
  364. case ESlice: ReplaceSlice(v)
  365. case *[]error: ReplaceSlice(ESlice(*v))
  366. case []error: ReplaceSlice(ESlice(v))
  367. case nil: *s = (*s)[:1]
  368. default: (*s)[1] = v.(error)
  369. *s = (*s)[:2]
  370. }
  371. }
  372. }
  373. func (s ESlice) Find(v interface{}) (i int, found bool) {
  374. if v, ok := v.(error); ok {
  375. for j, x := range s {
  376. if x == v {
  377. i = j
  378. found = true
  379. break
  380. }
  381. }
  382. }
  383. return
  384. }
  385. func (s ESlice) FindN(v interface{}, n int) (i ISlice) {
  386. if v, ok := v.(error); ok {
  387. i = make(ISlice, 0, 0)
  388. for j, x := range s {
  389. if x == v {
  390. i = append(i, j)
  391. if len(i) == n {
  392. break
  393. }
  394. }
  395. }
  396. }
  397. return
  398. }
  399. func (s *ESlice) KeepIf(f interface{}) {
  400. a := *s
  401. p := 0
  402. switch f := f.(type) {
  403. case error: for i, v := range a {
  404. if i != p {
  405. a[p] = v
  406. }
  407. if v == f {
  408. p++
  409. }
  410. }
  411. case func(error) bool: for i, v := range a {
  412. if i != p {
  413. a[p] = v
  414. }
  415. if f(v) {
  416. p++
  417. }
  418. }
  419. case func(interface{}) bool: for i, v := range a {
  420. if i != p {
  421. a[p] = v
  422. }
  423. if f(v) {
  424. p++
  425. }
  426. }
  427. default: panic(f)
  428. }
  429. s.release_references(p, len(a) - p)
  430. *s = a[:p]
  431. }
  432. func (s ESlice) ReverseEach(f interface{}) {
  433. switch f := f.(type) {
  434. case func(error): for i := len(s) - 1; i > -1; i-- { f(s[i]) }
  435. case func(int, error): for i := len(s) - 1; i > -1; i-- { f(i, s[i]) }
  436. case func(interface{}, error): for i := len(s) - 1; i > -1; i-- { f(i, s[i]) }
  437. case func(interface{}): for i := len(s) - 1; i > -1; i-- { f(s[i]) }
  438. case func(int, interface{}): for i := len(s) - 1; i > -1; i-- { f(i, s[i]) }
  439. case func(interface{}, interface{}): for i := len(s) - 1; i > -1; i-- { f(i, s[i]) }
  440. default: panic(f)
  441. }
  442. }
  443. func (s ESlice) ReplaceIf(f interface{}, r interface{}) {
  444. replacement := r.(error)
  445. switch f := f.(type) {
  446. case error: for i, v := range s {
  447. if v == f {
  448. s[i] = replacement
  449. }
  450. }
  451. case func(error) bool: for i, v := range s {
  452. if f(v) {
  453. s[i] = replacement
  454. }
  455. }
  456. case func(interface{}) bool: for i, v := range s {
  457. if f(v) {
  458. s[i] = replacement
  459. }
  460. }
  461. default: panic(f)
  462. }
  463. }
  464. func (s *ESlice) Replace(o interface{}) {
  465. switch o := o.(type) {
  466. case error: *s = ESlice{o}
  467. case ESlice: *s = o
  468. case []error: *s = ESlice(o)
  469. default: panic(o)
  470. }
  471. }
  472. func (s ESlice) Select(f interface{}) interface{} {
  473. r := make(ESlice, 0, len(s) / 4)
  474. switch f := f.(type) {
  475. case error: for _, v := range s {
  476. if v == f {
  477. r = append(r, v)
  478. }
  479. }
  480. case func(error) bool: for _, v := range s {
  481. if f(v) {
  482. r = append(r, v)
  483. }
  484. }
  485. case func(interface{}) bool: for _, v := range s {
  486. if f(v) {
  487. r = append(r, v)
  488. }
  489. }
  490. default: panic(f)
  491. }
  492. return r
  493. }
  494. func (s *ESlice) Uniq() {
  495. a := *s
  496. if len(a) > 0 {
  497. p := 0
  498. m := make(map[error] bool)
  499. for _, v := range a {
  500. if ok := m[v]; !ok {
  501. m[v] = true
  502. a[p] = v
  503. p++
  504. }
  505. }
  506. *s = a[:p]
  507. }
  508. }
  509. func (s ESlice) Pick(n ...int) interface{} {
  510. r := make(ESlice, 0, len(n))
  511. for _, v := range n {
  512. r = append(r, s[v])
  513. }
  514. return r
  515. }
  516. func (s *ESlice) Insert(i int, v interface{}) {
  517. switch v := v.(type) {
  518. case error: l := s.Len() + 1
  519. n := make(ESlice, l, l)
  520. copy(n, (*s)[:i])
  521. n[i] = v
  522. copy(n[i + 1:], (*s)[i:])
  523. *s = n
  524. case ESlice: l := s.Len() + len(v)
  525. n := make(ESlice, l, l)
  526. copy(n, (*s)[:i])
  527. copy(n[i:], v)
  528. copy(n[i + len(v):], (*s)[i:])
  529. *s = n
  530. case []error: s.Insert(i, ESlice(v))
  531. default: panic(v)
  532. }
  533. }
  534. func (s *ESlice) Pop() (r error, ok bool) {
  535. if end := s.Len() - 1; end > -1 {
  536. r = (*s)[end]
  537. s.Clear(end)
  538. *s = (*s)[:end]
  539. ok = true
  540. }
  541. return
  542. }