Click here to Skip to main content
Licence CPOL
First Posted 18 Jul 2008
Views 9,627
Downloads 38
Bookmarked 10 times

Simple Programming Challenges - Enumerator Bits

By | 18 Jul 2008 | Article
Displays how to create flag-friendly enumerations while managing cases where certain flags should not be used together.

Introduction

In this article, we will see how to create and manage a series of strings which are used to create a flags-based enumerator. The solution also provides relationship management for cases where values should not be used together.

Background

hover-help-vside.jpg

I created this simple project as an effort to simplify my life in another project I'm working on. I often found the need to create enumerations that could have their values mixed, but also had cases where certain values should not be used together. Keeping this basic logic in mind, I created this project.

Using the Code

The code itself is fairly simplistic. The FlaggableEnumerator class works like a normal list except that behind the scenes, it's handling relationship management as well. The relationships work fairly simply; for each entry in the list, there's an index-relative value for every other value in the list. Which means that it uses a two-dimensional boolean array. If the relationship is true between two entries, the two are considered an illegal pair.

By default, all values are false; it keeps the management of the list down to a minimum, but forces actual modification of the relationships to be inverted in the UI, MainDialog.

The only difficult aspect would be obtaining the value for each entry. It involves comparing the current set against the previous sets, and then including its own set information.

internal SeriesValue GetSeriesValue(int index)
{
    bool[] flags = new bool[this.CountUniqueBits()];
    //Uniqueness value.
    flags[index] = true;
    int c = this.Count;
    int currentCount = 0;
    /* *
     * Count the number of sets after
     * this series that relate back.
     * */
    for (int i = index + 1; i < c; i++)
        if (relationships[index, i])
            currentCount++;
    /* *
     * Accumulator for each index prior
     * to determine the bit span of each 
     * set so the appropriate values can
     * be accounted for.
     * */
    int[] previousCounts = new int[index];
    /* *
     * Indexer for each of the previous sets
     * so that the proper flag for that set
     * is checked.
     * */
    int[] previousIndices = new int[index];
    int flagOffset = c;
    for (int i = 0; i < index; i++)
    {
        previousIndices[i] = -1;
        for (int j = i + 1; j < c; j++)
            if (relationships[i, j])
            {
                if (j == index)
                    previousIndices[i] = previousCounts[i];
                previousCounts[i]++;
            }
        /* *
         * If the current series
         * exists in the earlier set, then
         * using its index, and the current flag
         * offset, set the appropriate flag.
         * */
        if (previousIndices[i] != -1)
            flags[flagOffset + previousIndices[i]] = true;
        flagOffset += previousCounts[i];
    }
    /* *
     * For the current series, all the flags
     * that are set for elements after the 
     * current set are true.
     * */
    for (int i = 0; i < currentCount; i++, flagOffset++)
        flags[flagOffset] = true;
    uint result = 0;
    //Build the flag, bits are powers of two.
    for (int i = 0; i < flags.Length; i++)
        if (flags[i])
            result |= (uint)Math.Pow(2, i);
    return new SeriesValue(result, flags);
}

The interface itself I kept fairly simple, and I did so for good reason.

spc.bits.maindialog.jpg

Points of Interest

I found this project fairly simple. I found it interesting and somewhat fun to break down the logic I've been using for a while now to handle making enumerations with unique values and in a way where there are values that are bit-wise, ineffective together.

History

  • July 18, 2008 - First upload.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Allen C. Copeland Jr.



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow about this [modified] PinmemberPIEBALDconsult4:26 19 Jul '08  
AnswerRe: How about this [modified] PinmemberAllen C. Copeland Jr.7:01 19 Jul '08  
GeneralRe: How about this PinmemberPIEBALDconsult9:52 19 Jul '08  
GeneralRe: How about this PinmemberAllen C. Copeland Jr.18:56 19 Jul '08  
GeneralRe: How about this PinmemberPIEBALDconsult4:20 20 Jul '08  
GeneralRe: How about this PinmemberAllen C. Copeland Jr.6:08 20 Jul '08  
GeneralRe: How about this PinmemberPIEBALDconsult7:51 20 Jul '08  
GeneralRe: How about this PinmemberAllen C. Copeland Jr.15:02 20 Jul '08  
GeneralRe: How about this PinmemberPIEBALDconsult16:02 20 Jul '08  
GeneralRe: How about this PinmemberPIEBALDconsult10:00 19 Jul '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 18 Jul 2008
Article Copyright 2008 by Allen C. Copeland Jr.
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid