[DATA STRUCTURE & Other Objects Using JAVA] Chapter 4 Linked Lists

Class IntNode

An IntNode provides a node for a linked list with integer data in each node. Lists can be of any length, limited only by the amount of free memory on the heap. But beyond Integer.MAX_VALUE, the answer from listLength() is incorrect because of arithmetic overflow.

Field

Constructor

Method

Implementation

IntLinkedBag

An IntLinkedBag is a collection of int numbers.

Limitations

  1. Beyond Int.MAX_VALUE elements, countOccurrence, size and grab are wrong.
  2. Because of the slow linear algorithms of this class, large bags have poor performance.

Constructor

Method

Implementation

Objective-C Block

objc_block

In Objective-C, you can pass a primitive type variable, a pointer or a object to a function. You can also pass a piece of code, which can be regarded as a “function”, to a function. This is called block in Objective-C. Block looks complicated at first, however, once you start to analyze the syntax, you will find out that the syntax is really intuitive and straightforward.

Here are two great posts that I want to recommend to you.

Also, here is a great sample code from GitHub

Since I am really familiar with C, I also want to summarize the block syntax by talking about C syntax first.

Objective-C Block Syntax and C

I believe that one of the best ways to learn new ticks is to relate those new ones to some old ones that I have already knew. I am familiar with C, so I’ll start with C.

There are four modifiers that are extensively used in Objective-C block: ^, *, [], {}. Fortunately, only the first one is not included in C. The others are really common modifiers in C. If you are a C person, you will find them familiar.

It is important to keep how to read a declaration in mind. The first thing is to find the variable name or identifier. Then, your should go right until you can’t and the start over from the left of the variable. A declaration can only have 1 basic type and it’s the left most word of the declarator. Another important thing is that [] and () have higher precedence over * and ^.

Here is a list of C declarators

1. Basic declarator.

a is the variable name, and int is the type.

2. The pointer modifier

3. The array modifier

4. The function identifier.

The function name is a, and the return type is int.

5. Array of pointers.

Since [] has higher precedence than *, a is an array first. Then find the basic type which is the pointer to int. Thus, a is an array of points to int.

6. Pointer to array.

Again, because of the precedence rule, if you want a pointer to array, you need a pair of parenthesizes like this.

7. Function takes array as parameter

You can’t have an array of function and a function cannot return an array of a function. However, A function can take an array as argument.

8. Function returns pointer

As () has higher precedence than *, f is a function first and returns an int.

9. Pointer to a function

Similar to the relationship between array and pointer, if you want a pointer to a function, you need to use parenthesizes.

The Block Pointer ^

The block pointer ^ is similar to a pointer modifier. Since the block is similar to a function, you declare a block the same way you would declare a pointer to a function. Just replace the * in int (*f)() with the block pointer ^.

b is the block pointer to a function that returns an int.

A block can also have parameters.

Abstract Declarator

Declarator = Abstract Declarator + Identifier

The abstract declarator for the block above will be int (^)(long a, long b) or int (^)(long, long)

Examples:

How to Use Blocks

As a local variable

Template:

Example:

Here is an example which uses block to calculate addition. You will the output of 300.

As a property

As a method parameter

As an argument to a method call

Template:

Example:

As a typedef

Template:

Example:

Game Mechanics and Dynamics Analysis – DotA (Defense of the Ancient)

79815 I started to play DotA when I was in middle school. I really like this game, so I want to write this essay to analysis mechanics and dynamics of DotA. DotA pits two teams of players against each other, and the objective is for each team to destroy the opponents’ Ancient, heavily guarded structures at opposing corners of the map. Each player controls a single powerful unit called hero, and is assisted by allied heroes and AI-controlled fighters. Each heroes has four unique abilities. Heroes need experience to gain levels to enhance their abilities. Gold is the only resource that players need to manage, and is used to buy equipments to strengthen heroes and gain extra abilities. Heroes obtain experience and gold by killing hostile units, base structures, and enemy heroes. The first mechanic is called “farm”, which refers to the act of systematically killing hostile and neutral units to earn gold. Every players “farms” throughout the entire game, because gold plays a vital role in updating equipment which is extremely important for victory. Each team has a core hero called carry who is able to obtain the greatest power and equipment as the game progress. Carries bear the responsibility for ultimate victory, so they are dedicate and concentrate on farming most of the time. maxresdefault “Gank” is an other important mechanic in DotA. “Gank” means bringing several heroes to gang up on a target to kill it. Gank is one of the most useful ways to prevent carries from gaining more gold, because when a hero is kill, it takes a period of time which is determined by the level of the hero to rebirth. “Gank” is usually toward carries, because they need gold to become strong. “Gank” is not always toward carries. “Gank” can be toward anyone to prevent him or her from getting enough gold to obtain some specific vital equipments. AAW_Attack_GankerVenn There are three lanes that connect two Ancients of the two teams. In order to destroy the Ancient to win the game, each team needs to travel along a lane to work together to fight and kill enemy buildings. This action is called “push”. According to the objective of the game, “push” is the most important mechanic. Your hero can be low-leveled and poor, but as soon as you destroy your enemies’ Ancient, you will win the game. “Push” is the only mechanic which works directly to destroy buildings, so I believe that “push” is the core mechanic of DotA. You can be high-leveled, have fancy equipments and kill a lot of heroes, but you may lose the game if you are not able to destroy your enemies’ Ancient. Thus every time when I play, I will keep the ultimate goal which is to destroy the Ancient in mind. I also apply this to my daily life. I have numerous options of what to do every day. I always set up goals and keep them in mind, so I can complete the most important things to achieve my goals. The dynamic I want to analysis is the constantly changing of the visible area on the map. Only area around your hero and your allies, including allied heroes and allied computer-controlled units, will be visible to you. Farm, gank and push result in the change of the position of your hero and your allied heroes, so your visible area changes accordingly. You can see your enemy heroes only when they are close to you. You can see them when you are ganking or pushing. However, when you are farming, you can not see them, because they are not around you. In order to avoid being ganked, you need to anticipate your enemies’ position based on your analysis of the game, which is one of the most challenging parts of DotA.

Interesting C Array

This is a block of easy C code, right? This will print out 4. This block of code can also be written as the following:

a can be placed inside the square brackets. This syntax is correct! The output will be 4 as well!

Vim Basic Commands

I decided to start to migrate to vim from other text editors, such as BBEdit. One reason is that vim will be super efficient if I can master this editor. Another one is that vim is easy to be deployed to other machines. I read Byte of Vim once last quarter, and I just start to read it one more time. Here is my vim repository on github. I will keep updating it.

vi-vim-cheat-sheet

Insert mode

Move your cursor the, Vim way

Words, sentences, paragraphs

Make your mark

Jump around

Parts of the text

Reading and wriing files

Cut, Copy and Paste

Time machine using undo/redo

A powerful search engine but not a dotcom

Multiple Sections Using Folds

Multiple Windows

Multiple Tabs