File: StringArray.h

package info (click to toggle)
libstatgen 1.0.15-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,588 kB
  • sloc: cpp: 49,624; ansic: 1,408; makefile: 320; sh: 60
file content (143 lines) | stat: -rw-r--r-- 3,600 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 *  Copyright (C) 2010  Regents of the University of Michigan
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __STRING_ARRAY_H__
#define __STRING_ARRAY_H__

#include "StringBasics.h"

class StringArray
{
protected:
    String ** strings;
    int size, count;

public:
    static int alloc;
    static bool lazyMemoryManagement;

    StringArray(int startsize = 0);
    StringArray(StringArray & original);
    virtual ~StringArray();

    // Each line in a file is parsed into a separate array element
    //

    void Read(FILE * f);
    void Write(FILE * f);
    void WriteLine(FILE * f);
    void Read(const char * filename);
    void Write(const char * filename);
    void WriteLine(const char * filename);

    void Read(IFILE & f);

    // Write all strings to the screen
    void Print();
    void PrintLine();

    // Write all strings to a file
    void Print(FILE * f);
    void PrintLine(FILE * f);

    void Grow(int newsize);
    void Clear();

    int Length() const
    {
        return count;
    }
    int Dimension(int newcount);
    int CharLength();

    String & operator [](int i)
    {
        return *(strings[i]);
    }
    const String & operator [](int i) const
    {
        return *(strings[i]);
    }

    // These functions divide a string into tokens and append these to the
    // array. Return value is the new array length
    //

    int AddColumns(const String & s, char ch = '\t');
    int AddColumns(const String & s, char ch, int maxColumns);
    int AddTokens(const String & s, char ch);
    int AddTokens(const String & s, const String & separators = " \t\r\n");

    int ReplaceColumns(const String & s, char ch = '\t')
    {
        Clear();
        return AddColumns(s, ch);
    }
    int ReplaceTokens(const String & s, const String & separators = " \t\r\n")
    {
        Clear();
        return AddTokens(s, separators);
    }

    // These functions add, insert or remove a single array element
    //

    int  Add(const String & s);
    void InsertAt(int position, const String & s);
    void Delete(int position);

    // These functions manipulate a string as a stack
    //

    String & Last() const;
    int      Push(const String & s)
    {
        return Add(s);
    }
    String   Pop();

    // Linear search (N/2 comparisons on average) for a single element
    // If searching is required, StringMaps are a better option
    //

    int Find(const String & s) const;
    int FastFind(const String & s) const;
    int SlowFind(const String & s) const;

    // Alphetically orders strings
    //
    void Sort();

    // Trims strings to remove whitespace
    void Trim();

    StringArray & operator = (const StringArray & rhs);

    bool operator == (const StringArray & rhs) const;
    bool operator != (const StringArray & rhs) const
    {
        return !(*this == rhs);
    }

    void Swap(StringArray & s);

private:
    static int ComparisonForSort(const void * a, const void * b);
};

#endif