diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 9ec0734..397fe0e 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -39,4 +39,4 @@ jobs: - uses: actions/setup-go@v4 - run: go install golang.org/x/lint/golint@latest - run: golint -set_exit_status ./... - - run: go test ./... + - run: go test ./... -v diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cdbc4ec..3a76473 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,5 +17,3 @@ repos: rev: v2.1.0 hooks: - id: codespell - exclude: | - (?x)^.*(\.go)$ diff --git a/README.md b/README.md index 902721c..a19b314 100644 --- a/README.md +++ b/README.md @@ -3,186 +3,161 @@ Slice is a Go package that offers a versatile set of pre-built slices with extended functionality. It abstracts common list operations, such as appending, deleting, concatenating, mapping, and more, making it easier to work with slices in Go. -Gopher artwork was sourced from [egonelbre/gophers](https://github.com/egonelbre/gophers). [![Go Reference](https://pkg.go.dev/badge/github.com/lindsaygelle/slice.svg)](https://pkg.go.dev/github.com/lindsaygelle/slice) [![GitHub](https://img.shields.io/github/license/lindsaygelle/slice)](/LICENSE) -## Features -* Common List Operations: Slice provides a comprehensive set of list operations like appending, deleting, concatenating, mapping, reversing, and more, all seamlessly integrated into single package. -* Memory Management: Slice takes care of allocating and deallocating memory as needed, making your code cleaner and safer. -* Type-Agnostic: Slice works effortlessly with a mix of data types, making it a valuable tool for dealing with collections of data. -* Generic Go Struct Wrapping: You can easily wrap the slice.Slice type in your custom Go struct to handle type-specific operations, allowing for robust, type-safe code. -* Intuitive Constructor Functions: Each Slice interface comes with a dedicated constructor function for creating slices conveniently. -* No Underlying Slice Exposure: To prevent mixing incompatible data types, Slice interfaces do not expose the underlying slice.Slice. While recommended for custom implementations, it's not mandatory. - - ## Installation -Add slice as a dependency to your Go project using the following command: - +Getting started with Slice is a breeze. You can install it in your Go project using `go get`: ```sh -go get -u github.com/lindsaygelle/slice +go get github.com/lindsaygelle/slice ``` -## Docker -You can utilize slice within a Docker container with the provided Dockerfile. Here's how to build and run the container: +## Usage +To begin using Slice, simply import the package into your Go code: -Building the Docker container. -```sh -docker build . -t slice +```Go +import ( + "github.com/lindsaygelle/slice" +) ``` -Developing and running Go from within the Docker container. -```sh -docker run -it --rm --name slice slice +Creating and Initializing a Slice: +```Go +// Create an empty slice of integers +s := &slice.Slice[int]{} ``` -A docker-compose file has also been added for convenience. -```sh -docker-compose up -d +Appending Elements to a Slice: +```Go +// Append values to the slice +s.Append(1, 2, 3) ``` -## Usage -To create a slice for a provided data type, you can use a provided constructor function. - +Getting the Length of a Slice: ```Go -// Example for string slice -s := slice.NewString("apple", "banana", "cherry") +// Get the length of the slice +length := s.Length() ``` -Use the methods provided by the initialized slice interface to perform various list-like operations. Here are some common operations: - +Deleting an Element from a Slice: ```Go -// Append elements to the slice -s.Append("date", "fig") - -// Check if an index is within bounds -inBounds := s.Bounds(2) - -// Pop the last element from the slice -poppedElement := s.Pop() +// Delete an element at index 2 +s.Delete(2) ``` -You can sort the slice and iterate over its elements easily: - +Iterating Over a Slice: ```Go -// Example for sorting and iteration -sortedSlice := s.Sort() -sortedSlice.Each(func(index int, value string) { - fmt.Println(index, value) +// Iterate over the slice and print each element +s.Each(func(index int, value int) { + fmt.Printf("Index: %d, Value: %d\n", index, value) }) ``` -That's it! You're ready to use the slice package to simplify your slice operations in Go. Explore the package documentation for a complete list of available methods and advanced usage patterns. - +Reversing a Slice: +```Go +// Reverse the order of elements in the slice +s.Reverse() +``` -## Slices +Slicing a Slice: +```Go +// Slice the slice from index 1 to 3 +s.Slice(1, 3) +``` -Each slice is designed to handle a specific Go type. You can easily create, manipulate, and check bounds on these slices through the provided methods. +Swapping Elements in a Slice: +```Go +// Swap elements at indices 1 and 2 +s.Swap(1, 2) +``` +More complicated examples: ```Go -package main +// Create a slice of strings +strSlice := &slice.Slice[string]{"apple", "banana", "cherry"} -import ( - "fmt" +// Append multiple values to the slice +strSlice.Append("date", "elderberry") - "github.com/lindsaygelle/slice" -) +// Check if the slice contains a specific value +containsCherry := strSlice.Contains("cherry") // Should return true -var ( - // []byte - b slice.Byte - // []complex64 - c64 slice.Complex64 - // []complex128 - c128 slice.Complex128 - // []float32 - f32 slice.Float32 - // []float64 - f64 slice.Float64 - // []interface{} - i slice.Int - // []int8 - i8 slice.Int8 - // []int16 - i16 slice.Int16 - // []int32 - i32 slice.Int32 - // []int64 - i64 slice.Int64 - // []rune - r slice.Rune - // []interface{} - s *slice.Slice - // []uint - u slice.UInt - // []uint8 - u8 slice.UInt8 - // []uint16 - u16 slice.UInt16 - // []uint32 - u32 slice.UInt32 - // []uint64 - u64 slice.UInt64 -) +// Replace the element at index 2 with "grape" +strSlice.Replace(2, "grape") + +// Get the length of the slice +strLength := strSlice.Length() + +// Iterate over the slice and print each element +strSlice.Each(func(index int, value string) { + fmt.Printf("Index %d: %s\n", index, value) +}) ``` +Using a complex type: ```Go -import ( - "github.com/lindsaygelle/slice" -) +// Define a custom struct +type Person struct { + Name string + Age int + Email string +} -func main() { - var ( - numbers = slice.NewInt(6, 1, 2, 3) - ) - numbers.Sort().Each(func(i int, n int) { - fmt.Println(i, n) // (0, 1), (1, 2), (2, 3), (3, 6) - }) +// Create a slice of Person structs +people := &slice.Slice[Person]{ + {Name: "Alice", Age: 30, Email: "alice@example.com"}, + {Name: "Bob", Age: 25, Email: "bob@example.com"}, } -``` -## Extending +// Append a new person to the slice +newPerson := Person{Name: "Charlie", Age: 35, Email: "charlie@example.com"} +people.Append(newPerson) -Slice supports interface extension by wrapping the Slice in an struct and exposing a corresponding interface. This is the same pattern implemented by this package and is used for the provided interfaces. +// Find the index of a person with a specific email address +index := people.FindIndex(func(p Person) bool { + return p.Email == "bob@example.com" +}) -```Go -package food +// Slice the slice to include only people aged 30 or older +people.Slice(1, people.Length()) -import ( - "github.com/lindsaygelle/slice" -) +// Reverse the order of people in the slice +people.Reverse() -// Food is a struct that describes food. -type Food struct { - Name string -} +// Iterate over the slice and print each person's details +people.Each(func(index int, person Person) { + fmt.Printf("Index %d: Name: %s, Age: %d, Email: %s\n", index, person.Name, person.Age, person.Email) +}) +``` -// FoodSlice is an interface that contains a collection of Food. -type FoodSlice interface { - Append(Food) FoodSlice - Prepend(Food) FoodSlice -} +## Docker +Slice is Docker-friendly! You can easily incorporate Slice into a Docker container using the provided Dockerfile. Here are the steps to build and run the container: -// food is a struct that interfaces with slice.Slice. -type food struct { s *slice.Slice } +Building the Docker container: +```sh +docker build . -t slice +``` -// Append adds Food structs to the tail of the FoodSlice. -func (f *food) Append(food ...Food) FoodSlice { - f.s.Append(food...) - return f -} +Developing and running Go within the Docker container: +```sh +docker run -it --rm --name slice slice +``` -// Prepend adds Food structs to the head of the FoodSlice. -func (f *food) Prepend(food ...Food) FoodSlice { - f.s.Prepend(food...) - return f -} +A docker-compose file has also been included for convenience: +```sh +docker-compose up -d ``` ## Contributing -Contributions to Slice are welcome! If you have any ideas, bug reports, or enhancements, please submit them as GitHub issues or create a pull request with your changes. For major contributions, it is recommended to discuss your ideas first by creating an issue to ensure alignment with the project's goals and direction. Please see the [CONTRIBUTION](./CONTRIBUTING.md) file fore more details. +We warmly welcome contributions to Slice. Whether you have innovative ideas, bug reports, or enhancements in mind, please share them with us by submitting GitHub issues or creating pull requests. For substantial contributions, it's a good practice to start a discussion by creating an issue to ensure alignment with the project's goals and direction. Refer to the [CONTRIBUTING](./CONTRIBUTING.md) file for comprehensive details. + +## Branching +For a smooth collaboration experience, we have established branch naming conventions and guidelines. Please consult the [BRANCH_NAMING_CONVENTION](./BRANCH_NAMING_CONVENTION.md) document for comprehensive information and best practices. ## License -Slice is licensed under the MIT License. Feel free to use, modify, and distribute the code within this repository as per the terms of the license. Please see the [LICENSE](./LICENSE) file for more details. +Slice is released under the MIT License, granting you the freedom to use, modify, and distribute the code within this repository in accordance with the terms of the license. For additional information, please review the [LICENSE](./LICENSE) file. + +## Acknowledgements +We express our gratitude to [egonelbre/gophers](https://github.com/egonelbre/gophers) for providing the delightful Gopher artwork used in our social preview. Don't hesitate to pay them a visit! diff --git a/byte.go b/byte.go deleted file mode 100644 index 056080d..0000000 --- a/byte.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// Byte is the interface that handles a byte collection. -type Byte interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...byte) Byte - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...byte) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s Byte) Byte - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s Byte) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) Byte - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, byte)) Byte - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, byte) bool) Byte - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, byte)) Byte - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, byte) bool) Byte - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) byte - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (byte, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (byte, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (byte, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) Byte - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...byte) Byte - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...byte) Byte - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, byte) byte) Byte - // Poll removes the first element from the slice and returns that removed element. - Poll() byte - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (byte, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (byte, bool) - // Pop removes the last element from the slice and returns that element. - Pop() byte - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (byte, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (byte, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s Byte) Byte - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s Byte) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...byte) Byte - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...byte) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...byte) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v byte) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() Byte - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() Byte - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) Byte - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...byte) int - // Values returns the internal values of the slice. - Values() []byte -} - -// NewByte returns a new Byte interface. -func NewByte(i ...byte) Byte { - return (&byteContainer{&Slice{}}).Append(i...) -} - -type byteContainer struct{ s *Slice } - -// Append implements Append for Byte. -func (u *byteContainer) Append(i ...byte) Byte { - u.s.Append(byteToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for Byte. -func (u *byteContainer) AppendLength(i ...byte) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for Byte. -func (u *byteContainer) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for Byte. -func (u *byteContainer) Concatenate(v Byte) Byte { - u.s.Concatenate(v.(*byteContainer).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for Byte. -func (u *byteContainer) ConcatenateLength(v Byte) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for Byte. -func (u *byteContainer) Delete(i int) Byte { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for Byte. -func (u *byteContainer) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for Byte. -func (u *byteContainer) Each(fn func(int, byte)) Byte { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(byte))) - }) - return u -} - -// EachBreak implements EachBreak for Byte. -func (u *byteContainer) EachBreak(fn func(int, byte) bool) Byte { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(byte))) - }) - return u -} - -// EachReverse implements EachReverse for Byte. -func (u *byteContainer) EachReverse(fn func(int, byte)) Byte { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(byte))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for Byte. -func (u *byteContainer) EachReverseBreak(fn func(int, byte) bool) Byte { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(byte))) - }) - return u -} - -// Fetch implements Fetch for Byte. -func (u *byteContainer) Fetch(i int) byte { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for Byte. -func (u *byteContainer) FetchLength(i int) (byte, int) { - v, i := u.s.FetchLength(i) - return v.(byte), i -} - -// Get implements Get for Byte. -func (u *byteContainer) Get(i int) (byte, bool) { - var ( - ok bool - s byte - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(byte) - } - return s, ok -} - -// GetLength implements GetLength for Byte. -func (u *byteContainer) GetLength(i int) (byte, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(byte), l, ok -} - -// Len implements Len for Byte. -func (u *byteContainer) Len() int { - return u.s.Len() -} - -// Less implements Less for Byte. -func (u *byteContainer) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for Byte. -func (u *byteContainer) Make(i int) Byte { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for Byte. -func (u *byteContainer) MakeEach(v ...byte) Byte { - u.s.MakeEach(byteToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for Byte. -func (u *byteContainer) MakeEachReverse(v ...byte) Byte { - u.s.MakeEachReverse(byteToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for Byte. -func (u *byteContainer) Map(fn func(int, byte) byte) Byte { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(byte))) - }) - return u -} - -// Poll implements Poll for Byte. -func (u *byteContainer) Poll() byte { - var ( - s byte - v = u.s.Poll() - ) - if v != nil { - s = (v.(byte)) - } - return s -} - -// PollLength implements PollLength for Byte. -func (u *byteContainer) PollLength() (byte, int) { - v, l := u.s.PollLength() - return v.(byte), l -} - -// PollOK implements PollOK for Byte. -func (u *byteContainer) PollOK() (byte, bool) { - v, ok := u.s.PollOK() - return v.(byte), ok -} - -// Pop implements Pop for Byte. -func (u *byteContainer) Pop() byte { - var ( - s byte - v = u.s.Pop() - ) - if v != nil { - s = (v.(byte)) - } - return s -} - -// PopLength implements PopLength for Byte. -func (u *byteContainer) PopLength() (byte, int) { - v, l := u.s.PopLength() - return v.(byte), l -} - -// PopOK implements PopOK for Byte. -func (u *byteContainer) PopOK() (byte, bool) { - v, ok := u.s.PopOK() - return v.(byte), ok -} - -// Precatenate implements Precatenate for Byte. -func (u *byteContainer) Precatenate(v Byte) Byte { - u.s.Precatenate(v.(*byteContainer).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for Byte. -func (u *byteContainer) PrecatenateLength(v Byte) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for Byte. -func (u *byteContainer) Prepend(i ...byte) Byte { - u.s.Prepend(byteToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for Byte. -func (u *byteContainer) PrependLength(v ...byte) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for Byte. -func (u *byteContainer) Push(i ...byte) int { - return u.s.Push(byteToInterfaceSlice(i...)) -} - -// Replace implements Replace for Byte. -func (u *byteContainer) Replace(i int, n byte) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for Byte. -func (u *byteContainer) Reverse() Byte { - u.s.Reverse() - return u -} - -// Set implements Set for Byte. -func (u *byteContainer) Set() Byte { - u.s.Set() - return u -} - -// Slice implements Slice for Byte. -func (u *byteContainer) Slice(i int, j int) Byte { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for Byte. -func (u *byteContainer) Sort() Byte { - sort.Sort(u) - return u -} - -// Swap implements Swap for Byte. -func (u *byteContainer) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for Byte. -func (u *byteContainer) Unshift(i ...byte) int { - return (u.s.Unshift(byteToInterfaceSlice(i...))) -} - -// Values implements Values for Byte. -func (u *byteContainer) Values() []byte { - var v = make([]byte, u.Len()) - u.Each(func(i int, n byte) { - v[i] = n - }) - return v -} - -func byteToInterfaceSlice(n ...byte) []interface{} { - var ( - i int - v byte - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/complex128.go b/complex128.go deleted file mode 100644 index 777e8ae..0000000 --- a/complex128.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// Complex128 is the interface that handles a complex128 collection. -type Complex128 interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...complex128) Complex128 - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...complex128) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s Complex128) Complex128 - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s Complex128) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) Complex128 - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, complex128)) Complex128 - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, complex128) bool) Complex128 - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, complex128)) Complex128 - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, complex128) bool) Complex128 - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) complex128 - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (complex128, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (complex128, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (complex128, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) Complex128 - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...complex128) Complex128 - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...complex128) Complex128 - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, complex128) complex128) Complex128 - // Poll removes the first element from the slice and returns that removed element. - Poll() complex128 - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (complex128, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (complex128, bool) - // Pop removes the last element from the slice and returns that element. - Pop() complex128 - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (complex128, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (complex128, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s Complex128) Complex128 - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s Complex128) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...complex128) Complex128 - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...complex128) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...complex128) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v complex128) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() Complex128 - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() Complex128 - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) Complex128 - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...complex128) int - // Values returns the internal values of the slice. - Values() []complex128 -} - -// NewComplex128 returns a new Complex128 interface. -func NewComplex128(i ...complex128) Complex128 { - return (&complex128Container{&Slice{}}).Append(i...) -} - -type complex128Container struct{ s *Slice } - -// Append implements Append for Complex128. -func (u *complex128Container) Append(i ...complex128) Complex128 { - u.s.Append(complex128ToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for Complex128. -func (u *complex128Container) AppendLength(i ...complex128) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for Complex128. -func (u *complex128Container) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for Complex128. -func (u *complex128Container) Concatenate(v Complex128) Complex128 { - u.s.Concatenate(v.(*complex128Container).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for Complex128. -func (u *complex128Container) ConcatenateLength(v Complex128) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for Complex128. -func (u *complex128Container) Delete(i int) Complex128 { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for Complex128. -func (u *complex128Container) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for Complex128. -func (u *complex128Container) Each(fn func(int, complex128)) Complex128 { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(complex128))) - }) - return u -} - -// EachBreak implements EachBreak for Complex128. -func (u *complex128Container) EachBreak(fn func(int, complex128) bool) Complex128 { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(complex128))) - }) - return u -} - -// EachReverse implements EachReverse for Complex128. -func (u *complex128Container) EachReverse(fn func(int, complex128)) Complex128 { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(complex128))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for Complex128. -func (u *complex128Container) EachReverseBreak(fn func(int, complex128) bool) Complex128 { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(complex128))) - }) - return u -} - -// Fetch implements Fetch for Complex128. -func (u *complex128Container) Fetch(i int) complex128 { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for Complex128. -func (u *complex128Container) FetchLength(i int) (complex128, int) { - v, i := u.s.FetchLength(i) - return v.(complex128), i -} - -// Get implements Get for Complex128. -func (u *complex128Container) Get(i int) (complex128, bool) { - var ( - ok bool - s complex128 - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(complex128) - } - return s, ok -} - -// GetLength implements GetLength for Complex128. -func (u *complex128Container) GetLength(i int) (complex128, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(complex128), l, ok -} - -// Len implements Len for Complex128. -func (u *complex128Container) Len() int { - return u.s.Len() -} - -// Less implements Less for Complex128. -func (u *complex128Container) Less(i int, j int) bool { - return real(u.Fetch(i)) < real(u.Fetch(j)) -} - -// Make implements Make for Complex128. -func (u *complex128Container) Make(i int) Complex128 { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for Complex128. -func (u *complex128Container) MakeEach(v ...complex128) Complex128 { - u.s.MakeEach(complex128ToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for Complex128. -func (u *complex128Container) MakeEachReverse(v ...complex128) Complex128 { - u.s.MakeEachReverse(complex128ToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for Complex128. -func (u *complex128Container) Map(fn func(int, complex128) complex128) Complex128 { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(complex128))) - }) - return u -} - -// Poll implements Poll for Complex128. -func (u *complex128Container) Poll() complex128 { - var ( - s complex128 - v = u.s.Poll() - ) - if v != nil { - s = (v.(complex128)) - } - return s -} - -// PollLength implements PollLength for Complex128. -func (u *complex128Container) PollLength() (complex128, int) { - v, l := u.s.PollLength() - return v.(complex128), l -} - -// PollOK implements PollOK for Complex128. -func (u *complex128Container) PollOK() (complex128, bool) { - v, ok := u.s.PollOK() - return v.(complex128), ok -} - -// Pop implements Pop for Complex128. -func (u *complex128Container) Pop() complex128 { - var ( - s complex128 - v = u.s.Pop() - ) - if v != nil { - s = (v.(complex128)) - } - return s -} - -// PopLength implements PopLength for Complex128. -func (u *complex128Container) PopLength() (complex128, int) { - v, l := u.s.PopLength() - return v.(complex128), l -} - -// PopOK implements PopOK for Complex128. -func (u *complex128Container) PopOK() (complex128, bool) { - v, ok := u.s.PopOK() - return v.(complex128), ok -} - -// Precatenate implements Precatenate for Complex128. -func (u *complex128Container) Precatenate(v Complex128) Complex128 { - u.s.Precatenate(v.(*complex128Container).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for Complex128. -func (u *complex128Container) PrecatenateLength(v Complex128) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for Complex128. -func (u *complex128Container) Prepend(i ...complex128) Complex128 { - u.s.Prepend(complex128ToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for Complex128. -func (u *complex128Container) PrependLength(v ...complex128) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for Complex128. -func (u *complex128Container) Push(i ...complex128) int { - return u.s.Push(complex128ToInterfaceSlice(i...)) -} - -// Replace implements Replace for Complex128. -func (u *complex128Container) Replace(i int, n complex128) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for Complex128. -func (u *complex128Container) Reverse() Complex128 { - u.s.Reverse() - return u -} - -// Set implements Set for Complex128. -func (u *complex128Container) Set() Complex128 { - u.s.Set() - return u -} - -// Slice implements Slice for Complex128. -func (u *complex128Container) Slice(i int, j int) Complex128 { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for Complex128. -func (u *complex128Container) Sort() Complex128 { - sort.Sort(u) - return u -} - -// Swap implements Swap for Complex128. -func (u *complex128Container) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for Complex128. -func (u *complex128Container) Unshift(i ...complex128) int { - return (u.s.Unshift(complex128ToInterfaceSlice(i...))) -} - -// Values implements Values for Complex128. -func (u *complex128Container) Values() []complex128 { - var v = make([]complex128, u.Len()) - u.Each(func(i int, n complex128) { - v[i] = n - }) - return v -} - -func complex128ToInterfaceSlice(n ...complex128) []interface{} { - var ( - i int - v complex128 - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/complex64.go b/complex64.go deleted file mode 100644 index 9208a85..0000000 --- a/complex64.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// Complex64 is the interface that handles a complex64 collection. -type Complex64 interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...complex64) Complex64 - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...complex64) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s Complex64) Complex64 - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s Complex64) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) Complex64 - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, complex64)) Complex64 - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, complex64) bool) Complex64 - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, complex64)) Complex64 - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, complex64) bool) Complex64 - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) complex64 - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (complex64, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (complex64, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (complex64, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) Complex64 - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...complex64) Complex64 - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...complex64) Complex64 - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, complex64) complex64) Complex64 - // Poll removes the first element from the slice and returns that removed element. - Poll() complex64 - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (complex64, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (complex64, bool) - // Pop removes the last element from the slice and returns that element. - Pop() complex64 - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (complex64, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (complex64, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s Complex64) Complex64 - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s Complex64) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...complex64) Complex64 - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...complex64) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...complex64) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v complex64) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() Complex64 - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() Complex64 - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) Complex64 - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...complex64) int - // Values returns the internal values of the slice. - Values() []complex64 -} - -// NewComplex64 returns a new Complex64 interface. -func NewComplex64(i ...complex64) Complex64 { - return (&complex64Container{&Slice{}}).Append(i...) -} - -type complex64Container struct{ s *Slice } - -// Append implements Append for Complex64. -func (u *complex64Container) Append(i ...complex64) Complex64 { - u.s.Append(complex64ToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for Complex64. -func (u *complex64Container) AppendLength(i ...complex64) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for Complex64. -func (u *complex64Container) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for Complex64. -func (u *complex64Container) Concatenate(v Complex64) Complex64 { - u.s.Concatenate(v.(*complex64Container).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for Complex64. -func (u *complex64Container) ConcatenateLength(v Complex64) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for Complex64. -func (u *complex64Container) Delete(i int) Complex64 { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for Complex64. -func (u *complex64Container) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for Complex64. -func (u *complex64Container) Each(fn func(int, complex64)) Complex64 { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(complex64))) - }) - return u -} - -// EachBreak implements EachBreak for Complex64. -func (u *complex64Container) EachBreak(fn func(int, complex64) bool) Complex64 { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(complex64))) - }) - return u -} - -// EachReverse implements EachReverse for Complex64. -func (u *complex64Container) EachReverse(fn func(int, complex64)) Complex64 { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(complex64))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for Complex64. -func (u *complex64Container) EachReverseBreak(fn func(int, complex64) bool) Complex64 { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(complex64))) - }) - return u -} - -// Fetch implements Fetch for Complex64. -func (u *complex64Container) Fetch(i int) complex64 { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for Complex64. -func (u *complex64Container) FetchLength(i int) (complex64, int) { - v, i := u.s.FetchLength(i) - return v.(complex64), i -} - -// Get implements Get for Complex64. -func (u *complex64Container) Get(i int) (complex64, bool) { - var ( - ok bool - s complex64 - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(complex64) - } - return s, ok -} - -// GetLength implements GetLength for Complex64. -func (u *complex64Container) GetLength(i int) (complex64, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(complex64), l, ok -} - -// Len implements Len for Complex64. -func (u *complex64Container) Len() int { - return u.s.Len() -} - -// Less implements Less for Complex64. -func (u *complex64Container) Less(i int, j int) bool { - return real(u.Fetch(i)) < real(u.Fetch(j)) -} - -// Make implements Make for Complex64. -func (u *complex64Container) Make(i int) Complex64 { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for Complex64. -func (u *complex64Container) MakeEach(v ...complex64) Complex64 { - u.s.MakeEach(complex64ToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for Complex64. -func (u *complex64Container) MakeEachReverse(v ...complex64) Complex64 { - u.s.MakeEachReverse(complex64ToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for Complex64. -func (u *complex64Container) Map(fn func(int, complex64) complex64) Complex64 { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(complex64))) - }) - return u -} - -// Poll implements Poll for Complex64. -func (u *complex64Container) Poll() complex64 { - var ( - s complex64 - v = u.s.Poll() - ) - if v != nil { - s = (v.(complex64)) - } - return s -} - -// PollLength implements PollLength for Complex64. -func (u *complex64Container) PollLength() (complex64, int) { - v, l := u.s.PollLength() - return v.(complex64), l -} - -// PollOK implements PollOK for Complex64. -func (u *complex64Container) PollOK() (complex64, bool) { - v, ok := u.s.PollOK() - return v.(complex64), ok -} - -// Pop implements Pop for Complex64. -func (u *complex64Container) Pop() complex64 { - var ( - s complex64 - v = u.s.Pop() - ) - if v != nil { - s = (v.(complex64)) - } - return s -} - -// PopLength implements PopLength for Complex64. -func (u *complex64Container) PopLength() (complex64, int) { - v, l := u.s.PopLength() - return v.(complex64), l -} - -// PopOK implements PopOK for Complex64. -func (u *complex64Container) PopOK() (complex64, bool) { - v, ok := u.s.PopOK() - return v.(complex64), ok -} - -// Precatenate implements Precatenate for Complex64. -func (u *complex64Container) Precatenate(v Complex64) Complex64 { - u.s.Precatenate(v.(*complex64Container).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for Complex64. -func (u *complex64Container) PrecatenateLength(v Complex64) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for Complex64. -func (u *complex64Container) Prepend(i ...complex64) Complex64 { - u.s.Prepend(complex64ToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for Complex64. -func (u *complex64Container) PrependLength(v ...complex64) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for Complex64. -func (u *complex64Container) Push(i ...complex64) int { - return u.s.Push(complex64ToInterfaceSlice(i...)) -} - -// Replace implements Replace for Complex64. -func (u *complex64Container) Replace(i int, n complex64) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for Complex64. -func (u *complex64Container) Reverse() Complex64 { - u.s.Reverse() - return u -} - -// Set implements Set for Complex64. -func (u *complex64Container) Set() Complex64 { - u.s.Set() - return u -} - -// Slice implements Slice for Complex64. -func (u *complex64Container) Slice(i int, j int) Complex64 { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for Complex64. -func (u *complex64Container) Sort() Complex64 { - sort.Sort(u) - return u -} - -// Swap implements Swap for Complex64. -func (u *complex64Container) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for Complex64. -func (u *complex64Container) Unshift(i ...complex64) int { - return (u.s.Unshift(complex64ToInterfaceSlice(i...))) -} - -// Values implements Values for Complex64. -func (u *complex64Container) Values() []complex64 { - var v = make([]complex64, u.Len()) - u.Each(func(i int, n complex64) { - v[i] = n - }) - return v -} - -func complex64ToInterfaceSlice(n ...complex64) []interface{} { - var ( - i int - v complex64 - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/doc.go b/doc.go index 93b96cf..465e711 100644 --- a/doc.go +++ b/doc.go @@ -1,15 +1,20 @@ -// Package slice is a package of slice interfaces to handle common list-like operations. +// Package slice provides a versatile Go library for effortless slice management. // -// Slice contains a single Slice struct that exposes methods to perform traversal and mutation operations -// for a collection of Go interfaces. The Slice struct can be extended to handle -// the acceptance and selection of interface specific types. To extend the Slice an interface -// can be defined that calls the exposed Slice methods. +// Welcome to the Slice package, a powerful and versatile Go library designed to simplify and elevate your experience with slices in Go. +// This package provides a generic slice implementation that goes beyond the basics, offering an extensive range of methods for traversal and mutation operations based on numeric indices. // -// Package slice comes with all Go primative types as interfaces out of the box. +// Why Slice? // -// Each slice interface comes with a constructor function that takes zero to n arguments of the -// slice interface type. +// Slices are fundamental data structures in Go, but managing them efficiently can often be a complex and error-prone task. +// The Slice package steps in to alleviate these challenges, offering a comprehensive set of utilities that make working with slices a breeze. // -// The slice interfaces to not expose the underlying interface slice to prevent a dirty reference. -// This pattern should be adopted when wrapping the Slice struct. +// Key Features: +// +// - Traverse with Confidence: Access and navigate your slices with ease using a rich set of traversal methods. +// - Effortless Mutation: Seamlessly modify your slices by leveraging a variety of mutation operations based on numeric indices. +// - Enhanced Functionality: Go beyond the basics with extended functionality that simplifies complex slice manipulations. +// +// Package Documentation: +// +// Explore the full range of features and functions available in the Slice package by referring to the comprehensive package documentation. package slice diff --git a/float32.go b/float32.go deleted file mode 100644 index 1e7cf80..0000000 --- a/float32.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// Float32 is the interface that handles a float32 collection. -type Float32 interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...float32) Float32 - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...float32) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s Float32) Float32 - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s Float32) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) Float32 - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, float32)) Float32 - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, float32) bool) Float32 - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, float32)) Float32 - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, float32) bool) Float32 - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) float32 - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (float32, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (float32, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (float32, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) Float32 - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...float32) Float32 - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...float32) Float32 - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, float32) float32) Float32 - // Poll removes the first element from the slice and returns that removed element. - Poll() float32 - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (float32, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (float32, bool) - // Pop removes the last element from the slice and returns that element. - Pop() float32 - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (float32, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (float32, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s Float32) Float32 - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s Float32) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...float32) Float32 - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...float32) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...float32) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v float32) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() Float32 - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() Float32 - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) Float32 - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...float32) int - // Values returns the internal values of the slice. - Values() []float32 -} - -// NewFloat32 returns a new Float32 interface. -func NewFloat32(i ...float32) Float32 { - return (&float32Container{&Slice{}}).Append(i...) -} - -type float32Container struct{ s *Slice } - -// Append implements Append for Float32. -func (u *float32Container) Append(i ...float32) Float32 { - u.s.Append(float32ToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for Float32. -func (u *float32Container) AppendLength(i ...float32) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for Float32. -func (u *float32Container) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for Float32. -func (u *float32Container) Concatenate(v Float32) Float32 { - u.s.Concatenate(v.(*float32Container).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for Float32. -func (u *float32Container) ConcatenateLength(v Float32) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for Float32. -func (u *float32Container) Delete(i int) Float32 { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for Float32. -func (u *float32Container) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for Float32. -func (u *float32Container) Each(fn func(int, float32)) Float32 { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(float32))) - }) - return u -} - -// EachBreak implements EachBreak for Float32. -func (u *float32Container) EachBreak(fn func(int, float32) bool) Float32 { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(float32))) - }) - return u -} - -// EachReverse implements EachReverse for Float32. -func (u *float32Container) EachReverse(fn func(int, float32)) Float32 { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(float32))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for Float32. -func (u *float32Container) EachReverseBreak(fn func(int, float32) bool) Float32 { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(float32))) - }) - return u -} - -// Fetch implements Fetch for Float32. -func (u *float32Container) Fetch(i int) float32 { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for Float32. -func (u *float32Container) FetchLength(i int) (float32, int) { - v, i := u.s.FetchLength(i) - return v.(float32), i -} - -// Get implements Get for Float32. -func (u *float32Container) Get(i int) (float32, bool) { - var ( - ok bool - s float32 - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(float32) - } - return s, ok -} - -// GetLength implements GetLength for Float32. -func (u *float32Container) GetLength(i int) (float32, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(float32), l, ok -} - -// Len implements Len for Float32. -func (u *float32Container) Len() int { - return u.s.Len() -} - -// Less implements Less for Float32. -func (u *float32Container) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for Float32. -func (u *float32Container) Make(i int) Float32 { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for Float32. -func (u *float32Container) MakeEach(v ...float32) Float32 { - u.s.MakeEach(float32ToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for Float32. -func (u *float32Container) MakeEachReverse(v ...float32) Float32 { - u.s.MakeEachReverse(float32ToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for Float32. -func (u *float32Container) Map(fn func(int, float32) float32) Float32 { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(float32))) - }) - return u -} - -// Poll implements Poll for Float32. -func (u *float32Container) Poll() float32 { - var ( - s float32 - v = u.s.Poll() - ) - if v != nil { - s = (v.(float32)) - } - return s -} - -// PollLength implements PollLength for Float32. -func (u *float32Container) PollLength() (float32, int) { - v, l := u.s.PollLength() - return v.(float32), l -} - -// PollOK implements PollOK for Float32. -func (u *float32Container) PollOK() (float32, bool) { - v, ok := u.s.PollOK() - return v.(float32), ok -} - -// Pop implements Pop for Float32. -func (u *float32Container) Pop() float32 { - var ( - s float32 - v = u.s.Pop() - ) - if v != nil { - s = (v.(float32)) - } - return s -} - -// PopLength implements PopLength for Float32. -func (u *float32Container) PopLength() (float32, int) { - v, l := u.s.PopLength() - return v.(float32), l -} - -// PopOK implements PopOK for Float32. -func (u *float32Container) PopOK() (float32, bool) { - v, ok := u.s.PopOK() - return v.(float32), ok -} - -// Precatenate implements Precatenate for Float32. -func (u *float32Container) Precatenate(v Float32) Float32 { - u.s.Precatenate(v.(*float32Container).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for Float32. -func (u *float32Container) PrecatenateLength(v Float32) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for Float32. -func (u *float32Container) Prepend(i ...float32) Float32 { - u.s.Prepend(float32ToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for Float32. -func (u *float32Container) PrependLength(v ...float32) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for Float32. -func (u *float32Container) Push(i ...float32) int { - return u.s.Push(float32ToInterfaceSlice(i...)) -} - -// Replace implements Replace for Float32. -func (u *float32Container) Replace(i int, n float32) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for Float32. -func (u *float32Container) Reverse() Float32 { - u.s.Reverse() - return u -} - -// Set implements Set for Float32. -func (u *float32Container) Set() Float32 { - u.s.Set() - return u -} - -// Slice implements Slice for Float32. -func (u *float32Container) Slice(i int, j int) Float32 { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for Float32. -func (u *float32Container) Sort() Float32 { - sort.Sort(u) - return u -} - -// Swap implements Swap for Float32. -func (u *float32Container) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for Float32. -func (u *float32Container) Unshift(i ...float32) int { - return (u.s.Unshift(float32ToInterfaceSlice(i...))) -} - -// Values implements Values for Float32. -func (u *float32Container) Values() []float32 { - var v = make([]float32, u.Len()) - u.Each(func(i int, n float32) { - v[i] = n - }) - return v -} - -func float32ToInterfaceSlice(n ...float32) []interface{} { - var ( - i int - v float32 - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/float64.go b/float64.go deleted file mode 100644 index 736549c..0000000 --- a/float64.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// Float64 is the interface that handles a float64 collection. -type Float64 interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...float64) Float64 - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...float64) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s Float64) Float64 - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s Float64) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) Float64 - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, float64)) Float64 - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, float64) bool) Float64 - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, float64)) Float64 - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, float64) bool) Float64 - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) float64 - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (float64, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (float64, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (float64, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) Float64 - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...float64) Float64 - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...float64) Float64 - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, float64) float64) Float64 - // Poll removes the first element from the slice and returns that removed element. - Poll() float64 - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (float64, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (float64, bool) - // Pop removes the last element from the slice and returns that element. - Pop() float64 - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (float64, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (float64, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s Float64) Float64 - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s Float64) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...float64) Float64 - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...float64) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...float64) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v float64) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() Float64 - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() Float64 - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) Float64 - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...float64) int - // Values returns the internal values of the slice. - Values() []float64 -} - -// NewFloat64 returns a new Float64 interface. -func NewFloat64(i ...float64) Float64 { - return (&float64Container{&Slice{}}).Append(i...) -} - -type float64Container struct{ s *Slice } - -// Append implements Append for Float64. -func (u *float64Container) Append(i ...float64) Float64 { - u.s.Append(float64ToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for Float64. -func (u *float64Container) AppendLength(i ...float64) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for Float64. -func (u *float64Container) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for Float64. -func (u *float64Container) Concatenate(v Float64) Float64 { - u.s.Concatenate(v.(*float64Container).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for Float64. -func (u *float64Container) ConcatenateLength(v Float64) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for Float64. -func (u *float64Container) Delete(i int) Float64 { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for Float64. -func (u *float64Container) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for Float64. -func (u *float64Container) Each(fn func(int, float64)) Float64 { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(float64))) - }) - return u -} - -// EachBreak implements EachBreak for Float64. -func (u *float64Container) EachBreak(fn func(int, float64) bool) Float64 { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(float64))) - }) - return u -} - -// EachReverse implements EachReverse for Float64. -func (u *float64Container) EachReverse(fn func(int, float64)) Float64 { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(float64))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for Float64. -func (u *float64Container) EachReverseBreak(fn func(int, float64) bool) Float64 { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(float64))) - }) - return u -} - -// Fetch implements Fetch for Float64. -func (u *float64Container) Fetch(i int) float64 { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for Float64. -func (u *float64Container) FetchLength(i int) (float64, int) { - v, i := u.s.FetchLength(i) - return v.(float64), i -} - -// Get implements Get for Float64. -func (u *float64Container) Get(i int) (float64, bool) { - var ( - ok bool - s float64 - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(float64) - } - return s, ok -} - -// GetLength implements GetLength for Float64. -func (u *float64Container) GetLength(i int) (float64, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(float64), l, ok -} - -// Len implements Len for Float64. -func (u *float64Container) Len() int { - return u.s.Len() -} - -// Less implements Less for Float64. -func (u *float64Container) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for Float64. -func (u *float64Container) Make(i int) Float64 { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for Float64. -func (u *float64Container) MakeEach(v ...float64) Float64 { - u.s.MakeEach(float64ToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for Float64. -func (u *float64Container) MakeEachReverse(v ...float64) Float64 { - u.s.MakeEachReverse(float64ToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for Float64. -func (u *float64Container) Map(fn func(int, float64) float64) Float64 { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(float64))) - }) - return u -} - -// Poll implements Poll for Float64. -func (u *float64Container) Poll() float64 { - var ( - s float64 - v = u.s.Poll() - ) - if v != nil { - s = (v.(float64)) - } - return s -} - -// PollLength implements PollLength for Float64. -func (u *float64Container) PollLength() (float64, int) { - v, l := u.s.PollLength() - return v.(float64), l -} - -// PollOK implements PollOK for Float64. -func (u *float64Container) PollOK() (float64, bool) { - v, ok := u.s.PollOK() - return v.(float64), ok -} - -// Pop implements Pop for Float64. -func (u *float64Container) Pop() float64 { - var ( - s float64 - v = u.s.Pop() - ) - if v != nil { - s = (v.(float64)) - } - return s -} - -// PopLength implements PopLength for Float64. -func (u *float64Container) PopLength() (float64, int) { - v, l := u.s.PopLength() - return v.(float64), l -} - -// PopOK implements PopOK for Float64. -func (u *float64Container) PopOK() (float64, bool) { - v, ok := u.s.PopOK() - return v.(float64), ok -} - -// Precatenate implements Precatenate for Float64. -func (u *float64Container) Precatenate(v Float64) Float64 { - u.s.Precatenate(v.(*float64Container).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for Float64. -func (u *float64Container) PrecatenateLength(v Float64) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for Float64. -func (u *float64Container) Prepend(i ...float64) Float64 { - u.s.Prepend(float64ToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for Float64. -func (u *float64Container) PrependLength(v ...float64) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for Float64. -func (u *float64Container) Push(i ...float64) int { - return u.s.Push(float64ToInterfaceSlice(i...)) -} - -// Replace implements Replace for Float64. -func (u *float64Container) Replace(i int, n float64) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for Float64. -func (u *float64Container) Reverse() Float64 { - u.s.Reverse() - return u -} - -// Set implements Set for Float64. -func (u *float64Container) Set() Float64 { - u.s.Set() - return u -} - -// Slice implements Slice for Float64. -func (u *float64Container) Slice(i int, j int) Float64 { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for Float64. -func (u *float64Container) Sort() Float64 { - sort.Sort(u) - return u -} - -// Swap implements Swap for Float64. -func (u *float64Container) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for Float64. -func (u *float64Container) Unshift(i ...float64) int { - return (u.s.Unshift(float64ToInterfaceSlice(i...))) -} - -// Values implements Values for Float64. -func (u *float64Container) Values() []float64 { - var v = make([]float64, u.Len()) - u.Each(func(i int, n float64) { - v[i] = n - }) - return v -} - -func float64ToInterfaceSlice(n ...float64) []interface{} { - var ( - i int - v float64 - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/go.mod b/go.mod index 36814b0..706b820 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/gellel/slice +module github.com/lindsaygelle/slice go 1.21 diff --git a/int.go b/int.go deleted file mode 100644 index 878e625..0000000 --- a/int.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// Int is the interface that handles a int collection. -type Int interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...int) Int - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...int) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s Int) Int - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s Int) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) Int - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, int)) Int - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, int) bool) Int - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, int)) Int - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, int) bool) Int - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) int - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (int, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (int, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (int, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) Int - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...int) Int - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...int) Int - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, int) int) Int - // Poll removes the first element from the slice and returns that removed element. - Poll() int - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (int, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (int, bool) - // Pop removes the last element from the slice and returns that element. - Pop() int - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (int, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (int, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s Int) Int - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s Int) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...int) Int - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...int) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...int) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v int) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() Int - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() Int - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) Int - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...int) int - // Values returns the internal values of the slice. - Values() []int -} - -// NewInt returns a new Int interface. -func NewInt(i ...int) Int { - return (&intContainer{&Slice{}}).Append(i...) -} - -type intContainer struct{ s *Slice } - -// Append implements Append for Int. -func (u *intContainer) Append(i ...int) Int { - u.s.Append(intToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for Int. -func (u *intContainer) AppendLength(i ...int) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for Int. -func (u *intContainer) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for Int. -func (u *intContainer) Concatenate(v Int) Int { - u.s.Concatenate(v.(*intContainer).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for Int. -func (u *intContainer) ConcatenateLength(v Int) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for Int. -func (u *intContainer) Delete(i int) Int { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for Int. -func (u *intContainer) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for Int. -func (u *intContainer) Each(fn func(int, int)) Int { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(int))) - }) - return u -} - -// EachBreak implements EachBreak for Int. -func (u *intContainer) EachBreak(fn func(int, int) bool) Int { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(int))) - }) - return u -} - -// EachReverse implements EachReverse for Int. -func (u *intContainer) EachReverse(fn func(int, int)) Int { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(int))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for Int. -func (u *intContainer) EachReverseBreak(fn func(int, int) bool) Int { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(int))) - }) - return u -} - -// Fetch implements Fetch for Int. -func (u *intContainer) Fetch(i int) int { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for Int. -func (u *intContainer) FetchLength(i int) (int, int) { - v, i := u.s.FetchLength(i) - return v.(int), i -} - -// Get implements Get for Int. -func (u *intContainer) Get(i int) (int, bool) { - var ( - ok bool - s int - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(int) - } - return s, ok -} - -// GetLength implements GetLength for Int. -func (u *intContainer) GetLength(i int) (int, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(int), l, ok -} - -// Len implements Len for Int. -func (u *intContainer) Len() int { - return u.s.Len() -} - -// Less implements Less for Int. -func (u *intContainer) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for Int. -func (u *intContainer) Make(i int) Int { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for Int. -func (u *intContainer) MakeEach(v ...int) Int { - u.s.MakeEach(intToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for Int. -func (u *intContainer) MakeEachReverse(v ...int) Int { - u.s.MakeEachReverse(intToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for Int. -func (u *intContainer) Map(fn func(int, int) int) Int { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(int))) - }) - return u -} - -// Poll implements Poll for Int. -func (u *intContainer) Poll() int { - var ( - s int - v = u.s.Poll() - ) - if v != nil { - s = (v.(int)) - } - return s -} - -// PollLength implements PollLength for Int. -func (u *intContainer) PollLength() (int, int) { - v, l := u.s.PollLength() - return v.(int), l -} - -// PollOK implements PollOK for Int. -func (u *intContainer) PollOK() (int, bool) { - v, ok := u.s.PollOK() - return v.(int), ok -} - -// Pop implements Pop for Int. -func (u *intContainer) Pop() int { - var ( - s int - v = u.s.Pop() - ) - if v != nil { - s = (v.(int)) - } - return s -} - -// PopLength implements PopLength for Int. -func (u *intContainer) PopLength() (int, int) { - v, l := u.s.PopLength() - return v.(int), l -} - -// PopOK implements PopOK for Int. -func (u *intContainer) PopOK() (int, bool) { - v, ok := u.s.PopOK() - return v.(int), ok -} - -// Precatenate implements Precatenate for Int. -func (u *intContainer) Precatenate(v Int) Int { - u.s.Precatenate(v.(*intContainer).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for Int. -func (u *intContainer) PrecatenateLength(v Int) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for Int. -func (u *intContainer) Prepend(i ...int) Int { - u.s.Prepend(intToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for Int. -func (u *intContainer) PrependLength(v ...int) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for Int. -func (u *intContainer) Push(i ...int) int { - return u.s.Push(intToInterfaceSlice(i...)) -} - -// Replace implements Replace for Int. -func (u *intContainer) Replace(i int, n int) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for Int. -func (u *intContainer) Reverse() Int { - u.s.Reverse() - return u -} - -// Set implements Set for Int. -func (u *intContainer) Set() Int { - u.s.Set() - return u -} - -// Slice implements Slice for Int. -func (u *intContainer) Slice(i int, j int) Int { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for Int. -func (u *intContainer) Sort() Int { - sort.Sort(u) - return u -} - -// Swap implements Swap for Int. -func (u *intContainer) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for Int. -func (u *intContainer) Unshift(i ...int) int { - return (u.s.Unshift(intToInterfaceSlice(i...))) -} - -// Values implements Values for Int. -func (u *intContainer) Values() []int { - var v = make([]int, u.Len()) - u.Each(func(i int, n int) { - v[i] = n - }) - return v -} - -func intToInterfaceSlice(n ...int) []interface{} { - var ( - i int - v int - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/int16.go b/int16.go deleted file mode 100644 index 3f3f7a9..0000000 --- a/int16.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// Int16 is the interface that handles a int16 collection. -type Int16 interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...int16) Int16 - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...int16) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s Int16) Int16 - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s Int16) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) Int16 - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, int16)) Int16 - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, int16) bool) Int16 - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, int16)) Int16 - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, int16) bool) Int16 - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) int16 - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (int16, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (int16, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (int16, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) Int16 - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...int16) Int16 - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...int16) Int16 - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, int16) int16) Int16 - // Poll removes the first element from the slice and returns that removed element. - Poll() int16 - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (int16, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (int16, bool) - // Pop removes the last element from the slice and returns that element. - Pop() int16 - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (int16, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (int16, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s Int16) Int16 - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s Int16) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...int16) Int16 - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...int16) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...int16) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v int16) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() Int16 - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() Int16 - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) Int16 - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...int16) int - // Values returns the internal values of the slice. - Values() []int16 -} - -// NewInt16 returns a new Int16 interface. -func NewInt16(i ...int16) Int16 { - return (&int16Container{&Slice{}}).Append(i...) -} - -type int16Container struct{ s *Slice } - -// Append implements Append for Int16. -func (u *int16Container) Append(i ...int16) Int16 { - u.s.Append(int16ToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for Int16. -func (u *int16Container) AppendLength(i ...int16) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for Int16. -func (u *int16Container) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for Int16. -func (u *int16Container) Concatenate(v Int16) Int16 { - u.s.Concatenate(v.(*int16Container).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for Int16. -func (u *int16Container) ConcatenateLength(v Int16) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for Int16. -func (u *int16Container) Delete(i int) Int16 { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for Int16. -func (u *int16Container) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for Int16. -func (u *int16Container) Each(fn func(int, int16)) Int16 { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(int16))) - }) - return u -} - -// EachBreak implements EachBreak for Int16. -func (u *int16Container) EachBreak(fn func(int, int16) bool) Int16 { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(int16))) - }) - return u -} - -// EachReverse implements EachReverse for Int16. -func (u *int16Container) EachReverse(fn func(int, int16)) Int16 { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(int16))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for Int16. -func (u *int16Container) EachReverseBreak(fn func(int, int16) bool) Int16 { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(int16))) - }) - return u -} - -// Fetch implements Fetch for Int16. -func (u *int16Container) Fetch(i int) int16 { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for Int16. -func (u *int16Container) FetchLength(i int) (int16, int) { - v, i := u.s.FetchLength(i) - return v.(int16), i -} - -// Get implements Get for Int16. -func (u *int16Container) Get(i int) (int16, bool) { - var ( - ok bool - s int16 - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(int16) - } - return s, ok -} - -// GetLength implements GetLength for Int16. -func (u *int16Container) GetLength(i int) (int16, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(int16), l, ok -} - -// Len implements Len for Int16. -func (u *int16Container) Len() int { - return u.s.Len() -} - -// Less implements Less for Int16. -func (u *int16Container) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for Int16. -func (u *int16Container) Make(i int) Int16 { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for Int16. -func (u *int16Container) MakeEach(v ...int16) Int16 { - u.s.MakeEach(int16ToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for Int16. -func (u *int16Container) MakeEachReverse(v ...int16) Int16 { - u.s.MakeEachReverse(int16ToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for Int16. -func (u *int16Container) Map(fn func(int, int16) int16) Int16 { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(int16))) - }) - return u -} - -// Poll implements Poll for Int16. -func (u *int16Container) Poll() int16 { - var ( - s int16 - v = u.s.Poll() - ) - if v != nil { - s = (v.(int16)) - } - return s -} - -// PollLength implements PollLength for Int16. -func (u *int16Container) PollLength() (int16, int) { - v, l := u.s.PollLength() - return v.(int16), l -} - -// PollOK implements PollOK for Int16. -func (u *int16Container) PollOK() (int16, bool) { - v, ok := u.s.PollOK() - return v.(int16), ok -} - -// Pop implements Pop for Int16. -func (u *int16Container) Pop() int16 { - var ( - s int16 - v = u.s.Pop() - ) - if v != nil { - s = (v.(int16)) - } - return s -} - -// PopLength implements PopLength for Int16. -func (u *int16Container) PopLength() (int16, int) { - v, l := u.s.PopLength() - return v.(int16), l -} - -// PopOK implements PopOK for Int16. -func (u *int16Container) PopOK() (int16, bool) { - v, ok := u.s.PopOK() - return v.(int16), ok -} - -// Precatenate implements Precatenate for Int16. -func (u *int16Container) Precatenate(v Int16) Int16 { - u.s.Precatenate(v.(*int16Container).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for Int16. -func (u *int16Container) PrecatenateLength(v Int16) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for Int16. -func (u *int16Container) Prepend(i ...int16) Int16 { - u.s.Prepend(int16ToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for Int16. -func (u *int16Container) PrependLength(v ...int16) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for Int16. -func (u *int16Container) Push(i ...int16) int { - return u.s.Push(int16ToInterfaceSlice(i...)) -} - -// Replace implements Replace for Int16. -func (u *int16Container) Replace(i int, n int16) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for Int16. -func (u *int16Container) Reverse() Int16 { - u.s.Reverse() - return u -} - -// Set implements Set for Int16. -func (u *int16Container) Set() Int16 { - u.s.Set() - return u -} - -// Slice implements Slice for Int16. -func (u *int16Container) Slice(i int, j int) Int16 { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for Int16. -func (u *int16Container) Sort() Int16 { - sort.Sort(u) - return u -} - -// Swap implements Swap for Int16. -func (u *int16Container) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for Int16. -func (u *int16Container) Unshift(i ...int16) int { - return (u.s.Unshift(int16ToInterfaceSlice(i...))) -} - -// Values implements Values for Int16. -func (u *int16Container) Values() []int16 { - var v = make([]int16, u.Len()) - u.Each(func(i int, n int16) { - v[i] = n - }) - return v -} - -func int16ToInterfaceSlice(n ...int16) []interface{} { - var ( - i int - v int16 - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/int32.go b/int32.go deleted file mode 100644 index e2be7db..0000000 --- a/int32.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// Int32 is the interface that handles a int32 collection. -type Int32 interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...int32) Int32 - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...int32) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s Int32) Int32 - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s Int32) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) Int32 - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, int32)) Int32 - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, int32) bool) Int32 - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, int32)) Int32 - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, int32) bool) Int32 - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) int32 - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (int32, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (int32, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (int32, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) Int32 - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...int32) Int32 - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...int32) Int32 - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, int32) int32) Int32 - // Poll removes the first element from the slice and returns that removed element. - Poll() int32 - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (int32, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (int32, bool) - // Pop removes the last element from the slice and returns that element. - Pop() int32 - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (int32, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (int32, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s Int32) Int32 - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s Int32) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...int32) Int32 - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...int32) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...int32) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v int32) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() Int32 - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() Int32 - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) Int32 - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...int32) int - // Values returns the internal values of the slice. - Values() []int32 -} - -// NewInt32 returns a new Int32 interface. -func NewInt32(i ...int32) Int32 { - return (&int32Container{&Slice{}}).Append(i...) -} - -type int32Container struct{ s *Slice } - -// Append implements Append for Int32. -func (u *int32Container) Append(i ...int32) Int32 { - u.s.Append(int32ToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for Int32. -func (u *int32Container) AppendLength(i ...int32) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for Int32. -func (u *int32Container) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for Int32. -func (u *int32Container) Concatenate(v Int32) Int32 { - u.s.Concatenate(v.(*int32Container).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for Int32. -func (u *int32Container) ConcatenateLength(v Int32) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for Int32. -func (u *int32Container) Delete(i int) Int32 { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for Int32. -func (u *int32Container) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for Int32. -func (u *int32Container) Each(fn func(int, int32)) Int32 { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(int32))) - }) - return u -} - -// EachBreak implements EachBreak for Int32. -func (u *int32Container) EachBreak(fn func(int, int32) bool) Int32 { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(int32))) - }) - return u -} - -// EachReverse implements EachReverse for Int32. -func (u *int32Container) EachReverse(fn func(int, int32)) Int32 { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(int32))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for Int32. -func (u *int32Container) EachReverseBreak(fn func(int, int32) bool) Int32 { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(int32))) - }) - return u -} - -// Fetch implements Fetch for Int32. -func (u *int32Container) Fetch(i int) int32 { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for Int32. -func (u *int32Container) FetchLength(i int) (int32, int) { - v, i := u.s.FetchLength(i) - return v.(int32), i -} - -// Get implements Get for Int32. -func (u *int32Container) Get(i int) (int32, bool) { - var ( - ok bool - s int32 - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(int32) - } - return s, ok -} - -// GetLength implements GetLength for Int32. -func (u *int32Container) GetLength(i int) (int32, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(int32), l, ok -} - -// Len implements Len for Int32. -func (u *int32Container) Len() int { - return u.s.Len() -} - -// Less implements Less for Int32. -func (u *int32Container) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for Int32. -func (u *int32Container) Make(i int) Int32 { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for Int32. -func (u *int32Container) MakeEach(v ...int32) Int32 { - u.s.MakeEach(int32ToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for Int32. -func (u *int32Container) MakeEachReverse(v ...int32) Int32 { - u.s.MakeEachReverse(int32ToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for Int32. -func (u *int32Container) Map(fn func(int, int32) int32) Int32 { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(int32))) - }) - return u -} - -// Poll implements Poll for Int32. -func (u *int32Container) Poll() int32 { - var ( - s int32 - v = u.s.Poll() - ) - if v != nil { - s = (v.(int32)) - } - return s -} - -// PollLength implements PollLength for Int32. -func (u *int32Container) PollLength() (int32, int) { - v, l := u.s.PollLength() - return v.(int32), l -} - -// PollOK implements PollOK for Int32. -func (u *int32Container) PollOK() (int32, bool) { - v, ok := u.s.PollOK() - return v.(int32), ok -} - -// Pop implements Pop for Int32. -func (u *int32Container) Pop() int32 { - var ( - s int32 - v = u.s.Pop() - ) - if v != nil { - s = (v.(int32)) - } - return s -} - -// PopLength implements PopLength for Int32. -func (u *int32Container) PopLength() (int32, int) { - v, l := u.s.PopLength() - return v.(int32), l -} - -// PopOK implements PopOK for Int32. -func (u *int32Container) PopOK() (int32, bool) { - v, ok := u.s.PopOK() - return v.(int32), ok -} - -// Precatenate implements Precatenate for Int32. -func (u *int32Container) Precatenate(v Int32) Int32 { - u.s.Precatenate(v.(*int32Container).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for Int32. -func (u *int32Container) PrecatenateLength(v Int32) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for Int32. -func (u *int32Container) Prepend(i ...int32) Int32 { - u.s.Prepend(int32ToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for Int32. -func (u *int32Container) PrependLength(v ...int32) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for Int32. -func (u *int32Container) Push(i ...int32) int { - return u.s.Push(int32ToInterfaceSlice(i...)) -} - -// Replace implements Replace for Int32. -func (u *int32Container) Replace(i int, n int32) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for Int32. -func (u *int32Container) Reverse() Int32 { - u.s.Reverse() - return u -} - -// Set implements Set for Int32. -func (u *int32Container) Set() Int32 { - u.s.Set() - return u -} - -// Slice implements Slice for Int32. -func (u *int32Container) Slice(i int, j int) Int32 { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for Int32. -func (u *int32Container) Sort() Int32 { - sort.Sort(u) - return u -} - -// Swap implements Swap for Int32. -func (u *int32Container) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for Int32. -func (u *int32Container) Unshift(i ...int32) int { - return (u.s.Unshift(int32ToInterfaceSlice(i...))) -} - -// Values implements Values for Int32. -func (u *int32Container) Values() []int32 { - var v = make([]int32, u.Len()) - u.Each(func(i int, n int32) { - v[i] = n - }) - return v -} - -func int32ToInterfaceSlice(n ...int32) []interface{} { - var ( - i int - v int32 - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/int64.go b/int64.go deleted file mode 100644 index 47ba47f..0000000 --- a/int64.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// Int64 is the interface that handles a int64 collection. -type Int64 interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...int64) Int64 - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...int64) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s Int64) Int64 - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s Int64) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) Int64 - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, int64)) Int64 - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, int64) bool) Int64 - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, int64)) Int64 - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, int64) bool) Int64 - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) int64 - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (int64, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (int64, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (int64, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) Int64 - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...int64) Int64 - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...int64) Int64 - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, int64) int64) Int64 - // Poll removes the first element from the slice and returns that removed element. - Poll() int64 - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (int64, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (int64, bool) - // Pop removes the last element from the slice and returns that element. - Pop() int64 - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (int64, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (int64, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s Int64) Int64 - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s Int64) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...int64) Int64 - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...int64) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...int64) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v int64) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() Int64 - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() Int64 - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) Int64 - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...int64) int - // Values returns the internal values of the slice. - Values() []int64 -} - -// NewInt64 returns a new Int64 interface. -func NewInt64(i ...int64) Int64 { - return (&int64Container{&Slice{}}).Append(i...) -} - -type int64Container struct{ s *Slice } - -// Append implements Append for Int64. -func (u *int64Container) Append(i ...int64) Int64 { - u.s.Append(int64ToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for Int64. -func (u *int64Container) AppendLength(i ...int64) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for Int64. -func (u *int64Container) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for Int64. -func (u *int64Container) Concatenate(v Int64) Int64 { - u.s.Concatenate(v.(*int64Container).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for Int64. -func (u *int64Container) ConcatenateLength(v Int64) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for Int64. -func (u *int64Container) Delete(i int) Int64 { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for Int64. -func (u *int64Container) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for Int64. -func (u *int64Container) Each(fn func(int, int64)) Int64 { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(int64))) - }) - return u -} - -// EachBreak implements EachBreak for Int64. -func (u *int64Container) EachBreak(fn func(int, int64) bool) Int64 { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(int64))) - }) - return u -} - -// EachReverse implements EachReverse for Int64. -func (u *int64Container) EachReverse(fn func(int, int64)) Int64 { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(int64))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for Int64. -func (u *int64Container) EachReverseBreak(fn func(int, int64) bool) Int64 { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(int64))) - }) - return u -} - -// Fetch implements Fetch for Int64. -func (u *int64Container) Fetch(i int) int64 { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for Int64. -func (u *int64Container) FetchLength(i int) (int64, int) { - v, i := u.s.FetchLength(i) - return v.(int64), i -} - -// Get implements Get for Int64. -func (u *int64Container) Get(i int) (int64, bool) { - var ( - ok bool - s int64 - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(int64) - } - return s, ok -} - -// GetLength implements GetLength for Int64. -func (u *int64Container) GetLength(i int) (int64, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(int64), l, ok -} - -// Len implements Len for Int64. -func (u *int64Container) Len() int { - return u.s.Len() -} - -// Less implements Less for Int64. -func (u *int64Container) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for Int64. -func (u *int64Container) Make(i int) Int64 { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for Int64. -func (u *int64Container) MakeEach(v ...int64) Int64 { - u.s.MakeEach(int64ToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for Int64. -func (u *int64Container) MakeEachReverse(v ...int64) Int64 { - u.s.MakeEachReverse(int64ToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for Int64. -func (u *int64Container) Map(fn func(int, int64) int64) Int64 { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(int64))) - }) - return u -} - -// Poll implements Poll for Int64. -func (u *int64Container) Poll() int64 { - var ( - s int64 - v = u.s.Poll() - ) - if v != nil { - s = (v.(int64)) - } - return s -} - -// PollLength implements PollLength for Int64. -func (u *int64Container) PollLength() (int64, int) { - v, l := u.s.PollLength() - return v.(int64), l -} - -// PollOK implements PollOK for Int64. -func (u *int64Container) PollOK() (int64, bool) { - v, ok := u.s.PollOK() - return v.(int64), ok -} - -// Pop implements Pop for Int64. -func (u *int64Container) Pop() int64 { - var ( - s int64 - v = u.s.Pop() - ) - if v != nil { - s = (v.(int64)) - } - return s -} - -// PopLength implements PopLength for Int64. -func (u *int64Container) PopLength() (int64, int) { - v, l := u.s.PopLength() - return v.(int64), l -} - -// PopOK implements PopOK for Int64. -func (u *int64Container) PopOK() (int64, bool) { - v, ok := u.s.PopOK() - return v.(int64), ok -} - -// Precatenate implements Precatenate for Int64. -func (u *int64Container) Precatenate(v Int64) Int64 { - u.s.Precatenate(v.(*int64Container).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for Int64. -func (u *int64Container) PrecatenateLength(v Int64) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for Int64. -func (u *int64Container) Prepend(i ...int64) Int64 { - u.s.Prepend(int64ToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for Int64. -func (u *int64Container) PrependLength(v ...int64) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for Int64. -func (u *int64Container) Push(i ...int64) int { - return u.s.Push(int64ToInterfaceSlice(i...)) -} - -// Replace implements Replace for Int64. -func (u *int64Container) Replace(i int, n int64) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for Int64. -func (u *int64Container) Reverse() Int64 { - u.s.Reverse() - return u -} - -// Set implements Set for Int64. -func (u *int64Container) Set() Int64 { - u.s.Set() - return u -} - -// Slice implements Slice for Int64. -func (u *int64Container) Slice(i int, j int) Int64 { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for Int64. -func (u *int64Container) Sort() Int64 { - sort.Sort(u) - return u -} - -// Swap implements Swap for Int64. -func (u *int64Container) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for Int64. -func (u *int64Container) Unshift(i ...int64) int { - return (u.s.Unshift(int64ToInterfaceSlice(i...))) -} - -// Values implements Values for Int64. -func (u *int64Container) Values() []int64 { - var v = make([]int64, u.Len()) - u.Each(func(i int, n int64) { - v[i] = n - }) - return v -} - -func int64ToInterfaceSlice(n ...int64) []interface{} { - var ( - i int - v int64 - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/int8.go b/int8.go deleted file mode 100644 index eada936..0000000 --- a/int8.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// Int8 is the interface that handles a int8 collection. -type Int8 interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...int8) Int8 - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...int8) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s Int8) Int8 - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s Int8) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) Int8 - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, int8)) Int8 - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, int8) bool) Int8 - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, int8)) Int8 - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, int8) bool) Int8 - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) int8 - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (int8, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (int8, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (int8, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) Int8 - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...int8) Int8 - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...int8) Int8 - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, int8) int8) Int8 - // Poll removes the first element from the slice and returns that removed element. - Poll() int8 - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (int8, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (int8, bool) - // Pop removes the last element from the slice and returns that element. - Pop() int8 - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (int8, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (int8, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s Int8) Int8 - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s Int8) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...int8) Int8 - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...int8) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...int8) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v int8) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() Int8 - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() Int8 - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) Int8 - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...int8) int - // Values returns the internal values of the slice. - Values() []int8 -} - -// NewInt8 returns a new Int8 interface. -func NewInt8(i ...int8) Int8 { - return (&int8Container{&Slice{}}).Append(i...) -} - -type int8Container struct{ s *Slice } - -// Append implements Append for Int8. -func (u *int8Container) Append(i ...int8) Int8 { - u.s.Append(int8ToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for Int8. -func (u *int8Container) AppendLength(i ...int8) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for Int8. -func (u *int8Container) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for Int8. -func (u *int8Container) Concatenate(v Int8) Int8 { - u.s.Concatenate(v.(*int8Container).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for Int8. -func (u *int8Container) ConcatenateLength(v Int8) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for Int8. -func (u *int8Container) Delete(i int) Int8 { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for Int8. -func (u *int8Container) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for Int8. -func (u *int8Container) Each(fn func(int, int8)) Int8 { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(int8))) - }) - return u -} - -// EachBreak implements EachBreak for Int8. -func (u *int8Container) EachBreak(fn func(int, int8) bool) Int8 { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(int8))) - }) - return u -} - -// EachReverse implements EachReverse for Int8. -func (u *int8Container) EachReverse(fn func(int, int8)) Int8 { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(int8))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for Int8. -func (u *int8Container) EachReverseBreak(fn func(int, int8) bool) Int8 { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(int8))) - }) - return u -} - -// Fetch implements Fetch for Int8. -func (u *int8Container) Fetch(i int) int8 { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for Int8. -func (u *int8Container) FetchLength(i int) (int8, int) { - v, i := u.s.FetchLength(i) - return v.(int8), i -} - -// Get implements Get for Int8. -func (u *int8Container) Get(i int) (int8, bool) { - var ( - ok bool - s int8 - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(int8) - } - return s, ok -} - -// GetLength implements GetLength for Int8. -func (u *int8Container) GetLength(i int) (int8, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(int8), l, ok -} - -// Len implements Len for Int8. -func (u *int8Container) Len() int { - return u.s.Len() -} - -// Less implements Less for Int8. -func (u *int8Container) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for Int8. -func (u *int8Container) Make(i int) Int8 { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for Int8. -func (u *int8Container) MakeEach(v ...int8) Int8 { - u.s.MakeEach(int8ToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for Int8. -func (u *int8Container) MakeEachReverse(v ...int8) Int8 { - u.s.MakeEachReverse(int8ToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for Int8. -func (u *int8Container) Map(fn func(int, int8) int8) Int8 { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(int8))) - }) - return u -} - -// Poll implements Poll for Int8. -func (u *int8Container) Poll() int8 { - var ( - s int8 - v = u.s.Poll() - ) - if v != nil { - s = (v.(int8)) - } - return s -} - -// PollLength implements PollLength for Int8. -func (u *int8Container) PollLength() (int8, int) { - v, l := u.s.PollLength() - return v.(int8), l -} - -// PollOK implements PollOK for Int8. -func (u *int8Container) PollOK() (int8, bool) { - v, ok := u.s.PollOK() - return v.(int8), ok -} - -// Pop implements Pop for Int8. -func (u *int8Container) Pop() int8 { - var ( - s int8 - v = u.s.Pop() - ) - if v != nil { - s = (v.(int8)) - } - return s -} - -// PopLength implements PopLength for Int8. -func (u *int8Container) PopLength() (int8, int) { - v, l := u.s.PopLength() - return v.(int8), l -} - -// PopOK implements PopOK for Int8. -func (u *int8Container) PopOK() (int8, bool) { - v, ok := u.s.PopOK() - return v.(int8), ok -} - -// Precatenate implements Precatenate for Int8. -func (u *int8Container) Precatenate(v Int8) Int8 { - u.s.Precatenate(v.(*int8Container).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for Int8. -func (u *int8Container) PrecatenateLength(v Int8) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for Int8. -func (u *int8Container) Prepend(i ...int8) Int8 { - u.s.Prepend(int8ToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for Int8. -func (u *int8Container) PrependLength(v ...int8) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for Int8. -func (u *int8Container) Push(i ...int8) int { - return u.s.Push(int8ToInterfaceSlice(i...)) -} - -// Replace implements Replace for Int8. -func (u *int8Container) Replace(i int, n int8) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for Int8. -func (u *int8Container) Reverse() Int8 { - u.s.Reverse() - return u -} - -// Set implements Set for Int8. -func (u *int8Container) Set() Int8 { - u.s.Set() - return u -} - -// Slice implements Slice for Int8. -func (u *int8Container) Slice(i int, j int) Int8 { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for Int8. -func (u *int8Container) Sort() Int8 { - sort.Sort(u) - return u -} - -// Swap implements Swap for Int8. -func (u *int8Container) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for Int8. -func (u *int8Container) Unshift(i ...int8) int { - return (u.s.Unshift(int8ToInterfaceSlice(i...))) -} - -// Values implements Values for Int8. -func (u *int8Container) Values() []int8 { - var v = make([]int8, u.Len()) - u.Each(func(i int, n int8) { - v[i] = n - }) - return v -} - -func int8ToInterfaceSlice(n ...int8) []interface{} { - var ( - i int - v int8 - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/rune.go b/rune.go deleted file mode 100644 index ecf12b7..0000000 --- a/rune.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// Rune is the interface that handles a rune collection. -type Rune interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...rune) Rune - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...rune) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s Rune) Rune - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s Rune) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) Rune - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, rune)) Rune - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, rune) bool) Rune - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, rune)) Rune - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, rune) bool) Rune - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) rune - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (rune, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (rune, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (rune, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) Rune - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...rune) Rune - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...rune) Rune - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, rune) rune) Rune - // Poll removes the first element from the slice and returns that removed element. - Poll() rune - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (rune, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (rune, bool) - // Pop removes the last element from the slice and returns that element. - Pop() rune - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (rune, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (rune, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s Rune) Rune - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s Rune) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...rune) Rune - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...rune) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...rune) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v rune) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() Rune - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() Rune - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) Rune - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...rune) int - // Values returns the internal values of the slice. - Values() []rune -} - -// NewRune returns a new Rune interface. -func NewRune(i ...rune) Rune { - return (&runeContainer{&Slice{}}).Append(i...) -} - -type runeContainer struct{ s *Slice } - -// Append implements Append for Rune. -func (u *runeContainer) Append(i ...rune) Rune { - u.s.Append(runeToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for Rune. -func (u *runeContainer) AppendLength(i ...rune) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for Rune. -func (u *runeContainer) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for Rune. -func (u *runeContainer) Concatenate(v Rune) Rune { - u.s.Concatenate(v.(*runeContainer).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for Rune. -func (u *runeContainer) ConcatenateLength(v Rune) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for Rune. -func (u *runeContainer) Delete(i int) Rune { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for Rune. -func (u *runeContainer) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for Rune. -func (u *runeContainer) Each(fn func(int, rune)) Rune { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(rune))) - }) - return u -} - -// EachBreak implements EachBreak for Rune. -func (u *runeContainer) EachBreak(fn func(int, rune) bool) Rune { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(rune))) - }) - return u -} - -// EachReverse implements EachReverse for Rune. -func (u *runeContainer) EachReverse(fn func(int, rune)) Rune { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(rune))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for Rune. -func (u *runeContainer) EachReverseBreak(fn func(int, rune) bool) Rune { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(rune))) - }) - return u -} - -// Fetch implements Fetch for Rune. -func (u *runeContainer) Fetch(i int) rune { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for Rune. -func (u *runeContainer) FetchLength(i int) (rune, int) { - v, i := u.s.FetchLength(i) - return v.(rune), i -} - -// Get implements Get for Rune. -func (u *runeContainer) Get(i int) (rune, bool) { - var ( - ok bool - s rune - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(rune) - } - return s, ok -} - -// GetLength implements GetLength for Rune. -func (u *runeContainer) GetLength(i int) (rune, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(rune), l, ok -} - -// Len implements Len for Rune. -func (u *runeContainer) Len() int { - return u.s.Len() -} - -// Less implements Less for Rune. -func (u *runeContainer) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for Rune. -func (u *runeContainer) Make(i int) Rune { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for Rune. -func (u *runeContainer) MakeEach(v ...rune) Rune { - u.s.MakeEach(runeToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for Rune. -func (u *runeContainer) MakeEachReverse(v ...rune) Rune { - u.s.MakeEachReverse(runeToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for Rune. -func (u *runeContainer) Map(fn func(int, rune) rune) Rune { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(rune))) - }) - return u -} - -// Poll implements Poll for Rune. -func (u *runeContainer) Poll() rune { - var ( - s rune - v = u.s.Poll() - ) - if v != nil { - s = (v.(rune)) - } - return s -} - -// PollLength implements PollLength for Rune. -func (u *runeContainer) PollLength() (rune, int) { - v, l := u.s.PollLength() - return v.(rune), l -} - -// PollOK implements PollOK for Rune. -func (u *runeContainer) PollOK() (rune, bool) { - v, ok := u.s.PollOK() - return v.(rune), ok -} - -// Pop implements Pop for Rune. -func (u *runeContainer) Pop() rune { - var ( - s rune - v = u.s.Pop() - ) - if v != nil { - s = (v.(rune)) - } - return s -} - -// PopLength implements PopLength for Rune. -func (u *runeContainer) PopLength() (rune, int) { - v, l := u.s.PopLength() - return v.(rune), l -} - -// PopOK implements PopOK for Rune. -func (u *runeContainer) PopOK() (rune, bool) { - v, ok := u.s.PopOK() - return v.(rune), ok -} - -// Precatenate implements Precatenate for Rune. -func (u *runeContainer) Precatenate(v Rune) Rune { - u.s.Precatenate(v.(*runeContainer).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for Rune. -func (u *runeContainer) PrecatenateLength(v Rune) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for Rune. -func (u *runeContainer) Prepend(i ...rune) Rune { - u.s.Prepend(runeToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for Rune. -func (u *runeContainer) PrependLength(v ...rune) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for Rune. -func (u *runeContainer) Push(i ...rune) int { - return u.s.Push(runeToInterfaceSlice(i...)) -} - -// Replace implements Replace for Rune. -func (u *runeContainer) Replace(i int, n rune) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for Rune. -func (u *runeContainer) Reverse() Rune { - u.s.Reverse() - return u -} - -// Set implements Set for Rune. -func (u *runeContainer) Set() Rune { - u.s.Set() - return u -} - -// Slice implements Slice for Rune. -func (u *runeContainer) Slice(i int, j int) Rune { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for Rune. -func (u *runeContainer) Sort() Rune { - sort.Sort(u) - return u -} - -// Swap implements Swap for Rune. -func (u *runeContainer) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for Rune. -func (u *runeContainer) Unshift(i ...rune) int { - return (u.s.Unshift(runeToInterfaceSlice(i...))) -} - -// Values implements Values for Rune. -func (u *runeContainer) Values() []rune { - var v = make([]rune, u.Len()) - u.Each(func(i int, n rune) { - v[i] = n - }) - return v -} - -func runeToInterfaceSlice(n ...rune) []interface{} { - var ( - i int - v rune - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/slice.go b/slice.go index fe1e442..34de68c 100644 --- a/slice.go +++ b/slice.go @@ -2,221 +2,98 @@ package slice import ( "fmt" + "reflect" ) -var _ slicer = (&Slice{}) - -// slice is the private interface for a Slice. -type slicer interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...interface{}) *Slice - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...interface{}) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s *Slice) *Slice - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s *Slice) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) *Slice - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, interface{})) *Slice - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, interface{}) bool) *Slice - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, interface{})) *Slice - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, interface{}) bool) *Slice - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) interface{} - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (interface{}, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (interface{}, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (interface{}, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) *Slice - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...interface{}) *Slice - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...interface{}) *Slice - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, interface{}) interface{}) *Slice - // Poll removes the first element from the slice and returns that removed element. - Poll() interface{} - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (interface{}, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (interface{}, bool) - // Pop removes the last element from the slice and returns that element. - Pop() interface{} - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (interface{}, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (interface{}, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s *Slice) *Slice - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s *Slice) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...interface{}) *Slice - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...interface{}) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...interface{}) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v interface{}) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() *Slice - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() *Slice - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) *Slice - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...interface{}) int - // Values returns the internal values of the slice. - Values() []interface{} -} - -// NewSlice returns a new Slice. -func NewSlice(v ...interface{}) *Slice { - return (&Slice{}).MakeEach(v...) -} - // Slice is a list-like struct whose methods are used to perform traversal and mutation operations by numeric index. -// -// The Slice does not use a fixed address size and will dynamically allocate and deallocate space as new entries are pushed into the sequence. -// -// Slice is written to handle a mix content type. By default the Slice assumes that the data returned is something or nil. -// To handle returning a element from the Slice as a non-interface type it is best to create a custom -// interface or struct to handle the type-casting. -// -// To implement the Slice as single type, create a struct that contains a Slice pointer as a hidden field and -// compose an interface that exports the Slice's methods, using the wrapping struct to -// handle the transaction between the struct and the Slice. -type Slice []interface{} - -// Append adds n elements to the end of the slice -// and returns the modified slice. -func (slice *Slice) Append(i ...interface{}) *Slice { - (*slice) = (append(*slice, i...)) +type Slice[T any] []T + +// Append adds n elements to the end of the slice and returns the modified slice. +// It appends the specified values to the existing slice and returns a pointer to the modified slice. +func (slice *Slice[T]) Append(values ...T) *Slice[T] { + *slice = append(*slice, values...) return slice } // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. -func (slice *Slice) AppendLength(i ...interface{}) int { - return (slice.Append(i...).Len()) +// It appends the specified values to the existing slice and returns the length of the modified slice. +func (slice *Slice[T]) AppendLength(values ...T) int { + return slice.Append(values...).Length() } -// Bounds checks an integer value safely sits within the range of -// accessible values for the slice. -func (slice *Slice) Bounds(i int) bool { - return ((i > -1) && (i < len(*slice))) +// Bounds checks if an integer value safely sits within the range of accessible values for the slice. +// It returns true if the index is within bounds, otherwise false. +func (slice *Slice[T]) Bounds(i int) bool { + return i >= 0 && i < len(*slice) } -// Concatenate merges the elements from the argument slice -// to the the tail of the argument slice. -func (slice *Slice) Concatenate(s *Slice) *Slice { +// Concatenate merges the elements from the argument slice to the tail of the argument slice. +// If the provided slice (s) is not nil, it appends its elements to the receiver slice and returns a pointer to the modified slice. +func (slice *Slice[T]) Concatenate(s *Slice[T]) *Slice[T] { if s != nil { slice.Append((*s)...) } return slice } -// ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice -// and returns the length of the receiver slice. -func (slice *Slice) ConcatenateLength(s *Slice) int { - return (slice.Concatenate(s).Len()) +// ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice and returns the length of the receiver slice. +// If the provided slice (s) is not nil, it appends its elements to the receiver slice and returns the length of the modified slice. +func (slice *Slice[T]) ConcatenateLength(s *Slice[T]) int { + return slice.Concatenate(s).Length() +} + +// Contains checks if a value exists in the slice. +// It returns true if the value is found in the slice, otherwise false. +func (slice *Slice[T]) Contains(value T) bool { + for _, v := range *slice { + if reflect.DeepEqual(v, value) { + return true + } + } + return false } // Delete deletes the element from the argument index and returns the modified slice. -func (slice *Slice) Delete(i int) *Slice { - var ( - ok = slice.Bounds(i) - ) - if ok { - (*slice) = append((*slice)[:i], (*slice)[i+1:]...) +// If the index is within bounds, it removes the element at that index and returns a pointer to the modified slice. +func (slice *Slice[T]) Delete(i int) *Slice[T] { + if slice.Bounds(i) { + *slice = append((*slice)[:i], (*slice)[i+1:]...) } return slice } // DeleteLength deletes the element from the argument index and returns the new length of the slice. -func (slice *Slice) DeleteLength(i int) int { return slice.Delete(i).Len() } +// If the index is within bounds, it removes the element at that index and returns the new length of the modified slice. +func (slice *Slice[T]) DeleteLength(i int) int { + return slice.Delete(i).Length() +} // DeleteOK deletes the element from the argument index and returns the result of the transaction. -func (slice *Slice) DeleteOK(i int) bool { - var ( - ok = slice.Bounds(i) - ) +// If the index is within bounds, it removes the element at that index and returns true; otherwise, it returns false. +func (slice *Slice[T]) DeleteOK(i int) bool { + ok := slice.Bounds(i) if ok { - (*slice) = append((*slice)[:i], (*slice)[i+1:]...) + slice.Delete(i) } return ok } // Each executes a provided function once for each slice element and returns the slice. -func (slice *Slice) Each(fn func(int, interface{})) *Slice { - var ( - i int - v interface{} - ) - for i, v = range *slice { - fn(i, v) - } +// It iterates over each element of the slice, invoking the provided function, and returns a pointer to the modified slice. +func (slice *Slice[T]) Each(fn func(int, T)) *Slice[T] { + slice.EachBreak(func(i int, value T) bool { + fn(i, value) + return true + }) return slice } -// EachBreak executes a provided function once for each -// element with an optional break when the function returns false. +// EachBreak executes a provided function once for each element with an optional break when the function returns false. +// It iterates over each element of the slice, invoking the provided function. If the function returns false, the iteration stops. // Returns the slice at the end of the iteration. -func (slice *Slice) EachBreak(fn func(int, interface{}) bool) *Slice { - var ( - i int - ok = true - v interface{} - ) - for i, v = range *slice { - ok = fn(i, v) +func (slice *Slice[T]) EachBreak(fn func(int, T) bool) *Slice[T] { + for i, v := range *slice { + ok := fn(i, v) if !ok { break } @@ -224,30 +101,22 @@ func (slice *Slice) EachBreak(fn func(int, interface{}) bool) *Slice { return slice } -// EachReverse executes a provided function once for each -// element in the reverse order they are stored in the slice. -// Returns the slice at the end of the iteration. -func (slice *Slice) EachReverse(fn func(int, interface{})) *Slice { - var ( - i int - ) - for i = len(*slice) - 1; i >= 0; i-- { - fn(i, (*slice)[i]) - } +// EachReverse executes a provided function once for each element in the reverse order they are stored in the slice. +// It iterates over each element of the slice in reverse order, invoking the provided function, and returns a pointer to the modified slice. +func (slice *Slice[T]) EachReverse(fn func(int, T)) *Slice[T] { + slice.EachReverseBreak(func(i int, value T) bool { + fn(i, value) + return true + }) return slice } -// EachReverseBreak executes a provided function once for each -// element in the reverse order they are stored in the slice -// with an optional break when the function returns false. +// EachReverseBreak executes a provided function once for each element in the reverse order they are stored in the slice with an optional break when the function returns false. +// It iterates over each element of the slice in reverse order, invoking the provided function. If the function returns false, the iteration stops. // Returns the slice at the end of the iteration. -func (slice *Slice) EachReverseBreak(fn func(int, interface{}) bool) *Slice { - var ( - i int - ok = true - ) - for i = len(*slice) - 1; i >= 0; i-- { - ok = fn(i, (*slice)[i]) +func (slice *Slice[T]) EachReverseBreak(fn func(int, T) bool) *Slice[T] { + for i := len(*slice) - 1; i >= 0; i-- { + ok := fn(i, (*slice)[i]) if !ok { break } @@ -255,185 +124,207 @@ func (slice *Slice) EachReverseBreak(fn func(int, interface{}) bool) *Slice { return slice } -// Fetch retrieves the element held at the argument index. -// Returns the default type if index exceeds slice length. -func (slice *Slice) Fetch(i int) interface{} { - var v, _ = slice.Get(i) +// Fetch retrieves the element held at the argument index. Returns the default type if index exceeds slice length. +// It returns the element at the specified index and a boolean indicating whether the element was successfully retrieved. +func (slice *Slice[T]) Fetch(i int) T { + v, _ := slice.Get(i) return v } -// FetchLength retrives the element held at the argument index and the length of the slice. -// Returns the default type if index exceeds slice length. -func (slice *Slice) FetchLength(i int) (interface{}, int) { - var v = slice.Fetch(i) - var l = slice.Len() - return v, l +// FetchLength retrieves the element held at the argument index and the length of the slice. Returns the default type if index exceeds slice length. +// It returns the element at the specified index, the length of the slice, and a boolean indicating whether the element was successfully retrieved. +func (slice *Slice[T]) FetchLength(i int) (T, int) { + return slice.Fetch(i), slice.Length() +} + +// FindIndex finds the index of the first element that satisfies the given predicate function. +// It returns the index of the first matching element and true if found; otherwise, it returns -1 and false. +func (slice *Slice[T]) FindIndex(fn func(T) bool) (int, bool) { + for i, v := range *slice { + if fn(v) { + return i, true + } + } + return -1, false } -// Get returns the element held at the argument index and a boolean -// indicating if it was successfully retrieved. -func (slice *Slice) Get(i int) (interface{}, bool) { +// Get returns the element held at the argument index and a boolean indicating if it was successfully retrieved. +// It returns the element at the specified index and a boolean indicating whether the element was successfully retrieved. +func (slice *Slice[T]) Get(i int) (T, bool) { var ( ok = slice.Bounds(i) + v T ) if ok { - return (*slice)[i], ok + v = (*slice)[i] } - return nil, ok + return v, ok } -// GetLength returns the element at the argument index, the length of the slice -// and a boolean indicating if the element was successfully retrieved. -func (slice *Slice) GetLength(i int) (interface{}, int, bool) { - var v, ok = slice.Get(i) - var l = slice.Len() +// GetLength returns the element at the argument index, the length of the slice, and a boolean indicating if the element was successfully retrieved. +// It returns the element at the specified index, the length of the slice, and a boolean indicating whether the element was successfully retrieved. +func (slice *Slice[T]) GetLength(i int) (T, int, bool) { + v, ok := slice.Get(i) + l := slice.Length() return v, l, ok } -// Len returns the number of elements in the slice. -func (slice *Slice) Len() int { return (len(*slice)) } +// IsEmpty returns whether the slice is empty. +// It returns true if the slice is empty (length is zero), otherwise false. +func (slice Slice[T]) IsEmpty() bool { + return len(slice) == 0 +} + +// Length returns the number of elements in the slice. +// It returns the length of the slice. +func (slice *Slice[T]) Length() int { + return len(*slice) +} -// Make empties the slice, sets the new slice to the length of n and returns the modified slice. -func (slice *Slice) Make(i int) *Slice { - (*slice) = make(Slice, i) +// Make empties the slice, sets the new slice to the length of n, and returns the modified slice. +// It replaces the existing slice with a new slice of the specified length (n) and returns a pointer to the modified slice. +func (slice *Slice[T]) Make(i int) *Slice[T] { + *slice = make(Slice[T], i) return slice } -// MakeEach empties the slice, sets the new slice to the length of n and performs -// a for-each loop for the argument sequence, inserting each entry at the -// appropriate index before returning the modified slice. -func (slice *Slice) MakeEach(v ...interface{}) *Slice { - return slice.Make(len(v)).Each(func(i int, _ interface{}) { +// MakeEach empties the slice, sets the new slice to the length of n, and performs a for-each loop for the argument sequence, +// inserting each entry at the appropriate index before returning the modified slice. +// It replaces the existing slice with a new slice of the specified length (n) and populates it by performing a for-each loop on the provided values. +// Finally, it returns a pointer to the modified slice. +func (slice *Slice[T]) MakeEach(v ...T) *Slice[T] { + return slice.Make(len(v)).Each(func(i int, _ T) { slice.Replace(i, v[i]) }) } -// MakeEachReverse empties the slice, sets the new slice to the length of n and performs -// an inverse for-each loop for the argument sequence, inserting each entry at the -// appropriate index before returning the modified slice. -func (slice *Slice) MakeEachReverse(v ...interface{}) *Slice { - return slice.Make(len(v)).EachReverse(func(i int, _ interface{}) { +// MakeEachReverse empties the slice, sets the new slice to the length of n, and performs an inverse for-each loop for the argument sequence, +// inserting each entry at the appropriate index before returning the modified slice. +// It replaces the existing slice with a new slice of the specified length (n) and populates it by performing an inverse for-each loop on the provided values. +// Finally, it returns a pointer to the modified slice. +func (slice *Slice[T]) MakeEachReverse(v ...T) *Slice[T] { + return slice.Make(len(v)).EachReverse(func(i int, _ T) { slice.Replace(i, v[i]) }) } -// Map executes a provided function once for each element and sets -// the returned value to the current index. +// Map executes a provided function once for each element and sets the returned value to the current index. // Returns the slice at the end of the iteration. -func (slice *Slice) Map(fn func(int, interface{}) interface{}) *Slice { - slice.Each(func(i int, v interface{}) { - slice.Replace(i, fn(i, v)) +// It iterates over each element of the slice, applying the provided function to each element, +// and assigns the returned value to the current index in the slice. Returns a pointer to the modified slice. +func (slice *Slice[T]) Map(fn func(int, T) T) *Slice[T] { + slice.Each(func(i int, value T) { + slice.Replace(i, fn(i, value)) }) return slice } // Poll removes the first element from the slice and returns that removed element. -func (slice *Slice) Poll() interface{} { - var ( - l = slice.Len() - ok = l > 0 - v interface{} - ) - if ok { +// It removes and returns the first element of the slice if the slice is not empty. +func (slice *Slice[T]) Poll() T { + var v T + if !slice.IsEmpty() { v = (*slice)[0] - (*slice) = (*slice)[1:] + *slice = (*slice)[1:] } return v } -// PollLength removes the first element from the slice and returns the removed element and the length -// of the modified slice. -func (slice *Slice) PollLength() (interface{}, int) { - var v = slice.Poll() - var l = slice.Len() - return v, l +// PollLength removes the first element from the slice and returns the removed element and the length of the modified slice. +// It removes and returns the first element of the slice along with the new length of the modified slice if the slice is not empty. +func (slice *Slice[T]) PollLength() (T, int) { + return slice.Poll(), slice.Length() } // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. -func (slice *Slice) PollOK() (interface{}, bool) { - var v = slice.Poll() - return v, (v != nil) -} - -// Pop removes the last element from the slice and returns that element. -func (slice *Slice) Pop() interface{} { +// It removes the first element from the slice and returns true if the slice is not empty; otherwise, it returns false. +func (slice *Slice[T]) PollOK() (T, bool) { var ( - l = slice.Len() - ok = l > 0 - v interface{} + ok = !slice.IsEmpty() + v T ) if ok { + v = slice.Poll() + } + return v, ok +} + +// Pop removes the last element from the slice and returns that element. +// It removes and returns the last element of the slice if the slice is not empty. +func (slice *Slice[T]) Pop() T { + var v T + if !slice.IsEmpty() { + l := slice.Length() v = (*slice)[l-1] - (*slice) = (*slice)[:l-1] + *slice = (*slice)[:l-1] } return v } -// PopLength removes the last element from the slice and returns the removed element and the length -// of the modified slice. -func (slice *Slice) PopLength() (interface{}, int) { - var v = slice.Pop() - var l = slice.Len() - return v, l +// PopLength removes the last element from the slice and returns the removed element and the length of the modified slice. +// It removes and returns the last element of the slice along with the new length of the modified slice if the slice is not empty. +func (slice *Slice[T]) PopLength() (T, int) { + return slice.Pop(), slice.Length() } // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. -func (slice *Slice) PopOK() (interface{}, bool) { - var v = slice.Pop() - return v, (v != nil) +// It removes the last element from the slice and returns true if the slice is not empty; otherwise, it returns false. +func (slice *Slice[T]) PopOK() (T, bool) { + var ( + ok = !slice.IsEmpty() + v T + ) + if ok { + v = slice.Pop() + } + return v, ok } -// Precatenate merges the elements from the argument slice -// to the the head of the argument slice and returns the modified slice. -func (slice *Slice) Precatenate(s *Slice) *Slice { +// Precatenate merges the elements from the argument slice to the head of the argument slice and returns the modified slice. +// If the provided slice (s) is not nil, it prepends its elements to the receiver slice and returns a pointer to the modified slice. +func (slice *Slice[T]) Precatenate(s *Slice[T]) *Slice[T] { if s != nil { slice.Prepend((*s)...) } return slice } -// PrecatenateLength merges the elements from the argument slice to the head of the receiver slice -// and returns the length of the receiver slice. -func (slice *Slice) PrecatenateLength(s *Slice) int { - return (slice.Precatenate(s).Len()) +// PrecatenateLength merges the elements from the argument slice to the head of the receiver slice and returns the length of the receiver slice. +// If the provided slice (s) is not nil, it prepends its elements to the receiver slice and returns the length of the modified slice. +func (slice *Slice[T]) PrecatenateLength(s *Slice[T]) int { + return slice.Precatenate(s).Length() } -// Prepend adds one element to the head of the slice -// and returns the modified slice. -func (slice *Slice) Prepend(i ...interface{}) *Slice { - (*slice) = (append(i, *slice...)) +// Prepend adds one element to the head of the slice and returns the modified slice. +// It adds the specified values to the beginning of the existing slice and returns a pointer to the modified slice. +func (slice *Slice[T]) Prepend(values ...T) *Slice[T] { + *slice = append(values, *slice...) return slice } // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. -func (slice *Slice) PrependLength(i ...interface{}) int { - return (slice.Prepend(i...).Len()) -} - -// Push adds a new element to the end of the slice and -// returns the length of the modified slice. -func (slice *Slice) Push(i ...interface{}) int { - return (slice.Append(i...).Len()) +// It adds the specified values to the beginning of the existing slice and returns the length of the modified slice. +func (slice *Slice[T]) PrependLength(values ...T) int { + return slice.Prepend(values...).Length() } -// Replace changes the contents of the slice -// at the argument index if it is in bounds. -func (slice *Slice) Replace(i int, v interface{}) bool { - var ( - ok = slice.Bounds(i) - ) +// Replace changes the contents of the slice at the argument index if it is in bounds. +// It replaces the element at the specified index with the provided value if the index is within bounds and returns true. +// Otherwise, it returns false. +func (slice *Slice[T]) Replace(i int, value T) bool { + ok := slice.Bounds(i) if ok { - (*slice)[i] = v + (*slice)[i] = value } return ok } -// Reverse reverses the slice in linear time. -// Returns the slice at the end of the iteration. -func (slice *Slice) Reverse() *Slice { +// Reverse reverses the slice in linear time and returns the modified slice. +// It reverses the order of elements in the slice and returns a pointer to the modified slice. +func (slice *Slice[T]) Reverse() *Slice[T] { var ( i = 0 - j = slice.Len() + j = slice.Length() - 1 ) for i < j { slice.Swap(i, j) @@ -443,54 +334,46 @@ func (slice *Slice) Reverse() *Slice { return slice } -// Set returns a unique slice, removing duplicate -// elements that have the same hash value. -// Returns the modified at the end of the iteration. -func (slice *Slice) Set() *Slice { - const ( - f string = "%v" - ) +// Set returns a unique slice, removing duplicate elements that have the same hash value. +// Returns the modified slice at the end of the iteration. +// It removes duplicate elements from the slice, keeping only the first occurrence of each unique element. +// Returns a pointer to the modified slice with unique elements. +func (slice *Slice[T]) Set() *Slice[T] { var ( k string m = map[string]bool{} ok bool - s = &Slice{} + s = &Slice[T]{} ) - slice.Each(func(_ int, v interface{}) { - k = fmt.Sprintf(f, v) + slice.Each(func(_ int, value T) { + k = fmt.Sprintf("%v", value) // Todo: Check if there is a better way to generate key. _, ok = m[k] if !ok { - s.Append(v) + s.Append(value) } m[k] = true }) - (*slice) = (*s) + *slice = *s return slice } -// Slice slices the slice from i to j and returns the modified slice. -func (slice *Slice) Slice(i int, j int) *Slice { - if j > i { +// Slice[T any] slices the slice from i to j and returns the modified slice. +// It slices the slice from the specified start (i) index to the end (j) index (inclusive), +// and returns a pointer to the modified slice. +func (slice *Slice[T]) Slice(i int, j int) *Slice[T] { + if j < i { i, j = j, i } if slice.Bounds(i) && slice.Bounds(j) { - (*slice) = (*slice)[i:j] + *slice = (*slice)[i:j] } return slice } // Swap moves element i to j and j to i. -func (slice *Slice) Swap(i int, j int) { - (*slice)[i], (*slice)[j] = (*slice)[j], (*slice)[i] -} - -// Unshift adds one or more elements to the beginning of the slice and -// returns the new length of the modified slice. -func (slice *Slice) Unshift(i ...interface{}) int { - return (slice.Prepend(i...).Len()) -} - -// Values returns the internal values of the slice. -func (slice *Slice) Values() []interface{} { - return (*slice) +// If both indices (i and j) are within bounds, it swaps the elements at those positions in the slice. +func (slice *Slice[T]) Swap(i int, j int) { + if slice.Bounds(i) && slice.Bounds(j) { + (*slice)[i], (*slice)[j] = (*slice)[j], (*slice)[i] + } } diff --git a/slice_test.go b/slice_test.go index 2c0d096..bd1385b 100644 --- a/slice_test.go +++ b/slice_test.go @@ -1,344 +1,400 @@ package slice_test import ( - "math/rand" "testing" - "time" - "github.com/gellel/slice" + "github.com/lindsaygelle/slice" ) -var ( - s *slice.Slice -) - -func Test(t *testing.T) { - rand.NewSource(time.Now().UnixNano()) - s = &slice.Slice{} +// TestAppend tests Slice.Append. +func TestAppend(t *testing.T) { + s := &slice.Slice[int]{} + s.Append(1) + if ok := len(*s) == 1; !ok { + t.Fatalf("len(*Slice) != 1") + } + s = &slice.Slice[int]{} + s.Append(1, 2) + if ok := len(*s) == 2; !ok { + t.Fatalf("len(*Slice) != 2") + } } -func TestAppend(t *testing.T) { - if ok := (*s.Append("a"))[0].(string) == "a"; !ok { - t.Fatalf("(&slice.Slice.Append(interface{})) != (interface{}))") +// TestAppendLength tests Slice.AppendLength. +func TestAppendLength(t *testing.T) { + s := &slice.Slice[int]{} + if n := s.AppendLength(1, 2, 3, 4); n != len(*s) { + t.Fatalf("len(*Slice) != 4") } } +// TestBounds tests Slice.Bounds. func TestBounds(t *testing.T) { - if ok := s.Bounds(s.Len() - 1); !ok { - t.Fatalf("(&slice.Slice.Bounds(int) bool) != true") + s := &slice.Slice[int]{} + if ok := s.Bounds(0); ok { + t.Fatalf("*Slice.Bounds() != false") + } + s.Append(1) + if ok := s.Bounds(0); !ok { + t.Fatalf("*Slice.Bounds() != true") } } +// TestConcatenate tests Slice.Concatenate. func TestConcatenate(t *testing.T) { - if ok := (*s.Concatenate(&slice.Slice{"b"}))[1].(string) == "b"; !ok { - t.Fatalf("(&slice.Slice.Concatenate(interface{})) != (interface{}))") + s := &slice.Slice[int]{} + s.Append(1) + s.Concatenate(&slice.Slice[int]{2, 3}) + if ok := (*s)[0] == 1; !ok { + t.Fatalf("*Slice[0] != 1") + } + if ok := (*s)[1] == 2; !ok { + t.Fatalf("*Slice[1] != 2") } - var ( - n = len(*s) - ) - if ok := (len(*s.Concatenate(nil))) == n; !ok { - t.Fatalf("(&slice.Concatenate(nil).Len()) != nil") + if ok := (*s)[2] == 3; !ok { + t.Fatalf("*Slice[2] != 3") + } +} + +// TestConcatenateLength tests Slice.ConcatenateLength. +func TestConcatenateLength(t *testing.T) { + s := &slice.Slice[int]{} + if n := s.ConcatenateLength(&slice.Slice[int]{1}); n != len(*s) { + t.Fatalf("len(*Slice) != %d", len(*s)) } } +// TestDelete tests Slice.Delete. func TestDelete(t *testing.T) { - s.Append("c") - var ( - n = s.Len() - z = float64(s.Len()) / 2 - ) - var ( - mid = (*s)[int(z)] - ) - if ok := s.Delete(int(z)).Len() != n; !ok { - t.Fatalf("(&slice.Slice.Delete(int)) != true") - } - for _, v := range *s { - if ok := (v.(string)) != mid; !ok { - t.Fatalf("(&slice.Slice.Delete(int)) != true") - } + s := &slice.Slice[int]{1} + s.Delete(0) + if ok := len(*s) == 0; !ok { + t.Fatalf("len(*Slice) != 0") + } +} + +// TestDeleteLength tests Slice.DeleteLength. +func TestDeleteLength(t *testing.T) { + s := &slice.Slice[int]{1} + if n := s.DeleteLength(0); n != len(*s) { + t.Fatalf("len(*Slice) != 0") } } +// TestEach tests Slice.Each. func TestEach(t *testing.T) { - var ( - n int - ) - s.Each(func(i int, v interface{}) { - if ok := (*s)[i] == v; !ok { - t.Fatalf("(&slice.Slice.Each(int, interface{})) != (interface{})") + s := &slice.Slice[int]{1, 2, 3, 4, 5} + s.Each(func(i int, value int) { + if ok := (*s)[i] == value; !ok { + t.Fatalf("*Slice[%d] != %d", i, value) } - if ok := i == n; !ok { - t.Fatalf("(&slice.Slice.Each(i int, interface{})) != i") - } - n = n + 1 }) } +// TestEachBreak tests Slice.EachBreak. func TestEachBreak(t *testing.T) { - var ( - n int - ) - s.EachBreak(func(i int, _ interface{}) bool { - n = i + s := &slice.Slice[int]{1, 2, 3, 4, 5} + count := 0 + s.EachBreak(func(i int, value int) bool { + count = count + 1 return false }) - if ok := n == 0; !ok { - t.Fatalf("(&slice.Slice.EachBreak(int, interface{}) bool) != true") + if ok := count == 1; !ok { + t.Fatalf("count != 1") } } +// TestEachReverse tests Slice.EachReverse. func TestEachReverse(t *testing.T) { - var ( - n = s.Len() - 1 - ) - s.EachReverse(func(i int, v interface{}) { - if ok := (*s)[i] == v; !ok { - t.Fatalf("(&slice.Slice.EachReverse(int, interface{})) != (interface{})") - } - if ok := i == n; !ok { - t.Fatalf("(&slice.Slice.EachReverse(i int, interface{})) != i") + s := &slice.Slice[int]{1, 2, 3, 4, 5} + s.EachReverse(func(i int, value int) { + if ok := (*s)[i] == value; !ok { + t.Fatalf("*Slice[%d] != %d", i, value) } - n = n - 1 }) } +// TestEachReverseBreak tests Slice.EachReverseBreak. func TestEachReverseBreak(t *testing.T) { - var ( - n int - ) - s.EachReverseBreak(func(i int, _ interface{}) bool { - n = i + s := &slice.Slice[int]{1, 2, 3, 4, 5} + count := 0 + s.EachReverseBreak(func(i int, value int) bool { + count = count + 1 return false }) - if ok := n == s.Len()-1; !ok { - t.Fatalf("(&slice.Slice.EachReverseBreak(int, interface{}) bool) != true") + if ok := count == 1; !ok { + t.Fatalf("count != 1") } } +// TestFetch tests Slice.Fetch. func TestFetch(t *testing.T) { - if ok := s.Fetch(s.Len()+1) == nil; !ok { - t.Fatalf("(&slice.Slice.Fetch(int) interface{}) != true") + s := &slice.Slice[int]{1} + for i, _ := range *s { + value := s.Fetch(i) + if ok := value == (*s)[i]; !ok { + t.Fatalf("%d != %d", value, (*s)[i]) + } } - if ok := s.Fetch(0) != nil; !ok { - t.Fatalf("(&slice.Slice.Fetch(int) interface{}) != true") + // Deliberately empty and check default is returned. + s = &slice.Slice[int]{} + value := s.Fetch(1) + if ok := value == 0; !ok { + t.Fatalf("%d != 0", value) + } +} + +// TestFetchLength tests Slice.FetchLength. +func TestFetchLength(t *testing.T) { + s := &slice.Slice[int]{1, 2} + for i, _ := range *s { + _, value := s.FetchLength(i) + if ok := value == len(*s); !ok { + t.Fatalf("%d != %d", value, len(*s)) + } } } +// TestGet tests Slice.Get. func TestGet(t *testing.T) { - if _, ok := s.Get(0); !ok { - t.Fatalf("(&slice.Slice.Get(int) (_, bool)) != true") + s := &slice.Slice[int]{1} + for i, _ := range *s { + value, ok := s.Get(i) + if value != (*s)[i] { + t.Fatalf("%d != %d", value, (*s)[i]) + } + if !ok { + t.Fatalf("%t != true", ok) + } + } +} + +// TestGetLength tests Slice.GetLength. +func TestGetLength(t *testing.T) { + s := &slice.Slice[int]{1} + for i, _ := range *s { + value, length, ok := s.GetLength(i) + if value != (*s)[i] { + t.Fatalf("%d != %d", value, (*s)[i]) + } + if length != len(*s) { + t.Fatalf("%d = %d", length, len(*s)) + } + if !ok { + t.Fatalf("%t != true", ok) + } } - if v, _ := s.Get(0); v != (*s)[0] { - t.Fatalf("(&slice.Slice.Get(int) (interface{}, _) != interface{}") +} + +// TestLength tests Slice.Length. +func TestLength(t *testing.T) { + s := &slice.Slice[int]{} + if ok := s.Length() == len(*s); !ok { + t.Fatalf("len(*Slice) != %d", len(*s)) } } +// TestMake tests Slice.Make. func TestMake(t *testing.T) { - if ok := s.Make(10).Len() == 10; !ok { - t.Fatalf("(&slice.Make(int).Len()) != n") + s := &slice.Slice[int]{} + size := 10 + s.Make(size) + if ok := len(*s) == size; !ok { + t.Fatalf("len(*Slice) != %d", size) } } +// TestMakeEach tests Slice.MakeEach. func TestMakeEach(t *testing.T) { - var ( - v = []interface{}{1, 2, 3, 4, 5} - ) - if ok := s.MakeEach(v...).Len() == len(v); !ok { - t.Fatalf("(&slice.MakeEach(...interface{}).Len()) != n") - } - s.Each(func(i int, x interface{}) { - if ok := v[i] == x; !ok { - t.Fatalf("(&slice.MakeEach(...interface{})) != interface{}") + s := &slice.Slice[int]{} + s.MakeEach(1, 2, 3, 4) + for i, value := range *s { + if ok := (*s)[i] == value; !ok { + t.Fatalf("(*Slice)[%d] != %d", i, value) } - }) + } } +// TestMakeEachReverse tests Slice.MakeEachReverse. func TestMakeEachReverse(t *testing.T) { - var ( - v = []interface{}{1, 2, 3, 4, 5} - ) - if ok := s.MakeEachReverse(v...).Len() == len(v); !ok { - t.Fatalf("(&slice.MakeEachReverse(...interface{}).Len()) != n") - } - s.EachReverse(func(i int, x interface{}) { - if ok := v[i] == x; !ok { - t.Fatalf("(&slice.MakeEachReverse(...interface{})) != interface{}") + s := &slice.Slice[int]{} + s.MakeEachReverse(1, 2, 3, 4) + for i, value := range *s { + if ok := (*s)[i] == value; !ok { + t.Fatalf("(*Slice)[%d] != %d", i, value) } - }) + } } +// TestMap tests Slice.Map. func TestMap(t *testing.T) { - var ( - x = []int{} - ) - s.Each(func(_ int, v interface{}) { - x = append(x, v.(int)*2) - }) - s.Map(func(i int, v interface{}) interface{} { - var x = v.(int) - x = x * 2 - return x + s := &slice.Slice[int]{1, 2, 3, 4, 5} + x := []int{1, 2, 3, 4, 5} + s.Map(func(_ int, value int) int { + return value * 2 }) - s.Each(func(i int, v interface{}) { - if ok := x[i] == v.(int); !ok { - t.Fatalf("(&slice.Map(func(int, interface{}) interface{}})) != interface{}") + for i, value := range x { + if ok := value*2 == (*s)[i]; !ok { + t.Fatalf("%d != %d", value*2, (*s)[i]) } - }) + } } +// TestPoll tests Slice.Poll. func TestPoll(t *testing.T) { - var ( - n = rand.Intn(100) - ) - if n == 0 { - n = 1 - } - var ( - v = make([]interface{}, n) - ) - for i := range v { - v[i] = rand.Intn(100) - } - s.MakeEach(v...) - var ( - x = s.Poll() - ) - if ok := x == v[0]; !ok { - t.Fatalf("(&slice.Poll() interface{}) != interface{}") - } - if ok := len(v) != s.Len(); !ok { - t.Fatalf("(&slice.Poll() interface{}); (&slice.Len()) == len(v)") - } - for i := s.Len(); i > 0; i-- { - x = s.Poll() - if ok := x != nil; !ok { - t.Fatalf("(&slice.Poll() interface{}) != interface{}") - } - if ok := x == v[len(v)-i]; !ok { - t.Fatalf("(&slice.Poll() interface{}) != []interface{}[i]") - } + s := &slice.Slice[int]{1} + if value := s.Poll(); value != 1 { + t.Fatalf("%d != 1", value) + } +} + +// TestPollLength tests Slice.PollLength. +func TestPollLength(t *testing.T) { + s := &slice.Slice[int]{1} + value, length := s.PollLength() + if ok := value == 1; !ok { + t.Fatalf("%d != 1", value) + } + if ok := length == 0; !ok { + t.Fatalf("%d != 0", length) } - if ok := s.Len() == 0; !ok { - t.Fatalf("(&slice.Poll() interface{}); (&slice.Len()) != 0") +} + +// TestPollOK tests Slice.PollOK. +func TestPollOK(t *testing.T) { + s := &slice.Slice[int]{1, 2} + value, ok := s.PollOK() + if value != 1 { + t.Fatalf("%d != 1", value) + } + if !ok { + t.Fatalf("%t != true", ok) } } +// TestPop tests Slice.Pop. func TestPop(t *testing.T) { - var ( - n = rand.Intn(100) - ) - if n == 0 { - n = 1 - } - var ( - v = make([]interface{}, rand.Intn(100)) - ) - for i := range v { - v[i] = rand.Intn(100) - } - s.MakeEach(v...) - var ( - x = s.Pop() - ) - if ok := x == v[len(v)-1]; !ok { - t.Fatalf("(&slice.Pop() interface{}) != interface{}") - } - if ok := len(v) != s.Len(); !ok { - t.Fatalf("(&slice.Pop() interface{}); (&slice.Len()) == len(v)") - } - for i := s.Len(); i > 0; i-- { - x = s.Pop() - if ok := x != nil; !ok { - t.Fatalf("(&slice.Pop() interface{}) != interface{}") - } - if ok := x == v[i-1]; !ok { - t.Fatalf("(&slice.Pop() interface{}) != []interface{}[i]") - } + s := &slice.Slice[int]{1, 2} + value := s.Pop() + if ok := value == 2; !ok { + t.Fatalf("%d != 2", value) } - if ok := s.Len() == 0; !ok { - t.Fatalf("(&slice.Pop() interface{}); (&slice.Len()) != 0") +} + +// TestPopLength tests Slice.PopLength. +func TestPopLength(t *testing.T) { + s := &slice.Slice[int]{1, 2} + value, length := s.PopLength() + if ok := value == 2; !ok { + t.Fatalf("%d != 2", value) + } + if ok := length == len(*s); !ok { + t.Fatalf("len(*Slice) != %d", len(*s)) } } -func TestPrecatenate(t *testing.T) { - var ( - head = 1 + rand.Intn(10-1) - tail = head + rand.Intn(20-head) - ) - s = &slice.Slice{} - s.Append(head) - s.Precatenate((&slice.Slice{}).Append(tail)) - if ok := s.Len() == 2; !ok { - t.Fatalf("(&slice.Precatenate(&slice.Slice{}).Len()) != n") +// TestPopOK tests Slice.PopOK. +func TestPopOK(t *testing.T) { + s := &slice.Slice[int]{1, 2} + value, ok := s.PopOK() + if value != 2 { + t.Fatalf("%d != 2", value) } - if ok := s.Fetch(0) == tail; !ok { - t.Fatalf("(&slice.Precatenate(&slice.Slice{}).Fetch(0) != tail") + if !ok { + t.Fatalf("%t != true", ok) } - if ok := s.Fetch(1) == head; !ok { - t.Fatalf("(&slice.Precatenate(&slice.Slice{}).Fetch(1) != head") +} + +// TestPrecatenate tests Slice.Precatenate. +func TestPrecatenate(t *testing.T) { + s := &slice.Slice[int]{} + value := 1 + s.Precatenate(&slice.Slice[int]{value}) + if ok := (*s)[0] == 1; !ok { + t.Fatalf("(*Slice)[0] != %d", value) } - s.Precatenate(nil) - if ok := s.Len() == 2; !ok { - t.Fatalf("(&slice.Precatenate(nil).Len()) != nil") +} + +// TestPrecatenateLength tests Slice.PrecatenateLength. +func TestPrecatenateLength(t *testing.T) { + s := &slice.Slice[int]{} + value := 1 + length := s.PrecatenateLength(&slice.Slice[int]{value}) + if ok := length == len(*s); !ok { + t.Fatalf("len(*Slice) != %d", length) } } +// TestPrepend tests Slice.Prepend. func TestPrepend(t *testing.T) { - var ( - head = 1 + rand.Intn(10-1) - tail = head + rand.Intn(20-head) - ) - s = &slice.Slice{tail} - s.Prepend(head) - if ok := s.Len() == 2; !ok { - t.Fatalf("(&slice.Prepend(interface{}).Len()) != n") - } - if ok := s.Fetch(0) == head; !ok { - t.Fatalf("(&slice.Prepend(interface{}).Fetch(0) != head") - } - if ok := s.Fetch(1) == tail; !ok { - t.Fatalf("(&slice.Prepend(interface{}).Fetch(1) != tail") - } - s.Prepend() - if ok := s.Len() == 2; !ok { - t.Fatalf("(&slice.Prepend(nil).Len()) != nil") - } -} - -func TestPush(t *testing.T) { - var ( - n = rand.Intn(100) - ) - if n == 0 { - n = 1 - } - var ( - v = make([]interface{}, rand.Intn(100)) - ) - for i := range v { - v[i] = rand.Intn(100) - } - s = &slice.Slice{} - s.Push(v...) - if ok := s.Len() == len(v); !ok { - t.Fatalf("(&slice.Push(interface{}...).Len()) != n") - } - s.Each(func(i int, x interface{}) { - if ok := v[i] == x; !ok { - t.Fatalf("(&slice.Push([]interface{}...).Fetch(i) interface{}) != []interface{}[i]") - } - }) + s := &slice.Slice[int]{2} + value := 1 + s.Prepend(value) + if ok := (*s)[0] == value; !ok { + t.Fatalf("(*Slice)[0] != %d", value) + } +} + +// TestPrependLength tests Slice.PrependLength. +func TestPrependLength(t *testing.T) { + s := &slice.Slice[int]{} + length := s.PrependLength(1, 2, 3, 4, 5) + if ok := length == len(*s); !ok { + t.Fatalf("%d != %d", length, len(*s)) + } } +// TestReplace tests Slice.Replace. func TestReplace(t *testing.T) { - var ( - a = "a" - b = "b" - ) - s = &slice.Slice{a} - s.Replace(0, b) - if ok := (*s)[0].(string) == b; !ok { - t.Fatalf("(&slice.Replace(int, interface{}).Fetch(i)) != interface{}") + s := &slice.Slice[int]{1} + s.Replace(0, 2) + if ok := (*s)[0] == 2; !ok { + t.Fatalf("%d != 2", (*s)[0]) + } +} + +// TestReverse tests Slice.Reverse. +func TestReverse(t *testing.T) { + s := &slice.Slice[int]{1, 2} + s.Reverse() + if ok := (*s)[0] == 2; !ok { + t.Fatalf("(*Slice)[0] != %d", 2) + } +} + +// TestSet tests Slice.Set. +func TestSet(t *testing.T) { + s := &slice.Slice[int]{2, 2, 3, 3} + s.Set() + values := map[int]bool{} + for _, value := range *s { + if _, ok := values[value]; ok { + t.Fatalf("Slice contains duplicate value %d", value) + } + values[value] = true + } +} + +// TestSlice tests Slice.Slice. +func TestSlice(t *testing.T) { + s := &slice.Slice[int]{1, 2, 3} + s = s.Slice(0, 2) + if ok := len(*s) == 2; !ok { + t.Fatalf("len(*Slice) != %d", 2) + } + +} + +// TestSwap tests Slice.Swap. +func TestSwap(t *testing.T) { + a := 1 + b := 2 + s := &slice.Slice[int]{a, b} + s.Swap(0, 1) + if ok := (*s)[0] == b; !ok { + t.Fatalf("(*Slice)[0] != %d", b) + } + if ok := (*s)[1] == a; !ok { + t.Fatalf("(*Slice)[1] != %d", a) } } diff --git a/string.go b/string.go deleted file mode 100644 index 1af3e2e..0000000 --- a/string.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// String is the interface that handles a string collection. -type String interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...string) String - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...string) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s String) String - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s String) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) String - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, string)) String - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, string) bool) String - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, string)) String - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, string) bool) String - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) string - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (string, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (string, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (string, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) String - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...string) String - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...string) String - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, string) string) String - // Poll removes the first element from the slice and returns that removed element. - Poll() string - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (string, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (string, bool) - // Pop removes the last element from the slice and returns that element. - Pop() string - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (string, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (string, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s String) String - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s String) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...string) String - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...string) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...string) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v string) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() String - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() String - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) String - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...string) int - // Values returns the internal values of the slice. - Values() []string -} - -// NewString returns a new String interface. -func NewString(i ...string) String { - return (&stringContainer{&Slice{}}).Append(i...) -} - -type stringContainer struct{ s *Slice } - -// Append implements Append for String. -func (u *stringContainer) Append(i ...string) String { - u.s.Append(stringToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for String. -func (u *stringContainer) AppendLength(i ...string) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for String. -func (u *stringContainer) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for String. -func (u *stringContainer) Concatenate(v String) String { - u.s.Concatenate(v.(*stringContainer).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for String. -func (u *stringContainer) ConcatenateLength(v String) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for String. -func (u *stringContainer) Delete(i int) String { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for String. -func (u *stringContainer) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for String. -func (u *stringContainer) Each(fn func(int, string)) String { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(string))) - }) - return u -} - -// EachBreak implements EachBreak for String. -func (u *stringContainer) EachBreak(fn func(int, string) bool) String { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(string))) - }) - return u -} - -// EachReverse implements EachReverse for String. -func (u *stringContainer) EachReverse(fn func(int, string)) String { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(string))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for String. -func (u *stringContainer) EachReverseBreak(fn func(int, string) bool) String { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(string))) - }) - return u -} - -// Fetch implements Fetch for String. -func (u *stringContainer) Fetch(i int) string { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for String. -func (u *stringContainer) FetchLength(i int) (string, int) { - v, i := u.s.FetchLength(i) - return v.(string), i -} - -// Get implements Get for String. -func (u *stringContainer) Get(i int) (string, bool) { - var ( - ok bool - s string - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(string) - } - return s, ok -} - -// GetLength implements GetLength for String. -func (u *stringContainer) GetLength(i int) (string, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(string), l, ok -} - -// Len implements Len for String. -func (u *stringContainer) Len() int { - return u.s.Len() -} - -// Less implements Less for String. -func (u *stringContainer) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for String. -func (u *stringContainer) Make(i int) String { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for String. -func (u *stringContainer) MakeEach(v ...string) String { - u.s.MakeEach(stringToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for String. -func (u *stringContainer) MakeEachReverse(v ...string) String { - u.s.MakeEachReverse(stringToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for String. -func (u *stringContainer) Map(fn func(int, string) string) String { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(string))) - }) - return u -} - -// Poll implements Poll for String. -func (u *stringContainer) Poll() string { - var ( - s string - v = u.s.Poll() - ) - if v != nil { - s = (v.(string)) - } - return s -} - -// PollLength implements PollLength for String. -func (u *stringContainer) PollLength() (string, int) { - v, l := u.s.PollLength() - return v.(string), l -} - -// PollOK implements PollOK for String. -func (u *stringContainer) PollOK() (string, bool) { - v, ok := u.s.PollOK() - return v.(string), ok -} - -// Pop implements Pop for String. -func (u *stringContainer) Pop() string { - var ( - s string - v = u.s.Pop() - ) - if v != nil { - s = (v.(string)) - } - return s -} - -// PopLength implements PopLength for String. -func (u *stringContainer) PopLength() (string, int) { - v, l := u.s.PopLength() - return v.(string), l -} - -// PopOK implements PopOK for String. -func (u *stringContainer) PopOK() (string, bool) { - v, ok := u.s.PopOK() - return v.(string), ok -} - -// Precatenate implements Precatenate for String. -func (u *stringContainer) Precatenate(v String) String { - u.s.Precatenate(v.(*stringContainer).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for String. -func (u *stringContainer) PrecatenateLength(v String) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for String. -func (u *stringContainer) Prepend(i ...string) String { - u.s.Prepend(stringToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for String. -func (u *stringContainer) PrependLength(v ...string) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for String. -func (u *stringContainer) Push(i ...string) int { - return u.s.Push(stringToInterfaceSlice(i...)) -} - -// Replace implements Replace for String. -func (u *stringContainer) Replace(i int, n string) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for String. -func (u *stringContainer) Reverse() String { - u.s.Reverse() - return u -} - -// Set implements Set for String. -func (u *stringContainer) Set() String { - u.s.Set() - return u -} - -// Slice implements Slice for String. -func (u *stringContainer) Slice(i int, j int) String { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for String. -func (u *stringContainer) Sort() String { - sort.Sort(u) - return u -} - -// Swap implements Swap for String. -func (u *stringContainer) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for String. -func (u *stringContainer) Unshift(i ...string) int { - return (u.s.Unshift(stringToInterfaceSlice(i...))) -} - -// Values implements Values for String. -func (u *stringContainer) Values() []string { - var v = make([]string, u.Len()) - u.Each(func(i int, n string) { - v[i] = n - }) - return v -} - -func stringToInterfaceSlice(n ...string) []interface{} { - var ( - i int - v string - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/uint.go b/uint.go deleted file mode 100644 index 391d82c..0000000 --- a/uint.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// UInt is the interface that handles a uint collection. -type UInt interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...uint) UInt - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...uint) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s UInt) UInt - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s UInt) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) UInt - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, uint)) UInt - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, uint) bool) UInt - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, uint)) UInt - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, uint) bool) UInt - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) uint - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (uint, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (uint, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (uint, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) UInt - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...uint) UInt - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...uint) UInt - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, uint) uint) UInt - // Poll removes the first element from the slice and returns that removed element. - Poll() uint - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (uint, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (uint, bool) - // Pop removes the last element from the slice and returns that element. - Pop() uint - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (uint, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (uint, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s UInt) UInt - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s UInt) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...uint) UInt - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...uint) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...uint) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v uint) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() UInt - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() UInt - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) UInt - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...uint) int - // Values returns the internal values of the slice. - Values() []uint -} - -// NewUInt returns a new UInt interface. -func NewUInt(i ...uint) UInt { - return (&uintContainer{&Slice{}}).Append(i...) -} - -type uintContainer struct{ s *Slice } - -// Append implements Append for UInt. -func (u *uintContainer) Append(i ...uint) UInt { - u.s.Append(uintToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for UInt. -func (u *uintContainer) AppendLength(i ...uint) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for UInt. -func (u *uintContainer) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for UInt. -func (u *uintContainer) Concatenate(v UInt) UInt { - u.s.Concatenate(v.(*uintContainer).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for UInt. -func (u *uintContainer) ConcatenateLength(v UInt) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for UInt. -func (u *uintContainer) Delete(i int) UInt { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for UInt. -func (u *uintContainer) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for UInt. -func (u *uintContainer) Each(fn func(int, uint)) UInt { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(uint))) - }) - return u -} - -// EachBreak implements EachBreak for UInt. -func (u *uintContainer) EachBreak(fn func(int, uint) bool) UInt { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(uint))) - }) - return u -} - -// EachReverse implements EachReverse for UInt. -func (u *uintContainer) EachReverse(fn func(int, uint)) UInt { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(uint))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for UInt. -func (u *uintContainer) EachReverseBreak(fn func(int, uint) bool) UInt { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(uint))) - }) - return u -} - -// Fetch implements Fetch for UInt. -func (u *uintContainer) Fetch(i int) uint { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for UInt. -func (u *uintContainer) FetchLength(i int) (uint, int) { - v, i := u.s.FetchLength(i) - return v.(uint), i -} - -// Get implements Get for UInt. -func (u *uintContainer) Get(i int) (uint, bool) { - var ( - ok bool - s uint - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(uint) - } - return s, ok -} - -// GetLength implements GetLength for UInt. -func (u *uintContainer) GetLength(i int) (uint, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(uint), l, ok -} - -// Len implements Len for UInt. -func (u *uintContainer) Len() int { - return u.s.Len() -} - -// Less implements Less for UInt. -func (u *uintContainer) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for UInt. -func (u *uintContainer) Make(i int) UInt { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for UInt. -func (u *uintContainer) MakeEach(v ...uint) UInt { - u.s.MakeEach(uintToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for UInt. -func (u *uintContainer) MakeEachReverse(v ...uint) UInt { - u.s.MakeEachReverse(uintToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for UInt. -func (u *uintContainer) Map(fn func(int, uint) uint) UInt { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(uint))) - }) - return u -} - -// Poll implements Poll for UInt. -func (u *uintContainer) Poll() uint { - var ( - s uint - v = u.s.Poll() - ) - if v != nil { - s = (v.(uint)) - } - return s -} - -// PollLength implements PollLength for UInt. -func (u *uintContainer) PollLength() (uint, int) { - v, l := u.s.PollLength() - return v.(uint), l -} - -// PollOK implements PollOK for UInt. -func (u *uintContainer) PollOK() (uint, bool) { - v, ok := u.s.PollOK() - return v.(uint), ok -} - -// Pop implements Pop for UInt. -func (u *uintContainer) Pop() uint { - var ( - s uint - v = u.s.Pop() - ) - if v != nil { - s = (v.(uint)) - } - return s -} - -// PopLength implements PopLength for UInt. -func (u *uintContainer) PopLength() (uint, int) { - v, l := u.s.PopLength() - return v.(uint), l -} - -// PopOK implements PopOK for UInt. -func (u *uintContainer) PopOK() (uint, bool) { - v, ok := u.s.PopOK() - return v.(uint), ok -} - -// Precatenate implements Precatenate for UInt. -func (u *uintContainer) Precatenate(v UInt) UInt { - u.s.Precatenate(v.(*uintContainer).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for UInt. -func (u *uintContainer) PrecatenateLength(v UInt) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for UInt. -func (u *uintContainer) Prepend(i ...uint) UInt { - u.s.Prepend(uintToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for UInt. -func (u *uintContainer) PrependLength(v ...uint) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for UInt. -func (u *uintContainer) Push(i ...uint) int { - return u.s.Push(uintToInterfaceSlice(i...)) -} - -// Replace implements Replace for UInt. -func (u *uintContainer) Replace(i int, n uint) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for UInt. -func (u *uintContainer) Reverse() UInt { - u.s.Reverse() - return u -} - -// Set implements Set for UInt. -func (u *uintContainer) Set() UInt { - u.s.Set() - return u -} - -// Slice implements Slice for UInt. -func (u *uintContainer) Slice(i int, j int) UInt { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for UInt. -func (u *uintContainer) Sort() UInt { - sort.Sort(u) - return u -} - -// Swap implements Swap for UInt. -func (u *uintContainer) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for UInt. -func (u *uintContainer) Unshift(i ...uint) int { - return (u.s.Unshift(uintToInterfaceSlice(i...))) -} - -// Values implements Values for UInt. -func (u *uintContainer) Values() []uint { - var v = make([]uint, u.Len()) - u.Each(func(i int, n uint) { - v[i] = n - }) - return v -} - -func uintToInterfaceSlice(n ...uint) []interface{} { - var ( - i int - v uint - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/uint16.go b/uint16.go deleted file mode 100644 index 798cee4..0000000 --- a/uint16.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// UInt16 is the interface that handles a uint16 collection. -type UInt16 interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...uint16) UInt16 - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...uint16) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s UInt16) UInt16 - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s UInt16) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) UInt16 - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, uint16)) UInt16 - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, uint16) bool) UInt16 - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, uint16)) UInt16 - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, uint16) bool) UInt16 - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) uint16 - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (uint16, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (uint16, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (uint16, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) UInt16 - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...uint16) UInt16 - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...uint16) UInt16 - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, uint16) uint16) UInt16 - // Poll removes the first element from the slice and returns that removed element. - Poll() uint16 - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (uint16, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (uint16, bool) - // Pop removes the last element from the slice and returns that element. - Pop() uint16 - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (uint16, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (uint16, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s UInt16) UInt16 - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s UInt16) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...uint16) UInt16 - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...uint16) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...uint16) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v uint16) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() UInt16 - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() UInt16 - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) UInt16 - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...uint16) int - // Values returns the internal values of the slice. - Values() []uint16 -} - -// NewUInt16 returns a new UInt16 interface. -func NewUInt16(i ...uint16) UInt16 { - return (&uint16Container{&Slice{}}).Append(i...) -} - -type uint16Container struct{ s *Slice } - -// Append implements Append for UInt16. -func (u *uint16Container) Append(i ...uint16) UInt16 { - u.s.Append(uint16ToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for UInt16. -func (u *uint16Container) AppendLength(i ...uint16) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for UInt16. -func (u *uint16Container) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for UInt16. -func (u *uint16Container) Concatenate(v UInt16) UInt16 { - u.s.Concatenate(v.(*uint16Container).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for UInt16. -func (u *uint16Container) ConcatenateLength(v UInt16) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for UInt16. -func (u *uint16Container) Delete(i int) UInt16 { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for UInt16. -func (u *uint16Container) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for UInt16. -func (u *uint16Container) Each(fn func(int, uint16)) UInt16 { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(uint16))) - }) - return u -} - -// EachBreak implements EachBreak for UInt16. -func (u *uint16Container) EachBreak(fn func(int, uint16) bool) UInt16 { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(uint16))) - }) - return u -} - -// EachReverse implements EachReverse for UInt16. -func (u *uint16Container) EachReverse(fn func(int, uint16)) UInt16 { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(uint16))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for UInt16. -func (u *uint16Container) EachReverseBreak(fn func(int, uint16) bool) UInt16 { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(uint16))) - }) - return u -} - -// Fetch implements Fetch for UInt16. -func (u *uint16Container) Fetch(i int) uint16 { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for UInt16. -func (u *uint16Container) FetchLength(i int) (uint16, int) { - v, i := u.s.FetchLength(i) - return v.(uint16), i -} - -// Get implements Get for UInt16. -func (u *uint16Container) Get(i int) (uint16, bool) { - var ( - ok bool - s uint16 - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(uint16) - } - return s, ok -} - -// GetLength implements GetLength for UInt16. -func (u *uint16Container) GetLength(i int) (uint16, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(uint16), l, ok -} - -// Len implements Len for UInt16. -func (u *uint16Container) Len() int { - return u.s.Len() -} - -// Less implements Less for UInt16. -func (u *uint16Container) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for UInt16. -func (u *uint16Container) Make(i int) UInt16 { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for UInt16. -func (u *uint16Container) MakeEach(v ...uint16) UInt16 { - u.s.MakeEach(uint16ToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for UInt16. -func (u *uint16Container) MakeEachReverse(v ...uint16) UInt16 { - u.s.MakeEachReverse(uint16ToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for UInt16. -func (u *uint16Container) Map(fn func(int, uint16) uint16) UInt16 { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(uint16))) - }) - return u -} - -// Poll implements Poll for UInt16. -func (u *uint16Container) Poll() uint16 { - var ( - s uint16 - v = u.s.Poll() - ) - if v != nil { - s = (v.(uint16)) - } - return s -} - -// PollLength implements PollLength for UInt16. -func (u *uint16Container) PollLength() (uint16, int) { - v, l := u.s.PollLength() - return v.(uint16), l -} - -// PollOK implements PollOK for UInt16. -func (u *uint16Container) PollOK() (uint16, bool) { - v, ok := u.s.PollOK() - return v.(uint16), ok -} - -// Pop implements Pop for UInt16. -func (u *uint16Container) Pop() uint16 { - var ( - s uint16 - v = u.s.Pop() - ) - if v != nil { - s = (v.(uint16)) - } - return s -} - -// PopLength implements PopLength for UInt16. -func (u *uint16Container) PopLength() (uint16, int) { - v, l := u.s.PopLength() - return v.(uint16), l -} - -// PopOK implements PopOK for UInt16. -func (u *uint16Container) PopOK() (uint16, bool) { - v, ok := u.s.PopOK() - return v.(uint16), ok -} - -// Precatenate implements Precatenate for UInt16. -func (u *uint16Container) Precatenate(v UInt16) UInt16 { - u.s.Precatenate(v.(*uint16Container).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for UInt16. -func (u *uint16Container) PrecatenateLength(v UInt16) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for UInt16. -func (u *uint16Container) Prepend(i ...uint16) UInt16 { - u.s.Prepend(uint16ToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for UInt16. -func (u *uint16Container) PrependLength(v ...uint16) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for UInt16. -func (u *uint16Container) Push(i ...uint16) int { - return u.s.Push(uint16ToInterfaceSlice(i...)) -} - -// Replace implements Replace for UInt16. -func (u *uint16Container) Replace(i int, n uint16) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for UInt16. -func (u *uint16Container) Reverse() UInt16 { - u.s.Reverse() - return u -} - -// Set implements Set for UInt16. -func (u *uint16Container) Set() UInt16 { - u.s.Set() - return u -} - -// Slice implements Slice for UInt16. -func (u *uint16Container) Slice(i int, j int) UInt16 { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for UInt16. -func (u *uint16Container) Sort() UInt16 { - sort.Sort(u) - return u -} - -// Swap implements Swap for UInt16. -func (u *uint16Container) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for UInt16. -func (u *uint16Container) Unshift(i ...uint16) int { - return (u.s.Unshift(uint16ToInterfaceSlice(i...))) -} - -// Values implements Values for UInt16. -func (u *uint16Container) Values() []uint16 { - var v = make([]uint16, u.Len()) - u.Each(func(i int, n uint16) { - v[i] = n - }) - return v -} - -func uint16ToInterfaceSlice(n ...uint16) []interface{} { - var ( - i int - v uint16 - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/uint32.go b/uint32.go deleted file mode 100644 index d545915..0000000 --- a/uint32.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// UInt32 is the interface that handles a uint32 collection. -type UInt32 interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...uint32) UInt32 - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...uint32) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s UInt32) UInt32 - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s UInt32) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) UInt32 - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, uint32)) UInt32 - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, uint32) bool) UInt32 - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, uint32)) UInt32 - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, uint32) bool) UInt32 - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) uint32 - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (uint32, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (uint32, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (uint32, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) UInt32 - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...uint32) UInt32 - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...uint32) UInt32 - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, uint32) uint32) UInt32 - // Poll removes the first element from the slice and returns that removed element. - Poll() uint32 - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (uint32, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (uint32, bool) - // Pop removes the last element from the slice and returns that element. - Pop() uint32 - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (uint32, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (uint32, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s UInt32) UInt32 - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s UInt32) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...uint32) UInt32 - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...uint32) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...uint32) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v uint32) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() UInt32 - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() UInt32 - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) UInt32 - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...uint32) int - // Values returns the internal values of the slice. - Values() []uint32 -} - -// NewUInt32 returns a new UInt32 interface. -func NewUInt32(i ...uint32) UInt32 { - return (&uint32Container{&Slice{}}).Append(i...) -} - -type uint32Container struct{ s *Slice } - -// Append implements Append for UInt32. -func (u *uint32Container) Append(i ...uint32) UInt32 { - u.s.Append(uint32ToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for UInt32. -func (u *uint32Container) AppendLength(i ...uint32) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for UInt32. -func (u *uint32Container) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for UInt32. -func (u *uint32Container) Concatenate(v UInt32) UInt32 { - u.s.Concatenate(v.(*uint32Container).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for UInt32. -func (u *uint32Container) ConcatenateLength(v UInt32) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for UInt32. -func (u *uint32Container) Delete(i int) UInt32 { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for UInt32. -func (u *uint32Container) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for UInt32. -func (u *uint32Container) Each(fn func(int, uint32)) UInt32 { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(uint32))) - }) - return u -} - -// EachBreak implements EachBreak for UInt32. -func (u *uint32Container) EachBreak(fn func(int, uint32) bool) UInt32 { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(uint32))) - }) - return u -} - -// EachReverse implements EachReverse for UInt32. -func (u *uint32Container) EachReverse(fn func(int, uint32)) UInt32 { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(uint32))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for UInt32. -func (u *uint32Container) EachReverseBreak(fn func(int, uint32) bool) UInt32 { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(uint32))) - }) - return u -} - -// Fetch implements Fetch for UInt32. -func (u *uint32Container) Fetch(i int) uint32 { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for UInt32. -func (u *uint32Container) FetchLength(i int) (uint32, int) { - v, i := u.s.FetchLength(i) - return v.(uint32), i -} - -// Get implements Get for UInt32. -func (u *uint32Container) Get(i int) (uint32, bool) { - var ( - ok bool - s uint32 - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(uint32) - } - return s, ok -} - -// GetLength implements GetLength for UInt32. -func (u *uint32Container) GetLength(i int) (uint32, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(uint32), l, ok -} - -// Len implements Len for UInt32. -func (u *uint32Container) Len() int { - return u.s.Len() -} - -// Less implements Less for UInt32. -func (u *uint32Container) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for UInt32. -func (u *uint32Container) Make(i int) UInt32 { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for UInt32. -func (u *uint32Container) MakeEach(v ...uint32) UInt32 { - u.s.MakeEach(uint32ToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for UInt32. -func (u *uint32Container) MakeEachReverse(v ...uint32) UInt32 { - u.s.MakeEachReverse(uint32ToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for UInt32. -func (u *uint32Container) Map(fn func(int, uint32) uint32) UInt32 { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(uint32))) - }) - return u -} - -// Poll implements Poll for UInt32. -func (u *uint32Container) Poll() uint32 { - var ( - s uint32 - v = u.s.Poll() - ) - if v != nil { - s = (v.(uint32)) - } - return s -} - -// PollLength implements PollLength for UInt32. -func (u *uint32Container) PollLength() (uint32, int) { - v, l := u.s.PollLength() - return v.(uint32), l -} - -// PollOK implements PollOK for UInt32. -func (u *uint32Container) PollOK() (uint32, bool) { - v, ok := u.s.PollOK() - return v.(uint32), ok -} - -// Pop implements Pop for UInt32. -func (u *uint32Container) Pop() uint32 { - var ( - s uint32 - v = u.s.Pop() - ) - if v != nil { - s = (v.(uint32)) - } - return s -} - -// PopLength implements PopLength for UInt32. -func (u *uint32Container) PopLength() (uint32, int) { - v, l := u.s.PopLength() - return v.(uint32), l -} - -// PopOK implements PopOK for UInt32. -func (u *uint32Container) PopOK() (uint32, bool) { - v, ok := u.s.PopOK() - return v.(uint32), ok -} - -// Precatenate implements Precatenate for UInt32. -func (u *uint32Container) Precatenate(v UInt32) UInt32 { - u.s.Precatenate(v.(*uint32Container).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for UInt32. -func (u *uint32Container) PrecatenateLength(v UInt32) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for UInt32. -func (u *uint32Container) Prepend(i ...uint32) UInt32 { - u.s.Prepend(uint32ToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for UInt32. -func (u *uint32Container) PrependLength(v ...uint32) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for UInt32. -func (u *uint32Container) Push(i ...uint32) int { - return u.s.Push(uint32ToInterfaceSlice(i...)) -} - -// Replace implements Replace for UInt32. -func (u *uint32Container) Replace(i int, n uint32) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for UInt32. -func (u *uint32Container) Reverse() UInt32 { - u.s.Reverse() - return u -} - -// Set implements Set for UInt32. -func (u *uint32Container) Set() UInt32 { - u.s.Set() - return u -} - -// Slice implements Slice for UInt32. -func (u *uint32Container) Slice(i int, j int) UInt32 { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for UInt32. -func (u *uint32Container) Sort() UInt32 { - sort.Sort(u) - return u -} - -// Swap implements Swap for UInt32. -func (u *uint32Container) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for UInt32. -func (u *uint32Container) Unshift(i ...uint32) int { - return (u.s.Unshift(uint32ToInterfaceSlice(i...))) -} - -// Values implements Values for UInt32. -func (u *uint32Container) Values() []uint32 { - var v = make([]uint32, u.Len()) - u.Each(func(i int, n uint32) { - v[i] = n - }) - return v -} - -func uint32ToInterfaceSlice(n ...uint32) []interface{} { - var ( - i int - v uint32 - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/uint64.go b/uint64.go deleted file mode 100644 index 0a11b76..0000000 --- a/uint64.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// UInt64 is the interface that handles a uint64 collection. -type UInt64 interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...uint64) UInt64 - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...uint64) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s UInt64) UInt64 - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s UInt64) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) UInt64 - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, uint64)) UInt64 - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, uint64) bool) UInt64 - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, uint64)) UInt64 - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, uint64) bool) UInt64 - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) uint64 - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (uint64, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (uint64, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (uint64, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) UInt64 - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...uint64) UInt64 - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...uint64) UInt64 - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, uint64) uint64) UInt64 - // Poll removes the first element from the slice and returns that removed element. - Poll() uint64 - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (uint64, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (uint64, bool) - // Pop removes the last element from the slice and returns that element. - Pop() uint64 - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (uint64, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (uint64, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s UInt64) UInt64 - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s UInt64) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...uint64) UInt64 - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...uint64) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...uint64) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v uint64) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() UInt64 - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() UInt64 - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) UInt64 - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...uint64) int - // Values returns the internal values of the slice. - Values() []uint64 -} - -// NewUInt64 returns a new UInt64 interface. -func NewUInt64(i ...uint64) UInt64 { - return (&uint64Container{&Slice{}}).Append(i...) -} - -type uint64Container struct{ s *Slice } - -// Append implements Append for UInt64. -func (u *uint64Container) Append(i ...uint64) UInt64 { - u.s.Append(uint64ToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for UInt64. -func (u *uint64Container) AppendLength(i ...uint64) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for UInt64. -func (u *uint64Container) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for UInt64. -func (u *uint64Container) Concatenate(v UInt64) UInt64 { - u.s.Concatenate(v.(*uint64Container).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for UInt64. -func (u *uint64Container) ConcatenateLength(v UInt64) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for UInt64. -func (u *uint64Container) Delete(i int) UInt64 { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for UInt64. -func (u *uint64Container) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for UInt64. -func (u *uint64Container) Each(fn func(int, uint64)) UInt64 { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(uint64))) - }) - return u -} - -// EachBreak implements EachBreak for UInt64. -func (u *uint64Container) EachBreak(fn func(int, uint64) bool) UInt64 { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(uint64))) - }) - return u -} - -// EachReverse implements EachReverse for UInt64. -func (u *uint64Container) EachReverse(fn func(int, uint64)) UInt64 { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(uint64))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for UInt64. -func (u *uint64Container) EachReverseBreak(fn func(int, uint64) bool) UInt64 { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(uint64))) - }) - return u -} - -// Fetch implements Fetch for UInt64. -func (u *uint64Container) Fetch(i int) uint64 { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for UInt64. -func (u *uint64Container) FetchLength(i int) (uint64, int) { - v, i := u.s.FetchLength(i) - return v.(uint64), i -} - -// Get implements Get for UInt64. -func (u *uint64Container) Get(i int) (uint64, bool) { - var ( - ok bool - s uint64 - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(uint64) - } - return s, ok -} - -// GetLength implements GetLength for UInt64. -func (u *uint64Container) GetLength(i int) (uint64, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(uint64), l, ok -} - -// Len implements Len for UInt64. -func (u *uint64Container) Len() int { - return u.s.Len() -} - -// Less implements Less for UInt64. -func (u *uint64Container) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for UInt64. -func (u *uint64Container) Make(i int) UInt64 { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for UInt64. -func (u *uint64Container) MakeEach(v ...uint64) UInt64 { - u.s.MakeEach(uint64ToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for UInt64. -func (u *uint64Container) MakeEachReverse(v ...uint64) UInt64 { - u.s.MakeEachReverse(uint64ToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for UInt64. -func (u *uint64Container) Map(fn func(int, uint64) uint64) UInt64 { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(uint64))) - }) - return u -} - -// Poll implements Poll for UInt64. -func (u *uint64Container) Poll() uint64 { - var ( - s uint64 - v = u.s.Poll() - ) - if v != nil { - s = (v.(uint64)) - } - return s -} - -// PollLength implements PollLength for UInt64. -func (u *uint64Container) PollLength() (uint64, int) { - v, l := u.s.PollLength() - return v.(uint64), l -} - -// PollOK implements PollOK for UInt64. -func (u *uint64Container) PollOK() (uint64, bool) { - v, ok := u.s.PollOK() - return v.(uint64), ok -} - -// Pop implements Pop for UInt64. -func (u *uint64Container) Pop() uint64 { - var ( - s uint64 - v = u.s.Pop() - ) - if v != nil { - s = (v.(uint64)) - } - return s -} - -// PopLength implements PopLength for UInt64. -func (u *uint64Container) PopLength() (uint64, int) { - v, l := u.s.PopLength() - return v.(uint64), l -} - -// PopOK implements PopOK for UInt64. -func (u *uint64Container) PopOK() (uint64, bool) { - v, ok := u.s.PopOK() - return v.(uint64), ok -} - -// Precatenate implements Precatenate for UInt64. -func (u *uint64Container) Precatenate(v UInt64) UInt64 { - u.s.Precatenate(v.(*uint64Container).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for UInt64. -func (u *uint64Container) PrecatenateLength(v UInt64) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for UInt64. -func (u *uint64Container) Prepend(i ...uint64) UInt64 { - u.s.Prepend(uint64ToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for UInt64. -func (u *uint64Container) PrependLength(v ...uint64) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for UInt64. -func (u *uint64Container) Push(i ...uint64) int { - return u.s.Push(uint64ToInterfaceSlice(i...)) -} - -// Replace implements Replace for UInt64. -func (u *uint64Container) Replace(i int, n uint64) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for UInt64. -func (u *uint64Container) Reverse() UInt64 { - u.s.Reverse() - return u -} - -// Set implements Set for UInt64. -func (u *uint64Container) Set() UInt64 { - u.s.Set() - return u -} - -// Slice implements Slice for UInt64. -func (u *uint64Container) Slice(i int, j int) UInt64 { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for UInt64. -func (u *uint64Container) Sort() UInt64 { - sort.Sort(u) - return u -} - -// Swap implements Swap for UInt64. -func (u *uint64Container) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for UInt64. -func (u *uint64Container) Unshift(i ...uint64) int { - return (u.s.Unshift(uint64ToInterfaceSlice(i...))) -} - -// Values implements Values for UInt64. -func (u *uint64Container) Values() []uint64 { - var v = make([]uint64, u.Len()) - u.Each(func(i int, n uint64) { - v[i] = n - }) - return v -} - -func uint64ToInterfaceSlice(n ...uint64) []interface{} { - var ( - i int - v uint64 - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -} diff --git a/uint8.go b/uint8.go deleted file mode 100644 index da0771d..0000000 --- a/uint8.go +++ /dev/null @@ -1,396 +0,0 @@ -package slice - -import ( - "sort" -) - -// UInt8 is the interface that handles a uint8 collection. -type UInt8 interface { - // Append adds n elements to the end of the slice - // and returns the modified slice. - Append(i ...uint8) UInt8 - // AppendLength adds n elements to the end of the slice and returns the length of the modified slice. - AppendLength(i ...uint8) int - // Bounds checks an integer value safely sits within the range of - // accessible values for the slice. - Bounds(i int) bool - // Concatenate merges the elements from the argument slice - // to the the tail of the argument slice. - Concatenate(s UInt8) UInt8 - // ConcatenateLength merges the elements from the argument slice to the tail of the receiver slice - // and returns the length of the receiver slice. - ConcatenateLength(s UInt8) int - // Delete deletes the element from the argument index and returns the modified slice. - Delete(i int) UInt8 - // DeleteLength deletes the element from the argument index and returns the new length of the slice. - DeleteLength(i int) int - // Each executes a provided function once for each slice element and returns the slice. - Each(fn func(int, uint8)) UInt8 - // EachBreak executes a provided function once for each - // element with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachBreak(fn func(int, uint8) bool) UInt8 - // EachReverse executes a provided function once for each - // element in the reverse order they are stored in the slice. - // Returns the slice at the end of the iteration. - EachReverse(fn func(int, uint8)) UInt8 - // EachReverseBreak executes a provided function once for each - // element in the reverse order they are stored in the slice - // with an optional break when the function returns false. - // Returns the slice at the end of the iteration. - EachReverseBreak(fn func(int, uint8) bool) UInt8 - // Fetch retrieves the element held at the argument index. - // Returns the default type if index exceeds slice length. - Fetch(i int) uint8 - // FetchLength retrives the element held at the argument index and the length of the slice. - // Returns the default type if index exceeds slice length. - FetchLength(i int) (uint8, int) - // Get returns the element held at the argument index and a boolean - // indicating if it was successfully retrieved. - Get(i int) (uint8, bool) - // GetLength returns the element at the argument index, the length of the slice - // and a boolean indicating if the element was successfully retrieved. - GetLength(i int) (uint8, int, bool) - // Len returns the number of elements in the slice. - Len() int - // Make empties the slice, sets the new slice to the length of n and returns the modified slice. - Make(i int) UInt8 - // MakeEach empties the slice, sets the new slice to the length of n and performs - // a for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEach(v ...uint8) UInt8 - // MakeEachReverse empties the slice, sets the new slice to the length of n and performs - // an inverse for-each loop for the argument sequence, inserting each entry at the - // appropriate index before returning the modified slice. - MakeEachReverse(v ...uint8) UInt8 - // Map executes a provided function once for each element and sets - // the returned value to the current index. - // Returns the slice at the end of the iteration. - Map(fn func(int, uint8) uint8) UInt8 - // Poll removes the first element from the slice and returns that removed element. - Poll() uint8 - // PollLength removes the first element from the slice and returns the removed element and the length - // of the modified slice. - PollLength() (uint8, int) - // PollOK removes the first element from the slice and returns a boolean on the outcome of the transaction. - PollOK() (uint8, bool) - // Pop removes the last element from the slice and returns that element. - Pop() uint8 - // PopLength removes the last element from the slice and returns the removed element and the length - // of the modified slice. - PopLength() (uint8, int) - // PopOK removes the last element from the slice and returns a boolean on the outcome of the transaction. - PopOK() (uint8, bool) - // Precatenate merges the elements from the argument slice - // to the the head of the argument slice and returns the modified slice. - Precatenate(s UInt8) UInt8 - // PrecatenateLength merges the elements from the argument slice to the head of the receiver slice - // and returns the length of the receiver slice. - PrecatenateLength(s UInt8) int - // Prepend adds one element to the head of the slice - // and returns the modified slice. - Prepend(i ...uint8) UInt8 - // PrependLength adds n elements to the head of the slice and returns the length of the modified slice. - PrependLength(i ...uint8) int - // Push adds a new element to the end of the slice and - // returns the length of the modified slice. - Push(i ...uint8) int - // Replace changes the contents of the slice - // at the argument index if it is in bounds. - Replace(i int, v uint8) bool - // Reverse reverses the slice in linear time. - // Returns the slice at the end of the iteration. - Reverse() UInt8 - // Set returns a unique slice, removing duplicate - // elements that have the same hash value. - // Returns the modified at the end of the iteration. - Set() UInt8 - // Slice slices the slice from i to j and returns the modified slice. - Slice(i int, j int) UInt8 - // Swap moves element i to j and j to i. - Swap(i int, j int) - // Unshift adds one or more elements to the beginning of the slice and - // returns the new length of the modified slice. - Unshift(i ...uint8) int - // Values returns the internal values of the slice. - Values() []uint8 -} - -// NewUInt8 returns a new UInt8 interface. -func NewUInt8(i ...uint8) UInt8 { - return (&uint8Container{&Slice{}}).Append(i...) -} - -type uint8Container struct{ s *Slice } - -// Append implements Append for UInt8. -func (u *uint8Container) Append(i ...uint8) UInt8 { - u.s.Append(uint8ToInterfaceSlice(i...)...) - return u -} - -// AppendLength implements Append for UInt8. -func (u *uint8Container) AppendLength(i ...uint8) int { - return u.Append(i...).Len() -} - -// Bounds implements Bounds for UInt8. -func (u *uint8Container) Bounds(i int) bool { - return u.s.Bounds(i) -} - -// Concatenate implements Concatenate for UInt8. -func (u *uint8Container) Concatenate(v UInt8) UInt8 { - u.s.Concatenate(v.(*uint8Container).s) - return u -} - -// ConcatenateLength implements ConcatenateLength for UInt8. -func (u *uint8Container) ConcatenateLength(v UInt8) int { - return u.Concatenate(u).Len() -} - -// Delete implements Delete for UInt8. -func (u *uint8Container) Delete(i int) UInt8 { - u.s.Delete(i) - return u -} - -// DeleteLength implements DeleteLength for UInt8. -func (u *uint8Container) DeleteLength(i int) int { - return u.Delete(i).Len() -} - -// Each implements Each for UInt8. -func (u *uint8Container) Each(fn func(int, uint8)) UInt8 { - u.s.Each(func(i int, v interface{}) { - fn(i, (v.(uint8))) - }) - return u -} - -// EachBreak implements EachBreak for UInt8. -func (u *uint8Container) EachBreak(fn func(int, uint8) bool) UInt8 { - u.s.EachBreak(func(i int, v interface{}) bool { - return fn(i, (v.(uint8))) - }) - return u -} - -// EachReverse implements EachReverse for UInt8. -func (u *uint8Container) EachReverse(fn func(int, uint8)) UInt8 { - u.s.EachReverse(func(i int, v interface{}) { - fn(i, (v.(uint8))) - }) - return u -} - -// EachReverseBreak implements EachReverseBreak for UInt8. -func (u *uint8Container) EachReverseBreak(fn func(int, uint8) bool) UInt8 { - u.s.EachReverseBreak(func(i int, v interface{}) bool { - return fn(i, (v.(uint8))) - }) - return u -} - -// Fetch implements Fetch for UInt8. -func (u *uint8Container) Fetch(i int) uint8 { - var s, _ = u.Get(i) - return s -} - -// FetchLength implements FetchLength for UInt8. -func (u *uint8Container) FetchLength(i int) (uint8, int) { - v, i := u.s.FetchLength(i) - return v.(uint8), i -} - -// Get implements Get for UInt8. -func (u *uint8Container) Get(i int) (uint8, bool) { - var ( - ok bool - s uint8 - ) - ok = u.Bounds(i) - if ok { - s = (u.s.Fetch(i)).(uint8) - } - return s, ok -} - -// GetLength implements GetLength for UInt8. -func (u *uint8Container) GetLength(i int) (uint8, int, bool) { - v, l, ok := u.s.GetLength(i) - return v.(uint8), l, ok -} - -// Len implements Len for UInt8. -func (u *uint8Container) Len() int { - return u.s.Len() -} - -// Less implements Less for UInt8. -func (u *uint8Container) Less(i int, j int) bool { - return u.Fetch(i) < u.Fetch(j) -} - -// Make implements Make for UInt8. -func (u *uint8Container) Make(i int) UInt8 { - u.s.Make(i) - return u -} - -// MakeEach implements MakeEach for UInt8. -func (u *uint8Container) MakeEach(v ...uint8) UInt8 { - u.s.MakeEach(uint8ToInterfaceSlice(v...)...) - return u -} - -// MakeEachReverse implements MakeEachReverse for UInt8. -func (u *uint8Container) MakeEachReverse(v ...uint8) UInt8 { - u.s.MakeEachReverse(uint8ToInterfaceSlice(v...)...) - return u -} - -// Map implements Map for UInt8. -func (u *uint8Container) Map(fn func(int, uint8) uint8) UInt8 { - u.s.Map(func(i int, v interface{}) interface{} { - return fn(i, (v.(uint8))) - }) - return u -} - -// Poll implements Poll for UInt8. -func (u *uint8Container) Poll() uint8 { - var ( - s uint8 - v = u.s.Poll() - ) - if v != nil { - s = (v.(uint8)) - } - return s -} - -// PollLength implements PollLength for UInt8. -func (u *uint8Container) PollLength() (uint8, int) { - v, l := u.s.PollLength() - return v.(uint8), l -} - -// PollOK implements PollOK for UInt8. -func (u *uint8Container) PollOK() (uint8, bool) { - v, ok := u.s.PollOK() - return v.(uint8), ok -} - -// Pop implements Pop for UInt8. -func (u *uint8Container) Pop() uint8 { - var ( - s uint8 - v = u.s.Pop() - ) - if v != nil { - s = (v.(uint8)) - } - return s -} - -// PopLength implements PopLength for UInt8. -func (u *uint8Container) PopLength() (uint8, int) { - v, l := u.s.PopLength() - return v.(uint8), l -} - -// PopOK implements PopOK for UInt8. -func (u *uint8Container) PopOK() (uint8, bool) { - v, ok := u.s.PopOK() - return v.(uint8), ok -} - -// Precatenate implements Precatenate for UInt8. -func (u *uint8Container) Precatenate(v UInt8) UInt8 { - u.s.Precatenate(v.(*uint8Container).s) - return u -} - -// PrecatenateLength implements PrecatenateLength for UInt8. -func (u *uint8Container) PrecatenateLength(v UInt8) int { - return u.Precatenate(v).Len() -} - -// Prepend implements Prepend for UInt8. -func (u *uint8Container) Prepend(i ...uint8) UInt8 { - u.s.Prepend(uint8ToInterfaceSlice(i...)...) - return u -} - -// PrependLength implements PrependLength for UInt8. -func (u *uint8Container) PrependLength(v ...uint8) int { - return u.Prepend(v...).Len() -} - -// Push implements Push for UInt8. -func (u *uint8Container) Push(i ...uint8) int { - return u.s.Push(uint8ToInterfaceSlice(i...)) -} - -// Replace implements Replace for UInt8. -func (u *uint8Container) Replace(i int, n uint8) bool { - return (u.s.Replace(i, n)) -} - -// Reverse implements Reverse for UInt8. -func (u *uint8Container) Reverse() UInt8 { - u.s.Reverse() - return u -} - -// Set implements Set for UInt8. -func (u *uint8Container) Set() UInt8 { - u.s.Set() - return u -} - -// Slice implements Slice for UInt8. -func (u *uint8Container) Slice(i int, j int) UInt8 { - u.s.Slice(i, j) - return u -} - -// Sort implements Sort for UInt8. -func (u *uint8Container) Sort() UInt8 { - sort.Sort(u) - return u -} - -// Swap implements Swap for UInt8. -func (u *uint8Container) Swap(i int, j int) { - u.s.Swap(i, j) -} - -// Unshift implements Unshift for UInt8. -func (u *uint8Container) Unshift(i ...uint8) int { - return (u.s.Unshift(uint8ToInterfaceSlice(i...))) -} - -// Values implements Values for UInt8. -func (u *uint8Container) Values() []uint8 { - var v = make([]uint8, u.Len()) - u.Each(func(i int, n uint8) { - v[i] = n - }) - return v -} - -func uint8ToInterfaceSlice(n ...uint8) []interface{} { - var ( - i int - v uint8 - x = make([]interface{}, (len(n))) - ) - for i, v = range n { - x[i] = v - } - return x -}