473,408 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

3-D String Array (or better alternative?)

I have data that looks something like this when returned from a stored
procedure (3 columns, X rows):

Key1 Name1 Value1
Key1 Name2 Value2
Key1 Name3 Value3
Key1 Name4 Value4
Key2 Name1 Value1
Key2 Name2 Value2
Key3 Name1 Value1
KeyX NameY ValueY

Data type for all is string.
Number of rows returned from sp will never exceed 100.

You can see that there is a conceptual 1:many relationship between Keys and
Names; and a 1:1 relationship between Names and Values.

Order for Key, Name, or Value is unimportant to my application logic - so
could be returned from db sorted by alpha if necessary.

My application logic needs to (1) quickly find all of the Name/Value pairs
for a given key; and (2) update specific Value values (sorry!). Eventually
all the updated Value values will be written back to the database (i.e.,
Keys and Names will never be updated) The application logic will never add
or remove Keys, Names, or Values. We're only reading lists of Name/Value
pairs, and updating Value values.

I would prefer to *not* store this in an in-memory ADO.NET DataTable for
performance and size reasons.

I'm thinking a 3-dimensional array might give me best performance - but I
don't know the syntax required to populate it or to retrieve and update its
data. I could look it up, but don't know if doing that would yield the best
performance.

Recommendations? Suggestions? (syntax examples, if possible, would be very
much appreciated).

Thank You!
Nov 16 '05 #1
6 3528
You can do this with 2 System.Collections.Hashtable(s) ala:

HashTable<key, hashTable>
HashTable<name, value>

1st table holds key values and references to name/value pair hash for each key... FYI .NET configuration file info is stored in memory in Hashtables as string keys with to object or Hashtable values; Hashtable values store string keys with object or Hashtable values, and so on, and so on...

--OR--

You can take advantage of 1:1 between Name & Value and use only 1 hashtable ala:

HashTable<key + name, value> keyNameValuePairs;

When inserting into Hash literally use "key + name" as the key. Substring can be used to seperate "key + name" back into 2 pieces if needed.

--Richard

"Doug" wrote:
I have data that looks something like this when returned from a stored
procedure (3 columns, X rows):

Key1 Name1 Value1
Key1 Name2 Value2
Key1 Name3 Value3
Key1 Name4 Value4
Key2 Name1 Value1
Key2 Name2 Value2
Key3 Name1 Value1
KeyX NameY ValueY

Data type for all is string.
Number of rows returned from sp will never exceed 100.

You can see that there is a conceptual 1:many relationship between Keys and
Names; and a 1:1 relationship between Names and Values.

Order for Key, Name, or Value is unimportant to my application logic - so
could be returned from db sorted by alpha if necessary.

My application logic needs to (1) quickly find all of the Name/Value pairs
for a given key; and (2) update specific Value values (sorry!). Eventually
all the updated Value values will be written back to the database (i.e.,
Keys and Names will never be updated) The application logic will never add
or remove Keys, Names, or Values. We're only reading lists of Name/Value
pairs, and updating Value values.

I would prefer to *not* store this in an in-memory ADO.NET DataTable for
performance and size reasons.

I'm thinking a 3-dimensional array might give me best performance - but I
don't know the syntax required to populate it or to retrieve and update its
data. I could look it up, but don't know if doing that would yield the best
performance.

Recommendations? Suggestions? (syntax examples, if possible, would be very
much appreciated).

Thank You!

Nov 16 '05 #2

"Doug" <Cl******@Livermore.com> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...
I have data that looks something like this when returned from a stored
procedure (3 columns, X rows):

Key1 Name1 Value1
Key1 Name2 Value2
Key1 Name3 Value3
Key1 Name4 Value4
Key2 Name1 Value1
Key2 Name2 Value2
Key3 Name1 Value1
KeyX NameY ValueY

Data type for all is string.
Number of rows returned from sp will never exceed 100.
.... I would prefer to *not* store this in an in-memory ADO.NET DataTable for
performance and size reasons.


....

These two statements are inconsistent. With fewer than 100 rows, you can
have no valid objection to using a DataTable.

That being said you could load these items into a hashtable. The key here
is that the hashtable entries would be collections. SOmething like this:
Hashtable ht = new Hashtable();
IDictionary keyItems;
if (!ht.Contains(keyName))
{
keyItems = new System.Collections.Specialized.ListDictionary();
ht.Add(keyName,keyItems);
}
else
{
keyItems = (IDictionary) ht[keyName];
}
keyItems.Add(name,value);
Here using a Hashtable for the outer dictionary and a linked list for the
inner one.

David
Nov 16 '05 #3
<<With fewer than 100 rows, you can have no valid objection to using a
DataTable>>

There might be this objection given the context in which this thing will be
used: The context is that this structure (whatever I end up with) will exist
in an ASP.NET Web Application Session state. So, start multiplying the
"fewer than 100 rows" by the number of Sessions and suddenly size matters
more and more as the number of Sessions increases.

Perhaps I'm still wrong though. The unnecessary size I'm thinking of is the
XML involved in storing this as a DataTable (I haven't measured the bytes
involved, but we all know XML is verbose - more so than a 3d array or
Hashtable - and DataTables are XML structures.).

Thanks for the sample code on the Hashtable.
Nov 16 '05 #4
Not sure why it hasn't been offered but the HybridDictionary is a great space
saving device that switches from using an internal array and linear look-up to
using a Hashtable at a predefined performance tuned size. The class was
specifically built to improve performance of look-up and memory usage when
using lightly populated collections of name/value pairs. In context this would
automatically switch you to the most performant method depending on the
numbers of records rather than requiring large amounts of code.

To speak in the defense of the DataSet, it doesn't use XML until you serialize
it. You aren't wasting memory by using a DataSet at all. There is some small
extra overhead in using it because of extra properties about the data that are
stored but in general all you have is:

A DataSet, and you don't even have that unless you want one.
A DataTable - DataColumnCollection and DataRowCollection
DataColumnCollection - Consists of DataColumn's one for each
DataRowCollection - Consists of DataRow's one for each row

DataRow - int's that offset into DataStorage. Doesn't store values
DataColumn - Information about the actual column + a DataStorage element
that allows indexing by the DataRow into the internal storage.
StringStorage is the DataStorage override used and it has a string[] backing
store.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Doug" <Cl******@Livermore.com> wrote in message
news:eC**************@TK2MSFTNGP09.phx.gbl...
<<With fewer than 100 rows, you can have no valid objection to using a
DataTable>>

There might be this objection given the context in which this thing will be
used: The context is that this structure (whatever I end up with) will exist
in an ASP.NET Web Application Session state. So, start multiplying the
"fewer than 100 rows" by the number of Sessions and suddenly size matters
more and more as the number of Sessions increases.

Perhaps I'm still wrong though. The unnecessary size I'm thinking of is the
XML involved in storing this as a DataTable (I haven't measured the bytes
involved, but we all know XML is verbose - more so than a 3d array or
Hashtable - and DataTables are XML structures.).

Thanks for the sample code on the Hashtable.

Nov 16 '05 #5
A 1:many relationship between Keys and Values? Then you cannot use a
Hasthtable or HybridDictionary. A key needs to reference one and only one
value, and it must be unique in the collection (no duplicates as in your
example data).

For 100 entries use a DataTable. You can then use the DefaultView to Filter
and/or Find the rows you need.

kevin aubuchon
"Doug" <Cl******@Livermore.com> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...
I have data that looks something like this when returned from a stored
procedure (3 columns, X rows):

Key1 Name1 Value1
Key1 Name2 Value2
Key1 Name3 Value3
Key1 Name4 Value4
Key2 Name1 Value1
Key2 Name2 Value2
Key3 Name1 Value1
KeyX NameY ValueY

Data type for all is string.
Number of rows returned from sp will never exceed 100.

You can see that there is a conceptual 1:many relationship between Keys and Names; and a 1:1 relationship between Names and Values.

Order for Key, Name, or Value is unimportant to my application logic - so
could be returned from db sorted by alpha if necessary.

My application logic needs to (1) quickly find all of the Name/Value pairs
for a given key; and (2) update specific Value values (sorry!). Eventually
all the updated Value values will be written back to the database (i.e.,
Keys and Names will never be updated) The application logic will never add
or remove Keys, Names, or Values. We're only reading lists of Name/Value
pairs, and updating Value values.

I would prefer to *not* store this in an in-memory ADO.NET DataTable for
performance and size reasons.

I'm thinking a 3-dimensional array might give me best performance - but I
don't know the syntax required to populate it or to retrieve and update its data. I could look it up, but don't know if doing that would yield the best performance.

Recommendations? Suggestions? (syntax examples, if possible, would be very
much appreciated).

Thank You!

Nov 16 '05 #6
Kevin Aubuchon <ke************@sbcglobal.net> wrote:
A 1:many relationship between Keys and Values? Then you cannot use a
Hasthtable or HybridDictionary. A key needs to reference one and only one
value, and it must be unique in the collection (no duplicates as in your
example data).


There's nothing to stop you from using Hashtable even so - just make
the value set in the hashtable be a reference to an ArrayList
containing all the entries for that key.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
6
by: andrewcw | last post by:
The split function takes as a parameter what I understand as array of Char. I can define the delimiter like this: string innerText = new string; char chSplit={'='};...
6
by: Jozef Jarosciak | last post by:
Quickest way to find the string in 1 dimensional string array! I have a queue 1 dimensional array of strings called 'queue' and I need a fast way to search it. Once there is match, I don't need...
23
by: mrvendetta1 | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> #define N 33 void StringToIntArray(const char *s1); void takeinteger(int i); int main() { char string; printf("enter a string of...
10
by: zahy[dot]bnaya[At]gmail[dot]com | last post by:
Hi, I am trying to come up with a c style string reverser, I want it to take 1 argument Altough I would never do this in real life. Is there a way to do it? I wrote this function that fails : ...
7
by: Felix85 | last post by:
I am trying to make a command interpreter for a mud that i am working on the problem i am having right now is that i cannot convert the string into a char array. This is the error I am getting...
7
by: lovecreatesbea... | last post by:
K&R 2, sec. 5.11 says that no need to precede function and array names with address-of operators &, why?
14
by: aqazi | last post by:
Hi folks I am trying to write a program which acts like a p2p server. When the program starts it reads a file from whereit will read broadcast IP address, port and another port number. Now I am...
6
by: Arnshea | last post by:
(apologies for the crosspost) I'm working with an MFC based COM object. From C# I'd like to be able to call a method on the COM object that takes a string array and modifies the contents. Is...
2
by: Bart Kastermans | last post by:
Summary: can't verify big O claim, how to properly time this? On Jun 15, 2:34 pm, "Terry Reedy" <tjre...@udel.eduwrote: Thanks for the idea. I would expect the separation to lead to somewhat...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.