A collection with two keys and an index
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I'm working on WEB API project and I just start coding the first CRUD operations. So far, I'm using a JSON file as a "database" to serialize/deserialize a list of Books
. So for that, I have a C# class BookCollection
that will:
- Add, delete, update, and read a
Book
. - Maintain a collection of
Book
s with uniqueId
, uniqueTitle
and uniqueRank
also:- Unique
Id
even after serialize/deserialize. For that We have a propertyNextId
and aDictionary<int, Book>
- Unique
Title
that could be changed later on (that's Why We can't rely on it as a "real" ID). For that We have theHashSet<string>
Rank
is kind of order of preference for eachBook
. For example, We can have book1, book2, and book3 with an order of preference (Rank
) of 3, 1, and 2. Which means,Rank
should be unique and between [1..NumberOfBooks]. Also When changing aRank
for one book, We need to update theRank
for all other books accordingly. In the title, I saidindex
, because to meRank
is the same as an array index.
- Unique
Here is my code:
public class Book
public int Id set; get;
public string Title set; get;
public int Rank set; get;
public class BookCollection
public int NextId set; get;
private readonly IDictionary<int, Book> m_Dictionary;
private readonly ISet<string> m_HashSet;
public BookCollection()
m_Dictionary = new Dictionary<int, Book>();
m_HashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
public bool Add(Book item)
item.Id = NextId++;
if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_Dictionary.Add(item.Id, item);
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;
public bool Remove(Book item)
bool found = m_Dictionary.ContainsKey(item.Id);
if (!found) return false;
m_Dictionary.Remove(item.Id);
m_HashSet.Remove(item.Title);
updateRanks(item.Rank, false);
return true;
public bool Update(Book item)
if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_HashSet.Remove(m_Dictionary[item.Id].Title);
m_Dictionary[item.Id] = item;
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;
public IEnumerable<Book> GetAllBooks()
return m_Dictionary.Values;
public bool Contains(Book item)
return m_Dictionary.ContainsKey(item.Id);
private void updateRanks(int rank, bool increment)
foreach (var book in m_Dictionary.Values)
if (rank <= book.Rank)
book.Rank = increment ? book.Rank + 1 : book.Rank - 1;
I want to build a collection that could handle all CRUD operations in the most performant way.
Any suggestion How to make this collection better?
c#
add a comment |Â
up vote
0
down vote
favorite
I'm working on WEB API project and I just start coding the first CRUD operations. So far, I'm using a JSON file as a "database" to serialize/deserialize a list of Books
. So for that, I have a C# class BookCollection
that will:
- Add, delete, update, and read a
Book
. - Maintain a collection of
Book
s with uniqueId
, uniqueTitle
and uniqueRank
also:- Unique
Id
even after serialize/deserialize. For that We have a propertyNextId
and aDictionary<int, Book>
- Unique
Title
that could be changed later on (that's Why We can't rely on it as a "real" ID). For that We have theHashSet<string>
Rank
is kind of order of preference for eachBook
. For example, We can have book1, book2, and book3 with an order of preference (Rank
) of 3, 1, and 2. Which means,Rank
should be unique and between [1..NumberOfBooks]. Also When changing aRank
for one book, We need to update theRank
for all other books accordingly. In the title, I saidindex
, because to meRank
is the same as an array index.
- Unique
Here is my code:
public class Book
public int Id set; get;
public string Title set; get;
public int Rank set; get;
public class BookCollection
public int NextId set; get;
private readonly IDictionary<int, Book> m_Dictionary;
private readonly ISet<string> m_HashSet;
public BookCollection()
m_Dictionary = new Dictionary<int, Book>();
m_HashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
public bool Add(Book item)
item.Id = NextId++;
if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_Dictionary.Add(item.Id, item);
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;
public bool Remove(Book item)
bool found = m_Dictionary.ContainsKey(item.Id);
if (!found) return false;
m_Dictionary.Remove(item.Id);
m_HashSet.Remove(item.Title);
updateRanks(item.Rank, false);
return true;
public bool Update(Book item)
if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_HashSet.Remove(m_Dictionary[item.Id].Title);
m_Dictionary[item.Id] = item;
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;
public IEnumerable<Book> GetAllBooks()
return m_Dictionary.Values;
public bool Contains(Book item)
return m_Dictionary.ContainsKey(item.Id);
private void updateRanks(int rank, bool increment)
foreach (var book in m_Dictionary.Values)
if (rank <= book.Rank)
book.Rank = increment ? book.Rank + 1 : book.Rank - 1;
I want to build a collection that could handle all CRUD operations in the most performant way.
Any suggestion How to make this collection better?
c#
Update is more like Insert. Cannot change rank or title?
â paparazzo
May 12 at 15:56
@paparazzo We could change everyBook
s attribute exceptId
. So changing theRank
is like insert in a List
â Mhd
May 12 at 20:47
Actually can change ID as it has a set. If you change Rank directly it is not guaranteed to be unique.
â paparazzo
May 13 at 13:59
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm working on WEB API project and I just start coding the first CRUD operations. So far, I'm using a JSON file as a "database" to serialize/deserialize a list of Books
. So for that, I have a C# class BookCollection
that will:
- Add, delete, update, and read a
Book
. - Maintain a collection of
Book
s with uniqueId
, uniqueTitle
and uniqueRank
also:- Unique
Id
even after serialize/deserialize. For that We have a propertyNextId
and aDictionary<int, Book>
- Unique
Title
that could be changed later on (that's Why We can't rely on it as a "real" ID). For that We have theHashSet<string>
Rank
is kind of order of preference for eachBook
. For example, We can have book1, book2, and book3 with an order of preference (Rank
) of 3, 1, and 2. Which means,Rank
should be unique and between [1..NumberOfBooks]. Also When changing aRank
for one book, We need to update theRank
for all other books accordingly. In the title, I saidindex
, because to meRank
is the same as an array index.
- Unique
Here is my code:
public class Book
public int Id set; get;
public string Title set; get;
public int Rank set; get;
public class BookCollection
public int NextId set; get;
private readonly IDictionary<int, Book> m_Dictionary;
private readonly ISet<string> m_HashSet;
public BookCollection()
m_Dictionary = new Dictionary<int, Book>();
m_HashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
public bool Add(Book item)
item.Id = NextId++;
if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_Dictionary.Add(item.Id, item);
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;
public bool Remove(Book item)
bool found = m_Dictionary.ContainsKey(item.Id);
if (!found) return false;
m_Dictionary.Remove(item.Id);
m_HashSet.Remove(item.Title);
updateRanks(item.Rank, false);
return true;
public bool Update(Book item)
if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_HashSet.Remove(m_Dictionary[item.Id].Title);
m_Dictionary[item.Id] = item;
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;
public IEnumerable<Book> GetAllBooks()
return m_Dictionary.Values;
public bool Contains(Book item)
return m_Dictionary.ContainsKey(item.Id);
private void updateRanks(int rank, bool increment)
foreach (var book in m_Dictionary.Values)
if (rank <= book.Rank)
book.Rank = increment ? book.Rank + 1 : book.Rank - 1;
I want to build a collection that could handle all CRUD operations in the most performant way.
Any suggestion How to make this collection better?
c#
I'm working on WEB API project and I just start coding the first CRUD operations. So far, I'm using a JSON file as a "database" to serialize/deserialize a list of Books
. So for that, I have a C# class BookCollection
that will:
- Add, delete, update, and read a
Book
. - Maintain a collection of
Book
s with uniqueId
, uniqueTitle
and uniqueRank
also:- Unique
Id
even after serialize/deserialize. For that We have a propertyNextId
and aDictionary<int, Book>
- Unique
Title
that could be changed later on (that's Why We can't rely on it as a "real" ID). For that We have theHashSet<string>
Rank
is kind of order of preference for eachBook
. For example, We can have book1, book2, and book3 with an order of preference (Rank
) of 3, 1, and 2. Which means,Rank
should be unique and between [1..NumberOfBooks]. Also When changing aRank
for one book, We need to update theRank
for all other books accordingly. In the title, I saidindex
, because to meRank
is the same as an array index.
- Unique
Here is my code:
public class Book
public int Id set; get;
public string Title set; get;
public int Rank set; get;
public class BookCollection
public int NextId set; get;
private readonly IDictionary<int, Book> m_Dictionary;
private readonly ISet<string> m_HashSet;
public BookCollection()
m_Dictionary = new Dictionary<int, Book>();
m_HashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
public bool Add(Book item)
item.Id = NextId++;
if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_Dictionary.Add(item.Id, item);
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;
public bool Remove(Book item)
bool found = m_Dictionary.ContainsKey(item.Id);
if (!found) return false;
m_Dictionary.Remove(item.Id);
m_HashSet.Remove(item.Title);
updateRanks(item.Rank, false);
return true;
public bool Update(Book item)
if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_HashSet.Remove(m_Dictionary[item.Id].Title);
m_Dictionary[item.Id] = item;
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;
public IEnumerable<Book> GetAllBooks()
return m_Dictionary.Values;
public bool Contains(Book item)
return m_Dictionary.ContainsKey(item.Id);
private void updateRanks(int rank, bool increment)
foreach (var book in m_Dictionary.Values)
if (rank <= book.Rank)
book.Rank = increment ? book.Rank + 1 : book.Rank - 1;
I want to build a collection that could handle all CRUD operations in the most performant way.
Any suggestion How to make this collection better?
c#
asked May 12 at 14:49
Mhd
1364
1364
Update is more like Insert. Cannot change rank or title?
â paparazzo
May 12 at 15:56
@paparazzo We could change everyBook
s attribute exceptId
. So changing theRank
is like insert in a List
â Mhd
May 12 at 20:47
Actually can change ID as it has a set. If you change Rank directly it is not guaranteed to be unique.
â paparazzo
May 13 at 13:59
add a comment |Â
Update is more like Insert. Cannot change rank or title?
â paparazzo
May 12 at 15:56
@paparazzo We could change everyBook
s attribute exceptId
. So changing theRank
is like insert in a List
â Mhd
May 12 at 20:47
Actually can change ID as it has a set. If you change Rank directly it is not guaranteed to be unique.
â paparazzo
May 13 at 13:59
Update is more like Insert. Cannot change rank or title?
â paparazzo
May 12 at 15:56
Update is more like Insert. Cannot change rank or title?
â paparazzo
May 12 at 15:56
@paparazzo We could change every
Book
s attribute except Id
. So changing the Rank
is like insert in a Listâ Mhd
May 12 at 20:47
@paparazzo We could change every
Book
s attribute except Id
. So changing the Rank
is like insert in a Listâ Mhd
May 12 at 20:47
Actually can change ID as it has a set. If you change Rank directly it is not guaranteed to be unique.
â paparazzo
May 13 at 13:59
Actually can change ID as it has a set. If you change Rank directly it is not guaranteed to be unique.
â paparazzo
May 13 at 13:59
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194263%2fa-collection-with-two-keys-and-an-index%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Update is more like Insert. Cannot change rank or title?
â paparazzo
May 12 at 15:56
@paparazzo We could change every
Book
s attribute exceptId
. So changing theRank
is like insert in a Listâ Mhd
May 12 at 20:47
Actually can change ID as it has a set. If you change Rank directly it is not guaranteed to be unique.
â paparazzo
May 13 at 13:59