-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsort_strings.go
More file actions
51 lines (45 loc) · 2.13 KB
/
sort_strings.go
File metadata and controls
51 lines (45 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package extsort
import "cmp"
// StringSorter provides external sorting for strings, maintaining backward compatibility
// with the legacy string-specific API. It embeds GenericSorter[string] and uses
// simple byte slice conversion for serialization.
type StringSorter struct {
GenericSorter[string]
}
// fromBytesString converts a byte slice back to a string.
// This is used for deserialization during the external sort process.
// Always succeeds and returns nil error.
func fromBytesString(d []byte) (string, error) {
return string(d), nil
}
// toBytesString converts a string to a byte slice for serialization.
// This enables strings to be written to and read from temporary files during sorting.
// Always succeeds and returns nil error.
func toBytesString(s string) ([]byte, error) {
return []byte(s), nil
}
// Strings performs external sorting on a channel of strings using lexicographic ordering.
// Returns the sorter instance, output channel with sorted strings, and error channel.
// This function provides backward compatibility with the legacy string-specific API.
//
// IMPORTANT: The input channel MUST be closed to signal the end of data.
// Sort() will continue reading from the input channel until it is closed.
func Strings(input <-chan string, config *Config) (*StringSorter, <-chan string, <-chan error) {
genericSorter, output, errChan := Generic(input, fromBytesString, toBytesString, cmp.Compare, config)
if genericSorter == nil {
return nil, output, errChan
}
s := &StringSorter{GenericSorter: *genericSorter}
return s, output, errChan
}
// StringsMock performs external sorting on strings with a mock implementation that limits
// the number of strings to sort. Useful for testing with a controlled dataset size.
// The parameter n specifies the maximum number of strings to process.
func StringsMock(input <-chan string, config *Config, n int) (*StringSorter, <-chan string, <-chan error) {
genericSorter, output, errChan := MockGeneric(input, fromBytesString, toBytesString, cmp.Compare, config, n)
if genericSorter == nil {
return nil, output, errChan
}
s := &StringSorter{GenericSorter: *genericSorter}
return s, output, errChan
}