Lexicographic Ordering for Total Permutation

#include <cliext/algorithm>

#using <c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll>

using namespace cliext;
using namespace System;
using namespace System::Text;
using namespace System::Collections;
using namespace System::Collections::Generic;

public ref class Permu
{
private:

    static String^ GetLexicoMinInPermu(array<Char>^ input)
    {
        array<Char>^ temp = gcnew array<Char>(input->Length);
        Array::Copy(input, temp, input->Length);
        Array::Sort(temp, 0, temp->Length);
        return gcnew String(temp);
    }

    static String^ GetLexicoMaxInPermu(array<Char>^ input)
    {
        array<Char>^ temp = gcnew array<Char>(input->Length);
        Array::Copy(input, temp, input->Length);
        Array::Sort(temp, 0, temp->Length);
        Array::Reverse(temp);
        return gcnew String(temp);
    }

    // Lexicographic order algorithm.
    static HashSet<String^>^ LexicoSortForPermu(String^ begin, String^ end)
    {
        HashSet<String^>^ result = gcnew HashSet<String^>();
        result->Add(begin);

        array<Char>^ temp = begin->ToCharArray();

        String^ current = begin;
        while(current < end)
        {
            for (int i = temp->Length – 2; i >= 0; i–)
            {
                if (temp[i] < temp[i + 1])
                {
                    int index_of_num_gt_min = i + 1;

                    int j;
                    for (j = i + 1; j < temp->Length; j++)
                    {
                        // If there are duplicated number in array, use the most right one.
                        if (temp[i] < temp[j] && temp[j] <= temp[index_of_num_gt_min])
                        {
                            index_of_num_gt_min = j;
                        }
                    }

                    int temp_0 = temp[index_of_num_gt_min];
                    temp[index_of_num_gt_min] = temp[i];
                    temp[i] = temp_0;

                    // reverse all the numbers start from temp[i + 1]
                    int begin_index = i + 1;
                    for (j = temp->Length – 1; j >= begin_index; j–, begin_index++)
                    {
                        if (j <= begin_index)
                        {
                            break;
                        }
                        else
                        {
                            int temp_0 = temp[j];
                            temp[j] = temp[begin_index];
                            temp[begin_index] = temp_0;
                        }
                    }

                    current = gcnew String(temp);
                    if (!result->Contains(current))
                    {
                        result->Add(current);
                    }
                    break;
                }
            } // end for
        } // end while

        result->Add(end);
        return result;
    }

public:

    static HashSet<String^>^ Compute(array<Char>^ input)
    {
        String^ begin = GetLexicoMinInPermu(input);
        String^ end     = GetLexicoMaxInPermu(input);

        return LexicoSortForPermu(begin, end);
    }
};

int main(array<System::String ^> ^args)
{
    array<Char>^ input = { ‘2’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’};
    HashSet<String^>^ result = Permu::Compute(input);

    int count = 0;
    for each(String^ temp in result)
    {
        if (temp->Contains("35") || temp->Contains("53") || temp->IndexOf("4") == 3)
            continue;
        Console::WriteLine(temp);
        count++;
    }

    Console::WriteLine("Total count of Perm result: {0}", result->Count);
    Console::WriteLine("Total count of filtered result: {0}", count);
    return 0;
}

Visual Studio Automation

Find something – Visual Studio Automation and Extensibility API – useful when navigating through the MSDN. These APIs are COM based, but luckily, they’ve been wrapped in managed code,so we can use them directly in C#. Here is some code, I used to quick learn these APIs. It can detect if an instance of Visual Studio is existing, if the debugger of this Visual Studio instance is launched, and if so, print out the stacktrace of the debuggee controlled by this debugger. (Error handling is stripped off)
 

#include <stdio.h>

#include <atlbase.h>

 

#pragma warning( disable : 4278 )

#pragma warning( disable : 4146 )

 

#import "libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2" version("8.0") lcid("0") raw_interfaces_only named_guids

#import "libid:1A31287A-4D7D-413e-8E32-3B374931BD89" version("8.0") lcid("0") raw_interfaces_only named_guids

 

#pragma warning( default : 4146 )

#pragma warning( default : 4278 )

 

using namespace EnvDTE;

 

int _tmain(int argc, _TCHAR* argv[])

{

          CoInitialize(NULL);

 

          CLSID clsid2;

          CLSIDFromProgID(L"VisualStudio.DTE.9.0", &clsid2);

          IUnknownPtr punk2;

          HRESULT hr = GetActiveObject(clsid2, NULL, &punk2);

          if (punk2 == NULL)

          {

                   wprintf(L"No instance of Visual Studio is running.\n");

          }

          else

          {

                   _DTEPtr pDTE = punk2;

                   if (pDTE)

                   {

                             DebuggerPtr pDebugger; hr = pDTE->get_Debugger(&pDebugger);

                             dbgDebugMode mode; hr = pDebugger->get_CurrentMode(&mode);

                             if (mode != dbgDesignMode)

                             {

                                      wprintf(L"Debugger is active.\n");

                                      ThreadPtr pThread; hr = pDebugger->get_CurrentThread(&pThread);

                                      StackFramesPtr pStackFrames; hr = pThread->get_StackFrames(&pStackFrames);

                                      long lStackFrameCount; hr = pStackFrames->get_Count(&lStackFrameCount);

                                      int sfIndex = 1;

                                      while (sfIndex <= lStackFrameCount)

                                      {

                                                VARIANT var1;

                                                var1.vt = VT_INT;

                                                var1.lVal = sfIndex;

                                                StackFramePtr pStackFrame; hr = pStackFrames->Item(var1, &pStackFrame);

                                                BSTR returnType; hr = pStackFrame->get_ReturnType(&returnType);

                                                BSTR module; hr = pStackFrame->get_Module(&module);

                                                BSTR function; hr = pStackFrame->get_FunctionName(&function);

                                                ExpressionsPtr pExpressions; hr = pStackFrame->get_Arguments(&pExpressions);

                                                long lArgumentCount; hr = pExpressions->get_Count(&lArgumentCount);

                                                wprintf(L"%s!%s", module, function);

                                                int argIndex = 1;

                                                wprintf(L"(");

                                                while (argIndex <= lArgumentCount)

                                                {

                                                          VARIANT var2;

                                                          var2.vt = VT_INT;

                                                          var2.lVal = argIndex;

                                                          ExpressionPtr pExpression; hr = pExpressions->Item(var2, &pExpression);

                                                          BSTR argType; pExpression->get_Type(&argType);

                                                          BSTR argName; pExpression->get_Name(&argName);

                                                          wprintf(L"%s %s", argType, argName);

                                                          argIndex++;

                                                          if (argIndex < lArgumentCount)

                                                          {

                                                                   wprintf(L", ");

                                                          }

                                                }

                                                wprintf(L")\n");

                                                sfIndex++;

                                      }

                             }

                             else

                             {

                                      wprintf(L"Debugger is not active.\n");

                             }

                   }

          }

 

          CoUninitialize();

          return 0;

}

 
Austin.D

SKy chord ~大人になる君へ~

 
素直なうたが歌えない かざりつけてしまうから
いつからこんなに楽に 自分守ることを覚えたの
校庭から见える空 君には何色にうつる
ただ真っ白な云でも 时に真っ黒に変えたくなる
みっかんない Sky chord 昔ならあったのに
なくした Sky chord 谁のせいでもなく自分
きっと大人になることなんかより 大切なものがあるの
きっとそれを见つけられないまま 大人になってゆくんだ yeah yeah
朝まで起きていたかった もどかしい子どもの顷
今は时间に追われて 眠ることすらできないでいる 
みっかんない Sky chord 昔ならあったのに
なくした Sky chord 君に教えてほしいよ
きっと大人になることなんかより 大切なものがあるの
きっとそれを见つけられないまま 大人になってゆくんだ
ずっとこままじゃいれないって 分かってるよ 歩き出せ
そっとノートに书いてた文字は 変わってなんかいないの 
きっと大人になることなんかより 大切なものがあるの
きっとそれを见つけられないまま 大人になってゆくんだ
子供のままじゃいられない