C# Class RTools.Util.CharBuffer

Buffer for characters. This approximates StringBuilder but is designed to be faster for specific operations. This is about 30% faster for the operations I'm interested in (Append, Clear, Length, ToString). This trades off memory for speed.

To make Remove from the head fast, this is implemented as a ring buffer.

This uses head and tail indices into a fixed-size array. This will grow the array as necessary.

Datei anzeigen Open project: PaulMineau/AIMA.Net Class Usage Examples

Public Methods

Method Description
Append ( CharBuffer s ) : void

Append a string to this buffer.

Append ( char c ) : void

Append a character to this buffer.

Append ( string s ) : void

Append a string to this buffer.

CharBuffer ( ) : System

Default constructor.

CharBuffer ( int capacity ) : System

Construct with a specific capacity.

Clear ( ) : void

Empty the buffer.

IndexOf ( char c ) : int

Find the first instance of a character in the buffer, and return its index. This returns -1 if the character is not found.

Remove ( int i ) : void

Remove a character at the specified index.

Remove ( int i, int n ) : void

Remove a specified number of characters at the specified index.

SetBuffer ( char b, int len ) : void

Overwrite this object's underlying buffer with the specified buffer.

SpeedTest ( ) : void

Compare speed to StringBuilder.

TestSelf ( ) : bool

Simple self test.

ToString ( ) : String

Return the current contents as a string.

this ( int index ) : char

Indexer.

Protected Methods

Method Description
CheckCapacity ( int requestedLength ) : void

Ensure that we're set for the requested length by potentially growing or shifting contents.

Grow ( int requestedLen ) : void

Reallocate the buffer to be larger. For the new size, this uses the max of the requested length and double the current capacity. This does not shift, meaning it does not change the head or tail indices.

ShiftToZero ( ) : void

Move the buffer contents such that headIndex becomes 0.

Method Details

Append() public method

Append a string to this buffer.
public Append ( CharBuffer s ) : void
s CharBuffer The string to append.
return void

Append() public method

Append a character to this buffer.
public Append ( char c ) : void
c char
return void

Append() public method

Append a string to this buffer.
public Append ( string s ) : void
s string The string to append.
return void

CharBuffer() public method

Default constructor.
public CharBuffer ( ) : System
return System

CharBuffer() public method

Construct with a specific capacity.
public CharBuffer ( int capacity ) : System
capacity int
return System

CheckCapacity() protected method

Ensure that we're set for the requested length by potentially growing or shifting contents.
protected CheckCapacity ( int requestedLength ) : void
requestedLength int
return void

Clear() public method

Empty the buffer.
public Clear ( ) : void
return void

Grow() protected method

Reallocate the buffer to be larger. For the new size, this uses the max of the requested length and double the current capacity. This does not shift, meaning it does not change the head or tail indices.
protected Grow ( int requestedLen ) : void
requestedLen int The new requested length.
return void

IndexOf() public method

Find the first instance of a character in the buffer, and return its index. This returns -1 if the character is not found.
public IndexOf ( char c ) : int
c char The character to find.
return int

Remove() public method

Remove a character at the specified index.
public Remove ( int i ) : void
i int The index of the character to remove.
return void

Remove() public method

Remove a specified number of characters at the specified index.
public Remove ( int i, int n ) : void
i int The index of the characters to remove.
n int The number of characters to remove.
return void

SetBuffer() public method

Overwrite this object's underlying buffer with the specified buffer.
public SetBuffer ( char b, int len ) : void
b char The character array.
len int The number of characters to consider filled /// in the input buffer.
return void

ShiftToZero() protected method

Move the buffer contents such that headIndex becomes 0.
protected ShiftToZero ( ) : void
return void

SpeedTest() public static method

Compare speed to StringBuilder.
public static SpeedTest ( ) : void
return void

TestSelf() public static method

Simple self test.
public static TestSelf ( ) : bool
return bool

ToString() public method

Return the current contents as a string.
public ToString ( ) : String
return String

this() public method

Indexer.
public this ( int index ) : char
index int
return char