Last change
on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
910 bytes
|
Line | |
---|
1 | "use strict"
|
---|
2 |
|
---|
3 | function unique_pred(list, compare) {
|
---|
4 | var ptr = 1
|
---|
5 | , len = list.length
|
---|
6 | , a=list[0], b=list[0]
|
---|
7 | for(var i=1; i<len; ++i) {
|
---|
8 | b = a
|
---|
9 | a = list[i]
|
---|
10 | if(compare(a, b)) {
|
---|
11 | if(i === ptr) {
|
---|
12 | ptr++
|
---|
13 | continue
|
---|
14 | }
|
---|
15 | list[ptr++] = a
|
---|
16 | }
|
---|
17 | }
|
---|
18 | list.length = ptr
|
---|
19 | return list
|
---|
20 | }
|
---|
21 |
|
---|
22 | function unique_eq(list) {
|
---|
23 | var ptr = 1
|
---|
24 | , len = list.length
|
---|
25 | , a=list[0], b = list[0]
|
---|
26 | for(var i=1; i<len; ++i, b=a) {
|
---|
27 | b = a
|
---|
28 | a = list[i]
|
---|
29 | if(a !== b) {
|
---|
30 | if(i === ptr) {
|
---|
31 | ptr++
|
---|
32 | continue
|
---|
33 | }
|
---|
34 | list[ptr++] = a
|
---|
35 | }
|
---|
36 | }
|
---|
37 | list.length = ptr
|
---|
38 | return list
|
---|
39 | }
|
---|
40 |
|
---|
41 | function unique(list, compare, sorted) {
|
---|
42 | if(list.length === 0) {
|
---|
43 | return list
|
---|
44 | }
|
---|
45 | if(compare) {
|
---|
46 | if(!sorted) {
|
---|
47 | list.sort(compare)
|
---|
48 | }
|
---|
49 | return unique_pred(list, compare)
|
---|
50 | }
|
---|
51 | if(!sorted) {
|
---|
52 | list.sort()
|
---|
53 | }
|
---|
54 | return unique_eq(list)
|
---|
55 | }
|
---|
56 |
|
---|
57 | module.exports = unique
|
---|
Note:
See
TracBrowser
for help on using the repository browser.