A collection of GO data structures

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

First, what is a collection

A collection is an unordered aggregation of different objects. So what's the link between a list and a set? Let's change a magic trick. As a linked list of colors:

Now let's turn the list into a collection step at a pace.

The first step is to cut the link

Step two, remove the duplicates.

The third step in a box, shake it and it's all set.

You can see that the collection has these characteristics:

    • Unordered: The linked list removes the link, which is to remove the ordered state between elements.
    • Do not repeat: Remove the duplicate Rose red.

Although the collection is a mathematical concept, in real life there is no place to reveal the collection. For example, a class of students is a collection. The boys in the class are also a collection.

II. structure of the set

Brother David here, in order to simplify the concept of the description, continue to use a single-linked list to represent the collection, but in the calculation of the collection when the single-linked list is not appropriate, when the data is large, its drawbacks will appear, in the back of the data structure and algorithm, we look back to improve the previous data interface implementation.

Third, the interface description and implementation

2. Init
Initializes the set, essentially initializing the linked list.

Func (set *set) Init (Match ... Matchfun) {lst: = new (list) (*set). List = LST If Len (match) = = 0 {lst. Init ()} else {lst. Init (Match[0])}}

To compare the elements in the collection, we have to pass in a comparison function, where match is our custom type Matchfun, and you can see the definition in the code.
2. Insert
Put elements into the collection.

Func (set *set) Insert (data Object) bool {    if (!set. IsMember (data)) {        return (*set). List. Append (data)    }    return False}

3, IsEmpty
Whether it is an empty collection.

Func (set *set) ismember (data Object) bool {    return (*set). List. IsMember (data);}

4, IsMember
Whether it is a collection element.

Func (set *set) ismember (data Object) bool {    return (*set). List. IsMember (data);}

5. Remove
Deletes the specified collection element.

Func (set *set) Remove (data Object) bool {    return (*set). List. Remove (data)}

6. Union
And the calculation of the set.

Func (set *set) Union (Set1 *set) *set {    if (Set1 = nil) {        return nil    }    Nset: = new (set)    Nset.init (( * ((*set). List)). Mymatch)    if (set. IsEmpty () && Set1. IsEmpty ()) {         return Nset    } for    I: = UInt64 (0); I < set.getsize (); i++ {        Nset.insert (Set.getat (i))    }    var data Object for    i: = UInt64 (0); I < set1.getsize (); i++ {        data = Set1.getat (i)        if (!nset.ismember (DA TA)) {            Nset.insert (data)        }    }    return Nset}

Computes the set and Set1 of the sets.
7, Intersection
Computes the intersection.

Func (set *set) intersection (Set1 *set) *set {    if (Set1 = nil) {        return nil    }    Nset: = new (set)    nset.i NIT ((* (*set). List). Mymatch)    if (set. IsEmpty () | | Set1. IsEmpty ()) {        return nset    }    fSet: = Set    SSet: = Set1    Lenth: = Set.getsize ()    if (Set1.getsize ( ) < Lenth) {        FSet = Set1        SSet = set    }    var data Object for    i: = UInt64 (0); i < lenth; i++ {        data = Fset.getat (i)        if (Sset.ismember (data)) {            Nset.insert (data)        }    }    return Nset}

8, Difference
Calculates the difference set.

Func (set *set) difference (Set1 *set) *set {    if (Set1 = nil) {        return nil    }    Nset: = new (set)    Nset.ini T ((* (*set). List). Mymatch)    if (set. IsEmpty ()) {        return nset    }    var data Object for    i: = UInt64 (0); I < set.getsize (); i++ {        data = SE T.getat (i)        if (!set1. IsMember (data) {            Nset.insert (data)}    }    return Nset}

The returned collection is a collection that belongs to set, but does not belong to Set1.
9, Issubset

Func (set *set) Issubset (subset *set) bool {    if (set = = nil) {        return false    }    if (subset = nil) {
   return true    } for    I: = UInt64 (0), I < Subset.getsize (), i++ {        if (!) ( Set. IsMember (Subset.getat (i))) {            return False        }    }    return True}

Confirm if subset is a subset of set.
10. Equals

Func (set *set) Equals (Set1 *set) bool {    if (set = = Nil | | set1 = = nil) {        return false    }    if (set. IsEmpty () && Set1. IsEmpty ()) {        return True    }    nset: = set. Intersection (SET1)    return (set.getsize () = = Nset.getsize ())}

Determines whether set and Set1 elements are the same.
11. Accessing collection elements
Because the collection is not sequential, it is not possible to use the ordinal to access the collection element (although it is implemented in a single-linked list). Here we use iterators to implement the element's access. First we define an interface for an iterator.
(1) Iterator

Type Iterator interface{    hasnext () bool    Next () Object}

(2) Setiterator

Type setiterator struct {    index UInt64    set *set}

Because iterator is an interface and cannot save state, we have to define a type to hold the cursor for each access. The cursor here is the ordinal number.
(3) Getiterator
Returns an object that implements the iterator interface.

Func (set *set) Getiterator () *setiterator {    iterator: = new (Setiterator)    (*iterator). Index = 0    (*iterator) . Set = Set    return iterator}

(4) Hasnext
Are there other elements that are not accessed?

Func (iterator *setiterator) Hasnext () bool {    set: = (*iterator). Set    Index: = (*iterator). Index    return ( Index < Set.getsize ())}

This is the implementation of the Hasnext method in iterator.
(5) Next
Gets the other elements.

Func (iterator *setiterator) Next () Object {    set: = (*iterator). Set    Index: = (*iterator). Index    if (Index & Lt Set.getsize ()) {        Data: = Set.getat (index)        (*iterator). index++        return Data        }    return nil}

Iv. Summary
The set has many applications in probability, and here we simply implement the set with a single-linked list, which is very inefficient under a large amount of data. As the learning progresses, we will optimize the implementation of these data interfaces.

Code download

1014 Reads
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.