Monthly Archives: May 2010

HowTo: C Function Pointers for a Linear Search, void star pointer trick, comparison using memcmp, string replace

Well, I was messing around in C again today, sometimes I just love C, it’s a fun language when you understand it, which I really don’t, but am beginning to grok it. Anyway, I was checking out a little tutorial on C and saw this cool little trick that I thought I would share. It’s the beginnings of a generalized linear search algorithm. #include #include #include int linearSearch (void * key, /* pointer to the data to compar*/ void * base, /* base of the data*/ int n, /* number of elements */ int size, /* quantum of data*/ int ( * cmpfunc)(void * e1,void * e2, int size) /* comparison function */); int simpleCompare(void * e1, void * e2, int size); int main ( int argc, char *argv[]) { int array[5]; /* a simple array of ints*/ int i; for (i = 0; i < 5; i++) { array[i] = i; /*load the array so we can search*/ } int s = 24;/* this is the search value, change it around to test */ /*we pass in the addresses of s, the array, which is a pointer already, the number of elements, the sizeof the data, and the address of the function*/ if (linearSearch(&s, array, 5, sizeof(int), &simpleCompare) != -1) { printf(“%i was found in array. \n”,s); } else { printf(“%i was not found in array.\n”,s); } } int linearSearch (void * key, /* pointer to the data to compar*/ void * base, /* base of the data*/ int n, /* number of elements […]

HowTo ActionScript 3 (as3 flash or flex or AIR) Class and Object Inheritance, extends

It seems I have been getting alot of hits due to my post on AS3 inheritance, and I notice that most of the documentation and examples out there don’t really cover the whys and the hows in a thorough manner. There are a couple of parts to this little tutorial, the first being a kind of very basic demonstration of extending, and then some concrete realworld samples. You can jump ahead to the realworld samples if you are just looking to see how/why you might use extend to solve some real problems. Basic/intro to oop and extending in as3 extending event dispatcher to manage your own event queue extending event to create a custom event and finally extending Panel to make a drag/drop Panel Having classes and objects does not equal OOP. To understand a class, you need to understand that an object without state or behavior is meaningless, so to be an object, it must have those things. The object itself is an environment, it is like a scope, it’s a container which imparts a kind of semantic meaning to the function and variables attached to it. Having objects is very nice, but there needs to be something more, and that something more is inheritance. Without inheritance, objects are simply wrappers, and it would be just as well to create functions and simply pass in some kind of key to ameliorate what to do. With inheritance, you share state and behavior between objects. The common example is with a person, people have names, and […]

Firefox Content Encoding Error, PHP Website

I looked around online, and couldn’t find any solutions to this problem, so here is one possibility for you. I have a client with an intranet website with a custom MVC framework in PHP. In one of the controller actions, there was an echo statement. Normally, this shouldn’t really affect it, i.e. when you start echoing things, PHP normally sends the start of output headers and just plugs along. Everything worked fine awhile around, but then someone upgraded the server, and put the latest PHP there. Now those pages were broken, giving the Content Encoding Error. Removing the echo statements that were coming before the start of the page got rid of the error.

How To Flash CS5: Keyboard Navigation Action Script 3 KeyboardEvents 6

Well, I was watching an interesting little tutorial on keyboard navigation with AS3. Now, the guy’s tutorial was actually really good, however, it wasn’t really what I would call AS3. It was very on target for what it was doing, but didn’t leave much room for expansion in my opinion. So I decided to try my hand at changing it up a littler bit. Here is the code: This is put on the main timeline, in the first frame on an actions layer. That is, you just create a new layer, and name it actions, it’s just a convention. import; import; var currentKeyDown:uint = 0; var Keys:Object = {up: 38, down: 40, left: 37, right: 39,r:82, shift: 16, ctrl: 17, z:90, x:88}; var CurrentKeysDown:Object = {up: false, down: false, left: false, right: false, shift: false, ctrl:false}; stage.addEventListener(KeyboardEvent.KEY_DOWN,setCurrentKeyDown); stage.addEventListener(KeyboardEvent.KEY_UP,clearCurrentKeyDown); setTimeout(updateCanvas,100); function setCurrentKeyDown(event:KeyboardEvent):void { trace(event.keyCode); for (var key:String in Keys) { if (Keys[key] == event.keyCode) { CurrentKeysDown[key] = true; } } } function clearCurrentKeyDown(event:KeyboardEvent):void { for (var key:String in Keys) { if (Keys[key] == event.keyCode) { CurrentKeysDown[key] = false; } } } function moveEntity(entity:MovieClip):void { currentKeyDown = 0; var x:Number = 0; var y:Number = 0; for (var key:String in Keys) { if (CurrentKeysDown[key]) { currentKeyDown = Keys[key]; } switch (currentKeyDown) { case Keys.shift: entity.speed += entity.speed * .05; break; case Keys.ctrl: entity.speed -= entity.speed * .05; break; case Keys.z: entity.rotationSpeed += entity.speed * .06; break; case Keys.x: entity.rotationSpeed -= entity.speed * .06; break; case Keys.r: entity.x = 200; entity.y = 200; break; case Keys.left: […]

Simple PHP closure/lamda/anonymous function usage.

This is a fun little example of the new Closure features in php. class TArray { public $pMembers = array(); public $pCurrentIndex = 0; public function __construct() { $args = func_get_args(); if (empty($args)) return; if (count($args) == 1) $args = $args[0]; foreach ($args as $arg) array_push($this->pMembers,$arg); } public function push() { $args = func_get_args(); foreach ($args as $arg) { array_push($this->pMembers,$arg); } } public function pop() { return array_pop($this->pMembers); } public function each() { $args = func_get_args(); if (empty($args)) { if ($this->pCurrentIndex > count($this->pMembers)) { $this->pCurrentIndex = 0; return null; } $ret = $this->pMembers[$this->pCurrentIndex]; $this->pCurrentIndex++; return $ret; } else if (is_callable($args[0])) { array_walk($this->pMembers,$args[0]); } else { throw new Exception(“Array->each does not understand that…”); } } public function resetIndex() { $this->pCurrentIndex = 0; } } $array = new TArray(1,2,3,4,5,6); while ($i = $array->each()) { echo “$i\n”; } $array->each(function ($elem) { echo “$elem\n”;}); We can go a little further, and do like this. class Person { public $name; public function __construct($name) { $this->name = $name; } } $array = new TArray(new Person(‘John’), new Person(‘Jane’)); $array->each(function ($person) { echo “{$person->name}\n”;}); An issue that comes up is, how do I get the $this variable in there. The short answer is, you don’t. You can finaigle it after a fashion, but it’s just ugly. The issue is, people want the $this var, because they want to be able to manipulate the values, in a permanent fashion, or do something else. How you accomplish it, in a pretty way is: 1. Give up the ghost, you can’t have $this, it’s just a […]

The Essentials of Computer Programming, Part I: Binary Math (Addition, Subtraction, Multiplication, and Division)

At some point or another I got it into my head that I wanted to learn more about computers, and how they work. There seem to be a lot of places to start, even down at the deep hardware level, but, from a software perspective, the lowest it goes, is Binary. What the hell is binary? Binary is a numbering system, like decimal, or hexadecimal, it just means that there are only 2 numbers, in this case, 0 and 1. In the end, it’s really rather arbitrary, it could have been A and B, or 3 and 8, but 0 and 1 has it’s own kind of logic. In binary the highest you can count is one. 0 + 0 = 0, 0 + 1 = 1, 1+1 = 0 and so on. I know, looks insane, but as it turns out, it’s so completely dead simple that if the numbers didn’t get so horribly long, you’d wish all math was binary math. The one gotcha is, when you add 1 + 1 = 0…and carry the 1. Addition For instance: 0 0 0 0 0 + 0 0 0 1 we add 1 0 0 011 <- notice the carry + 0 0 0 1 again we add 1 0 0 1 0 and we get 2 + 0 0 0 1 add 1 0 0 1 1 and get 3 And it just goes on like that. What’s the next in the chain? 4 right, so what’s 1 + 1? 0 carry the […]