Swift Lane Stack implementation

Today in practice design patterns when there is no sudden discovery Swift stack - Stack class is。Since I was shining Java version of the design patterns to learn,So I had no choice but to implement a look at the。

But speaking,In Apple's official manual, "generic" chapter really have a stack of chestnuts,It is the structure implemented。This is actually in line with the Swift's style - after all,, Stack did not need to achieve with the class - in the structure of the Swift is。

From Wikipedia

From Wikipedia

Here we briefly review what is the stack - in fact, it may have different definitions in different levels,We refer here to the definition of Wikipedia:

StackEnglishstack),It can also be directly calledStackFor TaiwanStacking,incomputer sciencein,It is a special form of serial data structures,It is special because only allow one end of the linked list or array (called the stack top index,Englishtop) Were added data (Englishpush) And output data (Englishpop) Operation。In addition it can also be stacked with one-dimensionalArrayorSerial LinkForm to complete。Stacking another mode of operation is called a relativeQueue

Since the stack data structure may only be performed at one end,Thus according to the LIFO (LIFO, Last In First Out) principle of operation。

Stack data structure uses two basic operations:Push (push) and pop (pop):

  • Push:Put data stack to the top (in the form of an array or tandem),Plus a stack top top Indicators。
  • pop up:The top data output (return),Save a data stack to the top。

Generic version

Here we use a generic array as a stack of storage,Then is a simple push and pop,Other,I also added two for detecting whether the stack is empty and does not remove the top to see only way,In that case,On and in Java Stack class almost。

To use the words to declare a generic manner in accordance with: where GVersionStack = StackG<SomeClass>()

Because here it is the structure we use,So to declare a variable rather than a constant。Also it is therefore,Our part of the structure in vivo data have marked a modified approach mutating Keyword。

Here we look at a use of chestnuts:

I readily wrote a class,While we use the stack to store the instance of this class (reference)。

I declared three different instances,Give them a different serial number to track,Then pushed onto the stack in。Because it is generic,So after stack of data must be SomeClass Type (or subclass),Otherwise you can not enter。This is also consistent with the type-safe language style Swift。

Class version

but,I still want to get it on like Java Stack class as,How to do it? I put the stack changed to change:

This time I used a class instead of a structure to achieve,So get rid of the generic statement when more convenient,It can also be declared as constant - because it saved a reference to the class。

Here we declare an array of type became Any ,So that it can refer to any type of,Including classes,Swift also included in the basic types。

note

Because Swift in the basic types is through packaging structure,And they are not inherited from AnyObject ,If so write as OC,It can not be put into basic types! and,Swift does not have a default inherit the base class habits,So we use Any rather than AnyObject

but,In this way there will be no type,No matter what you put into it,Swift does not help you check,Because they have become Any ,Then we will manually check type。All right,Chestnut still on top of that SomeClass ,This time we use a new stack to try:

This time I not only pushed the SomeClass ,Also pressed into the string and shaping the digital,Everything is so perfect。

Then,I turn pop the stack of data,But I have to convert them before using them in formats,Otherwise, you can not use - because they do not know the Swift already。

Further reading

At last,I have written these two stack foul to spread their shameless Github on,For you to download and use reference;

About generic content,Self-referenceHere

About StackWikipedia

 

Original article written by Gerber drop-off:R0uter's Blog » Swift Lane Stack implementation

Reproduced Please keep the source and description link:https://www.logcg.com/archives/1505.html

About the Author

R0uter

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *