Creating the next available unique name in C #

If your application should have a naming system in which the application contains 100 actions that create new objects, for example:

Blur
Sharpen
Contrast
Darken
Matte
...

and every time you use one of them, a new instance with a unique editable name, for example Blur01, Blur02, Blur03, Sharpen01, Matte01, etc. How would you generate the next available unique name, so this is an O (1) operation or an almost constant time. Keep in mind that the user can also change the name to user names, for example RemoveFaceDetails, etc.

It is permissible to have some restrictions, for example, limiting the number of characters to 100 using letters, numbers, underscores, etc.

EDIT: You can also offer solutions without "filling in the gaps" that will not reuse already used but deleted names, except for custom ones, of course.

+5
source share
8 answers

I would create a static integer in an action class that gets incremented and assigned as part of every new instance of the class. For example:

class Blur
{
    private static int count = 0;

    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public Blur()
    {
        _name = "Blur" + count++.ToString();
    }
}

Since count is static, every time you create a new class, it will grow and be added to the default name. O (1).

EDIT

If you need to fill holes when removing, I would suggest the following. It will automatically queue when items are renamed, but it will be more expensive:

class Blur
    {
        private static int count = 0;
        private static Queue<int> deletions = new Queue<int>();

        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                Delete();
            }
        }

        private int assigned;

        public Blur()
        {
            if (deletions.Count > 0)
            {
                assigned = deletions.Dequeue();
            }
            else
            {
                assigned = count++;
            }
            _name = "Blur" + assigned.ToString();
        }

        public void Delete()
        {
            if (assigned >= 0)
            {
                deletions.Enqueue(assigned);
                assigned = -1;
            }
        }
    }

, .Delete() .

CounterClass

class CounterClass
{
   private int count;
   private Queue<int> deletions;

   public CounterClass()
   {
      count = 0;
      deletions = new Queue<int>();
   }

   public string GetNumber()
   {
      if (deletions.Count > 0)
      {
          return deletions.Dequeue().ToString();
      }
      return count++.ToString();
   }

   public void Delete(int num)
   {
      deletions.Enqueue(num);
   }
}

. , .Delete(int) , .

+7

. :

  • .
  • : .

, , , , , .

: (, "Sharpen01" ), , , . , . O (N 2), , .

- O (N 2) , HashSet . HashSet, . HashSet , , ; . , O (N).

O (N) . O (1). "Sharpen" , - .

+7

O(m), m - ( n, .

  • S. S , .
  • S , S+"01" . (, S+"02", .

, "" .

, " " , m .

: , , , , . "01"? , , , . , , . "_01" "01", .

+3

- :

    private Dictionary<string, int> instanceCounts = new Dictionary<string, int>();

    private string GetNextName(string baseName)
    {
        int count = 1;

        if (instanceCounts.TryGetValue(baseName, out count))
        {
            // the thing already exists, so add one to it
            count++;
        }

        // update the dictionary with the new value
        instanceCounts[baseName] = count;

        // format the number as desired
        return baseName + count.ToString("00");
    }

, GetNextName (...) , , string myNextName = GetNextName("Blur");

, . , . , O (1).

+2

, . O (1) .

private IDictionary<String, Int32> NextFreeActionNumbers = null;       

private void InitializeNextFreeActionNumbers()
{
   this.NextFreeActionNumbers = new Dictionary<String, Int32>();

   this.NextFreeActionNumbers.Add("Blur", 1);
   this.NextFreeActionNumbers.Add("Sharpen", 1);
   this.NextFreeActionNumbers.Add("Contrast", 1);
   // ... and so on ...
}

private String GetNextActionName(String action)
{
   Int32 number = this.NextFreeActionNumbers[action];

   this.NextFreeActionNumbers[action] = number + 1;

   return String.Format("{0} {1}", action, number);
}

. . . , , , . ( , , .)

- . . , .

+1

.

- , ? , , () ? , ( ) .

  • (Ben F)
  • Blur (Adrian H)
  • (Ben F)

, , , , - . , !

+1

O (1), , . - , , .

0

GUID .

"Blur04", , , ? "CustomName01" - ?

You can use the dictionary to check for duplicates O (1) times. You can increase counters for each type of effect in the class that creates new instances of the effect. As Kevin mentioned, it gets more complicated if you need to fill in the numbering gaps when the effect is removed.

0
source

All Articles