public void init(int length)
 {
     di = new int[length];
     for (int i = 0; i < length; i++)
     {
         di[i] = -1;
     }
     seq   = 0;
     Group = "";
     Range = null;
 }
        public Filler Mark(string group, string names)
        {
            Filler f = new Filler(this, names);

            f.di    = mark(f.names, group);
            f.Group = group;
            tRange t = null;

            if (_igroup.ContainsKey(group))
            {
                t = _igroup[group];
            }
            f.Range = t;
            return(f);
        }
        private int[] mark(string[] names, string group)
        {
            if (names == null || names.Length < 1)
            {
                return(null);
            }
            tRange t = new tRange(0, buffer.Bag.Count - 1);

            if (group != null)
            {
                if (!_igroup.ContainsKey(group))
                {
                    return(null);
                }
                t = _igroup[group];
            }

            int[] marks = new int[names.Length];

            for (int i = 0; i < marks.Length; i++)
            {
                marks[i] = -1;
            }

            for (int i = t.head; i <= t.toe; i++)
            {
                if (buffer.Bag[i].Kin == AType.NAME)
                {
                    for (int j = 0; j < names.Length; j++)
                    {
                        if (String.Compare(
                                names[j],
                                buffer.Bag[i].Tag,
                                true) == 0)
                        {
                            marks[j] = i;
                            break;
                        }
                    }
                }
            }
            return(marks);
        }
        private static void Indexing(string aname,
                                     AType kin,
                                     int i,
                                     Dictionary <string, int> dd,
                                     Dictionary <string, tRange> gg)
        {
            switch (kin)
            {
            case AType.NAME:         // index all names
                dd[aname] = i;
                break;

            case AType.OPEN:         // index all groups
                if (!gg.ContainsKey(aname))
                {
                    gg[aname] = new tRange(i, -1);
                }
                else
                {
                    gg[aname].head = i;
                }
                break;

            case AType.CLOSE:
                if (!gg.ContainsKey(aname))
                {
                    gg[aname] = new tRange(-1, i);
                }
                else
                {
                    gg[aname].toe = i;
                }
                break;

            default:
                break;
            }
        }
        public void New(string group, int seq = 0)
        {
            // place new group copied from old group just below it

            if (!(iGroups.ContainsKey(group) &&
                  _igroup.ContainsKey(group) &&
                  seq > 0))
            {
                return;
            }

            tRange newT       = null;
            tRange t          = iGroups[group];
            int    beginRange = _igroup[group].toe + 1;

            for (int i = t.head; i <= t.toe; i++)
            {
                buffer.Bag.Insert(beginRange,
                                  new Atom(tokens.Bag[i].Tag,
                                           tokens.Bag[i].Kin,
                                           tokens.Bag[i].Data));
                beginRange++;
            }

            newT = new tRange(t.toe + 1, t.toe + (t.toe - t.head + 1));

            // rename past group
            string pastGroup = group + "_" + seq;

            t = _igroup[group];
            buffer.Bag[t.head].Tag = pastGroup;
            buffer.Bag[t.toe].Tag  = pastGroup;

            _igroup[pastGroup] = t;

            // change group indexes
            _igroup[group] = newT;
        }
        public void Init(string Content = null)
        {
            content = Content;

            tokens = new Atom("", AType.GROUP);

            iNames  = new Dictionary <string, int>();
            iGroups = new Dictionary <string, tRange>();

            // parse content into tokens
            string namePattern = namePre + namepat + namePost;
            string patterns    =
                "(?<var>" + namePattern + ")|" +
                "(?<head>" + comment0 + namePattern + ":" + comment1 + ")|" +
                "(?<toe>" + comment0 + ":" + namePattern + comment1 + ")";
            Regex           jn  = new Regex(justName, RegexOptions.Compiled);
            Regex           r   = new Regex(patterns, RegexOptions.Compiled);
            MatchCollection ms  = r.Matches(content);
            int             pre = 0;

            foreach (Match m in ms)
            {
                tokens.Add(content.Substring(pre, m.Index - pre));
                int idx = -1;
                if (m.Groups.Count >= 3)
                {
                    string          aname = "";
                    MatchCollection x     = jn.Matches(m.Value);
                    if (x.Count > 0 && x[0].Groups.Count > 1)
                    {
                        aname = x[0].Groups[1].ToString();
                    }
                    AType t = AType.VALUE;

                    if (m.Groups[1].Length > 0)
                    {
                        t = AType.NAME;
                    }
                    if (m.Groups[2].Length > 0)
                    {
                        t = AType.OPEN;
                    }
                    if (m.Groups[3].Length > 0)
                    {
                        t = AType.CLOSE;
                    }
                    if (aname.Length > 0)
                    {
                        tokens.Add(aname, t);

                        idx = tokens.Bag.Count - 1;
                    }
                    Indexing(aname, t, idx, iNames, iGroups);
                }
                pre = m.Index + m.Length;
            }
            if (pre < content.Length)
            {
                tokens.Add(content.Substring(pre, content.Length - pre));
            }

            // copy tokens into buffer
            buffer = new Atom("", AType.GROUP);
            for (int i = 0; i < tokens.Bag.Count; i++)
            {
                buffer.Add(tokens.Bag[i].Tag, tokens.Bag[i].Kin);
            }

            // initialize index of output names
            _iname = new Dictionary <string, int>();
            foreach (string k in iNames.Keys)
            {
                _iname[k] = iNames[k];
            }

            // initialize index of output groups
            _igroup = new Dictionary <string, tRange>();
            foreach (string k in iGroups.Keys)
            {
                tRange t = iGroups[k];
                _igroup[k] = new tRange(t.head, t.toe);
            }
        }