Wednesday, February 18, 2015

C++ Ordered Map

The std::map class is not ordered. Coming from any other programming language with a nice standard library this seems like a serious hole in C++. Boost has one, but not everyone can use Boost. So, here is one a threw together. Sure it's missing things, but that's what you get for free. Enjoy!


#ifndef ORDEREDMAP_H
#define ORDEREDMAP_H

#include 
#include 
#include 


template 
struct pair
{
    typedef _T1 first_type;
    typedef _T2 second_type;
    
    first_type first;
    second_type second;
    
    pair(first_type Value1, second_type Value2) {
        first = Value1;
        second = Value2;
    }
};

template 
class OrderedMap {
public:
    typedef TKey key_type;
    typedef TValue mapped_type;
    typedef pair container_type;
    
private:
    typedef std::map map_type;
    typedef std::vector list_type;
    
    map_type FMap;
    list_type FList;
    
    typename list_type::iterator FindListItem(const key_type Key) {
        typename list_type::iterator result = FList.end();
        
        for (typename list_type::iterator iterator = FList.begin(); iterator != FList.end(); iterator++) {
            container_type *item = *iterator;
            
            if (item->first == Key) {
                result = iterator;
                break;
            }
        }
        
        return result;
    }
    
public:
    OrderedMap() {
    }
    
    ~OrderedMap() {
        for (typename list_type::iterator iterator = FList.begin(); iterator != FList.end(); iterator++) {
            container_type *item = *iterator;
            delete item;
        }
    }
    
    void Append(key_type Key, mapped_type Value) {
        container_type *item = new container_type(Key, Value);
        item->first = Key;
        item->second = Value;
        
        FMap.insert(std::pair(Key, item));
        FList.push_back(item);
    }
    
    void Insert(size_t Index, key_type Key, mapped_type Value) {
        container_type *item = new container_type(Key, Value);
        item->first = Key;
        item->second = Value;
        
        FMap.insert(std::pair(Key, item));
        FList.insert(FList.begin() + Index, item);
    }
    
    void Remove(key_type Key) {
        typename list_type::iterator iterator = FindListItem(Key);
        
        if (iterator != FList.end()) {
            FMap.erase(Key);
            FList.erase(iterator);
        }
    }
    
    void Remove(size_t Index) {
        typename list_type::iterator iterator = FList.begin() + Index;
        
        if (iterator != FList.end()) {
            container_type* item = *iterator;
        
            if (item != NULL) {
                FMap.erase(item->first);
                FList.erase(iterator);
            }
        }
    }
    
    mapped_type &operator[](key_type Key) {
        container_type* item = FMap[Key];
        assert(item != NULL);
        
        if (item != NULL) {
            return item->second;
        }
        
        throw std::out_of_range("Index out of range");
    }
    
    mapped_type &operator[](size_t Index) {
        assert(Index >= 0 && Index < Count());
        container_type* item = FList[Index];
        assert(item != NULL);
        
        if (item != NULL) {
            return item->second;
        }
        
        throw std::out_of_range("Index out of range");
    }
    
    size_t Count() {
        return FList.size();
    }
};

#endif //ORDEREDMAP_H

Thursday, January 8, 2015

New Years Resolution #1 - Be More Healthy by Not Drinking Soda

If you stop drinking soda you will loose 5lbs in the first month. Seriously. Don't switch to diet soda, drink water instead.

Search Google and you'll come up with thousands of links such as the first three that support what I'm saying:

The-Daily-Cost-Of-Your-Soda-Habit
Want to Lose Weight Fast? Cut Out Soda from Your Diet
Can You Lose Body Fat by Stopping Drinking Soda?

If you replace it with anything that contains calories such as lemon aid it won't help. Replace the soda with water or maybe tea. Obviously the occasional soda is fine, but once you cut it out and water becomes your new habit, you won't be able to finish an entire 12oz soda. I have friends who have done this and it has worked for them. Make this your number one New Year's resolution and you won't be disappointed.

Wednesday, December 31, 2014

New Years Resolutions

Every year millions of Americans declare New Year's resolutions. Here is a great article about how this whole tradition started:

The Surprising Reason We Make New Year's Resolutions

Since most people I know always have losing weight, staying fit and healthy and enjoying life on their list, I'm going to dedicate all my posts in January to things that I do (or wish I did) to satisfy a fit and healthy lifestyle. Everything will be published under a new label Lifestyle.

I'm also hoping to get my sister who is a physical therapist to write a few posts because she has a lot of extremely valuable detailed information from her education that most of us would only dream of knowing. Or maybe we wouldn't want to know because once you know you have to do things better!

So stay tuned and check back often because this should be fun!

Tuesday, December 30, 2014

iOS Paralax

To disable parallax in iOS 7 and 8, open Settings and navigate to General, Accessibility and turn the “Reduce Motion” option on.

I had this turned off prior to the latest iOS 8.* update.



Monday, December 29, 2014

How To Tie a Bow Tie With Bill Nye

Bow Ties are cool. Here's how to tie one.

Bash Script to Cleanup File

This bash script will do three things: 1. Remove Carriage Return (\r) 2. Replace tab character with four spaces (configure as you want) 3. Add a trailing line feed to the file

#!/bin/bash

platform='unknown'
unamestr=`uname`
if [[ "$unamestr" == 'Darwin' ]]; then
   platform='MAC'
elif [[ "$unamestr" == *CYGWIN* ]]; then
   platform='WIN'
elif [[ "$unamestr" == "Linux" ]]; then
   platform='LINUX'
fi

file=$1

# Remove \r
tr -d '\r' < $file > $file.new
rm $file
mv $file.new $file

# replace \t with space
tr '\t' '    ' < $file > $file.new
rm $file
mv $file.new $file

if [[ $platform == 'MAC' ]]; then
  # On Mac, add trailing \n
  sed -i '' -e '$a\' $file
else
  sed -i -e '$a\' file
fi

Tuesday, December 23, 2014

Star Wars Monopoly


You know what's awesome? Star Wars. Monopoly is also awesome! Combine the two, get some Star Wars costumes and you have one heck of a Monopoly party!

Next you are going to ask if I took that picture. Yes, yes I did! However, I borrowed everything in it to play Star Wars Monopoly. I do silly things like that.


Monday, December 22, 2014

Thursday, December 18, 2014

Objective-C Auto Reference Counting

There is a lot of code out in the wild that uses automatic reference counting (ARC). You'll see this error when trying to build such a project with Xcode: "NSAutoreleasePool is unavailable"

You can disable ARC on an existing project in build settings.
  • Click on the project, in the left hand window of Xcode.
  • Select the target.
  • Select the Build Settings tab at the top.
  • Search for "Objective-C Automatic Reference Counting"
  • Change to yes or no depending on what you want.

Wednesday, December 17, 2014

Programming Language and Development Environments

Many developers find one programming language and one development environment they like and stick with it. Some switch once in a while. Others, like me, use a lot of different ones daily. Everyone has different reasons for this, many just like to dabble. Discussing programming languages can be like discussing politics, but I'm going to do it anyway.


I learned Commodore BASIC as my first programming language in 4th grade. It was awesome once my school got a cassette tape so I could save my programs! Years later I learned C/C++ and assembly, and from there I can't recall the order but Pascal must've been next. Java, C#, Fortran, Prologue at some point, Perl, Python, PHP, Javascript, Lua, Bash, Swift, etc etc. Now on a daily basis I use Visual Studio, Xcode and NetBeans, (I prefer IntelliJ) and various other things that aren't really worth mentioning like Eclipse, Delphi, C++ Builder, JDeveloper and Xamarin Studio.

Delphi and C++Builder used to be my favorite, but their market is so small compared to anything else out there I'm sorry to say they are hardly worth mentioning. Visual Studio is by far the best IDE available. Xamarin is actually pretty good and getting better. So is Xcode. Although I wish the debugger were better because I really can't stand gdb.

I really like a strongly typed programming language. When I'm sitting holding an object and I don't know what it is, I guess it doesn't matter but sometimes it really does and I don't like it. I find this problematic with Swift. But swift has been growing on me. Programming languages are pretty much the same. I like Delphi, but C++ is my favorite go to language, C# and Java are both very good. They both have huge amounts of support code out there so you can do just about anything. Microsoft's recent announcement to bring .NET to every platform is really cool. Nobody should write real software with Perl, Python, PHP or Bash (even thought I've seen it done). Javascript is a necessary evil.

So there you have it. My quick thoughts on programming languages and development environments.

Tuesday, December 16, 2014

Dividing Into Even Parts

One problem that comes up when doing wood work is dividing some odd number evenly into parts. This video shows an easy method at index 1:42 how to do this with very simple math by measuring across at an angle using a number that can be divided very easily. Just watch the video, it makes a lot more sense than my describing.

https://www.youtube.com/watch?v=gSXrmGaSxLU

Monday, December 15, 2014

Synchronizing VMWare guest Operating System Time

I run Ubuntu as a VM on my Mac using VMWare Fusion and it has problems synchronizing the time with the host operating system. The VMWare commands are to do this are:

vmware-toolbox-cmd timesync status
vmware-toolbox-cmd timesync enable
vmware-toolbox-cmd timesync disable

And they simply don't work. So I had to find another way to get the time synced. Running the command:

sudo ntpdate -s time.nist.gov

Syncs the time splendidly, so the question is how to do this at boot time automatically. After all automatic is what computers are all about. To do this, edit the file /etc/rc.local and add following anywhere in the file before the "exit 0":

ntpdate -s time.nist.gov&

And that should keep your clock synced. If you sleep your VM, the time could get out of sync and I haven't come up with a automatic solution for that but I don't usually sleep because shared directories are sometimes gone when it wakes and the VM has a big problem with that.