Heap implementation in Swift

Hello everyone,

As you can see, I haven’t been blogging for more than three years. That is a long time. But  I had my reasons. A lot has happened in both my personal, and professional life that kept me too busy and sparing little time to focus on blogging. However, I think it is time to start writing again. I like to write about technical topics. So I restart my blogging by kicking it off with this small post, and small snippet of code.

I love the heap data structure. It is a nifty little DS that maintains items in an order, giving you O(1) access to the min/max item, and pops/inserts new items in O(lgN) time. The same structure is used to implement a priority queue, which has uses in many places, e.g. Dijkstra algorithm. So keeping with my habit of implementing a known data structure in short period of free time, I give you a quick implementation of heap written in Swift. Let me know what you think of it, or if you find any problems in it.

Thank you for checking out my blog. I would love to hear from you in the comments section. And lets hope I come up with good content for you to read in the future.

Book Review: Effective Objective-C 2.0 by Matt Galloway

I love reading technical books. They provide you with a broader picture on a topic and go in depth on different issues that online tutorials just simply skim. That’s why each month I pick up a book related to the platforms I work with. My last month’s pick was “Effective Objective-C 2.0” by Matt Galloway. It was an enlightening read, so I thought I would share a review with my blog readers.

effective-objc-2.0

If you are like me, you picked Objective-C as a language while learning iOS development. I have over the years picked and improved my know-how of the language that is Objective-C, while being a full time iOS developer. The problem with learning Obj-C as you learn iOS development is that you are for the most part learning only higher level Cocoa (Touch) APIs and hardly get to see the efficient implementation behind these APIs on the language level. Thus I think it is important to set aside sometime, give Cocoa a break, and play with Obj-C language itself and get to know how it works under the hood, many of the new improvements it has to offer since 2.0 version, and some of the new ways the new Apple compiler, LLVM, has improved on it.

“Effective Objective-C 2.0” provides you with a clear path to knowing your programming language. It is divided into 52 items touching different parts of the language, divided into seven chapters.

Chapter 1: Accustoming Yourself to Objective-C

A simple start to the book, introducing you to Objective-C’s roots, how headers import work and how to effectively use it. Also touches on literal syntax, macros, and enumerations.

Chapter 2: Objects, Messaging, and the Runtime

Many newcomers to the language will go a long way working with Cocoa touch without realising that Objective-C has a very powerful runtime whose power can be harnessed to perform many useful tricks. This chapter talks about properties, objects equality, associating custom data to existing classes during runtime, talks about objc_msgSend and how message forwarding works. It also touches upon the very powerful Obj-C runtime feature of method swizzling.

Chapter 3: Interface and API Design

This chapter talks about how to properly implement your classes with clear and useful interfaces.

Chapter 4: Protocols and Categories

Protocols and Categories are prevalent throughout the Cocoa framework. This chapter talks about effectively using categories to provide additional functionality on top of your classes.

Chapter 5: Memory Management

Many people think that after introduction of ARC, studying memory management on iOS is not important. Nothing can be far from the truth. Understanding how memory management works on the platform is essential to understanding how ARC works and to avoid some of the gotchas ARC can have if you don’t understand how to avoid retain cycles even when ARC is enabled.

Chapter 6: Blocks and Grand Central Dispatch

One of the modern features of Obj-C are Blocks. This chapter talks about how to effectively use blocks. It also talks about the C API to handle concurrency on platform namely GCD and compares it to Operation Queues. It talks about how one can use GCD to allow thread synchronisation, instead of the @synthesize construct that most of us are used to.

Chapter 7: The System Frameworks

Last chapter talks about features provided in system frameworks and when should they be preferred. For example using block enumerations to for loops, using toll free bridging for memory management, using NSCache instead of NSDictionary to avail caching etc.

Conclusion:

This is a great book to have read. It will certainly make you more aware of what goes under the hood when you use any of the system libraries, and put you in a better position to write better classes of your own and debug like a pro.