Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
688f730
commit eb38e30
Showing
10 changed files
with
191 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,49 @@ | ||
public f<String> getLastFragment(String &haystack, const string needle) { | ||
/** | ||
* Split the given haystack by the needle and return the last fragment | ||
* | ||
* @param haystack Input string | ||
* @param needle String to search | ||
* @return Last fragment | ||
*/ | ||
public f<String> getLastFragment(const String &haystack, const string needle) { | ||
const unsigned long index = haystack.rfind(needle); | ||
if index == -1l { | ||
return haystack; | ||
} | ||
return haystack.getSubstring(index + getRawLength(needle)); | ||
} | ||
|
||
/** | ||
* Generate a circular import message from the given source files | ||
* | ||
* @param sourceFiles Source files building the circular dependency chain | ||
* @return Error message | ||
*/ | ||
public f<String> getCircularImportMessage(const Vector<const SourceFile*>& sourceFiles) { | ||
String message; | ||
message += "*-----*\n"; | ||
message += "| |\n"; | ||
for unsigned long i = 0l; i < sourceFiles.getSize(); i++ { | ||
const SourceFile* sourceFile = sourceFiles[i]; | ||
if i != 0 { message += "| |\n"; } | ||
message += "| "; | ||
message += sourceFile.fileName; | ||
message += "\n"; | ||
} | ||
message += "| |\n"; | ||
message += "*-----*\n"; | ||
return message; | ||
} | ||
|
||
/** | ||
* Generate the version info string for the Spice driver | ||
* | ||
* @return Version info string | ||
*/ | ||
public f<String> getVersionInfo() { | ||
String versionString; | ||
versionString += "spice version" + SPICE_VERSION + " " + SPICE_TARGET_OS + "/" + SPICE_TARGET_ARCH + "\n"; | ||
versionString += "built by: " + SPICE_BUILT_BY + "\n\n"; | ||
versionString += "(c) Marc Auberer 2021-2024"; | ||
return versionString; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
import "std/data/vector"; | ||
import "std/data/linked-list"; | ||
import "std/data/pair"; | ||
import "std/data/optional"; | ||
import "std/math/hash"; | ||
|
||
// Generic types for key and value | ||
type K dyn; | ||
type V dyn; | ||
|
||
type HashEntry<K, V> struct { | ||
K key | ||
V value | ||
} | ||
|
||
public type HashTable<K, V> struct { | ||
Vector<LinkedList<Pair<K, V>>> table | ||
unsigned long bucketCount | ||
Vector<LinkedList<HashEntry<K, V>>> table | ||
} | ||
|
||
public p HashTable.ctor(unsigned long bucketCount = 100l) { | ||
this.bucketCount = bucketCount; | ||
this.table = Vector<LinkedList<Pair<K, V>>>(bucketCount); | ||
this.table = Vector<LinkedList<HashEntry<K, V>>>(bucketCount); | ||
for unsigned long i = 0l; i < bucketCount; i++ { | ||
this.table.pushBack(LinkedList<HashEntry<K, V>>()); | ||
} | ||
} | ||
|
||
public p HashTable.insert(const K& key, const V& value) { | ||
public p HashTable.upsert(const K& key, const V& value) { | ||
const unsigned long index = this.hash(key); | ||
LinkedList<Pair<K, V>>& bucket = this.table.get(index); | ||
foreach Pair<K, V>& pair : bucket { | ||
if pair.getFirst() == key { | ||
pair.setSecond(value); | ||
const LinkedList<HashEntry<K, V>>& list = this.table.get(index); | ||
foreach const HashEntry<K, V>& entry : list { | ||
if (entry.key == key) { | ||
entry.value = value; | ||
return; | ||
} | ||
} | ||
this.table.pushBack(LinkedList<Pair<K, V>>()); | ||
LinkedList<Pair<K, V>>& bucket = this.table.back(); | ||
bucket.pushBack(Pair<K, V>(key, value)); | ||
} | ||
|
||
public p HashTable.delete(const K& key) { | ||
public f<Optional<V>> HashTable.get(const K& key) { | ||
const unsigned long index = this.hash(key); | ||
const LinkedList<Pair<K, V>>& bucket = this.table.at(index); | ||
|
||
for unsigned long i = 0l; i < bucket.getSize(); i++ { | ||
Pair<K, V>& candidate = bucket.at(i); | ||
if candidate.getFirst() == key { | ||
bucket.remove(i); | ||
return; | ||
const LinkedList<HashEntry<K, V>>& list = this.table.get(index); | ||
foreach const HashEntry<K, V>& entry : list { | ||
if (entry.key == key) { | ||
return Optional<V>(entry.value); | ||
} | ||
} | ||
return Optional<V>(); | ||
} | ||
|
||
public f<V*> HashTable.get(const K& key) { | ||
unsigned long index = this.hash(key); | ||
const LinkedList<Pair<K, V>>& bucket = this.table.at(index); | ||
|
||
for unsigned long i = 0l; i < bucket.getSize(); i++ { | ||
Pair<K, V>& candidate = bucket.at(i); | ||
if candidate.getFirst() == key { | ||
return &candidate.getSecond(); | ||
public p HashTable.remove(const K& key) { | ||
const unsigned long index = this.hash(key); | ||
LinkedList<HashEntry<K, V>>& list = this.table.get(index); | ||
for (unsigned long i = 0l; i < list.getSize(); i++) { | ||
if (list.get(i).key == key) { | ||
list.remove(i); | ||
return; | ||
} | ||
} | ||
return nil<V*>; | ||
} | ||
|
||
inline f<unsigned long> HashTable.hash(const K& key) { | ||
return hash(key) % this.bucketCount; | ||
K keyCopy = key; | ||
return hash(keyCopy) % this.table.getSize(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
All assertions passed! |
69 changes: 69 additions & 0 deletions
69
test/test-files/std/iterators/linked-list-iterators/source.spice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import "std/data/linked-list"; | ||
import "std/data/pair"; | ||
|
||
f<int> main() { | ||
// Create test vector to iterate over | ||
LinkedList<long> lll = LinkedList<long>(); | ||
lll.pushBack(123l); | ||
lll.pushBack(4321l); | ||
lll.pushBack(9876l); | ||
assert lll.getSize() == 3; | ||
|
||
// Test base functionality | ||
dyn it = lll.getIterator(); | ||
assert it.isValid(); | ||
assert it.get() == 123l; | ||
assert it.get() == 123l; | ||
it.next(); | ||
assert it.get() == 4321l; | ||
assert it.isValid(); | ||
it.next(); | ||
dyn pair = it.getIdx(); | ||
assert pair.getFirst() == 2; | ||
assert pair.getSecond() == 9876l; | ||
it.next(); | ||
assert !it.isValid(); | ||
|
||
// Add new items to the vector | ||
lll.pushBack(321l); | ||
lll.pushBack(-99l); | ||
assert it.isValid(); | ||
|
||
// Test overloaded operators | ||
it -= 3; | ||
assert it.get() == 123l; | ||
assert it.isValid(); | ||
it++; | ||
assert it.get() == 4321l; | ||
it--; | ||
assert it.get() == 123l; | ||
it += 4; | ||
assert it.get() == -99l; | ||
it.next(); | ||
assert !it.isValid(); | ||
|
||
// Test foreach value | ||
foreach long item : lll { | ||
item++; | ||
} | ||
assert lll.get(0) == 123l; | ||
assert lll.get(1) == 4321l; | ||
assert lll.get(2) == 9876l; | ||
|
||
// Test foreach ref | ||
foreach long& item : lll.getIterator() { | ||
item++; | ||
} | ||
assert lll.get(0) == 124l; | ||
assert lll.get(1) == 4322l; | ||
assert lll.get(2) == 9877l; | ||
|
||
foreach long idx, long& item : lll { | ||
item += idx; | ||
} | ||
assert lll.get(0) == 124l; | ||
assert lll.get(1) == 4323l; | ||
assert lll.get(2) == 9879l; | ||
|
||
printf("All assertions passed!"); | ||
} |