About Me

My photo
I'm a colonist who has declared war on machines and intend to conquer them some day. You'll often find me deep in the trenches fighting off bugs and ugly defects in code. When I'm not tappity-tapping at my WMD (also, known as keyboard), you'll find me chatting with friends, reading comics or playing a PC game.

Thursday, October 27, 2011

How to erase specific elements from an STL container - the C++ way.

STL iterators are tricky little fellows. One moment they act clean and the very next, they throw their hands up and say something cryptic. Consider this snippet that erases all elements whose values are 20 from a vector of integers.

typedef std::vector<int> IntVectorType;
IntVectorType vInt;

for(IntVectorType::const_iterator iter = vInt.begin();
iter != vInt.end(); ++iter)
{
if( *iter == 20 )
{
vInt.erase(iter);
}
}

Lo and behold! The program crashes! This is because when you erase an element from a container all iterators to its position are invalidated. This just happens to be our loop iterator. So, what is an innocent programmer to do?

Well, there are two ways to erase individual elements in an STL container.

Method 1: The crude way ( recommended for cavemen/barbarians/C -programmers ;) )

IntVectorType::iterator iter = vInt.begin();
while(iter != vInt.end())
{
if( *iter == 20 )
{
iter = vInt.erase(iter); // Get iterator to next element.
}
else
{
++iter; // Increment if not erased.
}
}

The method vector::erase() returns an iterator to the element that subsequently follows the last element that was erased. So, we just use that iterator to continue looping. Good! Problem solved! Is there a better way?

Method 2: The modern C++ way (recommended for rockstars/you/me)

struct EraseFromVector
{
// Functor
bool operator ()(const int value) const
{
return (value == 20);
}
};

vInt.erase(std::remove_if(vInt.begin(), vInt.end(), EraseFromVector()), vInt.end());


Looks like g(r)eek, you say? Alright then, let me explain. Basically, std::remove_if() is a magical chap. He takes two iterators as a range within which to work. For each element within that range, he then checks the unary predicate(our third argument) to see whether the element should be removed or not. If the predicate returns true, the element is removed.

I did say "removed" but in actuality, remove_if() sends the doomed elements to the back of the container. He doesn't actually remove them. How could he? He doesn't know what kind of container he's working with. Only the container can remove elements.

The nice thing about remove_if() is that he is guaranteed by the standard to preserve the order of the elements that were not removed. So, if our vector contained the elements 10, 20, 30, 20, 40 in that order, it will now contain the elements 10, 30, 40, 20, 20.

Finally, remove_if() returns an iterator to the start of the sequence containing all those miserable elements just sitting there at the back waiting for the axe to fall. We pass this iterator as the first argument to vector::erase() and an iterator to the end of the vector as the second argument. In one full swoop, all elements in the range are erased.

Now, doesn't this seem a lot more fun than a boring old while loop? :)

Saturday, October 8, 2011

R.I.P Steve Jobs

At first, the world saw him as nothing more than a nerd, a high school drop out, a rebellious kid. Then the world saw him as a geek, a competitor, an irrational human. Being fired from Apple was the best thing that could have happened because that's what made him a real entrepreneur.

A man who looked at the potential in smaller companies and brought out the best in them. He believed in beauty and elegance in computers unlike the rest of the world. The world resisted his ideology but never killed it.

He returned and changed the music industry for the better. He went through a near-death experience. He beat it, came back and changed the entire way we perceived a digital world with the release of the iPhone. Now, the world sees him in a different light - Visionary, Creator, Future Technologist.

Requiem De Pace - Steve Jobs. You will sorely be missed.

Thursday, September 8, 2011

ACSVParser: A simple CSV parser written in standard C++.

Hey folks,
Its been a while since I posted back here... ok, its been a looong while. A lot has been going on. I've pushed forth my career in gaming all the way to Singapore! Yes, that's one of the four asian tigers and it sure is one of the best places to be working as a game developer. That, of course, does not mean I will let this blog lie fallow. So here goes...

One of the most interesting (vexing?) things about programming is that some of the simplest things turn out to be not so simple at all! Consider a CSV reader. We've probably used one (if not written one of our own) and yet we all know that it could do with a whole lot of customization. Some of the general peeves, I've heard are:
"I don't want to use commas, I like using the caret symbol!"
"Why can't I have new lines in my CSV content?"
"I want to have embedded spaces in my CSV data!"
"I want to sneeze with my eyes open, darn it!"

All of these (except the last one), led me to stop kicking the can down the road and come up with my own parser. I called it ACSVParser(after yours truly :))

The project is hosted on github at https://github.com/angelorohit/ACSVParser
Do check it out and let me know what you think (feedback, suggestions, issues).

Cheers!

Friday, May 20, 2011

Poem - The Warning

While going through an old diary of mine, I happened to stumble upon a poem I'd written in high school. It made me remember that I did want to be a writer before I became a programmer. Perhaps someday, I'll be able to do both.


The Warning

Spare me your protests for peace and love,
You do nothing but put on a lively show…
For then you disbelieve the one above,
And meekly forsake the man next door.

If autumn leaves were to fall in spring time,
Or Snowy flakes were to cover a summer street,
You would still call this poem a crime,
You would still throw tantrums and stamp your feet.

Your hunger-strikes won’t get what you want,
This isn’t the way the battle should be won,
I see starved looks on faces gaunt,
But is what you demand inevitably done?

The bomb blasts you decry aren’t one of a kind,
Hold on a bit and press rewind,
This happened before but you didn’t mind,
Now why would you get that petition signed?

Please don’t belittle this poem of mine,
I’d really appreciate it if you spent the time,
To work through each and every line,
You’ll see there’s always a reason and rhyme.

Friday, December 31, 2010

Happy New Year!

Happy 2011 everyone! Have a wonderful year ahead!

" Ring out the old, ring in the new,
Ring, happy bells, across the snow;
The year is going, let him go;
Ring out the false, ring in the true. "
~ Alfred Tennyson, 1850

(1600 x 1200 Designed with Gimp 2.6.1)


Friday, September 24, 2010

Game Review: NinJump for the iPhone.

NinJump from Backflip Studios is one of those games that captures your attention from the get go due to its lovely visuals and simple gameplay. Our protagonist is a Ninja with an uncanny ability to defy gravity and run up walls. Of course, being a Ninja you also get to deal with all manner of enemies ranging from cute little twitter blue birds, squirrels and rival clan Ninjas. Your wall-running quest ends when an enemy manages to tag you. Gameplay goes in short bursts hardly lasting more than 5 minutes at a stretch before you run into an enemy and lose. The game is free to download from the AppStore but includes ads. If you find the ads particularly annoying, you can disable them via an In-App purchase.

Basic Mechanics:
There are two walls on either side of the screen held in portrait mode and you can bound from wall to wall by simply tapping the screen. When jumping from one wall to the other, the Ninja does a slicing action with his Katana (sword), rending anything that gets in his way. Timing is key. The jumps must be timed so that you slice through your enemies. If you knock into an enemy while running up the wall, its game over.

Power-ups:
The game isn't as bland as a simple run and slash. There are god-awesome powers too! If you manage to kill three enemies of the same type one after the other, you gain a power-boost that is specific to the enemy that was killed. For instance, if you manage to kill three blue birds in a row, the character will sprout wings and take to the air, smiting anything that gets in his way. After a short while, the power will end and you will become your usual vulnerable self again. The power-ups are particularly visually appealing and when you see them for the first time, you are bound to go "Oh YEAH!" (or some such similar form of exclamation).

There are also tiny bubbles on the walls which you can grab to get a protective force-shield. The force-shield is good for one hit.

Score System:
The score you accumulate is a factor of the distance you've covered. This is naturally dependent on how long you manage to survive and how many power-boosts you gain. Destroying attackers is great fun but unfortunately, it doesn't contribute to your score.

The Good:
- Good replay value.
- Cute visuals.
- Addicting gameplay.
- FREE!

The Bad:
- Really simple (can be seen as a good thing too)
- Needs more bad guys.
- Needs a better scoring system.
- No unlockables? :(

Get NinJump on the AppStore here.












Saturday, July 10, 2010

iDream wallpaper done with the Gimp

I did a nice wallpaper while listening to some cool House/Trance music and this is what I ended up with. Enjoy!

(1440 x 900 widescreen)


(1600 x 1200)